Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Erc20 contracts #2811

Merged
merged 30 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0e923ee
ERC20 && NativeToken
0xverin Jun 12, 2024
4a6574a
Token
0xverin Jun 12, 2024
07df037
unit tests
0xverin Jun 12, 2024
28376c4
fmt
0xverin Jun 12, 2024
ca7a9be
add erc20(trx) test
0xverin Jun 12, 2024
7349753
update mock server
0xverin Jun 12, 2024
ff90125
add decimals_factor constant
0xverin Jun 13, 2024
b94a2e8
add decimals_factor
0xverin Jun 13, 2024
e6c9a79
native token && erc20 unit tests
0xverin Jun 13, 2024
1ea7f9d
erc20 abstract contract
0xverin Jun 13, 2024
e125635
Add nodereal token (missing)
0xverin Jun 13, 2024
fab3e6b
fix url
0xverin Jun 13, 2024
a4e25f7
add decimals_factor
0xverin Jun 13, 2024
fffeade
Merge branch 'dev' into token-holding-amount
0xverin Jun 13, 2024
0f4d676
fmt && remove unuse code
0xverin Jun 13, 2024
2e72a6c
fix url
0xverin Jun 13, 2024
1c59a5b
Merge branch 'dev' into token-holding-amount
0xverin Jun 14, 2024
6f5fd79
refactor erc20
0xverin Jun 14, 2024
ca99ccd
Merge branch 'token-holding-amount' of https://github.com/litentry/li…
0xverin Jun 14, 2024
6643efe
Fix duplicate loop
0xverin Jun 14, 2024
5ce760b
fix noderal_json_rpc mock server
0xverin Jun 14, 2024
b3111c0
Separate directories
0xverin Jun 14, 2024
05dcb15
fix networkTokenAddresses map
0xverin Jun 14, 2024
bfaeabd
add all tokens
0xverin Jun 14, 2024
c0bc886
add all tokens
0xverin Jun 14, 2024
4e7201c
remove unused imports
0xverin Jun 14, 2024
30b5431
rename Wbtc contract
0xverin Jun 14, 2024
224926a
Merge branch 'dev' into token-holding-amount
0xverin Jun 17, 2024
40915e9
remove unused tests
0xverin Jun 17, 2024
ed158fa
one more
0xverin Jun 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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 "../libraries/Identities.sol";
import "../libraries/Utils.sol";
import { TokenHoldingAmount } from "./TokenHoldingAmount.sol";
import { NoderealClient } from "./NoderealClient.sol";
abstract contract ERC20 is TokenHoldingAmount {
mapping(uint32 => string) internal networkTokenAddresses;

mapping(uint32 => string) internal networkUrls;
mapping(uint32 => bool) private queriedNetworks;
constructor() {
networkUrls[Web3Networks.Bsc] = "https://bsc-mainnet.nodereal.io/v1/";
networkUrls[
Web3Networks.Ethereum
] = "https://eth-mainnet.nodereal.io/v1";
// Add more networks as needed

// below url is used for test against mock server
// "http://localhost:19530/nodereal_jsonrpc/v1/",
}
function getTokenDecimals() internal pure override returns (uint8) {
return 18;
}

function queryBalance(
Identity memory identity,
uint32 network,
string[] memory secrets
) internal virtual override returns (uint256) {
(bool identityToStringSuccess, string memory identityString) = Utils
.identityToString(network, identity.value);

if (identityToStringSuccess) {
string memory url;
uint32[] memory networks = getTokenNetworks();
uint256 totalBalance = 0;

for (uint32 i = 0; i < networks.length; i++) {
// Check if this network has been queried
if (!queriedNetworks[network]) {
string memory _tokenContractAddress = networkTokenAddresses[
network
];

url = string(
abi.encodePacked(networkUrls[networks[i]], secrets[0])
);

(bool success, uint256 balance) = NoderealClient
.getTokenBalance(
url,
_tokenContractAddress,
identityString
);

if (success) {
totalBalance += balance;
}
// Mark this network as queried
queriedNetworks[network] = true;
}
}
return totalBalance;
}
return 0;
}

function getTokenNetworks() internal pure returns (uint32[] memory) {
uint32[] memory networks = new uint32[](2);
networks[0] = Web3Networks.Ethereum;
networks[1] = Web3Networks.Bsc;
// Add more networks as needed
return networks;
}

function isSupportedNetwork(
uint32 network
) internal pure override returns (bool) {
return network == Web3Networks.Bsc || network == Web3Networks.Ethereum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,49 @@ pragma solidity ^0.8.8;

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

library NoderealClient {
function getEthBalance(string memory url, string memory account)
internal
returns (bool, uint256)
{
HttpHeader[] memory headers = new HttpHeader[](0);
string memory request = string(
abi.encodePacked(
'{"jsonrpc": "2.0", "method": "eth_getBalance", "id": 1, "params": ["',
account,
'", "latest"]}'
)
);
(bool result, string memory balance) = Http.PostString(
url,
"/result",
request,
headers
);
if (result) {
return Utils.hexToNumber(balance);
} else {
return (false, 0);
}
}

function getErc20Balance(
string memory url,
string memory tokenContractAddress,
string memory account
) internal returns (bool, uint256) {
HttpHeader[] memory headers = new HttpHeader[](0);
string memory request = string(
abi.encodePacked(
'{"jsonrpc": "2.0", "method": "nr_getTokenBalance20", "id": 1, "params": ["',
tokenContractAddress,
'","',
account,
'", "latest"]}'
)
);
(bool result, string memory balance) = Http.PostString(
url,
"/result",
request,
headers
);
if (result) {
return Utils.hexToNumber(balance);
} else {
return (false, 0);
}
}
function getTokenBalance(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted it to have 1 function per 1 rpc method but that's fine too.

Copy link
Contributor Author

@0xverin 0xverin Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not very convenient to use externally, and requires a lot of if statements, so I merged them here.

string memory url,
string memory tokenContractAddress,
string memory account
) internal returns (bool, uint256) {
HttpHeader[] memory headers = new HttpHeader[](0);
string memory request;
if (
keccak256(bytes(tokenContractAddress)) == keccak256("Native Token")
) {
// Use eth_getBalance method
request = string(
abi.encodePacked(
'{"jsonrpc": "2.0", "method": "eth_getBalance", "id": 1, "params": ["',
account,
'", "latest"]}'
)
);
} else if (bytes(tokenContractAddress).length == 42) {
// Use nr_getTokenBalance20 method
request = string(
abi.encodePacked(
'{"jsonrpc": "2.0", "method": "nr_getTokenBalance20", "id": 1, "params": ["',
tokenContractAddress,
'","',
account,
'", "latest"]}'
)
);
} else {
return (false, 0);
}
(bool result, string memory balance) = Http.PostString(
url,
"/result",
request,
headers
);
if (result) {
return Utils.hexToNumber(balance);
} else {
return (false, 0);
}
}
}
Loading