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

contract: get token balances util contract #1367

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"transactions": [
{
"hash": "0x7b2858178d1d8415bcadda3cae98addf8bda4bc82e7a907d9b98642e3173d71f",
"transactionType": "CALL",
"contractName": null,
"contractAddress": "0xe554fe204af7b99e566fc86ee9b4f34f8364b7b1",
"function": "setTokens(address[])",
"arguments": [
"[0x176211869cA2b568f2A7D4EE941E073a821EE1ff, 0x3aAB2285ddcDdaD8edf438C1bAB47e1a9D05a9b4, 0x636B22bC471c955A8DB60f28D4795066a8201fa3, 0xA219439258ca9da29E9Cc4cE5596924745e12B93, 0xe5D7C2a44FfDDf6b295A15c148167daaAf5Cf34f, 0xEB466342C4d449BC9f53A865D5Cb90586f405215]"
],
"transaction": {
"from": "0x2f321372e8a9755cd2ca6114eb8da32a14f8100b",
"to": "0xe554fe204af7b99e566fc86ee9b4f34f8364b7b1",
"gas": "0x3d6ee",
"value": "0x0",
"input": "0x625adaf200000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000006000000000000000000000000176211869ca2b568f2a7d4ee941e073a821ee1ff0000000000000000000000003aab2285ddcddad8edf438c1bab47e1a9d05a9b4000000000000000000000000636b22bc471c955a8db60f28d4795066a8201fa3000000000000000000000000a219439258ca9da29e9cc4ce5596924745e12b93000000000000000000000000e5d7c2a44ffddf6b295a15c148167daaaf5cf34f000000000000000000000000eb466342c4d449bc9f53a865d5cb90586f405215",
"nonce": "0xbb",
"chainId": "0xe708"
},
"additionalContracts": [],
"isFixedGasLimit": false
}
],
"receipts": [
{
"status": "0x1",
"cumulativeGasUsed": "0x67d54",
"logs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"type": "0x2",
"transactionHash": "0x7b2858178d1d8415bcadda3cae98addf8bda4bc82e7a907d9b98642e3173d71f",
"transactionIndex": "0x3",
"blockHash": "0xb6af3a3f3dbe5a7bc700b4e10c7fe5b8314fb17fc193c271ad79bcac5eee391d",
"blockNumber": "0xb4390d",
"gasUsed": "0x2c7a0",
"effectiveGasPrice": "0x122dee40",
"from": "0x2f321372e8a9755cd2ca6114eb8da32a14f8100b",
"to": "0xe554fe204af7b99e566fc86ee9b4f34f8364b7b1",
"contractAddress": null
}
],
"libraries": [],
"pending": [],
"returns": {},
"timestamp": 1731098711,
"chain": 59144,
"commit": "679b506f"
}

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions packages/contract/script/deployV2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ set -e
# ETHERSCAN_API_KEY_... for each target chain

