Skip to content

Commit

Permalink
Properly check that native bridging was successful
Browse files Browse the repository at this point in the history
  • Loading branch information
gvladika committed Jul 9, 2024
1 parent ba9f2f9 commit 3af86a0
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions scripts/ethcommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ async function bridgeNativeToken(argv: any, parentChainUrl: string, chainUrl: st

argv.to = "address_" + inboxAddr;

// snapshot balance before deposit
const childProvider = new ethers.providers.WebSocketProvider(chainUrl);
const bridger = namedAccount(argv.from, argv.threadId).connect(childProvider)
const bridgerBalanceBefore = await bridger.getBalance()

// get token contract
const bridgerParentChain = namedAccount(argv.from, argv.threadId).connect(argv.provider)
const nativeTokenContract = new ethers.Contract(token, ERC20.abi, bridgerParentChain)
Expand All @@ -77,12 +82,25 @@ async function bridgeNativeToken(argv: any, parentChainUrl: string, chainUrl: st

argv.provider.destroy();
if (argv.wait) {
const childProvider = new ethers.providers.WebSocketProvider(chainUrl);
const bridger = namedAccount(argv.from, argv.threadId).connect(childProvider)
const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));

// calculate amount being minted on child chain
let expectedMintedAmount = depositAmount
if(decimals < 18) {
// inflate up to 18 decimals
expectedMintedAmount = depositAmount.mul(BigNumber.from('10').pow(18 - decimals))
} else if(decimals > 18) {
// deflate down to 18 decimals, rounding up
const quotient = BigNumber.from('10').pow(decimals - 18)
expectedMintedAmount = depositAmount.div(quotient)
if(expectedMintedAmount.mul(quotient).lt(depositAmount)) {
expectedMintedAmount = expectedMintedAmount.add(1)
}
}

while (true) {
const balance = await bridger.getBalance()
if (balance.gte(0)) {
const bridgerBalanceAfter = await bridger.getBalance()
if (bridgerBalanceAfter.sub(bridgerBalanceBefore).eq(expectedMintedAmount)) {
return
}
await sleep(100)
Expand Down

0 comments on commit 3af86a0

Please sign in to comment.