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 6 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
41 changes: 25 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 @@ -54,9 +54,10 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {

async getL1BridgeContracts() {
const addresses = await this._providerL2().getDefaultBridgeAddresses();
// TODO: wethL1 is not returned at the moment
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if we can remove it if its not a big change?

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 +178,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 +247,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 +301,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 +313,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 +602,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 +618,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 +639,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 +653,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 +664,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {

const {
contractAddress,
l1Value,
l2Value,
calldata,
l2GasLimit,
Expand All @@ -677,16 +684,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
63 changes: 63 additions & 0 deletions sdk/zksync-web3.js/tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ethers } from 'ethers';

import { Wallet, Provider, utils } from "../build/src";

const MNEMONIC="fine music test violin matrix prize squirrel panther purchase material script deal"
const DERIVE_PATH = "m/44'/60'/0'/0/1";

const WETH_ADDRESS = "0x8D4F22C9f1F22Ea745C7a828391Ef0E8D1692D4a";

const printBalance = async (wallet: Wallet,tag: string) => {
const wethBalance = await wallet.getBalanceL1(WETH_ADDRESS);
console.log(`[${tag}]WETH balance L1: `, ethers.utils.formatEther(wethBalance));

let balance = await wallet.getBalance();
console.log(`[${tag}]ETH balance L2: `, ethers.utils.formatEther(balance));
}

const test = async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im not sure if we should commit this file, otherwise need to clean it a bit


let zkWallet = Wallet.fromMnemonic(MNEMONIC, DERIVE_PATH);
zkWallet = zkWallet.connect(new Provider("http://10.202.3.175:3050"));
zkWallet = zkWallet.connectToL1(new ethers.providers.JsonRpcProvider("https://eth-sepolia.public.blastapi.io"));

const fee = await zkWallet.getFullRequiredDepositFee({
token: utils.ETH_ADDRESS,
to: zkWallet.address,
})

console.log(fee);

await printBalance(zkWallet, "before deposit")

const tx = await zkWallet.deposit({
token: utils.ETH_ADDRESS,
amount: ethers.utils.parseEther("10"),
overrides: {
gasLimit: 1000000,
}
});
await tx.waitFinalize();

await printBalance(zkWallet, "after deposit")


const withdrawTx = await zkWallet.withdraw({
token: utils.ETH_ADDRESS,
amount: ethers.utils.parseEther("10"),
overrides: {
gasLimit: 1000000,
}
})
const receipt = await withdrawTx.waitFinalize();

const finalizeWithdrawTx = await zkWallet.finalizeWithdrawal(receipt.transactionHash)

await finalizeWithdrawTx.wait();

await printBalance(zkWallet, "after withdraw")
}

test();


Loading