Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
fix: resolve issues related to TransactionResponse.wait() and `Tran…
Browse files Browse the repository at this point in the history
…sactionResponse.waitFinalize()`
  • Loading branch information
danijelTxFusion committed Sep 29, 2023
1 parent 75c14e9 commit 9b02e95
Show file tree
Hide file tree
Showing 12 changed files with 416 additions and 161 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/npm.publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Publish NPM package
on:
workflow_dispatch:
inputs:
ref:
description: "ref or tag to publish NPM package from"
default: ""
required: false
tag:
required: true
type: choice
description: package tag
default: latest
options:
- latest
- next
- beta
jobs:
zksync:
name: Publish JavaScript SDK
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ inputs.ref }}
- uses: actions/setup-node@v3
with:
node-version: 18
registry-url: 'https://registry.npmjs.org'
- name: Build package and publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
npm install
npm run build
npm publish --tag ${{ inputs.tag }}
22 changes: 11 additions & 11 deletions src/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import {
undoL1ToL2Alias
} from './utils';
import {
IERC20__factory,
IL1Bridge__factory,
IL2Bridge__factory,
IERC20__factory, IL1Bridge,
IL1Bridge__factory, IL2Bridge,
IL2Bridge__factory, IZkSync,
IZkSync__factory
} from '../typechain';
import {Address, BalancesMap, Eip712Meta, FullDepositFee, Log, PriorityOpResponse, TransactionResponse} from './types';
import {Address, BalancesMap, Eip712Meta, FullDepositFee, PriorityOpResponse, TransactionResponse} from './types';

type Constructor<T = {}> = new (...args: any[]) => T;

Expand All @@ -48,12 +48,12 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
throw new Error('Must be implemented by the derived class!');
}

