Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into p-932-daren-platform-u…
Browse files Browse the repository at this point in the history
…ser-vc
  • Loading branch information
BillyWooo committed Jul 16, 2024
2 parents 695bbdb + 26acdc2 commit f2050e9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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=",
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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/";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -57,57 +45,48 @@ 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;
}
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;
}
}

0 comments on commit f2050e9

Please sign in to comment.