Skip to content

Commit 09004f4

Browse files
authored
p-866 Adding btc token holding amount (#2919)
* 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
1 parent 7315a3e commit 09004f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+610
-179
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2020-2024 Trust Computing GmbH.
2+
// This file is part of Litentry.
3+
//
4+
// Litentry is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Litentry is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.
16+
17+
// SPDX-License-Identifier: GPL-3.0-or-later
18+
19+
pragma solidity ^0.8.8;
20+
21+
import "@openzeppelin/contracts/utils/Strings.sol";
22+
import { HttpHeader } from "../libraries/Http.sol";
23+
24+
import "hardhat/console.sol";
25+
26+
contract MockHttpGetI64 {
27+
receive() external payable {}
28+
29+
fallback() external payable {
30+
(string memory url, string memory jsonPointer, ) = abi.decode(
31+
msg.data,
32+
(string, string, HttpHeader[])
33+
);
34+
35+
bool success = true;
36+
uint256 value = 0;
37+
38+
if (
39+
Strings.equal(
40+
url,
41+
"https://blockchain.info/multiaddr?active=bc1pg6qjsrxwg9cvqx0gxstl0t74ynhs2528t7rp0u7acl6etwn5t6vswxrzpa&n=0"
42+
)
43+
) {
44+
// 0.1(decimal is 8)
45+
value = 10000000;
46+
} else if (
47+
Strings.equal(
48+
url,
49+
"https://blockchain.info/multiaddr?active=bc1pqdk57wus42wuh989k3v700n6w584andwg7pvxnrd69ag3rs94cfq40qx2y&n=0"
50+
)
51+
) {
52+
value = 0;
53+
}
54+
55+
console.log("http_get_i64>", url, jsonPointer, value);
56+
57+
bytes memory encodedResult = abi.encode(success, value);
58+
59+
assembly {
60+
return(add(encodedResult, 0x20), mload(encodedResult))
61+
}
62+
}
63+
}

tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/BlockchainInfoClient.sol

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ pragma solidity ^0.8.8;
2020

2121
import "../libraries/Http.sol";
2222
import "../libraries/Utils.sol";
23+
import "../libraries/Identities.sol";
2324

2425
library BlockchainInfoClient {
2526
function getMultiAddress(
26-
string memory url,
2727
string[] memory accounts
2828
) internal returns (bool, int64) {
29+
string memory url = "https://blockchain.info/multiaddr";
2930
string memory activeQueryParam = "";
3031

3132
for (uint256 i = 0; i < accounts.length; i++) {
@@ -46,4 +47,25 @@ library BlockchainInfoClient {
4647
HttpHeader[] memory headers = new HttpHeader[](0);
4748
return Http.GetI64(url, "/wallet/final_balance", headers);
4849
}
50+
51+
function isSupportedNetwork(uint32 network) internal pure returns (bool) {
52+
return
53+
network == Web3Networks.BitcoinP2tr ||
54+
network == Web3Networks.BitcoinP2pkh ||
55+
network == Web3Networks.BitcoinP2sh ||
56+
network == Web3Networks.BitcoinP2wpkh ||
57+
network == Web3Networks.BitcoinP2wsh;
58+
}
59+
60+
function getTokenBalance(
61+
string[] memory accounts
62+
) internal returns (uint256) {
63+
(bool balanceSuccess, int64 balance) = BlockchainInfoClient
64+
.getMultiAddress(accounts);
65+
if (balanceSuccess) {
66+
return uint256(uint64(balance));
67+
} else {
68+
return 0;
69+
}
70+
}
4971
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2020-2024 Trust Computing GmbH.
2+
// This file is part of Litentry.
3+
//
4+
// Litentry is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Litentry is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.
16+
17+
// SPDX-License-Identifier: GPL-3.0-or-later
18+
19+
pragma solidity ^0.8.8;
20+
import "./Constants.sol";
21+
import { BRC20 } from "./brc20/BRC20.sol";
22+
library Btc {
23+
function getTokenRanges() internal pure returns (uint256[] memory) {
24+
// [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];
25+
// all ranges multiplied by decimals_factor(1000).
26+
uint256[] memory ranges = new uint256[](14);
27+
ranges[0] = 0;
28+
ranges[1] = 1;
29+
ranges[2] = 100;
30+
ranges[3] = 300;
31+
ranges[4] = 600;
32+
ranges[5] = 1000;
33+
ranges[6] = 2000;
34+
ranges[7] = 5000;
35+
ranges[8] = 10000;
36+
ranges[9] = 15000;
37+
ranges[10] = 25000;
38+
ranges[11] = 30000;
39+
ranges[12] = 40000;
40+
ranges[13] = 50000;
41+
return ranges;
42+
}
43+
44+
function getTokenInfo() internal pure returns (TokenInfo[] memory) {
45+
uint32[] memory networks = BRC20.getDefaultTokenNetworks();
46+
TokenInfo[] memory tokenInfoList = new TokenInfo[](networks.length);
47+
for (uint i = 0; i < networks.length; i++) {
48+
tokenInfoList[i] = TokenInfo(
49+
networks[i],
50+
"",
51+
DataProviderTypes.BlockchainInfoClient,
52+
8
53+
);
54+
}
55+
56+
return tokenInfoList;
57+
}
58+
}

tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/Constants.sol

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,21 @@
1818

1919
pragma solidity ^0.8.8;
2020

21-
library Constants {
22-
uint256 constant decimals_factor = 1000;
23-
}
2421
struct TokenInfo {
2522
uint32 network;
2623
string tokenAddress;
24+
uint8 dataprovierType;
25+
uint8 decimals;
26+
}
27+
28+
library Constants {
29+
uint256 constant decimals_factor = 1000;
30+
}
31+
32+
library DataProviderTypes {
33+
uint8 public constant AchainableClient = 0;
34+
uint8 public constant BlockchainInfoClient = 1;
35+
uint8 public constant GeniidataClient = 2;
36+
uint8 public constant MoralisClient = 3;
37+
uint8 public constant NoderealClient = 4;
2738
}

tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/MoralisClient.sol

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import "@openzeppelin/contracts/utils/Strings.sol";
2222
import "../libraries/Http.sol";
2323
import "../libraries/Json.sol";
2424
import "../libraries/Identities.sol";
25-
25+
import "../libraries/Utils.sol";
2626
struct SolanaTokenBalance {
2727
string mint;
2828
string amount;
@@ -220,4 +220,55 @@ library MoralisClient {
220220
url = "https://deep-index.moralis.io/api/v2.2";
221221
}
222222
}
223+
224+
function getTokenBalance(
225+
uint32 network,
226+
string memory apiKey,
227+
string memory account,
228+
string memory tokenContractAddress,
229+
uint8 tokenDecimals
230+
) internal returns (uint256) {
231+
if (Strings.equal(tokenContractAddress, "Native Token")) {
232+
(bool success, string memory solanaTokenBalance) = MoralisClient
233+
.getSolanaNativeBalance(network, apiKey, account);
234+
235+
if (success) {
236+
(bool parsedStatus, uint256 parsedAmount) = Utils.parseDecimal(
237+
solanaTokenBalance,
238+
tokenDecimals
239+
);
240+
if (parsedStatus) {
241+
return parsedAmount;
242+
}
243+
return 0;
244+
}
245+
} else {
246+
(
247+
bool success,
248+
SolanaTokenBalance[] memory solanaTokenBalance
249+
) = MoralisClient.getSolanaTokensBalance(network, apiKey, account);
250+
251+
if (success) {
252+
for (uint i = 0; i < solanaTokenBalance.length; i++) {
253+
if (
254+
Strings.equal(
255+
solanaTokenBalance[i].mint,
256+
tokenContractAddress
257+
)
258+
) {
259+
(bool parsedStatus, uint256 parsedAmount) = Utils
260+
.parseDecimal(
261+
solanaTokenBalance[i].amount,
262+
tokenDecimals
263+
);
264+
if (parsedStatus) {
265+
return parsedAmount;
266+
}
267+
return 0;
268+
}
269+
}
270+
}
271+
}
272+
return 0;
273+
}
223274
}

tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/TokenHoldingAmount.sol

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
106106
function calculateRange(
107107
uint256 balance,
108108
uint256[] memory ranges
109-
) private pure returns (uint256, uint256, int256) {
109+
) private view returns (uint256, uint256, int256) {
110110
uint256 index = ranges.length - 1;
111111
uint256 min = 0;
112112
int256 max = 0;
@@ -128,7 +128,6 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
128128
min = ranges[index];
129129
max = int256(ranges[index + 1]);
130130
}
131-
132131
return (index, min, max);
133132
}
134133

@@ -176,7 +175,7 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
176175
return assertions;
177176
}
178177

179-
function getTokenDecimals() internal pure virtual returns (uint8);
178+
function getTokenDecimals() internal view virtual returns (uint8);
180179

181180
function isSupportedNetwork(
182181
string memory tokenName,

tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/TokenMapping.sol

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ import { Usdt } from "./erc20/Usdt.sol";
7171
import { Wbtc } from "./erc20//Wbtc.sol";
7272
import { Cvx } from "./erc20/Cvx.sol";
7373
import { Usdd } from "./erc20/Usdd.sol";
74+
75+
// btc
76+
import { Btc } from "./Btc.sol";
7477
contract TokenMapping is TokenQueryLogic {
7578
constructor() {
7679
// btcs
@@ -115,6 +118,12 @@ contract TokenMapping is TokenQueryLogic {
115118
tokenInfo["sats"].push(BRC20.getBrc20TokenInfo()[i]);
116119
}
117120

121+
// Btc
122+
tokenRanges["btc"] = Btc.getTokenRanges();
123+
for (uint8 i = 0; i < Btc.getTokenInfo().length; i++) {
124+
tokenInfo["btc"].push(Btc.getTokenInfo()[i]);
125+
}
126+
118127
// ada
119128
tokenRanges["ada"] = Ada.getTokenRanges();
120129
for (uint8 i = 0; i < Ada.getTokenInfo().length; i++) {

0 commit comments

Comments
 (0)