Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: zksync-web3.js does not support custom gas token #26

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion sdk/zksync-web3.js/abi/IL1Bridge.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@
"internalType": "address",
"name": "_refundRecipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "_l1Amount",
"type": "uint256"
}
],
"name": "deposit",
Expand All @@ -171,7 +176,7 @@
"type": "bytes32"
}
],
"stateMutability": "payable",
"stateMutability": "nonpayable",
"type": "function"
},
{
Expand Down
51 changes: 34 additions & 17 deletions sdk/zksync-web3.js/abi/IZkSync.json
Original file line number Diff line number Diff line change
Expand Up @@ -1784,31 +1784,48 @@
},
{
"inputs": [
{
"internalType": "address",
"name": "_contractL2",
"type": "address"
},
{
"internalType": "uint256",
"name": "_l2Value",
"name": "_l1Value",
"type": "uint256"
},
{
"components": [
{
"internalType": "address",
"name": "l2Contract",
"type": "address"
},
{
"internalType": "uint256",
"name": "l2Value",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "gasAmount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "l2GasLimit",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "l2GasPerPubdataByteLimit",
"type": "uint256"
}
],
"internalType": "struct L2TransactionValue",
"name": "_txValue",
"type": "tuple"
},
{
"internalType": "bytes",
"name": "_calldata",
"type": "bytes"
},
{
"internalType": "uint256",
"name": "_l2GasLimit",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_l2GasPerPubdataByteLimit",
"type": "uint256"
},
{
"internalType": "bytes[]",
"name": "_factoryDeps",
Expand All @@ -1828,7 +1845,7 @@
"type": "bytes32"
}
],
"stateMutability": "payable",
"stateMutability": "nonpayable",
"type": "function"
},
{
Expand Down
4 changes: 2 additions & 2 deletions sdk/zksync-web3.js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zksync-web3",
"version": "0.15.2",
"name": "@xycro/zksync-web3",
"version": "0.15.2-test.1",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
"files": [
Expand Down
40 changes: 24 additions & 16 deletions sdk/zksync-web3.js/src/adapters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BigNumber, BigNumberish, BytesLike, ethers } from 'ethers';
import { IERC20MetadataFactory, IL1BridgeFactory, IL2BridgeFactory, IZkSyncFactory } from '../typechain';
import { IERC20MetadataFactory, IL1Bridge, IL1BridgeFactory, IL2BridgeFactory, IZkSyncFactory } from '../typechain';
import { Provider } from './provider';
import {
Address,
Expand Down Expand Up @@ -56,7 +56,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
const addresses = await this._providerL2().getDefaultBridgeAddresses();
return {
erc20: IL1BridgeFactory.connect(addresses.erc20L1, this._signerL1()),
weth: IL1BridgeFactory.connect(addresses.wethL1, this._signerL1())
weth: IL1BridgeFactory.connect(addresses.erc20L1, this._signerL1()),
thomas-nguy marked this conversation as resolved.
Show resolved Hide resolved
};
}

Expand Down Expand Up @@ -177,7 +177,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {

return this.requestExecute(depositTx);
} else {
const bridgeContracts = await this.getL1BridgeContracts();
const bridgeContracts = await this.getL1BridgeContracts();
if (transaction.approveERC20) {
const l2WethToken = await bridgeContracts.weth.l2TokenAddress(transaction.token);
// If the token is Wrapped Ether, use its bridge.
Expand Down Expand Up @@ -246,6 +246,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
overrides?: ethers.PayableOverrides;
}): Promise<any> {
const bridgeContracts = await this.getL1BridgeContracts();

if (transaction.bridgeAddress != null) {
bridgeContracts.erc20 = bridgeContracts.erc20.attach(transaction.bridgeAddress);
}
Expand Down Expand Up @@ -299,9 +300,8 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
);

if (token == ETH_ADDRESS) {
overrides.value ??= baseCost.add(operatorTip).add(amount);

return {
l1Value: baseCost.add(operatorTip).add(amount),
contractAddress: to,
calldata: '0x',
l2Value: amount,
Expand All @@ -312,17 +312,18 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
};
} else {
let refundRecipient = tx.refundRecipient ?? ethers.constants.AddressZero;
const args: [Address, Address, BigNumberish, BigNumberish, BigNumberish, Address] = [
const cost = baseCost.add(operatorTip);
const args: [Address, Address, BigNumberish, BigNumberish, BigNumberish, Address, BigNumberish] = [
to,
token,
amount,
tx.l2GasLimit,
tx.gasPerPubdataByte,
refundRecipient
refundRecipient,
cost
];

overrides.value ??= baseCost.add(operatorTip);
await checkBaseCost(baseCost, overrides.value);
await checkBaseCost(baseCost, cost);

const l2WethToken = await bridgeContracts.weth.l2TokenAddress(tx.token);
const bridge =
Expand Down Expand Up @@ -600,6 +601,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
}

async requestExecute(transaction: {
l1Value: BigNumberish;
contractAddress: Address;
calldata: BytesLike;
l2GasLimit?: BigNumberish;
Expand All @@ -615,6 +617,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
}

async estimateGasRequestExecute(transaction: {
l1Value?: BigNumberish;
contractAddress: Address;
calldata: BytesLike;
l2GasLimit?: BigNumberish;
Expand All @@ -635,6 +638,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
}

async getRequestExecuteTx(transaction: {
l1Value?: BigNumberish;
contractAddress: Address;
calldata: BytesLike;
l2GasLimit?: BigNumberish;
Expand All @@ -648,6 +652,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
const zksyncContract = await this.getMainContract();

const { ...tx } = transaction;
tx.l1Value ??= BigNumber.from(0);
tx.l2Value ??= BigNumber.from(0);
tx.operatorTip ??= BigNumber.from(0);
tx.factoryDeps ??= [];
Expand All @@ -658,6 +663,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {

const {
contractAddress,
l1Value,
l2Value,
calldata,
l2GasLimit,
Expand All @@ -677,16 +683,18 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
gasLimit: l2GasLimit
});

overrides.value ??= baseCost.add(operatorTip).add(l2Value);

await checkBaseCost(baseCost, overrides.value);
await checkBaseCost(baseCost, l1Value);

return await zksyncContract.populateTransaction.requestL2Transaction(
contractAddress,
l2Value,
l1Value,
{
l2Contract: contractAddress,
l2Value,
gasAmount: baseCost.add(operatorTip),
l2GasLimit,
l2GasPerPubdataByteLimit: REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_LIMIT
},
calldata,
l2GasLimit,
REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_LIMIT,
factoryDeps,
refundRecipient,
overrides
Expand Down
Empty file.
Loading