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 c3310e70aa9f..35fd3b0783ca 100644 --- a/build/src/adapters.js +++ b/build/src/adapters.js @@ -103,52 +103,48 @@ 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) { + 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 = {}); + (_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 +157,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, _l; const bridgeContracts = await this.getL1BridgeContracts(); if (transaction.bridgeAddress != null) { bridgeContracts.erc20 = bridgeContracts.erc20.attach(transaction.bridgeAddress); @@ -188,39 +184,45 @@ 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)); - 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) { + if (token == utils_1.ETH_ADDRESS) { + (_h = overrides.value) !== null && _h !== void 0 ? _h : (overrides.value = baseCost.add(operatorTip).add(amount)); + } + else { + (_j = overrides.value) !== null && _j !== void 0 ? _j : (overrides.value = baseCost.add(operatorTip)); + } + 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 = (_k = tx.refundRecipient) !== null && _k !== void 0 ? _k : ethers_1.ethers.constants.AddressZero; + const args = [ + to, + token, + amount, + tx.l2GasLimit, + tx.gasPerPubdataByte, + refundRecipient, + ]; + (_l = overrides.value) !== null && _l !== void 0 ? _l : (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 14ff05f86a50..81a9561fe69d 100644 --- a/src/adapters.ts +++ b/src/adapters.ts @@ -173,70 +173,74 @@ 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; - }): Promise { - const depositTx = await this.getDepositTx(transaction); + 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) { + console.error("NATIVE ERC20 VERSION") + const baseGasLimit = await this.estimateGasRequestExecute(depositTx); + const gasLimit = scaleGasLimit(baseGasLimit); + + depositTx.overrides ??= {}; + 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); - // if (transaction.token == ETH_ADDRESS) { - const baseGasLimit = await this.estimateGasRequestExecute(depositTx); - const gasLimit = scaleGasLimit(baseGasLimit); - - depositTx.overrides ??= {}; - 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), - // ); - // } + depositTx.gasLimit ??= gasLimit; + + return await this._providerL2().getPriorityOpResponse( + await this._signerL1().sendTransaction(depositTx), + ); + } } async estimateGasDeposit(transaction: { @@ -263,18 +267,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; - }): 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); @@ -328,43 +335,46 @@ export function AdapterL1>(Base: TBase) { tx.gasPerPubdataByte, ); - // if (token == ETH_ADDRESS) { - overrides.value ??= baseCost.add(operatorTip); + if (token == ETH_ADDRESS || nativeERC20 == token) { + if(token == ETH_ADDRESS) { + overrides.value ??= baseCost.add(operatorTip).add(amount); + } else { + overrides.value ??= baseCost.add(operatorTip); + } + 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, + ]; - 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); - // } + 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.