-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contract: get token balances util contract
- Loading branch information
1 parent
0efd4d1
commit 239f552
Showing
13 changed files
with
841 additions
and
10 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
packages/contract/broadcast/DeployTokenBalanceUtils.s.sol/10/run-latest.json
Large diffs are not rendered by default.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
packages/contract/broadcast/DeployTokenBalanceUtils.s.sol/137/run-latest.json
Large diffs are not rendered by default.
Oops, something went wrong.
79 changes: 79 additions & 0 deletions
79
packages/contract/broadcast/DeployTokenBalanceUtils.s.sol/42161/run-latest.json
Large diffs are not rendered by default.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
packages/contract/broadcast/DeployTokenBalanceUtils.s.sol/56/run-latest.json
Large diffs are not rendered by default.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
packages/contract/broadcast/DeployTokenBalanceUtils.s.sol/59144/run-latest.json
Large diffs are not rendered by default.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
packages/contract/broadcast/DeployTokenBalanceUtils.s.sol/8453/run-latest.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
pragma solidity ^0.8.13; | ||
|
||
import "forge-std/Script.sol"; | ||
|
||
import "../src/TokenBalanceUtils.sol"; | ||
import "./Constants.s.sol"; | ||
|
||
contract DeployTokenBalanceUtils is Script { | ||
function run() public { | ||
vm.startBroadcast(); | ||
|
||
address owner = tx.origin; | ||
console.log("owner:", owner); | ||
|
||
address tokenBalanceUtils = CREATE3.deploy( | ||
keccak256("TokenBalanceUtils-test4"), | ||
abi.encodePacked( | ||
type(TokenBalanceUtils).creationCode, | ||
abi.encode(owner) | ||
) | ||
); | ||
|
||
vm.stopBroadcast(); | ||
|
||
console.log( | ||
"token balance utils deployed at address:", | ||
tokenBalanceUtils | ||
); | ||
} | ||
|
||
// Exclude from forge coverage | ||
function test() public {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
pragma solidity ^0.8.12; | ||
|
||
import "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; | ||
import "openzeppelin-contracts/contracts/access/Ownable.sol"; | ||
|
||
contract TokenBalanceUtils is Ownable { | ||
IERC20[] public tokens; | ||
|
||
constructor(address initialOwner) Ownable(initialOwner) {} | ||
|
||
function getAllTokens() public view returns (IERC20[] memory) { | ||
return tokens; | ||
} | ||
|
||
function setTokens(IERC20[] memory _tokens) public onlyOwner { | ||
tokens = _tokens; | ||
} | ||
|
||
/** | ||
* @notice Get the balances for all saved tokens and the balance of the | ||
* native asset for an owner | ||
* @param owner The owner of the tokens | ||
* @return balances An array of balances, where the last element is the | ||
* balance of the native asset | ||
*/ | ||
function getTokenBalances( | ||
address owner | ||
) public view returns (uint256[] memory balances) { | ||
uint256 n = tokens.length; | ||
|
||
balances = new uint256[](n + 1); | ||
for (uint256 i = 0; i < n; ++i) { | ||
balances[i] = tokens[i].balanceOf(owner); | ||
} | ||
|
||
balances[n] = owner.balance; | ||
} | ||
|
||
/** | ||
* @notice Get the balances for a custom list of tokens and the balance of | ||
* the native asset for an owner | ||
* @param owner The owner of the tokens | ||
* @param tokenList The list of token addresses to get the balance of | ||
* @return balances An array of balances, where the last element is the | ||
* balance of the native asset | ||
*/ | ||
function getTokenBalancesBatch( | ||
address owner, | ||
IERC20[] calldata tokenList | ||
) public view returns (uint256[] memory balances) { | ||
uint256 n = tokenList.length; | ||
|
||
balances = new uint256[](n + 1); | ||
for (uint256 i = 0; i < n; ++i) { | ||
balances[i] = tokenList[i].balanceOf(owner); | ||
} | ||
|
||
balances[n] = owner.balance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.