Skip to content

Commit

Permalink
update abi, add gasreserve
Browse files Browse the repository at this point in the history
  • Loading branch information
KorbinianK committed Apr 19, 2024
1 parent 76abf15 commit ae9c2b0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 23 deletions.
67 changes: 61 additions & 6 deletions packages/bridge-ui/src/abi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,15 +525,30 @@ export const bridgeAbi = [
anonymous: false,
inputs: [
{
name: 'messageId',
internalType: 'uint256',
type: 'uint256',
name: 'msgHash',
internalType: 'bytes32',
type: 'bytes32',
indexed: true,
},
{
name: 'gasMeasured',
internalType: 'uint256',
type: 'uint256',
name: 'stats',
internalType: 'struct Bridge.GasStats',
type: 'tuple',
components: [
{ name: 'start', internalType: 'uint256', type: 'uint256' },
{
name: 'beforeInvocation',
internalType: 'uint256',
type: 'uint256',
},
{ name: 'afterInvocation', internalType: 'uint256', type: 'uint256' },
{
name: 'gasUsedInFeeCalc',
internalType: 'uint256',
type: 'uint256',
},
{ name: 'end', internalType: 'uint256', type: 'uint256' },
],
indexed: false,
},
{
Expand Down Expand Up @@ -565,6 +580,32 @@ export const bridgeAbi = [
],
name: 'Initialized',
},
{
type: 'event',
anonymous: false,
inputs: [
{
name: 'message',
internalType: 'struct IBridge.Message',
type: 'tuple',
components: [
{ name: 'id', internalType: 'uint64', type: 'uint64' },
{ name: 'fee', internalType: 'uint64', type: 'uint64' },
{ name: 'gasLimit', internalType: 'uint32', type: 'uint32' },
{ name: 'from', internalType: 'address', type: 'address' },
{ name: 'srcChainId', internalType: 'uint64', type: 'uint64' },
{ name: 'srcOwner', internalType: 'address', type: 'address' },
{ name: 'destChainId', internalType: 'uint64', type: 'uint64' },
{ name: 'destOwner', internalType: 'address', type: 'address' },
{ name: 'to', internalType: 'address', type: 'address' },
{ name: 'value', internalType: 'uint256', type: 'uint256' },
{ name: 'data', internalType: 'bytes', type: 'bytes' },
],
indexed: false,
},
],
name: 'MessageProcessed',
},
{
type: 'event',
anonymous: false,
Expand Down Expand Up @@ -3174,6 +3215,13 @@ export const erc20Abi = [
outputs: [{ name: '', internalType: 'uint48', type: 'uint48' }],
stateMutability: 'view',
},
{
type: 'function',
inputs: [],
name: 'currentSnapshotId',
outputs: [{ name: '', internalType: 'uint256', type: 'uint256' }],
stateMutability: 'view',
},
{
type: 'function',
inputs: [],
Expand Down Expand Up @@ -3283,6 +3331,13 @@ export const erc20Abi = [
outputs: [],
stateMutability: 'nonpayable',
},
{
type: 'function',
inputs: [{ name: 'addr', internalType: 'address', type: 'address' }],
name: 'isAuthorizedForSnapshot',
outputs: [{ name: '', internalType: 'bool', type: 'bool' }],
stateMutability: 'view',
},
{
type: 'function',
inputs: [],
Expand Down
1 change: 1 addition & 0 deletions packages/bridge-ui/src/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const recommendProcessingFeeConfig = {
GAS_RESERVE: 650_000, // based on Bridge.sol
ethGasLimit: BigInt(900_000),
erc20NotDeployedGasLimit: BigInt(1_650_000),
erc20DeployedGasLimit: BigInt(1_100_000),
Expand Down
14 changes: 5 additions & 9 deletions packages/bridge-ui/src/libs/bridge/ERC20Bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ export class ERC20Bridge extends Bridge {
});

const gasLimit = !isTokenAlreadyDeployed
? BigInt(bridgeService.noERC20TokenDeployedGasLimit)
? Number(bridgeService.noERC20TokenDeployedGasLimit)
: fee > 0
? bridgeService.noOwnerGasLimit
? Number(bridgeService.noOwnerGasLimit)
: BigInt(0);

log('Calculated gasLimit for message', gasLimit);

const sendERC20Args: BridgeTransferOp = {
destChainId: BigInt(destChainId),
destOwner: to,
Expand Down Expand Up @@ -127,13 +129,7 @@ export class ERC20Bridge extends Bridge {

if (!wallet || !wallet.account || !wallet.chain) throw new Error('Wallet is not connected');

const txHash = await writeContract(config, {
address: tokenAddress,
abi: erc20Abi,
functionName: 'approve',
args: [spenderAddress, amount],
chainId: wallet.chain.id,
});
const txHash = await writeContract(config, request);

log('Transaction hash for approve call', txHash);

Expand Down
19 changes: 11 additions & 8 deletions packages/bridge-ui/src/libs/bridge/ETHBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getWalletClient, simulateContract, writeContract } from '@wagmi/core';
import { getContract, UserRejectedRequestError } from 'viem';

import { bridgeAbi } from '$abi';
import { recommendProcessingFeeConfig } from '$config';
import { BridgePausedError, SendMessageError } from '$libs/error';
import type { BridgeProver } from '$libs/proof';
import { isBridgePaused } from '$libs/util/checkForPausedContracts';
Expand Down Expand Up @@ -56,16 +57,18 @@ export class ETHBridge extends Bridge {
id: BigInt(0), // will be set in contract
};

const getMinGasLimit = await bridgeContract.read.getMessageMinGasLimit([message]);
log('Min gas limit for message', getMinGasLimit);

// set msg.gasLimit to 105% of getMinGasLimit
const gasLimitPercentage = 105;
const getMinGasLimitPercentage = (getMinGasLimit * gasLimitPercentage) / 100;
// round it to the nearest integer
const gasLimit = Math.round(getMinGasLimitPercentage);
const minGasLimit = await bridgeContract.read.getMessageMinGasLimit([message]);
log('Min gas limit for message', minGasLimit);

const gasLimit = recommendProcessingFeeConfig.GAS_RESERVE + minGasLimit;
log('Calculated gasLimit for message', gasLimit);

// // set msg.gasLimit to 105% of getMinGasLimit
// const gasLimitPercentage = 105;
// const getMinGasLimitPercentage = (getMinGasLimit * gasLimitPercentage) / 100;
// // round it to the nearest integer
// const gasLimit = Math.round(getMinGasLimitPercentage);

message.gasLimit = gasLimit;

log('Preparing transaction with message', message);
Expand Down

0 comments on commit ae9c2b0

Please sign in to comment.