-
Notifications
You must be signed in to change notification settings - Fork 55
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
feat: hardhat task to test chain adapter token bridging #669
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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,89 @@ | ||
import assert from "assert"; | ||
import { getMnemonic } from "@uma/common"; | ||
import { task } from "hardhat/config"; | ||
import { HardhatRuntimeEnvironment } from "hardhat/types"; | ||
import { CHAIN_IDs, TOKEN_SYMBOLS_MAP } from "../utils/constants"; | ||
import { askYesNoQuestion } from "./utils"; | ||
|
||
// Chain adapter names are not 1:1 consistent with chain names, so some overrides are needed. | ||
const chains = { | ||
[CHAIN_IDs.ARBITRUM]: "Arbitrum_Adapter", | ||
[CHAIN_IDs.ALEPH_ZERO]: "Arbitrum_CustomGasToken_Adapter", | ||
[CHAIN_IDs.WORLD_CHAIN]: "WorldChain_Adapter", | ||
[CHAIN_IDs.ZK_SYNC]: "ZkSync_Adapter", | ||
}; | ||
|
||
task("testChainAdapter", "Verify a chain adapter") | ||
.addParam("chain", "chain ID of the adapter being tested") | ||
.addParam("token", "Token to bridge to the destination chain") | ||
.addParam("amount", "Amount to bridge to the destination chain") | ||
.setAction(async function (args, hre: HardhatRuntimeEnvironment) { | ||
const { deployments, ethers, getChainId, network } = hre; | ||
const provider = new ethers.providers.StaticJsonRpcProvider(network.config.url); | ||
const signer = new ethers.Wallet.fromMnemonic(getMnemonic()).connect(provider); | ||
|
||
const hubChainId = await getChainId(); | ||
const { address: hubPoolAddress, abi: hubPoolAbi } = await deployments.get("HubPool"); | ||
const hubPool = new ethers.Contract(hubPoolAddress, hubPoolAbi, provider); | ||
const spokeChainId = parseInt(args.chain); | ||
|
||
const [spokeName] = Object.entries(CHAIN_IDs).find(([, chainId]) => chainId === spokeChainId) ?? []; | ||
assert(spokeName, `Could not find any chain entry for chainId ${spokeChainId}.`); | ||
const adapterName = | ||
chains[spokeChainId] ?? `${spokeName[0].toUpperCase()}${spokeName.slice(1).toLowerCase()}_Adapter`; | ||
|
||
const { address: adapterAddress, abi: adapterAbi } = await deployments.get(adapterName); | ||
const adapter = new ethers.Contract(adapterAddress, adapterAbi, provider); | ||
const tokenSymbol = args.token.toUpperCase(); | ||
const tokenAddress = TOKEN_SYMBOLS_MAP[tokenSymbol].addresses[hubChainId]; | ||
|
||
// For USDC this will resolve to native USDC on CCTP-enabled chains. | ||
const l2Token = await hubPool.poolRebalanceRoute(spokeChainId, tokenAddress); | ||
if (l2Token === ethers.constants.AddressZero) { | ||
const proceed = await askYesNoQuestion( | ||
`\t\nWARNING: ${tokenSymbol} maps to address ${l2Token} on chain ${spokeChainId}\n\t\nProceed ?` | ||
); | ||
if (!proceed) process.exit(0); | ||
} else { | ||
console.log(`Resolved ${tokenSymbol} l2 token address on chain ${spokeChainId}: ${l2Token}.`); | ||
} | ||
|
||
const erc20 = (await ethers.getContractFactory("ExpandedERC20")).attach(tokenAddress); | ||
let balance = await erc20.balanceOf(adapterAddress); | ||
const decimals = await erc20.decimals(); | ||
const { amount } = args; | ||
const scaledAmount = ethers.utils.parseUnits(amount, decimals); | ||
|
||
if (balance.lt(amount)) { | ||
const proceed = await askYesNoQuestion( | ||
`\t\nWARNING: ${amount} ${tokenSymbol} may be lost.\n` + | ||
`\t\nProceed to send ${amount} ${tokenSymbol} to chain adapter ${adapterAddress} ?` | ||
); | ||
if (!proceed) process.exit(0); | ||
|
||
const txn = await erc20.connect(signer).transfer(adapterAddress, scaledAmount); | ||
console.log(`Transferring ${amount} ${tokenSymbol} -> ${adapterAddress}: ${txn.hash}`); | ||
await txn.wait(); | ||
} | ||
|
||
balance = await erc20.balanceOf(adapterAddress); | ||
const recipient = await signer.getAddress(); | ||
|
||
let populatedTxn = await adapter.populateTransaction.relayTokens(tokenAddress, l2Token, balance, recipient); | ||
const gasLimit = await provider.estimateGas(populatedTxn); | ||
|
||
// Any adapter requiring msg.value > 0 (i.e. Scroll) will fail here. | ||
const txn = await adapter.connect(signer).relayTokens( | ||
tokenAddress, | ||
l2Token, | ||
balance, | ||
recipient, | ||
{ gasLimit: gasLimit.mul(2) } // 2x the gas limit; this helps on OP stack bridges. | ||
); | ||
|
||
console.log( | ||
`Relaying ${balance} ${tokenSymbol} from ${adapterAddress}` + | ||
` to chain ${spokeChainId} recipient ${recipient}: ${txn.hash}.` | ||
); | ||
await txn.wait(); | ||
}); |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably
addParam("amount", ...)
here to allow the user to specify the amount to bridge.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
8cae0b6