diff --git a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/GeniidataClient.sol b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/GeniidataClient.sol index 4b564ed80d..c9b78ea3f2 100644 --- a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/GeniidataClient.sol +++ b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/GeniidataClient.sol @@ -19,18 +19,20 @@ pragma solidity ^0.8.8; import "../libraries/Http.sol"; +import "../libraries/Identities.sol"; import "../libraries/Utils.sol"; + library GeniidataClient { function getTokenBalance( string[] memory secrets, - string memory url, string memory identityString, string memory tokenName, uint8 tokenDecimals ) internal returns (uint256) { string memory encodePackedUrl = string( abi.encodePacked( - url, + // test against mock server => "http://localhost:19529/api/1/brc20/balance" + "https://api.geniidata.com/api/1/brc20/balance", "?tick=", tokenName, "&address=", @@ -58,4 +60,13 @@ library GeniidataClient { } return 0; } + + function isSupportedNetwork(uint32 network) internal pure returns (bool) { + return + network == Web3Networks.BitcoinP2tr || + network == Web3Networks.BitcoinP2pkh || + network == Web3Networks.BitcoinP2sh || + network == Web3Networks.BitcoinP2wpkh || + network == Web3Networks.BitcoinP2wsh; + } } diff --git a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/NoderealClient.sol b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/NoderealClient.sol index 6df76cee62..73bdf98ff4 100644 --- a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/NoderealClient.sol +++ b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/NoderealClient.sol @@ -19,10 +19,12 @@ pragma solidity ^0.8.8; import "../libraries/Http.sol"; +import "../libraries/Identities.sol"; import "../libraries/Utils.sol"; + library NoderealClient { function getTokenBalance( - string memory url, + uint32 network, string[] memory secrets, string memory tokenContractAddress, string memory account @@ -31,7 +33,7 @@ library NoderealClient { string memory request; string memory encodePackedUrl = string( - abi.encodePacked(url, secrets[0]) + abi.encodePacked(getNetworkUrl(network), secrets[0]) ); if ( keccak256(bytes(tokenContractAddress)) == keccak256("Native Token") @@ -69,5 +71,21 @@ library NoderealClient { } else { return (false, 0); } - } + } + + function isSupportedNetwork(uint32 network) internal pure returns (bool) { + return network == Web3Networks.Bsc || network == Web3Networks.Ethereum; + } + + function getNetworkUrl(uint32 network) + internal + pure + returns (string memory url) + { + if (network == Web3Networks.Bsc) { + url = "https://bsc-mainnet.nodereal.io/v1/"; + } else if (network == Web3Networks.Ethereum) { + url = "https://eth-mainnet.nodereal.io/v1/"; + } + } } diff --git a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/TokenHoldingAmount.sol b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/TokenHoldingAmount.sol index f0c62403df..957b5ee14d 100644 --- a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/TokenHoldingAmount.sol +++ b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/TokenHoldingAmount.sol @@ -92,7 +92,7 @@ abstract contract TokenHoldingAmount is DynamicAssertion { uint256 networksLength = identity.networks.length; for (uint32 j = 0; j < networksLength; j++) { uint32 network = identity.networks[j]; - if (isSupportedNetwork(network)) { + if (isSupportedNetwork(tokenName, network)) { total_balance += queryBalance( identity, network, @@ -182,8 +182,9 @@ abstract contract TokenHoldingAmount is DynamicAssertion { function getTokenDecimals() internal pure virtual returns (uint8); function isSupportedNetwork( + string memory tokenName, uint32 network - ) internal pure virtual returns (bool); + ) internal view virtual returns (bool); function queryBalance( Identity memory identity, diff --git a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/TokenQueryLogic.sol b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/TokenQueryLogic.sol index 07c446831a..0e12b8e687 100644 --- a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/TokenQueryLogic.sol +++ b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/TokenQueryLogic.sol @@ -23,26 +23,14 @@ import "../libraries/Utils.sol"; import { TokenHoldingAmount } from "./TokenHoldingAmount.sol"; import { NoderealClient } from "./NoderealClient.sol"; import { GeniidataClient } from "./GeniidataClient.sol"; + abstract contract TokenQueryLogic is TokenHoldingAmount { - mapping(uint32 => string) internal networkUrls; - mapping(uint32 => bool) private queriedNetworks; mapping(string => mapping(uint32 => string)) tokenAddresses; mapping(string => string) internal tokenBscAddress; mapping(string => string) internal tokenEthereumAddress; mapping(string => uint32[]) internal tokenNetworks; - constructor() { - networkUrls[Web3Networks.Bsc] = "https://bsc-mainnet.nodereal.io/v1/"; // test against mock server => "http://localhost:19530/nodereal_jsonrpc/" - networkUrls[ - Web3Networks.Ethereum - ] = "https://eth-mainnet.nodereal.io/v1/"; // test against mock server => "http://localhost:19530/nodereal_jsonrpc/" - - networkUrls[ - Web3Networks.BitcoinP2tr - ] = "https://api.geniidata.com/api/1/brc20/balance"; // test against mock server => "http://localhost:19529/api/1/brc20/balance" - // Add more networks as needed - } - + // TODO fix it for erc20 token, same token for different networks has different decimals. function getTokenDecimals() internal pure override returns (uint8) { return 18; } @@ -57,44 +45,29 @@ abstract contract TokenQueryLogic is TokenHoldingAmount { .identityToString(network, identity.value); if (identityToStringSuccess) { - string memory url; - uint32[] memory networks = tokenNetworks[tokenName]; uint256 totalBalance = 0; - for (uint32 i = 0; i < networks.length; i++) { - // Check if this network has been queried - url = networkUrls[networks[i]]; - - if (!queriedNetworks[networks[i]]) { - string memory _tokenContractAddress = tokenAddresses[ - tokenName - ][networks[i]]; - if (networks[i] == Web3Networks.BitcoinP2tr) { - uint256 balance = GeniidataClient.getTokenBalance( - secrets, - url, - identityString, - tokenName, - getTokenDecimals() - ); - totalBalance += balance; - } else if ( - networks[i] == Web3Networks.Bsc || - networks[i] == Web3Networks.Ethereum - ) { - (bool success, uint256 balance) = NoderealClient - .getTokenBalance( - url, - secrets, - _tokenContractAddress, - identityString - ); - if (success) { - totalBalance += balance; - } - } - // Mark this network as queried - queriedNetworks[networks[i]] = true; + string memory tokenContractAddress = tokenAddresses[tokenName][ + network + ]; + if (GeniidataClient.isSupportedNetwork(network)) { + uint256 balance = GeniidataClient.getTokenBalance( + secrets, + identityString, + tokenName, + getTokenDecimals() + ); + totalBalance += balance; + } else if (NoderealClient.isSupportedNetwork(network)) { + (bool success, uint256 balance) = NoderealClient + .getTokenBalance( + network, + secrets, + tokenContractAddress, + identityString + ); + if (success) { + totalBalance += balance; } } return totalBalance; @@ -102,12 +75,18 @@ abstract contract TokenQueryLogic is TokenHoldingAmount { return 0; } - function isSupportedNetwork( - uint32 network - ) internal pure override returns (bool) { - return - network == Web3Networks.Bsc || - network == Web3Networks.Ethereum || - network == Web3Networks.BitcoinP2tr; + function isSupportedNetwork(string memory tokenName, uint32 network) + internal + view + override + returns (bool) + { + uint32[] memory networks = tokenNetworks[tokenName]; + for (uint32 i = 0; i < networks.length; i++) { + if (network == networks[i]) { + return true; + } + } + return false; } }