Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaspai committed Oct 6, 2023
1 parent 15245f7 commit 89ae7a2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/clients/bridges/ArbitrumAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,25 @@ export class ArbitrumAdapter extends BaseAdapter {
);
}

async wrapEthIfAboveThreshold(threshold: BigNumber, simMode = false): Promise<TransactionResponse | null> {
// The arbitrum relayer expects to receive ETH steadily per HubPool bundle processed, since it is the L2 refund
// address hardcoded in the Arbitrum Adapter. Therefore, to avoid excessively wrapping ETH, we only wrap
// it once it gets above a certain threshold.
async wrapEthIfAboveThreshold(target: BigNumber, simMode = false): Promise<TransactionResponse | null> {
const { chainId } = this;
assert(42161 === chainId, `chainId ${chainId} is not supported`);

const weth = CONTRACT_ADDRESSES[this.chainId].weth;
const ethBalance = await this.getSigner(chainId).getBalance();

// For Arbitrum specifically, ETH should accumulate steadily. So reduce ETH balance down to the target
// if it exceeds a threshold.
// TODO: Add a configurable ETH target as opposed to just a threshold.
const threshold = target.mul(10);
if (ethBalance.gt(threshold)) {
const l2Signer = this.getSigner(chainId);
const contract = new Contract(weth.address, weth.abi, l2Signer);
const value = ethBalance.sub(threshold);
this.logger.debug({ at: this.getName(), message: "Wrapping ETH", threshold, value, ethBalance });
const value = ethBalance.sub(target);
this.logger.debug({ at: this.getName(), message: "Wrapping ETH", threshold, target, value, ethBalance });
return await this._wrapEthIfAboveThreshold(threshold, contract, value, simMode);
}
return null;
Expand Down
8 changes: 6 additions & 2 deletions src/clients/bridges/BaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,13 @@ export abstract class BaseAdapter {
const { chainId, txnClient } = this;
const method = "deposit";
const mrkdwn =
`Ether on chain ${this.chainId} was wrapped due to being over the threshold of ` +
`${createFormatFunction(2, 4, false, 18)(toBN(value).toString())} Ether on chain ${
this.chainId
} was wrapped due to being over the threshold of ` +
`${createFormatFunction(2, 4, false, 18)(toBN(wrapThreshold).toString())} ETH.`;
const message = `Eth wrapped on target chain ${this.chainId}🎁`;
const message = `${createFormatFunction(2, 4, false, 18)(toBN(value).toString())} Eth wrapped on target chain ${
this.chainId
}🎁`;
if (simMode) {
const { succeed, reason } = (
await txnClient.simulate([{ contract: l2WEthContract, chainId, method, args: [], value, mrkdwn, message }])
Expand Down

0 comments on commit 89ae7a2

Please sign in to comment.