Skip to content

Commit

Permalink
Use conversion rate when depositing, fix deposits also depositing the…
Browse files Browse the repository at this point in the history
… same amount of eth
  • Loading branch information
jrchatruc committed Jan 10, 2024
1 parent c34ebb9 commit 7b95c34
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
13 changes: 8 additions & 5 deletions build/src/adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function AdapterL1(Base) {
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).add(amount));
(_h = overrides.value) !== null && _h !== void 0 ? _h : (overrides.value = baseCost.add(operatorTip));
return {
contractAddress: to,
calldata: "0x",
Expand Down Expand Up @@ -398,7 +398,7 @@ function AdapterL1(Base) {
return this._providerL1().estimateGas(requestExecuteTx);
}
async getRequestExecuteTx(transaction) {
var _a, _b, _c, _d, _e, _f, _g, _h;
var _a, _b, _c, _d, _e, _f, _g;
const zksyncContract = await this.getMainContract();
const { ...tx } = transaction;
(_a = tx.l2Value) !== null && _a !== void 0 ? _a : (tx.l2Value = ethers_1.BigNumber.from(0));
Expand All @@ -411,13 +411,16 @@ function AdapterL1(Base) {
const { contractAddress, l2Value, calldata, l2GasLimit, factoryDeps, operatorTip, overrides, gasPerPubdataByte, refundRecipient, } = tx;
await insertGasPrice(this._providerL1(), overrides);
const gasPriceForEstimation = overrides.maxFeePerGas || overrides.gasPrice;
const baseCost = await this.getBaseCost({
// This base cost has to be priced in the ERC20 token because it will be paid on L2.
let baseCost = await this.getBaseCost({
gasPrice: await gasPriceForEstimation,
gasPerPubdataByte,
gasLimit: l2GasLimit,
});
(_h = overrides.value) !== null && _h !== void 0 ? _h : (overrides.value = baseCost.add(operatorTip).add(l2Value));
await (0, utils_1.checkBaseCost)(baseCost, overrides.value);
const conversionRate = await this._providerL2().getConversionRate();
baseCost = baseCost.mul(conversionRate);
overrides.value = 0;
await (0, utils_1.checkBaseCost)(baseCost, l2Value);
return await zksyncContract.populateTransaction.requestL2Transaction(contractAddress, l2Value, baseCost.add(operatorTip).add(l2Value), calldata, l2GasLimit, utils_1.REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_LIMIT, factoryDeps, refundRecipient, overrides);
}
};
Expand Down
1 change: 1 addition & 0 deletions build/src/provider.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export declare class Provider extends ethers.providers.JsonRpcProvider {
getTransactionDetails(txHash: BytesLike): Promise<TransactionDetails>;
getBytecodeByHash(bytecodeHash: BytesLike): Promise<Uint8Array>;
getRawBlockTransactions(number: number): Promise<RawBlockTransaction[]>;
getConversionRate(): Promise<BigNumber>;
getWithdrawTx(transaction: {
token: Address;
amount: BigNumberish;
Expand Down
3 changes: 3 additions & 0 deletions build/src/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@ class Provider extends ethers_1.ethers.providers.JsonRpcProvider {
async getRawBlockTransactions(number) {
return await this.send("zks_getRawBlockTransactions", [number]);
}
async getConversionRate() {
return await this.send("zks_getConversionRate", []);
}
async getWithdrawTx(transaction) {
var _a, _b, _c;
var _d;
Expand Down
12 changes: 8 additions & 4 deletions src/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
);

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

return {
contractAddress: to,
Expand Down Expand Up @@ -717,15 +717,19 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
await insertGasPrice(this._providerL1(), overrides);
const gasPriceForEstimation = overrides.maxFeePerGas || overrides.gasPrice;

const baseCost = await this.getBaseCost({
// This base cost has to be priced in the ERC20 token because it will be paid on L2.
let baseCost = await this.getBaseCost({
gasPrice: await gasPriceForEstimation,
gasPerPubdataByte,
gasLimit: l2GasLimit,
});

overrides.value ??= baseCost.add(operatorTip).add(l2Value);
const conversionRate = await this._providerL2().getConversionRate();
baseCost = baseCost.mul(conversionRate);

await checkBaseCost(baseCost, overrides.value);
overrides.value = 0;

await checkBaseCost(baseCost, l2Value);

return await zksyncContract.populateTransaction.requestL2Transaction(
contractAddress,
Expand Down
4 changes: 4 additions & 0 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@ export class Provider extends ethers.providers.JsonRpcProvider {
return await this.send("zks_getRawBlockTransactions", [number]);
}

async getConversionRate(): Promise<BigNumber> {
return await this.send("zks_getConversionRate", []);
}

async getWithdrawTx(transaction: {
token: Address;
amount: BigNumberish;
Expand Down

0 comments on commit 7b95c34

Please sign in to comment.