zksync-web3
Advanced tools
Comparing version 0.2.7 to 0.2.8
@@ -1,4 +0,4 @@ | ||
import { ethers, BigNumberish, BigNumber } from 'ethers'; | ||
import { ethers } from 'ethers'; | ||
import { Provider } from './provider'; | ||
import { Address, BlockTag, Signature, PriorityOpResponse, TransactionRequest, TransactionResponse } from './types'; | ||
import { BlockTag, Signature, TransactionRequest, TransactionResponse } from './types'; | ||
import { TypedDataSigner } from '@ethersproject/abstract-signer'; | ||
@@ -18,48 +18,72 @@ export declare const eip712Types: { | ||
} | ||
export declare class Signer extends ethers.providers.JsonRpcSigner { | ||
declare const Signer_base: { | ||
new (...args: any[]): { | ||
_providerL2(): Provider; | ||
_signerL2(): ethers.Signer; | ||
getBalance(token?: string, blockTag?: BlockTag): Promise<ethers.BigNumber>; | ||
_fillCustomData(data: import("./types").Eip712Meta): import("./types").Eip712Meta; | ||
withdraw(tx: { | ||
amount: ethers.BigNumberish; | ||
token: string; | ||
to?: string; | ||
feeToken?: string; | ||
nonce?: ethers.BigNumberish; | ||
validFrom?: number; | ||
validUntil?: number; | ||
gasLimit?: ethers.BigNumberish; | ||
}): Promise<ethers.providers.TransactionResponse>; | ||
transfer(tx: { | ||
to: string; | ||
amount: ethers.BigNumberish; | ||
token?: string; | ||
feeToken?: string; | ||
nonce?: ethers.BigNumberish; | ||
validFrom?: number; | ||
validUntil?: number; | ||
gasLimit?: ethers.BigNumberish; | ||
}): Promise<any>; | ||
sendTransaction(tx: ethers.providers.TransactionRequest): Promise<ethers.providers.TransactionResponse>; | ||
getAddress(): Promise<string>; | ||
}; | ||
} & typeof ethers.providers.JsonRpcSigner; | ||
export declare class Signer extends Signer_base { | ||
provider: Provider; | ||
eip712: EIP712Signer; | ||
_signerL2(): this; | ||
_providerL2(): Provider; | ||
static from(signer: ethers.providers.JsonRpcSigner & { | ||
provider: Provider; | ||
}): Signer; | ||
getBalance(token?: Address, blockTag?: BlockTag): Promise<ethers.BigNumber>; | ||
getNonce(blockTag?: BlockTag): Promise<number>; | ||
sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse>; | ||
withdraw(tx: { | ||
amount: BigNumberish; | ||
token: Address; | ||
to?: Address; | ||
feeToken?: Address; | ||
nonce?: Address; | ||
validFrom?: number; | ||
validUntil?: number; | ||
gasLimit?: BigNumberish; | ||
}): Promise<TransactionResponse>; | ||
transfer(tx: { | ||
to: Address; | ||
amount: BigNumberish; | ||
token?: Address; | ||
feeToken?: Address; | ||
nonce?: Address; | ||
validFrom?: number; | ||
validUntil?: number; | ||
gasLimit?: BigNumberish; | ||
}): Promise<any>; | ||
} | ||
export declare class L1Signer extends ethers.providers.JsonRpcSigner { | ||
declare const L1Signer_base: { | ||
new (...args: any[]): { | ||
_providerL2(): Provider; | ||
_providerL1(): ethers.providers.Provider; | ||
_signerL1(): ethers.Signer; | ||
getMainContract(): Promise<ethers.Contract>; | ||
getBalanceL1(token?: string, blockTag?: ethers.providers.BlockTag): Promise<ethers.BigNumber>; | ||
approveERC20(token: string, amount: ethers.BigNumberish, overrides?: ethers.CallOverrides): Promise<ethers.providers.TransactionResponse>; | ||
deposit(transaction: { | ||
token: string; | ||
amount: ethers.BigNumberish; | ||
to?: string; | ||
approveERC20?: boolean; | ||
overrides?: ethers.CallOverrides; | ||
approveOverrides?: ethers.CallOverrides; | ||
}): Promise<import("./types").PriorityOpResponse>; | ||
addToken(token: string, overrides?: ethers.CallOverrides): Promise<import("./types").PriorityOpResponse>; | ||
sendTransaction(tx: ethers.providers.TransactionRequest): Promise<ethers.providers.TransactionResponse>; | ||
getAddress(): Promise<string>; | ||
}; | ||
} & typeof ethers.providers.JsonRpcSigner; | ||
export declare class L1Signer extends L1Signer_base { | ||
providerL2: Provider; | ||
_providerL2(): Provider; | ||
_providerL1(): ethers.providers.JsonRpcProvider; | ||
_signerL1(): this; | ||
static from(signer: ethers.providers.JsonRpcSigner, zksyncProvider: Provider): L1Signer; | ||
connectToL2(provider: Provider): this; | ||
getMainContract(): Promise<ethers.Contract>; | ||
getBalanceL1(token?: Address, blockTag?: ethers.providers.BlockTag): Promise<BigNumber>; | ||
approveERC20(token: Address, amount: BigNumberish, overrides?: ethers.CallOverrides): Promise<ethers.providers.TransactionResponse>; | ||
deposit(transaction: { | ||
token: Address; | ||
amount: BigNumberish; | ||
to?: Address; | ||
approveERC20?: boolean; | ||
overrides?: ethers.CallOverrides; | ||
approveOverrides?: ethers.CallOverrides; | ||
}): Promise<PriorityOpResponse>; | ||
addToken(token: Address, overrides?: ethers.CallOverrides): Promise<PriorityOpResponse>; | ||
} | ||
export {}; |
@@ -7,2 +7,3 @@ "use strict"; | ||
const hash_1 = require("@ethersproject/hash"); | ||
const adapters_1 = require("./adapters"); | ||
exports.eip712Types = { | ||
@@ -58,3 +59,9 @@ TransactionRequest: [ | ||
// const tx = await signer.sendTransaction({ ... }); | ||
class Signer extends ethers_1.ethers.providers.JsonRpcSigner { | ||
class Signer extends (0, adapters_1.AdapterL2)(ethers_1.ethers.providers.JsonRpcSigner) { | ||
_signerL2() { | ||
return this; | ||
} | ||
_providerL2() { | ||
return this.provider; | ||
} | ||
static from(signer) { | ||
@@ -66,5 +73,2 @@ const newSigner = Object.setPrototypeOf(signer, Signer.prototype); | ||
} | ||
async getBalance(token, blockTag = 'committed') { | ||
return await this.provider.getBalance(await this.getAddress(), blockTag, token); | ||
} | ||
// an alias with a better name | ||
@@ -75,6 +79,7 @@ async getNonce(blockTag) { | ||
async sendTransaction(transaction) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; | ||
var _o, _p, _q, _r, _s, _t, _u; | ||
(_a = transaction.value) !== null && _a !== void 0 ? _a : (transaction.value = 0); | ||
(_b = transaction.data) !== null && _b !== void 0 ? _b : (transaction.data = '0x'); | ||
var _a, _b, _c, _d, _e; | ||
if (transaction.customData == null && transaction.type == null) { | ||
// use legacy txs by default | ||
transaction.type = 0; | ||
} | ||
if (transaction.customData == null && transaction.type != 0x70) { | ||
@@ -84,12 +89,8 @@ return (await super.sendTransaction(transaction)); | ||
else { | ||
(_a = transaction.value) !== null && _a !== void 0 ? _a : (transaction.value = 0); | ||
(_b = transaction.data) !== null && _b !== void 0 ? _b : (transaction.data = '0x'); | ||
(_c = transaction.nonce) !== null && _c !== void 0 ? _c : (transaction.nonce = await this.getNonce()); | ||
(_d = (_o = transaction.customData).feeToken) !== null && _d !== void 0 ? _d : (_o.feeToken = utils_1.ETH_ADDRESS); | ||
(_e = (_p = transaction.customData).validFrom) !== null && _e !== void 0 ? _e : (_p.validFrom = utils_1.MIN_TIMESTAMP); | ||
(_f = (_q = transaction.customData).validUntil) !== null && _f !== void 0 ? _f : (_q.validUntil = utils_1.MAX_TIMESTAMP); | ||
(_g = (_r = transaction.customData).ergsPerStorage) !== null && _g !== void 0 ? _g : (_r.ergsPerStorage = 0); | ||
(_h = (_s = transaction.customData).ergsPerPubdata) !== null && _h !== void 0 ? _h : (_s.ergsPerPubdata = 0); | ||
(_j = (_t = transaction.customData).withdrawToken) !== null && _j !== void 0 ? _j : (_t.withdrawToken = ethers_1.ethers.constants.AddressZero); | ||
(_k = (_u = transaction.customData).factoryDeps) !== null && _k !== void 0 ? _k : (_u.factoryDeps = []); | ||
(_l = transaction.gasPrice) !== null && _l !== void 0 ? _l : (transaction.gasPrice = await this.provider.getGasPrice()); | ||
(_m = transaction.gasLimit) !== null && _m !== void 0 ? _m : (transaction.gasLimit = await this.provider.estimateGas(transaction)); | ||
transaction.customData = this._fillCustomData(transaction.customData); | ||
(_d = transaction.gasPrice) !== null && _d !== void 0 ? _d : (transaction.gasPrice = await this.provider.getGasPrice()); | ||
(_e = transaction.gasLimit) !== null && _e !== void 0 ? _e : (transaction.gasLimit = await this.provider.estimateGas(transaction)); | ||
const signature = await this.eip712.sign(transaction); | ||
@@ -100,35 +101,2 @@ const txBytes = (0, utils_1.serialize)(transaction, signature); | ||
} | ||
async withdraw(tx) { | ||
var _a; | ||
return await this.sendTransaction({ | ||
value: tx.amount, | ||
to: (_a = tx.to) !== null && _a !== void 0 ? _a : (await this.getAddress()), | ||
nonce: tx.nonce, | ||
gasLimit: tx.gasLimit, | ||
customData: { | ||
feeToken: tx.feeToken, | ||
validFrom: tx.validFrom, | ||
validUntil: tx.validUntil, | ||
withdrawToken: tx.token | ||
} | ||
}); | ||
} | ||
async transfer(tx) { | ||
var _a; | ||
(_a = tx.token) !== null && _a !== void 0 ? _a : (tx.token = utils_1.ETH_ADDRESS); | ||
const token = new ethers_1.ethers.Contract(tx.token, utils_1.IERC20, this); | ||
const overrides = { | ||
nonce: tx.nonce, | ||
gasLimit: tx.gasLimit, | ||
customData: { | ||
feeToken: tx.feeToken, | ||
validFrom: tx.validFrom, | ||
validUntil: tx.validUntil | ||
} | ||
}; | ||
if (tx.feeToken == null && tx.validFrom == null && tx.validUntil == null) { | ||
delete overrides.customData; | ||
} | ||
return await token.transfer(tx.to, tx.amount, overrides); | ||
} | ||
} | ||
@@ -143,3 +111,12 @@ exports.Signer = Signer; | ||
// const tx = await signer.deposit({ ... }); | ||
class L1Signer extends ethers_1.ethers.providers.JsonRpcSigner { | ||
class L1Signer extends (0, adapters_1.AdapterL1)(ethers_1.ethers.providers.JsonRpcSigner) { | ||
_providerL2() { | ||
return this.providerL2; | ||
} | ||
_providerL1() { | ||
return this.provider; | ||
} | ||
_signerL1() { | ||
return this; | ||
} | ||
static from(signer, zksyncProvider) { | ||
@@ -154,71 +131,3 @@ const newSigner = Object.setPrototypeOf(signer, L1Signer.prototype); | ||
} | ||
async getMainContract() { | ||
const address = await this.providerL2.getMainContractAddress(); | ||
return new ethers_1.ethers.Contract(address, utils_1.ZKSYNC_MAIN_ABI, this); | ||
} | ||
async getBalanceL1(token, blockTag) { | ||
token !== null && token !== void 0 ? token : (token = utils_1.ETH_ADDRESS); | ||
if ((0, utils_1.isETH)(token)) { | ||
return await this.provider.getBalance(await this.getAddress(), blockTag); | ||
} | ||
else { | ||
const erc20contract = new ethers_1.ethers.Contract(token, utils_1.IERC20, this.provider); | ||
return await erc20contract.balanceOf(await this.getAddress()); | ||
} | ||
} | ||
async approveERC20(token, amount, overrides) { | ||
const erc20contract = new ethers_1.ethers.Contract(token, utils_1.IERC20, this); | ||
const mainContract = await this.providerL2.getMainContractAddress(); | ||
let gasLimit; | ||
if (overrides === null || overrides === void 0 ? void 0 : overrides.gasLimit) { | ||
gasLimit = await overrides.gasLimit; | ||
} | ||
else { | ||
// For some reason, gas estimation for approves may be imprecise. | ||
// At least in the localhost scenario. | ||
gasLimit = await erc20contract.estimateGas.approve(mainContract, amount); | ||
gasLimit = gasLimit.gt(utils_1.RECOMMENDED_GAS_LIMIT.ERC20_APPROVE) | ||
? gasLimit | ||
: utils_1.RECOMMENDED_GAS_LIMIT.ERC20_APPROVE; | ||
} | ||
return await erc20contract.approve(mainContract, amount, { gasLimit, ...overrides }); | ||
} | ||
async deposit(transaction) { | ||
var _a, _b; | ||
const zksyncContract = await this.getMainContract(); | ||
const { token, amount } = transaction; | ||
const depositTo = (_a = transaction.to) !== null && _a !== void 0 ? _a : (await this.getAddress()); | ||
if ((0, utils_1.isETH)(token)) { | ||
return await this.providerL2.getPriorityOpResponse(await zksyncContract.depositETH(depositTo, { | ||
value: ethers_1.BigNumber.from(amount), | ||
gasLimit: ethers_1.BigNumber.from(utils_1.RECOMMENDED_GAS_LIMIT.ETH_DEPOSIT), | ||
...transaction.overrides | ||
})); | ||
} | ||
else { | ||
let nonce = undefined; | ||
if (transaction.approveERC20) { | ||
const approveTx = await this.approveERC20(token, amount, transaction.approveOverrides); | ||
nonce = approveTx.nonce + 1; | ||
} | ||
const overrides = { nonce, ...transaction.overrides }; | ||
const args = [token, amount, depositTo]; | ||
if (overrides.gasLimit == null) { | ||
const gasEstimate = await zksyncContract.estimateGas | ||
.depositERC20(...args, overrides) | ||
.catch(() => ethers_1.BigNumber.from(0)); | ||
const recommendedGasLimit = (_b = utils_1.RECOMMENDED_GAS_LIMIT.ERC20_DEPOSIT[token]) !== null && _b !== void 0 ? _b : utils_1.RECOMMENDED_GAS_LIMIT.ERC20_DEFAULT_DEPOSIT; | ||
overrides.gasLimit = gasEstimate.gte(recommendedGasLimit) ? gasEstimate : recommendedGasLimit; | ||
} | ||
return await this.providerL2.getPriorityOpResponse(await zksyncContract.depositERC20(...args, overrides)); | ||
} | ||
} | ||
async addToken(token, overrides) { | ||
const zksyncContract = await this.getMainContract(); | ||
return await this.providerL2.getPriorityOpResponse(await zksyncContract.addToken(token, { | ||
gasLimit: ethers_1.BigNumber.from(utils_1.RECOMMENDED_GAS_LIMIT.ADD_TOKEN), | ||
...overrides | ||
})); | ||
} | ||
} | ||
exports.L1Signer = L1Signer; |
import { EIP712Signer } from './signer'; | ||
import { Provider } from './provider'; | ||
import { ethers, utils, BigNumberish, BigNumber } from 'ethers'; | ||
import { Address, PriorityOpResponse, BlockTag, TransactionResponse } from './types'; | ||
import { ethers, utils } from 'ethers'; | ||
import { BlockTag, TransactionResponse } from './types'; | ||
import { ProgressCallback } from '@ethersproject/json-wallets'; | ||
export declare class Wallet extends ethers.Wallet { | ||
declare const Wallet_base: { | ||
new (...args: any[]): { | ||
_providerL2(): Provider; | ||
_signerL2(): ethers.Signer; | ||
getBalance(token?: string, blockTag?: BlockTag): Promise<ethers.BigNumber>; | ||
_fillCustomData(data: import("./types").Eip712Meta): import("./types").Eip712Meta; | ||
withdraw(tx: { | ||
amount: ethers.BigNumberish; | ||
token: string; | ||
to?: string; | ||
feeToken?: string; | ||
nonce?: ethers.BigNumberish; | ||
validFrom?: number; | ||
validUntil?: number; | ||
gasLimit?: ethers.BigNumberish; | ||
}): Promise<ethers.providers.TransactionResponse>; | ||
transfer(tx: { | ||
to: string; | ||
amount: ethers.BigNumberish; | ||
token?: string; | ||
feeToken?: string; | ||
nonce?: ethers.BigNumberish; | ||
validFrom?: number; | ||
validUntil?: number; | ||
gasLimit?: ethers.BigNumberish; | ||
}): Promise<any>; | ||
sendTransaction(tx: ethers.providers.TransactionRequest): Promise<ethers.providers.TransactionResponse>; | ||
getAddress(): Promise<string>; | ||
}; | ||
} & { | ||
new (...args: any[]): { | ||
_providerL2(): Provider; | ||
_providerL1(): ethers.providers.Provider; | ||
_signerL1(): ethers.Signer; | ||
getMainContract(): Promise<ethers.Contract>; | ||
getBalanceL1(token?: string, blockTag?: ethers.providers.BlockTag): Promise<ethers.BigNumber>; | ||
approveERC20(token: string, amount: ethers.BigNumberish, overrides?: ethers.CallOverrides): Promise<ethers.providers.TransactionResponse>; | ||
deposit(transaction: { | ||
token: string; | ||
amount: ethers.BigNumberish; | ||
to?: string; | ||
approveERC20?: boolean; | ||
overrides?: ethers.CallOverrides; | ||
approveOverrides?: ethers.CallOverrides; | ||
}): Promise<import("./types").PriorityOpResponse>; | ||
addToken(token: string, overrides?: ethers.CallOverrides): Promise<import("./types").PriorityOpResponse>; | ||
sendTransaction(tx: ethers.providers.TransactionRequest): Promise<ethers.providers.TransactionResponse>; | ||
getAddress(): Promise<string>; | ||
}; | ||
} & typeof ethers.Wallet; | ||
export declare class Wallet extends Wallet_base { | ||
readonly provider: Provider; | ||
readonly providerL1?: ethers.providers.Provider; | ||
providerL1?: ethers.providers.Provider; | ||
eip712: EIP712Signer; | ||
getBalance(token?: Address, blockTag?: BlockTag): Promise<ethers.BigNumber>; | ||
_providerL1(): ethers.providers.Provider; | ||
_providerL2(): Provider; | ||
_signerL1(): ethers.Wallet; | ||
_signerL2(): this; | ||
getNonce(blockTag?: BlockTag): Promise<number>; | ||
getBalanceL1(token?: Address, blockTag?: ethers.providers.BlockTag): Promise<BigNumber>; | ||
connect(provider: Provider): Wallet; | ||
connectToL1(provider: ethers.providers.Provider): Wallet; | ||
static fromMnemonic(mnemonic: string, path?: string, wordlist?: ethers.Wordlist): Wallet; | ||
@@ -21,36 +74,4 @@ static fromEncryptedJson(json: string, password?: string | ethers.Bytes, callback?: ProgressCallback): Promise<Wallet>; | ||
signTransaction(transaction: ethers.providers.TransactionRequest): Promise<string>; | ||
withdraw(tx: { | ||
amount: BigNumberish; | ||
token: Address; | ||
to?: Address; | ||
feeToken?: Address; | ||
nonce?: Address; | ||
validFrom?: number; | ||
validUntil?: number; | ||
gasLimit?: BigNumberish; | ||
}): Promise<TransactionResponse>; | ||
transfer(tx: { | ||
to: Address; | ||
amount: BigNumberish; | ||
token?: Address; | ||
feeToken?: Address; | ||
nonce?: Address; | ||
validFrom?: number; | ||
validUntil?: number; | ||
gasLimit?: BigNumberish; | ||
}): Promise<any>; | ||
sendTransaction(transaction: ethers.providers.TransactionRequest): Promise<TransactionResponse>; | ||
getMainContract(): Promise<ethers.Contract>; | ||
ethWallet(): ethers.Wallet; | ||
connectToL1(provider: ethers.providers.Provider): Wallet; | ||
approveERC20(token: Address, amount: BigNumberish, overrides?: ethers.CallOverrides): Promise<ethers.providers.TransactionResponse>; | ||
deposit(transaction: { | ||
token: Address; | ||
amount: BigNumberish; | ||
to?: Address; | ||
approveERC20?: boolean; | ||
overrides?: ethers.CallOverrides; | ||
approveOverrides?: ethers.CallOverrides; | ||
}): Promise<PriorityOpResponse>; | ||
addToken(token: Address, overrides?: ethers.CallOverrides): Promise<PriorityOpResponse>; | ||
} | ||
export {}; |
@@ -7,3 +7,4 @@ "use strict"; | ||
const ethers_1 = require("ethers"); | ||
class Wallet extends ethers_1.ethers.Wallet { | ||
const adapters_1 = require("./adapters"); | ||
class Wallet extends (0, adapters_1.AdapterL2)((0, adapters_1.AdapterL1)(ethers_1.ethers.Wallet)) { | ||
constructor(privateKey, providerL2, providerL1) { | ||
@@ -18,5 +19,17 @@ super(privateKey, providerL2); | ||
} | ||
async getBalance(token, blockTag = 'committed') { | ||
return await this.provider.getBalance(await this.getAddress(), blockTag, token); | ||
_providerL1() { | ||
if (this.providerL1 == null) { | ||
throw new Error('L1 provider missing: use `connectToL1` to specify'); | ||
} | ||
return this.providerL1; | ||
} | ||
_providerL2() { | ||
return this.provider; | ||
} | ||
_signerL1() { | ||
return new ethers_1.ethers.Wallet(this._signingKey(), this._providerL1()); | ||
} | ||
_signerL2() { | ||
return this; | ||
} | ||
// an alias with a better name | ||
@@ -26,18 +39,8 @@ async getNonce(blockTag) { | ||
} | ||
async getBalanceL1(token, blockTag) { | ||
if (this.providerL1 == null) { | ||
throw new Error('L1 provider missing: use `connectToL1` to specify'); | ||
} | ||
token !== null && token !== void 0 ? token : (token = utils_1.ETH_ADDRESS); | ||
if ((0, utils_1.isETH)(token)) { | ||
return await this.providerL1.getBalance(this.address, blockTag); | ||
} | ||
else { | ||
const erc20contract = new ethers_1.ethers.Contract(token, utils_1.IERC20, this.providerL1); | ||
return await erc20contract.balanceOf(this.address); | ||
} | ||
} | ||
connect(provider) { | ||
return new Wallet(this._signingKey(), provider, this.providerL1); | ||
} | ||
connectToL1(provider) { | ||
return new Wallet(this._signingKey(), this.provider, provider); | ||
} | ||
static fromMnemonic(mnemonic, path, wordlist) { | ||
@@ -60,4 +63,7 @@ const wallet = super.fromMnemonic(mnemonic, path, wordlist); | ||
async populateTransaction(transaction) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j; | ||
var _k, _l, _m, _o, _p, _q, _r; | ||
var _a, _b; | ||
if (transaction.type == null && transaction.customData == null) { | ||
// use legacy txs by default | ||
transaction.type = 0; | ||
} | ||
transaction = await super.populateTransaction(transaction); | ||
@@ -69,9 +75,3 @@ if (transaction.customData == null && transaction.type != 0x70) { | ||
(_b = transaction.data) !== null && _b !== void 0 ? _b : (transaction.data = '0x'); | ||
(_c = (_k = transaction.customData).feeToken) !== null && _c !== void 0 ? _c : (_k.feeToken = utils_1.ETH_ADDRESS); | ||
(_d = (_l = transaction.customData).validFrom) !== null && _d !== void 0 ? _d : (_l.validFrom = utils_1.MIN_TIMESTAMP); | ||
(_e = (_m = transaction.customData).validUntil) !== null && _e !== void 0 ? _e : (_m.validUntil = utils_1.MAX_TIMESTAMP); | ||
(_f = (_o = transaction.customData).ergsPerStorage) !== null && _f !== void 0 ? _f : (_o.ergsPerStorage = 0); | ||
(_g = (_p = transaction.customData).ergsPerPubdata) !== null && _g !== void 0 ? _g : (_p.ergsPerPubdata = 0); | ||
(_h = (_q = transaction.customData).withdrawToken) !== null && _h !== void 0 ? _h : (_q.withdrawToken = ethers_1.ethers.constants.AddressZero); | ||
(_j = (_r = transaction.customData).factoryDeps) !== null && _j !== void 0 ? _j : (_r.factoryDeps = []); | ||
transaction.customData = this._fillCustomData(transaction.customData); | ||
return transaction; | ||
@@ -96,35 +96,2 @@ } | ||
} | ||
async withdraw(tx) { | ||
var _a; | ||
return await this.sendTransaction({ | ||
value: tx.amount, | ||
to: (_a = tx.to) !== null && _a !== void 0 ? _a : this.address, | ||
nonce: tx.nonce, | ||
gasLimit: tx.gasLimit, | ||
customData: { | ||
feeToken: tx.feeToken, | ||
validFrom: tx.validFrom, | ||
validUntil: tx.validUntil, | ||
withdrawToken: tx.token | ||
} | ||
}); | ||
} | ||
async transfer(tx) { | ||
var _a; | ||
(_a = tx.token) !== null && _a !== void 0 ? _a : (tx.token = utils_1.ETH_ADDRESS); | ||
const token = new ethers_1.ethers.Contract(tx.token, utils_1.IERC20, this); | ||
const overrides = { | ||
nonce: tx.nonce, | ||
gasLimit: tx.gasLimit, | ||
customData: { | ||
feeToken: tx.feeToken, | ||
validFrom: tx.validFrom, | ||
validUntil: tx.validUntil | ||
} | ||
}; | ||
if (tx.feeToken == null && tx.validFrom == null && tx.validUntil == null) { | ||
delete overrides.customData; | ||
} | ||
return await token.transfer(tx.to, tx.amount, overrides); | ||
} | ||
async sendTransaction(transaction) { | ||
@@ -135,70 +102,3 @@ // Typescript isn't smart enough to recognise that wallet.sendTransaction | ||
} | ||
async getMainContract() { | ||
const address = await this.provider.getMainContractAddress(); | ||
return new ethers_1.ethers.Contract(address, utils_1.ZKSYNC_MAIN_ABI, this.ethWallet()); | ||
} | ||
ethWallet() { | ||
if (this.providerL1 == null) { | ||
throw new Error('L1 provider missing: use `connectToL1` to specify'); | ||
} | ||
return new ethers_1.ethers.Wallet(this._signingKey(), this.providerL1); | ||
} | ||
connectToL1(provider) { | ||
return new Wallet(this._signingKey(), this.provider, provider); | ||
} | ||
async approveERC20(token, amount, overrides) { | ||
const erc20contract = new ethers_1.ethers.Contract(token, utils_1.IERC20, this.ethWallet()); | ||
const mainContract = await this.provider.getMainContractAddress(); | ||
let gasLimit; | ||
if (overrides === null || overrides === void 0 ? void 0 : overrides.gasLimit) { | ||
gasLimit = await overrides.gasLimit; | ||
} | ||
else { | ||
// For some reason, gas estimation for approves may be imprecise. | ||
// At least in the localhost scenario. | ||
gasLimit = await erc20contract.estimateGas.approve(mainContract, amount); | ||
gasLimit = gasLimit.gt(utils_1.RECOMMENDED_GAS_LIMIT.ERC20_APPROVE) | ||
? gasLimit | ||
: utils_1.RECOMMENDED_GAS_LIMIT.ERC20_APPROVE; | ||
} | ||
return await erc20contract.approve(mainContract, amount, { gasLimit, ...overrides }); | ||
} | ||
async deposit(transaction) { | ||
var _a, _b; | ||
const zksyncContract = await this.getMainContract(); | ||
const { token, amount } = transaction; | ||
const depositTo = (_a = transaction.to) !== null && _a !== void 0 ? _a : this.address; | ||
if ((0, utils_1.isETH)(token)) { | ||
return await this.provider.getPriorityOpResponse(await zksyncContract.depositETH(depositTo, { | ||
value: ethers_1.BigNumber.from(amount), | ||
gasLimit: ethers_1.BigNumber.from(utils_1.RECOMMENDED_GAS_LIMIT.ETH_DEPOSIT), | ||
...transaction.overrides | ||
})); | ||
} | ||
else { | ||
let nonce = undefined; | ||
if (transaction.approveERC20) { | ||
const approveTx = await this.approveERC20(token, amount, transaction.approveOverrides); | ||
nonce = approveTx.nonce + 1; | ||
} | ||
const overrides = { nonce, ...transaction.overrides }; | ||
const args = [token, amount, depositTo]; | ||
if (overrides.gasLimit == null) { | ||
const gasEstimate = await zksyncContract.estimateGas | ||
.depositERC20(...args, overrides) | ||
.catch(() => ethers_1.BigNumber.from(0)); | ||
const recommendedGasLimit = (_b = utils_1.RECOMMENDED_GAS_LIMIT.ERC20_DEPOSIT[token]) !== null && _b !== void 0 ? _b : utils_1.RECOMMENDED_GAS_LIMIT.ERC20_DEFAULT_DEPOSIT; | ||
overrides.gasLimit = gasEstimate.gte(recommendedGasLimit) ? gasEstimate : recommendedGasLimit; | ||
} | ||
return await this.provider.getPriorityOpResponse(await zksyncContract.depositERC20(...args, overrides)); | ||
} | ||
} | ||
async addToken(token, overrides) { | ||
const zksyncContract = await this.getMainContract(); | ||
return await this.provider.getPriorityOpResponse(await zksyncContract.addToken(token, { | ||
gasLimit: ethers_1.BigNumber.from(utils_1.RECOMMENDED_GAS_LIMIT.ADD_TOKEN), | ||
...overrides | ||
})); | ||
} | ||
} | ||
exports.Wallet = Wallet; |
{ | ||
"name": "zksync-web3", | ||
"version": "0.2.7", | ||
"version": "0.2.8", | ||
"main": "build/index.js", | ||
@@ -5,0 +5,0 @@ "types": "build/index.d.ts", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
95736
24
2945