diff --git a/src/adapters.ts b/src/adapters.ts index 2b99ca6..ce662cc 100644 --- a/src/adapters.ts +++ b/src/adapters.ts @@ -211,7 +211,10 @@ export function AdapterL1>(Base: TBase) { }): Promise { const depositTx = await this.getDepositTx(transaction); - if (transaction.token == ETH_ADDRESS) { + const zksyncContract = await this.getMainContract(); + const baseTokenAddress = await zksyncContract.baseTokenAddress(); + + if (transaction.token == baseTokenAddress) { const baseGasLimit = await this.estimateGasRequestExecute(depositTx); const gasLimit = scaleGasLimit(baseGasLimit); @@ -278,7 +281,10 @@ export function AdapterL1>(Base: TBase) { const depositTx = await this.getDepositTx(transaction); let baseGasLimit: bigint; - if (transaction.token == ETH_ADDRESS) { + + const zksyncContract = await this.getMainContract(); + const baseTokenAddress = await zksyncContract.baseTokenAddress(); + if (transaction.token == baseTokenAddress) { baseGasLimit = await this.estimateGasRequestExecute(depositTx); } else { baseGasLimit = await this._providerL1().estimateGas(depositTx); @@ -357,10 +363,11 @@ export function AdapterL1>(Base: TBase) { tx.gasPerPubdataByte, ); + const baseTokenAddress = await zksyncContract.baseTokenAddress(); + if (token == ETH_ADDRESS) { checkBridgeWETHAllowed(); overrides.value ??= baseCost + BigInt(operatorTip) + BigInt(amount); - return { contractAddress: to, calldata: "0x", @@ -369,8 +376,18 @@ export function AdapterL1>(Base: TBase) { l2GasLimit: tx.l2GasLimit!, ...tx, }; + } else if (token == baseTokenAddress) { + return { + contractAddress: to, + calldata: "0x", + l2Value: 0, + // 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.ZeroAddress; + const l1Amount = baseCost + BigInt(operatorTip) const args: [ Address, Address, @@ -379,10 +396,11 @@ export function AdapterL1>(Base: TBase) { BigNumberish, Address, BigNumberish, - ] = [to, token, amount, tx.l2GasLimit, tx.gasPerPubdataByte, refundRecipient, 0]; + ] = [to, token, amount, tx.l2GasLimit, tx.gasPerPubdataByte, refundRecipient, l1Amount]; - overrides.value ??= baseCost + BigInt(operatorTip); - await checkBaseCost(baseCost, overrides.value); + // overrides.value ??= baseCost + BigInt(operatorTip); + const value = baseCost + BigInt(operatorTip); + await checkBaseCost(baseCost, value); overrides.from ??= await this.getAddress(); let l2WethToken = ethers.ZeroAddress; @@ -463,10 +481,12 @@ export function AdapterL1>(Base: TBase) { tx.gasPerPubdataByte, ); - const selfBalanceETH = await this.getBalanceL1(); + const baseTokenAddress = await zksyncContract.baseTokenAddress(); + + const selfBalanceCRO = await this.getBalanceL1(baseTokenAddress); // We could zero in, because the final fee will anyway be bigger than - if (baseCost >= selfBalanceETH + dummyAmount) { + if (baseCost >= selfBalanceCRO + dummyAmount) { const recommendedETHBalance = BigInt( ALLOW_BRIDGE_WETH && tx.token == ETH_ADDRESS @@ -477,7 +497,7 @@ export function AdapterL1>(Base: TBase) { baseCost; const formattedRecommendedBalance = ethers.formatEther(recommendedETHBalance); throw new Error( - `Not enough balance for deposit. Under the provided gas price, the recommended balance to perform a deposit is ${formattedRecommendedBalance} ETH`, + `Not enough balance for deposit. Under the provided gas price, the recommended balance to perform a deposit is ${formattedRecommendedBalance} CRO`, ); } @@ -702,6 +722,7 @@ export function AdapterL1>(Base: TBase) { contractAddress: Address; calldata: string; l2GasLimit: BigNumberish; + amount: BigNumberish; l2Value?: BigNumberish; factoryDeps?: ethers.BytesLike[]; operatorTip?: BigNumberish; @@ -718,6 +739,7 @@ export function AdapterL1>(Base: TBase) { async estimateGasRequestExecute(transaction: { contractAddress: Address; calldata: string; + amount: BigNumberish; l2GasLimit?: BigNumberish; l2Value?: BigNumberish; factoryDeps?: ethers.BytesLike[]; @@ -738,6 +760,7 @@ export function AdapterL1>(Base: TBase) { async getRequestExecuteTx(transaction: { contractAddress: Address; calldata: string; + amount: BigNumberish; l2GasLimit?: BigNumberish; l2Value?: BigNumberish; factoryDeps?: ethers.BytesLike[]; @@ -768,6 +791,7 @@ export function AdapterL1>(Base: TBase) { overrides, gasPerPubdataByte, refundRecipient, + amount, } = tx; await insertGasPrice(this._providerL1(), overrides); @@ -779,9 +803,9 @@ export function AdapterL1>(Base: TBase) { gasLimit: l2GasLimit, }); - overrides.value ??= baseCost + BigInt(operatorTip) + BigInt(l2Value); - - await checkBaseCost(baseCost, overrides.value); + // fee on L2 is subsctracted from the amount + // overrides.value ??= baseCost + BigInt(operatorTip) + BigInt(l2Value); + // await checkBaseCost(baseCost, overrides.value); const l2Tx: L2TransactionStruct = { l2Contract: contractAddress, @@ -795,7 +819,7 @@ export function AdapterL1>(Base: TBase) { calldata, factoryDeps, refundRecipient, - 0, + amount, overrides, ); } diff --git a/src/index.ts b/src/index.ts index 6b9974f..22ec941 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ export * as utils from "./utils"; export * as types from "./types"; -export { EIP712Signer, Signer, L1Signer } from "./signer"; +export { EIP712Signer, Signer, L1Signer, L1VoidSigner } from "./signer"; export { Wallet } from "./wallet"; export { BrowserProvider, Provider } from "./provider"; export { ContractFactory, Contract } from "./contract"; diff --git a/src/provider.ts b/src/provider.ts index 286890b..a097af0 100644 --- a/src/provider.ts +++ b/src/provider.ts @@ -317,7 +317,6 @@ export function JsonRpcApiProvider