From e03fb16dbeae59a78d9ecc2eec3b60a694f89217 Mon Sep 17 00:00:00 2001 From: Santiago Pittella Date: Wed, 10 Jan 2024 10:13:53 +0100 Subject: [PATCH 1/3] uncomment non native erc20 deposits --- src/adapters.ts | 142 ++++++++++++++++++++++++------------------------ 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/src/adapters.ts b/src/adapters.ts index 2148d751d516..c3716b4c34af 100644 --- a/src/adapters.ts +++ b/src/adapters.ts @@ -186,10 +186,10 @@ export function AdapterL1>(Base: TBase) { overrides?: ethers.PayableOverrides; approveOverrides?: ethers.Overrides; customBridgeData?: BytesLike; - }): Promise { - const depositTx = await this.getDepositTx(transaction); - - // if (transaction.token == ETH_ADDRESS) { + }, nativeERC20?: Address): Promise { + const depositTx = await this.getDepositTx(transaction, nativeERC20); + + if (transaction.token == ETH_ADDRESS || nativeERC20 == transaction.token) { const baseGasLimit = await this.estimateGasRequestExecute(depositTx); const gasLimit = scaleGasLimit(baseGasLimit); @@ -197,46 +197,46 @@ export function AdapterL1>(Base: TBase) { depositTx.overrides.gasLimit ??= gasLimit; return this.requestExecute(depositTx); - // } else { - // const bridgeContracts = await this.getL1BridgeContracts(); - // if (transaction.approveERC20) { - // let l2WethToken = ethers.constants.AddressZero; - // try { - // l2WethToken = await bridgeContracts.weth.l2TokenAddress(transaction.token); - // } catch (e) {} - // // If the token is Wrapped Ether, use its bridge. - // const proposedBridge = - // l2WethToken != ethers.constants.AddressZero - // ? bridgeContracts.weth.address - // : bridgeContracts.erc20.address; - // const bridgeAddress = transaction.bridgeAddress - // ? transaction.bridgeAddress - // : proposedBridge; - - // // We only request the allowance if the current one is not enough. - // const allowance = await this.getAllowanceL1(transaction.token, bridgeAddress); - // if (allowance.lt(transaction.amount)) { - // const approveTx = await this.approveERC20( - // transaction.token, - // transaction.amount, - // { - // bridgeAddress, - // ...transaction.approveOverrides, - // }, - // ); - // await approveTx.wait(); - // } - // } - - // const baseGasLimit = await this._providerL1().estimateGas(depositTx); - // const gasLimit = scaleGasLimit(baseGasLimit); - - // depositTx.gasLimit ??= gasLimit; - - // return await this._providerL2().getPriorityOpResponse( - // await this._signerL1().sendTransaction(depositTx), - // ); - // } + } else { + const bridgeContracts = await this.getL1BridgeContracts(); + if (transaction.approveERC20) { + let l2WethToken = ethers.constants.AddressZero; + try { + l2WethToken = await bridgeContracts.weth.l2TokenAddress(transaction.token); + } catch (e) {} + // If the token is Wrapped Ether, use its bridge. + const proposedBridge = + l2WethToken != ethers.constants.AddressZero + ? bridgeContracts.weth.address + : bridgeContracts.erc20.address; + const bridgeAddress = transaction.bridgeAddress + ? transaction.bridgeAddress + : proposedBridge; + + // We only request the allowance if the current one is not enough. + const allowance = await this.getAllowanceL1(transaction.token, bridgeAddress); + if (allowance.lt(transaction.amount)) { + const approveTx = await this.approveERC20( + transaction.token, + transaction.amount, + { + bridgeAddress, + ...transaction.approveOverrides, + }, + ); + await approveTx.wait(); + } + } + + const baseGasLimit = await this._providerL1().estimateGas(depositTx); + const gasLimit = scaleGasLimit(baseGasLimit); + + depositTx.gasLimit ??= gasLimit; + + return await this._providerL2().getPriorityOpResponse( + await this._signerL1().sendTransaction(depositTx), + ); + } } async estimateGasDeposit(transaction: { @@ -274,7 +274,7 @@ export function AdapterL1>(Base: TBase) { customBridgeData?: BytesLike; refundRecipient?: Address; overrides?: ethers.PayableOverrides; - }): Promise { + }, nativeERC20?: Address): Promise { const bridgeContracts = await this.getL1BridgeContracts(); if (transaction.bridgeAddress != null) { bridgeContracts.erc20 = bridgeContracts.erc20.attach(transaction.bridgeAddress); @@ -328,7 +328,7 @@ export function AdapterL1>(Base: TBase) { tx.gasPerPubdataByte, ); - // if (token == ETH_ADDRESS) { + if (token == ETH_ADDRESS || nativeERC20 == token) { overrides.value ??= baseCost.add(operatorTip).add(amount); return { @@ -340,31 +340,31 @@ export function AdapterL1>(Base: TBase) { l2GasLimit: tx.l2GasLimit!, ...tx, }; - // } else { - // let refundRecipient = tx.refundRecipient ?? ethers.constants.AddressZero; - // const args: [Address, Address, BigNumberish, BigNumberish, BigNumberish, Address] = [ - // to, - // token, - // amount, - // tx.l2GasLimit, - // tx.gasPerPubdataByte, - // refundRecipient, - // ]; - - // overrides.value ??= baseCost.add(operatorTip); - // await checkBaseCost(baseCost, overrides.value); - - // let l2WethToken = ethers.constants.AddressZero; - // try { - // l2WethToken = await bridgeContracts.weth.l2TokenAddress(tx.token); - // } catch (e) {} - - // const bridge = - // l2WethToken != ethers.constants.AddressZero - // ? bridgeContracts.weth - // : bridgeContracts.erc20; - // return await bridge.populateTransaction.deposit(...args, overrides); - // } + } else { + let refundRecipient = tx.refundRecipient ?? ethers.constants.AddressZero; + const args: [Address, Address, BigNumberish, BigNumberish, BigNumberish, Address] = [ + to, + token, + amount, + tx.l2GasLimit, + tx.gasPerPubdataByte, + refundRecipient, + ]; + + overrides.value ??= baseCost.add(operatorTip); + await checkBaseCost(baseCost, overrides.value); + + let l2WethToken = ethers.constants.AddressZero; + try { + l2WethToken = await bridgeContracts.weth.l2TokenAddress(tx.token); + } catch (e) {} + + const bridge = + l2WethToken != ethers.constants.AddressZero + ? bridgeContracts.weth + : bridgeContracts.erc20; + return await bridge.populateTransaction.deposit(...args, overrides); + } } // Retrieves the full needed ETH fee for the deposit. From 1ebcaef7ba7a31cce2a62b956e51a3aa2e373879 Mon Sep 17 00:00:00 2001 From: Santiago Pittella Date: Wed, 10 Jan 2024 10:24:42 +0100 Subject: [PATCH 2/3] add build --- build/src/adapters.d.ts | 4 +- build/src/adapters.js | 158 ++++++++++++++++++++-------------------- build/src/signer.d.ts | 8 +- build/src/wallet.d.ts | 4 +- src/adapters.ts | 92 ++++++++++++----------- 5 files changed, 134 insertions(+), 132 deletions(-) diff --git a/build/src/adapters.d.ts b/build/src/adapters.d.ts index 11bf5ed9c3d6..327c5e65c3f1 100644 --- a/build/src/adapters.d.ts +++ b/build/src/adapters.d.ts @@ -40,7 +40,7 @@ export declare function AdapterL1>(Base: TBa overrides?: ethers.PayableOverrides; approveOverrides?: ethers.Overrides; customBridgeData?: BytesLike; - }): Promise; + }, nativeERC20?: Address): Promise; estimateGasDeposit(transaction: { token: Address; amount: BigNumberish; @@ -64,7 +64,7 @@ export declare function AdapterL1>(Base: TBa customBridgeData?: BytesLike; refundRecipient?: Address; overrides?: ethers.PayableOverrides; - }): Promise; + }, nativeERC20?: Address): Promise; getFullRequiredDepositFee(transaction: { token: Address; to?: Address; diff --git a/build/src/adapters.js b/build/src/adapters.js index a492c59ee365..f5421d1461ed 100644 --- a/build/src/adapters.js +++ b/build/src/adapters.js @@ -103,52 +103,47 @@ function AdapterL1(Base) { (_b = parameters.gasPerPubdataByte) !== null && _b !== void 0 ? _b : (parameters.gasPerPubdataByte = utils_1.REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_LIMIT); return ethers_1.BigNumber.from(await zksyncContract.l2TransactionBaseCost(parameters.gasPrice, parameters.gasLimit, parameters.gasPerPubdataByte)); } - async deposit(transaction) { - var _a, _b; - var _c; - const depositTx = await this.getDepositTx(transaction); - // if (transaction.token == ETH_ADDRESS) { - const baseGasLimit = await this.estimateGasRequestExecute(depositTx); - const gasLimit = (0, utils_1.scaleGasLimit)(baseGasLimit); - (_a = depositTx.overrides) !== null && _a !== void 0 ? _a : (depositTx.overrides = {}); - (_b = (_c = depositTx.overrides).gasLimit) !== null && _b !== void 0 ? _b : (_c.gasLimit = gasLimit); - return this.requestExecute(depositTx); - // } else { - // const bridgeContracts = await this.getL1BridgeContracts(); - // if (transaction.approveERC20) { - // let l2WethToken = ethers.constants.AddressZero; - // try { - // l2WethToken = await bridgeContracts.weth.l2TokenAddress(transaction.token); - // } catch (e) {} - // // If the token is Wrapped Ether, use its bridge. - // const proposedBridge = - // l2WethToken != ethers.constants.AddressZero - // ? bridgeContracts.weth.address - // : bridgeContracts.erc20.address; - // const bridgeAddress = transaction.bridgeAddress - // ? transaction.bridgeAddress - // : proposedBridge; - // // We only request the allowance if the current one is not enough. - // const allowance = await this.getAllowanceL1(transaction.token, bridgeAddress); - // if (allowance.lt(transaction.amount)) { - // const approveTx = await this.approveERC20( - // transaction.token, - // transaction.amount, - // { - // bridgeAddress, - // ...transaction.approveOverrides, - // }, - // ); - // await approveTx.wait(); - // } - // } - // const baseGasLimit = await this._providerL1().estimateGas(depositTx); - // const gasLimit = scaleGasLimit(baseGasLimit); - // depositTx.gasLimit ??= gasLimit; - // return await this._providerL2().getPriorityOpResponse( - // await this._signerL1().sendTransaction(depositTx), - // ); - // } + async deposit(transaction, nativeERC20) { + var _a, _b, _c; + var _d; + const depositTx = await this.getDepositTx(transaction, nativeERC20); + if (transaction.token == utils_1.ETH_ADDRESS || nativeERC20 == transaction.token) { + const baseGasLimit = await this.estimateGasRequestExecute(depositTx); + const gasLimit = (0, utils_1.scaleGasLimit)(baseGasLimit); + (_a = depositTx.overrides) !== null && _a !== void 0 ? _a : (depositTx.overrides = {}); + (_b = (_d = depositTx.overrides).gasLimit) !== null && _b !== void 0 ? _b : (_d.gasLimit = gasLimit); + return this.requestExecute(depositTx); + } + else { + const bridgeContracts = await this.getL1BridgeContracts(); + if (transaction.approveERC20) { + let l2WethToken = ethers_1.ethers.constants.AddressZero; + try { + l2WethToken = await bridgeContracts.weth.l2TokenAddress(transaction.token); + } + catch (e) { } + // If the token is Wrapped Ether, use its bridge. + const proposedBridge = l2WethToken != ethers_1.ethers.constants.AddressZero + ? bridgeContracts.weth.address + : bridgeContracts.erc20.address; + const bridgeAddress = transaction.bridgeAddress + ? transaction.bridgeAddress + : proposedBridge; + // We only request the allowance if the current one is not enough. + const allowance = await this.getAllowanceL1(transaction.token, bridgeAddress); + if (allowance.lt(transaction.amount)) { + const approveTx = await this.approveERC20(transaction.token, transaction.amount, { + bridgeAddress, + ...transaction.approveOverrides, + }); + await approveTx.wait(); + } + } + const baseGasLimit = await this._providerL1().estimateGas(depositTx); + const gasLimit = (0, utils_1.scaleGasLimit)(baseGasLimit); + (_c = depositTx.gasLimit) !== null && _c !== void 0 ? _c : (depositTx.gasLimit = gasLimit); + return await this._providerL2().getPriorityOpResponse(await this._signerL1().sendTransaction(depositTx)); + } } async estimateGasDeposit(transaction) { const depositTx = await this.getDepositTx(transaction); @@ -161,8 +156,8 @@ function AdapterL1(Base) { } return (0, utils_1.scaleGasLimit)(baseGasLimit); } - async getDepositTx(transaction) { - var _a, _b, _c, _d, _e, _f, _g, _h; + async getDepositTx(transaction, nativeERC20) { + var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; const bridgeContracts = await this.getL1BridgeContracts(); if (transaction.bridgeAddress != null) { bridgeContracts.erc20 = bridgeContracts.erc20.attach(transaction.bridgeAddress); @@ -188,39 +183,40 @@ function AdapterL1(Base) { const gasPriceForEstimation = overrides.maxFeePerGas || overrides.gasPrice; const zksyncContract = await this.getMainContract(); const baseCost = await zksyncContract.l2TransactionBaseCost(await gasPriceForEstimation, tx.l2GasLimit, tx.gasPerPubdataByte); - // if (token == ETH_ADDRESS) { - (_h = overrides.value) !== null && _h !== void 0 ? _h : (overrides.value = baseCost.add(operatorTip).add(amount)); - return { - contractAddress: to, - calldata: "0x", - l2Value: amount, - // For some reason typescript can not deduce that we've already set the - // tx.l2GasLimit - l2GasLimit: tx.l2GasLimit, - ...tx, - }; - // } else { - // let refundRecipient = tx.refundRecipient ?? ethers.constants.AddressZero; - // const args: [Address, Address, BigNumberish, BigNumberish, BigNumberish, Address] = [ - // to, - // token, - // amount, - // tx.l2GasLimit, - // tx.gasPerPubdataByte, - // refundRecipient, - // ]; - // overrides.value ??= baseCost.add(operatorTip); - // await checkBaseCost(baseCost, overrides.value); - // let l2WethToken = ethers.constants.AddressZero; - // try { - // l2WethToken = await bridgeContracts.weth.l2TokenAddress(tx.token); - // } catch (e) {} - // const bridge = - // l2WethToken != ethers.constants.AddressZero - // ? bridgeContracts.weth - // : bridgeContracts.erc20; - // return await bridge.populateTransaction.deposit(...args, overrides); - // } + if (token == utils_1.ETH_ADDRESS || nativeERC20 == token) { + (_h = overrides.value) !== null && _h !== void 0 ? _h : (overrides.value = baseCost.add(operatorTip).add(amount)); + return { + contractAddress: to, + calldata: "0x", + l2Value: amount, + // For some reason typescript can not deduce that we've already set the + // tx.l2GasLimit + l2GasLimit: tx.l2GasLimit, + ...tx, + }; + } + else { + let refundRecipient = (_j = tx.refundRecipient) !== null && _j !== void 0 ? _j : ethers_1.ethers.constants.AddressZero; + const args = [ + to, + token, + amount, + tx.l2GasLimit, + tx.gasPerPubdataByte, + refundRecipient, + ]; + (_k = overrides.value) !== null && _k !== void 0 ? _k : (overrides.value = baseCost.add(operatorTip)); + await (0, utils_1.checkBaseCost)(baseCost, overrides.value); + let l2WethToken = ethers_1.ethers.constants.AddressZero; + try { + l2WethToken = await bridgeContracts.weth.l2TokenAddress(tx.token); + } + catch (e) { } + const bridge = l2WethToken != ethers_1.ethers.constants.AddressZero + ? bridgeContracts.weth + : bridgeContracts.erc20; + return await bridge.populateTransaction.deposit(...args, overrides); + } } // Retrieves the full needed ETH fee for the deposit. // Returns the L1 fee and the L2 fee. diff --git a/build/src/signer.d.ts b/build/src/signer.d.ts index f52778f7acdf..47c9b9b8c7d4 100644 --- a/build/src/signer.d.ts +++ b/build/src/signer.d.ts @@ -104,7 +104,7 @@ declare const L1Signer_base: { overrides?: ethers.PayableOverrides; approveOverrides?: ethers.Overrides; customBridgeData?: ethers.utils.BytesLike; - }): Promise; + }, nativeERC20?: string): Promise; estimateGasDeposit(transaction: { token: string; amount: ethers.BigNumberish; @@ -128,7 +128,7 @@ declare const L1Signer_base: { customBridgeData?: ethers.utils.BytesLike; refundRecipient?: string; overrides?: ethers.PayableOverrides; - }): Promise; + }, nativeERC20?: string): Promise; getFullRequiredDepositFee(transaction: { token: string; to?: string; @@ -275,7 +275,7 @@ declare const L1VoidSigner_base: { overrides?: ethers.PayableOverrides; approveOverrides?: ethers.Overrides; customBridgeData?: ethers.utils.BytesLike; - }): Promise; + }, nativeERC20?: string): Promise; estimateGasDeposit(transaction: { token: string; amount: ethers.BigNumberish; @@ -299,7 +299,7 @@ declare const L1VoidSigner_base: { customBridgeData?: ethers.utils.BytesLike; refundRecipient?: string; overrides?: ethers.PayableOverrides; - }): Promise; + }, nativeERC20?: string): Promise; getFullRequiredDepositFee(transaction: { token: string; to?: string; diff --git a/build/src/wallet.d.ts b/build/src/wallet.d.ts index 2f1433d15368..fd7124cbbd7e 100644 --- a/build/src/wallet.d.ts +++ b/build/src/wallet.d.ts @@ -65,7 +65,7 @@ declare const Wallet_base: { overrides?: ethers.PayableOverrides; approveOverrides?: ethers.Overrides; customBridgeData?: ethers.utils.BytesLike; - }): Promise; + }, nativeERC20?: string): Promise; estimateGasDeposit(transaction: { token: string; amount: ethers.BigNumberish; @@ -89,7 +89,7 @@ declare const Wallet_base: { customBridgeData?: ethers.utils.BytesLike; refundRecipient?: string; overrides?: ethers.PayableOverrides; - }): Promise; + }, nativeERC20?: string): Promise; getFullRequiredDepositFee(transaction: { token: string; to?: string; diff --git a/src/adapters.ts b/src/adapters.ts index c3716b4c34af..848022cafa25 100644 --- a/src/adapters.ts +++ b/src/adapters.ts @@ -173,30 +173,33 @@ export function AdapterL1>(Base: TBase) { ); } - async deposit(transaction: { - token: Address; - amount: BigNumberish; - to?: Address; - operatorTip?: BigNumberish; - bridgeAddress?: Address; - approveERC20?: boolean; - l2GasLimit?: BigNumberish; - gasPerPubdataByte?: BigNumberish; - refundRecipient?: Address; - overrides?: ethers.PayableOverrides; - approveOverrides?: ethers.Overrides; - customBridgeData?: BytesLike; - }, nativeERC20?: Address): Promise { + async deposit( + transaction: { + token: Address; + amount: BigNumberish; + to?: Address; + operatorTip?: BigNumberish; + bridgeAddress?: Address; + approveERC20?: boolean; + l2GasLimit?: BigNumberish; + gasPerPubdataByte?: BigNumberish; + refundRecipient?: Address; + overrides?: ethers.PayableOverrides; + approveOverrides?: ethers.Overrides; + customBridgeData?: BytesLike; + }, + nativeERC20?: Address, + ): Promise { const depositTx = await this.getDepositTx(transaction, nativeERC20); - + if (transaction.token == ETH_ADDRESS || nativeERC20 == transaction.token) { - const baseGasLimit = await this.estimateGasRequestExecute(depositTx); - const gasLimit = scaleGasLimit(baseGasLimit); + const baseGasLimit = await this.estimateGasRequestExecute(depositTx); + const gasLimit = scaleGasLimit(baseGasLimit); - depositTx.overrides ??= {}; - depositTx.overrides.gasLimit ??= gasLimit; + depositTx.overrides ??= {}; + depositTx.overrides.gasLimit ??= gasLimit; - return this.requestExecute(depositTx); + return this.requestExecute(depositTx); } else { const bridgeContracts = await this.getL1BridgeContracts(); if (transaction.approveERC20) { @@ -263,18 +266,21 @@ export function AdapterL1>(Base: TBase) { return scaleGasLimit(baseGasLimit); } - async getDepositTx(transaction: { - token: Address; - amount: BigNumberish; - to?: Address; - operatorTip?: BigNumberish; - bridgeAddress?: Address; - l2GasLimit?: BigNumberish; - gasPerPubdataByte?: BigNumberish; - customBridgeData?: BytesLike; - refundRecipient?: Address; - overrides?: ethers.PayableOverrides; - }, nativeERC20?: Address): Promise { + async getDepositTx( + transaction: { + token: Address; + amount: BigNumberish; + to?: Address; + operatorTip?: BigNumberish; + bridgeAddress?: Address; + l2GasLimit?: BigNumberish; + gasPerPubdataByte?: BigNumberish; + customBridgeData?: BytesLike; + refundRecipient?: Address; + overrides?: ethers.PayableOverrides; + }, + nativeERC20?: Address, + ): Promise { const bridgeContracts = await this.getL1BridgeContracts(); if (transaction.bridgeAddress != null) { bridgeContracts.erc20 = bridgeContracts.erc20.attach(transaction.bridgeAddress); @@ -329,17 +335,17 @@ export function AdapterL1>(Base: TBase) { ); if (token == ETH_ADDRESS || nativeERC20 == token) { - overrides.value ??= baseCost.add(operatorTip).add(amount); - - return { - contractAddress: to, - calldata: "0x", - l2Value: amount, - // For some reason typescript can not deduce that we've already set the - // tx.l2GasLimit - l2GasLimit: tx.l2GasLimit!, - ...tx, - }; + overrides.value ??= baseCost.add(operatorTip).add(amount); + + return { + contractAddress: to, + calldata: "0x", + l2Value: amount, + // For some reason typescript can not deduce that we've already set the + // tx.l2GasLimit + l2GasLimit: tx.l2GasLimit!, + ...tx, + }; } else { let refundRecipient = tx.refundRecipient ?? ethers.constants.AddressZero; const args: [Address, Address, BigNumberish, BigNumberish, BigNumberish, Address] = [ From 71a910c5093878b84019d31267c6f1a9d006efb2 Mon Sep 17 00:00:00 2001 From: Santiago Pittella Date: Wed, 10 Jan 2024 11:09:17 +0100 Subject: [PATCH 3/3] add console.error --- build/src/adapters.js | 1 + src/adapters.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/build/src/adapters.js b/build/src/adapters.js index f5421d1461ed..e657d2fa1713 100644 --- a/build/src/adapters.js +++ b/build/src/adapters.js @@ -108,6 +108,7 @@ function AdapterL1(Base) { var _d; const depositTx = await this.getDepositTx(transaction, nativeERC20); if (transaction.token == utils_1.ETH_ADDRESS || nativeERC20 == transaction.token) { + console.error("NATIVE ERC20 VERSION"); const baseGasLimit = await this.estimateGasRequestExecute(depositTx); const gasLimit = (0, utils_1.scaleGasLimit)(baseGasLimit); (_a = depositTx.overrides) !== null && _a !== void 0 ? _a : (depositTx.overrides = {}); diff --git a/src/adapters.ts b/src/adapters.ts index 848022cafa25..75f4f3f28d46 100644 --- a/src/adapters.ts +++ b/src/adapters.ts @@ -193,6 +193,7 @@ export function AdapterL1>(Base: TBase) { const depositTx = await this.getDepositTx(transaction, nativeERC20); if (transaction.token == ETH_ADDRESS || nativeERC20 == transaction.token) { + console.error("NATIVE ERC20 VERSION") const baseGasLimit = await this.estimateGasRequestExecute(depositTx); const gasLimit = scaleGasLimit(baseGasLimit);