mirror of
https://github.com/grassrootseconomics/cic-go.git
synced 2024-11-23 06:16:46 +01:00
add: smart-contracts
This commit is contained in:
parent
098ad25b39
commit
2c8c45ba17
48
smart-contracts/on-chain-balances-resolver/Balances.sol
Normal file
48
smart-contracts/on-chain-balances-resolver/Balances.sol
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
Loading…
Reference in New Issue
Block a user