Skip to content

Commit

Permalink
Merge branch '120-check-asset-balance-before-gas-estimation' into 'dev'
Browse files Browse the repository at this point in the history
fix insufficient asset edge case in EVM

Closes #120

See merge request ergo/rosen-bridge/rosen-chains!145
  • Loading branch information
zargarzadehm committed Oct 12, 2024
2 parents 0219448 + 3265b21 commit 6e46f0d
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 35 deletions.
File renamed without changes.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/chains/ethereum/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @rosen-chains/ethereum

## 0.1.8

### Patch Changes

- Updated dependencies
- @rosen-chains/evm@4.1.1

## 0.1.7

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/chains/ethereum/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/ethereum",
"version": "0.1.7",
"version": "0.1.8",
"description": "this project contains ethereum chain for Rosen-bridge",
"repository": "https://github.com/rosen-bridge/rosen-chains",
"license": "GPL-3.0",
Expand Down Expand Up @@ -31,6 +31,6 @@
"@rosen-bridge/abstract-logger": "^1.0.0",
"@rosen-bridge/tokens": "^1.2.1",
"@rosen-chains/abstract-chain": "^9.0.2",
"@rosen-chains/evm": "^4.1.0"
"@rosen-chains/evm": "^4.1.1"
}
}
6 changes: 6 additions & 0 deletions packages/chains/evm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @rosen-chains/evm

## 4.1.1

### Patch Changes

- check lock address balance for required assets before estimating the tx required gas

## 4.1.0

### Minor Changes
Expand Down
50 changes: 25 additions & 25 deletions packages/chains/evm/lib/EvmChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,33 @@ abstract class EvmChain extends AbstractChain<Transaction> {
}

const gasPrice = await this.network.getMaxFeePerGas();
let totalGas = 0n;

// check the balance in the lock address
const requiredAssets: AssetBalance = ChainUtils.sumAssetBalance(
orderRequiredAssets,
{
nativeToken: this.tokenMap.wrapAmount(
this.NATIVE_TOKEN_ID,
this.configs.gasLimitCap * BigInt(orders.length) * gasPrice,
this.CHAIN
).amount,
tokens: [],
}
);
this.logger.debug(
`Required assets: ${JsonBigInt.stringify(requiredAssets)}`
);

if (!(await this.hasLockAddressEnoughAssets(requiredAssets))) {
const neededETH = requiredAssets.nativeToken.toString();
const neededTokens = JSONBigInt.stringify(requiredAssets.tokens);
throw new NotEnoughAssetsError(
`Locked assets cannot cover required assets. native: ${neededETH}, erc-20: ${neededTokens}`
);
}

// try to generate transactions
let totalGas = 0n;
const maxPriorityFeePerGas = await this.network.getMaxPriorityFeePerGas();
const evmTrxs: Array<PaymentTransaction> = [];
for (const singleOrder of orders) {
Expand Down Expand Up @@ -196,30 +220,6 @@ abstract class EvmChain extends AbstractChain<Transaction> {
nextNonce += 1;
}

// check the balance in the lock address
const requiredAssets: AssetBalance = ChainUtils.sumAssetBalance(
orderRequiredAssets,
{
nativeToken: this.tokenMap.wrapAmount(
this.NATIVE_TOKEN_ID,
totalGas * gasPrice,
this.CHAIN
).amount,
tokens: [],
}
);
this.logger.debug(
`Required assets: ${JsonBigInt.stringify(requiredAssets)}`
);

if (!(await this.hasLockAddressEnoughAssets(requiredAssets))) {
const neededETH = requiredAssets.nativeToken.toString();
const neededTokens = JSONBigInt.stringify(requiredAssets.tokens);
throw new NotEnoughAssetsError(
`Locked assets cannot cover required assets. native: ${neededETH}, erc-20: ${neededTokens}`
);
}

// report result
evmTrxs.forEach((trx) => {
this.logger.info(
Expand Down
2 changes: 1 addition & 1 deletion packages/chains/evm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/evm",
"version": "4.1.0",
"version": "4.1.1",
"description": "this project contains evm chains for Rosen-bridge",
"repository": "https://github.com/rosen-bridge/rosen-chains",
"license": "GPL-3.0",
Expand Down
7 changes: 7 additions & 0 deletions packages/networks/evm-rpc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @rosen-chains/evm-rpc

## 2.1.5

### Patch Changes

- Updated dependencies
- @rosen-chains/evm@4.1.1

## 2.1.4

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/networks/evm-rpc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/evm-rpc",
"version": "2.1.4",
"version": "2.1.5",
"description": "A package to be used as network api provider for @rosen-chains/evm package",
"repository": "https://github.com/rosen-bridge/rosen-chains",
"license": "GPL-3.0",
Expand Down Expand Up @@ -36,7 +36,7 @@
"@rosen-bridge/abstract-logger": "^1.0.0",
"@rosen-bridge/evm-address-tx-extractor": "^1.0.3",
"@rosen-chains/abstract-chain": "^9.0.2",
"@rosen-chains/evm": "^4.1.0",
"@rosen-chains/evm": "^4.1.1",
"typeorm": "^0.3.20"
}
}

0 comments on commit 6e46f0d

Please sign in to comment.