async getMainContract() {
async getMainContract(): Promise<IZkSync> {
const address = await this._providerL2().getMainContractAddress();
return IZkSync__factory.connect(address, this._signerL1());
}

async getL1BridgeContracts() {
async getL1BridgeContracts(): Promise<{erc20: IL1Bridge}> {
const addresses = await this._providerL2().getDefaultBridgeAddresses();
return {
erc20: IL1Bridge__factory.connect(addresses.erc20L1, this._signerL1())
Expand All @@ -80,7 +80,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
return await erc20contract.allowance(await this.getAddress(), bridgeAddress, { blockTag });
}

async l2TokenAddress(token: Address) {
async l2TokenAddress(token: Address): Promise<string> {
if (token == ETH_ADDRESS) {
return ETH_ADDRESS;
} else {
Expand Down Expand Up @@ -322,7 +322,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
tx.to ??= await this.getAddress();
tx.gasPerPubdataByte ??= REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_LIMIT;

let l2GasLimit = 0n;
let l2GasLimit = null;
if (tx.bridgeAddress != null) {
const customBridgeData =
tx.customBridgeData ?? (await getERC20DefaultBridgeData(tx.token, this._providerL1()));
Expand Down Expand Up @@ -461,7 +461,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
};
}

async finalizeWithdrawal(withdrawalHash: BytesLike, index: number = 0, overrides?: ethers.Overrides) {
async finalizeWithdrawal(withdrawalHash: BytesLike, index: number = 0, overrides?: ethers.Overrides): Promise<ethers.ContractTransactionResponse> {
const {l1BatchNumber, l2MessageIndex, l2TxNumberInBlock, message, sender, proof} =
await this.finalizeWithdrawalParams(withdrawalHash, index);

Expand Down Expand Up @@ -655,15 +655,15 @@ export function AdapterL2<TBase extends Constructor<TxSender>>(Base: TBase) {
throw new Error('Must be implemented by the derived class!');
}

async getBalance(token?: Address, blockTag: BlockTag = 'committed') {
async getBalance(token?: Address, blockTag: BlockTag = 'committed'): Promise<bigint> {
return await this._providerL2().getBalance(await this.getAddress(), blockTag, token);
}

async getAllBalances(): Promise<BalancesMap> {
return await this._providerL2().getAllAccountBalances(await this.getAddress());
}

async getL2BridgeContracts() {
async getL2BridgeContracts(): Promise<{erc20: IL2Bridge}> {
const addresses = await this._providerL2().getDefaultBridgeAddresses();
return {
erc20: IL2Bridge__factory.connect(addresses.erc20L2, this._signerL2())
Expand Down
2 changes: 1 addition & 1 deletion src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ const _formatTransactionReceipt = object(
type: allowNull(getNumber, 0),
l1BatchNumber: allowNull(getNumber),
l1BatchTxIndex: allowNull(getNumber),
l2ToL1Logs: arrayOf(formatL2ToL1Log)
l2ToL1Logs: allowNull(arrayOf(formatL2ToL1Log), [])
},
{
effectiveGasPrice: ['gasPrice'],
Expand Down
47 changes: 26 additions & 21 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
Eip1193Provider,
JsonRpcError,
JsonRpcResult,
JsonRpcPayload
JsonRpcPayload,
resolveProperties
} from 'ethers';
import { IERC20__factory, IEthToken__factory, IL2Bridge__factory } from '../typechain';
import {
Expand All @@ -30,7 +31,10 @@ import {
TransactionDetails,
BlockDetails,
ContractAccountInfo,
Network as ZkSyncNetwork, BatchDetails, Fee
Network as ZkSyncNetwork,
BatchDetails,
Fee,
Transaction
} from './types';
import {
isETH,
Expand Down Expand Up @@ -109,7 +113,7 @@ export class Provider extends ethers.JsonRpcProvider {
}
}

async l2TokenAddress(token: Address) {
async l2TokenAddress(token: Address): Promise<string> {
if (token == ETH_ADDRESS) {
return ETH_ADDRESS;
} else {
Expand All @@ -119,7 +123,7 @@ export class Provider extends ethers.JsonRpcProvider {
}
}

async l1TokenAddress(token: Address) {
async l1TokenAddress(token: Address): Promise<string> {
if (token == ETH_ADDRESS) {
return ETH_ADDRESS;
} else {
Expand Down Expand Up @@ -148,15 +152,6 @@ export class Provider extends ethers.JsonRpcProvider {
this.contractAddresses = {};
}

async getMessageProof(
blockNumber: number,
sender: Address,
messageHash: BytesLike,
logIndex?: number
): Promise<MessageProof | null> {
return await this.send('zks_getL2ToL1MsgProof', [blockNumber, sender, ethers.hexlify(messageHash), logIndex]);
}

async getLogProof(txHash: BytesLike, index?: number): Promise<MessageProof | null> {
return await this.send('zks_getL2ToL1LogProof', [ethers.hexlify(txHash), index]);
}
Expand Down Expand Up @@ -199,10 +194,6 @@ export class Provider extends ethers.JsonRpcProvider {
return tokens.map((token) => ({ address: token.l2Address, ...token }));
}

async getTokenPrice(token: Address): Promise<string | null> {
return await this.send('zks_getTokenPrice', [token]);
}

async getAllAccountBalances(address: Address): Promise<BalancesMap> {
let balances = await this.send('zks_getAllAccountBalances', [address]);
for (let token in balances) {
Expand Down Expand Up @@ -359,7 +350,7 @@ export class Provider extends ethers.JsonRpcProvider {
}

// This is inefficient. Status should probably be indicated in the transaction receipt.
async getTransactionStatus(txHash: string) {
async getTransactionStatus(txHash: string): Promise<TransactionStatus> {
const tx = await this.getTransaction(txHash);
if (tx == null) {
return TransactionStatus.NotFound;
Expand All @@ -375,7 +366,21 @@ export class Provider extends ethers.JsonRpcProvider {
}

override async broadcastTransaction(signedTx: string): Promise<TransactionResponse> {
return (await super.broadcastTransaction(signedTx)) as TransactionResponse;
const { blockNumber, hash, network } = await resolveProperties({
blockNumber: this.getBlockNumber(),
hash: this._perform({
method: "broadcastTransaction",
signedTransaction: signedTx
}),
network: this.getNetwork()
});

const tx = Transaction.from(signedTx);
if (tx.hash !== hash) {
throw new Error("@TODO: the returned hash did not match");
}

return this._wrapTransactionResponse(<any>tx, network).replaceableTransaction(blockNumber);
}

async getL2TransactionFromPriorityOp(l1TxResponse: ethers.TransactionResponse): Promise<TransactionResponse> {
Expand Down Expand Up @@ -563,10 +568,10 @@ export class BrowserProvider extends Provider {
}
}

return Signer.from((await super.getSigner(address)) as any);
return Signer.from((await super.getSigner(address)) as any, Number((await this.getNetwork()).chainId));
}

override async estimateGas(transaction: TransactionRequest) {
override async estimateGas(transaction: TransactionRequest): Promise<bigint> {
const gas = await super.estimateGas(transaction);
const metamaskMinimum = 21000n;
const isEIP712 = transaction.customData != null || transaction.type == EIP712_TX_TYPE;
Expand Down
10 changes: 4 additions & 6 deletions src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ export class Signer extends AdapterL2(ethers.JsonRpcSigner) {
return this.provider;
}

static from(signer: ethers.JsonRpcSigner & { provider: Provider }): Signer {
static from(signer: ethers.JsonRpcSigner & { provider: Provider }, chainId: number): Signer {
const newSigner: Signer = Object.setPrototypeOf(signer, Signer.prototype);
// @ts-ignore
newSigner.eip712 = new EIP712Signer(newSigner, newSigner.getChainId());
newSigner.eip712 = new EIP712Signer(newSigner, chainId);
return newSigner;
}

Expand Down Expand Up @@ -177,10 +176,9 @@ export class L2VoidSigner extends AdapterL2(ethers.VoidSigner) {
return this.provider;
}

static from(signer: ethers.VoidSigner & { provider: Provider }): L2VoidSigner {
static from(signer: ethers.VoidSigner & { provider: Provider }, chainId: number): L2VoidSigner {
const newSigner: L2VoidSigner = Object.setPrototypeOf(signer, L2VoidSigner.prototype);
// @ts-ignore
newSigner.eip712 = new EIP712Signer(newSigner, newSigner.getChainId());
newSigner.eip712 = new EIP712Signer(newSigner, chainId);
return newSigner;
}

Expand Down
Loading

0 comments on commit 9b02e95

Please sign in to comment.