Skip to content

Commit

Permalink
Merge pull request #2 from lambdaclass/use-conversion-rate-and-fixes
Browse files Browse the repository at this point in the history
Use conversion rate when depositing, fix deposits also depositing eth
  • Loading branch information
jrchatruc authored Jan 16, 2024
2 parents 71a910c + 6bc9681 commit 169f8f8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
24 changes: 16 additions & 8 deletions build/src/adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function AdapterL1(Base) {
return (0, utils_1.scaleGasLimit)(baseGasLimit);
}
async getDepositTx(transaction, nativeERC20) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
const bridgeContracts = await this.getL1BridgeContracts();
if (transaction.bridgeAddress != null) {
bridgeContracts.erc20 = bridgeContracts.erc20.attach(transaction.bridgeAddress);
Expand All @@ -185,7 +185,12 @@ function AdapterL1(Base) {
const zksyncContract = await this.getMainContract();
const baseCost = await zksyncContract.l2TransactionBaseCost(await gasPriceForEstimation, tx.l2GasLimit, tx.gasPerPubdataByte);
if (token == utils_1.ETH_ADDRESS || nativeERC20 == token) {
(_h = overrides.value) !== null && _h !== void 0 ? _h : (overrides.value = baseCost.add(operatorTip).add(amount));
if (token == utils_1.ETH_ADDRESS) {
(_h = overrides.value) !== null && _h !== void 0 ? _h : (overrides.value = baseCost.add(operatorTip).add(amount));
}
else {
(_j = overrides.value) !== null && _j !== void 0 ? _j : (overrides.value = baseCost.add(operatorTip));
}
return {
contractAddress: to,
calldata: "0x",
Expand All @@ -197,7 +202,7 @@ function AdapterL1(Base) {
};
}
else {
let refundRecipient = (_j = tx.refundRecipient) !== null && _j !== void 0 ? _j : ethers_1.ethers.constants.AddressZero;
let refundRecipient = (_k = tx.refundRecipient) !== null && _k !== void 0 ? _k : ethers_1.ethers.constants.AddressZero;
const args = [
to,
token,
Expand All @@ -206,7 +211,7 @@ function AdapterL1(Base) {
tx.gasPerPubdataByte,
refundRecipient,
];
(_k = overrides.value) !== null && _k !== void 0 ? _k : (overrides.value = baseCost.add(operatorTip));
(_l = overrides.value) !== null && _l !== void 0 ? _l : (overrides.value = baseCost.add(operatorTip));
await (0, utils_1.checkBaseCost)(baseCost, overrides.value);
let l2WethToken = ethers_1.ethers.constants.AddressZero;
try {
Expand Down Expand Up @@ -395,7 +400,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 @@ -408,13 +413,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
17 changes: 12 additions & 5 deletions src/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,11 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
);

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

if(token == ETH_ADDRESS) {
overrides.value ??= baseCost.add(operatorTip).add(amount);
} else {
overrides.value ??= baseCost.add(operatorTip);
}
return {
contractAddress: to,
calldata: "0x",
Expand Down Expand Up @@ -724,15 +727,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);

overrides.value = 0;

await checkBaseCost(baseCost, overrides.value);
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 169f8f8

Please sign in to comment.