Skip to content

Commit

Permalink
p-866 Adding btc token holding amount (#2919)
Browse files Browse the repository at this point in the history
* adding DataProviderTypes

* adding getTokenBalance

* adding btc mapping

* refactoring TokenQueryLogic

* removing hardhat/console.sol

* merge dev

* add decimals

* add MockHttpGetI64

* fmt

* add hardhat-gas-reporter

* remove gas reporter

* update lock file

* remove unused file

* fix cro configuration
  • Loading branch information
0xverin authored Jul 31, 2024
1 parent 7315a3e commit 09004f4
Show file tree
Hide file tree
Showing 52 changed files with 610 additions and 179 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2020-2024 Trust Computing GmbH.
// This file is part of Litentry.
//
// Litentry is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Litentry is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.

// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity ^0.8.8;

import "@openzeppelin/contracts/utils/Strings.sol";
import { HttpHeader } from "../libraries/Http.sol";

import "hardhat/console.sol";

contract MockHttpGetI64 {
receive() external payable {}

fallback() external payable {
(string memory url, string memory jsonPointer, ) = abi.decode(
msg.data,
(string, string, HttpHeader[])
);

bool success = true;
uint256 value = 0;

if (
Strings.equal(
url,
"https://blockchain.info/multiaddr?active=bc1pg6qjsrxwg9cvqx0gxstl0t74ynhs2528t7rp0u7acl6etwn5t6vswxrzpa&n=0"
)
) {
// 0.1(decimal is 8)
value = 10000000;
} else if (
Strings.equal(
url,
"https://blockchain.info/multiaddr?active=bc1pqdk57wus42wuh989k3v700n6w584andwg7pvxnrd69ag3rs94cfq40qx2y&n=0"
)
) {
value = 0;
}

console.log("http_get_i64>", url, jsonPointer, value);

bytes memory encodedResult = abi.encode(success, value);

assembly {
return(add(encodedResult, 0x20), mload(encodedResult))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ pragma solidity ^0.8.8;

import "../libraries/Http.sol";
import "../libraries/Utils.sol";
import "../libraries/Identities.sol";

library BlockchainInfoClient {
function getMultiAddress(
string memory url,
string[] memory accounts
) internal returns (bool, int64) {
string memory url = "https://blockchain.info/multiaddr";
string memory activeQueryParam = "";

for (uint256 i = 0; i < accounts.length; i++) {
Expand All @@ -46,4 +47,25 @@ library BlockchainInfoClient {
HttpHeader[] memory headers = new HttpHeader[](0);
return Http.GetI64(url, "/wallet/final_balance", headers);
}

function isSupportedNetwork(uint32 network) internal pure returns (bool) {
return
network == Web3Networks.BitcoinP2tr ||
network == Web3Networks.BitcoinP2pkh ||
network == Web3Networks.BitcoinP2sh ||
network == Web3Networks.BitcoinP2wpkh ||
network == Web3Networks.BitcoinP2wsh;
}

function getTokenBalance(
string[] memory accounts
) internal returns (uint256) {
(bool balanceSuccess, int64 balance) = BlockchainInfoClient
.getMultiAddress(accounts);
if (balanceSuccess) {
return uint256(uint64(balance));
} else {
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2020-2024 Trust Computing GmbH.
// This file is part of Litentry.
//
// Litentry is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Litentry is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.

// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity ^0.8.8;
import "./Constants.sol";
import { BRC20 } from "./brc20/BRC20.sol";
library Btc {
function getTokenRanges() internal pure returns (uint256[] memory) {
// [0.0, 0.001, 0.1, 0.3, 0.6, 1.0, 2.0, 5.0, 10.0, 15.0, 25.0, 30.0, 40.0, 50.0];
// all ranges multiplied by decimals_factor(1000).
uint256[] memory ranges = new uint256[](14);
ranges[0] = 0;
ranges[1] = 1;
ranges[2] = 100;
ranges[3] = 300;
ranges[4] = 600;
ranges[5] = 1000;
ranges[6] = 2000;
ranges[7] = 5000;
ranges[8] = 10000;
ranges[9] = 15000;
ranges[10] = 25000;
ranges[11] = 30000;
ranges[12] = 40000;
ranges[13] = 50000;
return ranges;
}

function getTokenInfo() internal pure returns (TokenInfo[] memory) {
uint32[] memory networks = BRC20.getDefaultTokenNetworks();
TokenInfo[] memory tokenInfoList = new TokenInfo[](networks.length);
for (uint i = 0; i < networks.length; i++) {
tokenInfoList[i] = TokenInfo(
networks[i],
"",
DataProviderTypes.BlockchainInfoClient,
8
);
}

return tokenInfoList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@

pragma solidity ^0.8.8;

library Constants {
uint256 constant decimals_factor = 1000;
}
struct TokenInfo {
uint32 network;
string tokenAddress;
uint8 dataprovierType;
uint8 decimals;
}

library Constants {
uint256 constant decimals_factor = 1000;
}

library DataProviderTypes {
uint8 public constant AchainableClient = 0;
uint8 public constant BlockchainInfoClient = 1;
uint8 public constant GeniidataClient = 2;
uint8 public constant MoralisClient = 3;
uint8 public constant NoderealClient = 4;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import "@openzeppelin/contracts/utils/Strings.sol";
import "../libraries/Http.sol";
import "../libraries/Json.sol";
import "../libraries/Identities.sol";

import "../libraries/Utils.sol";
struct SolanaTokenBalance {
string mint;
string amount;
Expand Down Expand Up @@ -220,4 +220,55 @@ library MoralisClient {
url = "https://deep-index.moralis.io/api/v2.2";
}
}

function getTokenBalance(
uint32 network,
string memory apiKey,
string memory account,
string memory tokenContractAddress,
uint8 tokenDecimals
) internal returns (uint256) {
if (Strings.equal(tokenContractAddress, "Native Token")) {
(bool success, string memory solanaTokenBalance) = MoralisClient
.getSolanaNativeBalance(network, apiKey, account);

if (success) {
(bool parsedStatus, uint256 parsedAmount) = Utils.parseDecimal(
solanaTokenBalance,
tokenDecimals
);
if (parsedStatus) {
return parsedAmount;
}
return 0;
}
} else {
(
bool success,
SolanaTokenBalance[] memory solanaTokenBalance
) = MoralisClient.getSolanaTokensBalance(network, apiKey, account);

if (success) {
for (uint i = 0; i < solanaTokenBalance.length; i++) {
if (
Strings.equal(
solanaTokenBalance[i].mint,
tokenContractAddress
)
) {
(bool parsedStatus, uint256 parsedAmount) = Utils
.parseDecimal(
solanaTokenBalance[i].amount,
tokenDecimals
);
if (parsedStatus) {
return parsedAmount;
}
return 0;
}
}
}
}
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
function calculateRange(
uint256 balance,
uint256[] memory ranges
) private pure returns (uint256, uint256, int256) {
) private view returns (uint256, uint256, int256) {
uint256 index = ranges.length - 1;
uint256 min = 0;
int256 max = 0;
Expand All @@ -128,7 +128,6 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
min = ranges[index];
max = int256(ranges[index + 1]);
}

return (index, min, max);
}

Expand Down Expand Up @@ -176,7 +175,7 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
return assertions;
}

function getTokenDecimals() internal pure virtual returns (uint8);
function getTokenDecimals() internal view virtual returns (uint8);

function isSupportedNetwork(
string memory tokenName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ import { Usdt } from "./erc20/Usdt.sol";
import { Wbtc } from "./erc20//Wbtc.sol";
import { Cvx } from "./erc20/Cvx.sol";
import { Usdd } from "./erc20/Usdd.sol";

// btc
import { Btc } from "./Btc.sol";
contract TokenMapping is TokenQueryLogic {
constructor() {
// btcs
Expand Down Expand Up @@ -115,6 +118,12 @@ contract TokenMapping is TokenQueryLogic {
tokenInfo["sats"].push(BRC20.getBrc20TokenInfo()[i]);
}

// Btc
tokenRanges["btc"] = Btc.getTokenRanges();
for (uint8 i = 0; i < Btc.getTokenInfo().length; i++) {
tokenInfo["btc"].push(Btc.getTokenInfo()[i]);
}

// ada
tokenRanges["ada"] = Ada.getTokenRanges();
for (uint8 i = 0; i < Ada.getTokenInfo().length; i++) {
Expand Down
Loading

0 comments on commit 09004f4

Please sign in to comment.