Skip to content

Commit

Permalink
pay contract: daimo pay batch quote and get balances
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewliu08 committed Nov 8, 2024
1 parent 72fd604 commit 679b506
Show file tree
Hide file tree
Showing 22 changed files with 4,857 additions and 48,110 deletions.

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.

Large diffs are not rendered by default.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

34 changes: 0 additions & 34 deletions packages/contract/script/DeployTokenBalanceUtils.s.sol

This file was deleted.

2 changes: 1 addition & 1 deletion packages/contract/script/deployV2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ set -e
SCRIPTS=(
# Utils
# "script/DeployCreate3Factory.s.sol"
# "script/DeployTokenBalanceUtils.s.sol"
# "script/pay/DeployDaimoPayBatchReadUtils.s.sol"

# Daimo Pay
# "script/pay/DeployDaimoPayAcrossBridger.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 {}
}
61 changes: 0 additions & 61 deletions packages/contract/src/TokenBalanceUtils.sol

This file was deleted.

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

0 comments on commit 679b506

Please sign in to comment.