SCRIPTS=(
# CREATE3
# Utils
# "script/DeployCreate3Factory.s.sol"
# "script/pay/DeployDaimoPayBatchReadUtils.s.sol"

# Daimo Pay.
# Daimo Pay
# "script/pay/DeployDaimoPayAcrossBridger.s.sol"
# "script/pay/DeployDaimoPayCCTPBridger.s.sol"
# "script/pay/DeployDaimoPayAxelarBridger.s.sol"
Expand Down
70 changes: 70 additions & 0 deletions packages/contract/script/pay/DeployDaimoPayBatchReadUtils.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.13;

import "forge-std/Script.sol";

import "../../src/pay/DaimoPayBatchReadUtils.sol";
import "../Constants.s.sol";

contract DeployDaimoPayBatchReadUtils is Script {
function run() public {
vm.startBroadcast();

address owner = tx.origin;
address quoteToken = _getQuoteToken(block.chainid);
uint128 quoteAmount = _getQuoteAmount(block.chainid);
address dfs = _getFlexSwapperAddress(block.chainid);

console.log("owner:", owner);
console.log("quoteToken:", quoteToken);
console.log("quoteAmount:", quoteAmount);
console.log("dfs:", dfs);

address daimoPayBatchReadUtils = CREATE3.deploy(
keccak256("DaimoPayBatchReadUtils-test5"),
abi.encodePacked(
type(DaimoPayBatchReadUtils).creationCode,
abi.encode(owner, quoteToken, quoteAmount, dfs)
)
);

vm.stopBroadcast();

console.log(
"daimo pay batch read utils deployed at address:",
daimoPayBatchReadUtils
);
}

function _getQuoteToken(uint256 chainId) internal pure returns (address) {
if (chainId == LINEA_MAINNET) {
return LINEA_MAINNET_BRIDGED_USDC;
} else if (chainId == BSC_MAINNET) {
return BSC_MAINNET_BRIDGED_USDC;
} else {
return _getUSDCAddress(chainId);
}
}

function _getQuoteAmount(uint256 chainId) internal pure returns (uint128) {
// 10,000 USDC
if (chainId == BSC_MAINNET) {
return 10_000_000_000_000_000_000_000; // BSC USDC has 18 decimals
} else {
return 10_000_000_000; // Other chains have 6 decimals
}
}

function _getFlexSwapperAddress(
uint256 chainId
) internal pure returns (address) {
if (chainId == ETH_MAINNET) {
return 0x207e87f84cff325715f324d09E63b21a03E53b61;
} else {
return 0xA9F5d58edb8dF8af90f875eac89AA49C57b87Db8;
}
}

// Exclude from forge coverage
function test() public {}
}
144 changes: 144 additions & 0 deletions packages/contract/src/pay/DaimoPayBatchReadUtils.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// 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/token/ERC20/extensions/IERC20Metadata.sol";
import "openzeppelin-contracts/contracts/access/Ownable.sol";

import "../DaimoFlexSwapper.sol";

contract DaimoPayBatchReadUtils is Ownable {
IERC20[] public tokens;
IERC20 public quoteToken;
uint128 public quoteAmount;
DaimoFlexSwapper public dfs;

constructor(
address initialOwner,
IERC20 _quoteToken,
uint128 _quoteAmount,
address _dfs
) Ownable(initialOwner) {
quoteToken = _quoteToken;
quoteAmount = _quoteAmount;
dfs = DaimoFlexSwapper(_dfs);
}

function getAllTokens() public view returns (IERC20[] memory) {
return tokens;
}

function setTokens(IERC20[] memory _tokens) public onlyOwner {
tokens = _tokens;
}

function setQuoterValues(
IERC20 _quoteToken,
uint128 _quoteAmount,
DaimoFlexSwapper _dfs
) public onlyOwner {
quoteToken = _quoteToken;
quoteAmount = _quoteAmount;
dfs = _dfs;
}

/**
* @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;
}

/**
* @notice Get the quotes for all saved tokens using the DaimoFlexSwapper.
* @return amountOut The amount of tokens output for swapping quoteAmount of
* quoteToken.
*/
function getQuotes() public view returns (uint256[] memory amountOut) {
uint256 n = tokens.length;

amountOut = new uint256[](n);
for (uint256 i = 0; i < n; ++i) {
(amountOut[i], ) = dfs.quote({
tokenIn: quoteToken,
amountIn: quoteAmount,
tokenOut: tokens[i]
});
}
}

/**
* @notice Get the quotes for a custom list of tokens using the DaimoFlexSwapper.
* @return amountOut The amount of tokens output for swapping quoteAmount of
* quoteToken.
*/
function getQuotesBatch(
IERC20[] calldata tokenList,
IERC20 _quoteToken,
uint128 _quoteAmount,
DaimoFlexSwapper _dfs
) public view returns (uint256[] memory amountOut) {
uint256 n = tokenList.length;

amountOut = new uint256[](n);
for (uint256 i = 0; i < n; ++i) {
(amountOut[i], ) = _dfs.quote({
tokenIn: _quoteToken,
amountIn: _quoteAmount,
tokenOut: tokenList[i]
});
}
}

/**
* @notice Get the balances and quotes for all saved tokens.
* @return balances The balances of the tokens
* @return amountOut The amount of tokens output for swapping quoteAmount of
* quoteToken.
*/
function getBalancesAndQuotes(
address owner
)
public
view
returns (uint256[] memory balances, uint256[] memory amountOut)
{
balances = getTokenBalances(owner);
amountOut = getQuotes();
}
}
Loading
Loading