add: smart-contracts

This commit is contained in:
Mohamed Sohail 2022-05-14 11:16:47 +03:00
parent 098ad25b39
commit 2c8c45ba17
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
3 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,48 @@
// SPDX-License-Identifier: MIT
// Adapted from https://github.com/MyCryptoHQ/eth-scan
pragma solidity >= 0.8.0;
contract Balances {
struct Result {
bool success;
uint256 balance;
}
function tokensBalance(address owner, address[] calldata contracts) external view returns (Result[] memory results) {
results = new Result[](contracts.length);
bytes memory data = abi.encodeWithSignature("balanceOf(address)", owner);
for (uint256 i = 0; i < contracts.length; i++) {
results[i] = staticCall(contracts[i], data, 8000000);
}
}
function staticCall(
address target,
bytes memory data,
uint256 gas
) private view returns (Result memory) {
uint256 size = codeSize(target);
if (size > 0) {
(bool success, bytes memory result) = target.staticcall{ gas: gas }(data);
if (success) {
uint256 balance = abi.decode(result, (uint256));
return Result(success, balance);
}
}
return Result(false, 0);
}
function codeSize(address _address) private view returns (uint256 size) {
// solhint-disable-next-line no-inline-assembly
assembly {
size := extcodesize(_address)
}
}
}

View File

@ -0,0 +1,38 @@
[
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "address[]",
"name": "contracts",
"type": "address[]"
}
],
"name": "tokensBalance",
"outputs": [
{
"components": [
{
"internalType": "bool",
"name": "success",
"type": "bool"
},
{
"internalType": "uint256",
"name": "balance",
"type": "uint256"
}
],
"internalType": "struct Balances.Result[]",
"name": "results",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
}
]

File diff suppressed because one or more lines are too long