Skip to content

Commit

Permalink
feat: align types and RPC endpoints with version v24.7.0 of a node
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Types and RPC endpoints are aligned with version
`v24.7.0` of a node.
  • Loading branch information
danijelTxFusion committed Jun 19, 2024
1 parent 417a8a9 commit a419e45
Show file tree
Hide file tree
Showing 6 changed files with 639 additions and 84 deletions.
16 changes: 8 additions & 8 deletions src/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
}

/**
* Returns `Contract` wrapper of the zkSync Era smart contract.
* Returns `Contract` wrapper of the ZKsync Era smart contract.
*/
async getMainContract(): Promise<IZkSyncHyperchain> {
const address = await this._providerL2().getMainContractAddress();
Expand Down Expand Up @@ -169,7 +169,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
*
* @param token The Ethereum address of the token.
* @param [bridgeAddress] The address of the bridge contract to be used.
* Defaults to the default zkSync Era bridge, either `L1EthBridge` or `L1ERC20Bridge`.
* Defaults to the default ZKsync Era bridge, either `L1EthBridge` or `L1ERC20Bridge`.
* @param [blockTag] The block in which an allowance should be checked.
* Defaults to 'committed', i.e., the latest processed block.
*/
Expand Down Expand Up @@ -197,7 +197,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
* Returns the L2 token address equivalent for a L1 token address as they are not necessarily equal.
* The ETH address is set to the zero address.
*
* @remarks Only works for tokens bridged on default zkSync Era bridges.
* @remarks Only works for tokens bridged on default ZKsync Era bridges.
*
* @param token The address of the token on L1.
*/
Expand All @@ -206,7 +206,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
}

/**
* Bridging ERC20 tokens from L1 requires approving the tokens to the zkSync Era smart contract.
* Bridging ERC20 tokens from L1 requires approving the tokens to the ZKsync Era smart contract.
*
* @param token The L1 address of the token.
* @param amount The amount of the token to be approved.
Expand Down Expand Up @@ -348,7 +348,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
* explicitly stated in the overrides, this field will be equal to the tip the operator will receive on top of
* the base cost of the transaction.
* @param [transaction.bridgeAddress] The address of the bridge contract to be used.
* Defaults to the default zkSync Era bridge (either `L1EthBridge` or `L1Erc20Bridge`).
* Defaults to the default ZKsync Era bridge (either `L1EthBridge` or `L1Erc20Bridge`).
* @param [transaction.approveERC20] Whether or not token approval should be performed under the hood.
* Set this flag to true if you bridge an ERC20 token and didn't call the {@link approveERC20} function beforehand.
* @param [transaction.approveBaseERC20] Whether or not base token approval should be performed under the hood.
Expand Down Expand Up @@ -689,7 +689,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
* explicitly stated in the overrides, this field will be equal to the tip the operator will receive on top of the
* base cost of the transaction.
* @param [transaction.bridgeAddress] The address of the bridge contract to be used.
* Defaults to the default zkSync Era bridge (either `L1EthBridge` or `L1Erc20Bridge`).
* Defaults to the default ZKsync Era bridge (either `L1EthBridge` or `L1Erc20Bridge`).
* @param [transaction.l2GasLimit] Maximum amount of L2 gas that the transaction can consume during execution on L2.
* @param [transaction.gasPerPubdataByte] The L2 gas price for each published L1 calldata byte.
* @param [transaction.customBridgeData] Additional data that can be sent to a bridge.
Expand Down Expand Up @@ -734,7 +734,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
* @param [transaction.operatorTip] (currently not used) If the ETH value passed with the transaction is not
* explicitly stated in the overrides, this field will be equal to the tip the operator will receive on top of the
* base cost of the transaction.
* @param [transaction.bridgeAddress] The address of the bridge contract to be used. Defaults to the default zkSync
* @param [transaction.bridgeAddress] The address of the bridge contract to be used. Defaults to the default ZKsync
* Era bridge (either `L1EthBridge` or `L1Erc20Bridge`).
* @param [transaction.l2GasLimit] Maximum amount of L2 gas that the transaction can consume during execution on L2.
* @param [transaction.gasPerPubdataByte] The L2 gas price for each published L1 calldata byte.
Expand Down Expand Up @@ -1191,7 +1191,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
* @param transaction.token The address of the token to deposit. ETH by default.
* @param [transaction.to] The address that will receive the deposited tokens on L2.
* @param [transaction.bridgeAddress] The address of the bridge contract to be used.
* Defaults to the default zkSync Era bridge (either `L1EthBridge` or `L1Erc20Bridge`).
* Defaults to the default ZKsync Era bridge (either `L1EthBridge` or `L1Erc20Bridge`).
* @param [transaction.customBridgeData] Additional data that can be sent to a bridge.
* @param [transaction.gasPerPubdataByte] The L2 gas price for each published L1 calldata byte.
* @param [transaction.overrides] Transaction's overrides which may be used to pass L1 `gasLimit`, `gasPrice`, `value`, etc.
Expand Down
38 changes: 26 additions & 12 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
RawBlockTransaction,
StorageProof,
PaymasterParams,
Eip712Meta,
Eip712Meta, LogProof,
} from './types';
import {
BOOTLOADER_FORMAL_ADDRESS,
Expand All @@ -60,7 +60,7 @@ import {Il2SharedBridge} from './typechain/Il2SharedBridge';
let defaultFormatter: Formatter | null = null;

/**
* A `Provider` extends {@link ethers.providers.JsonRpcProvider} and includes additional features for interacting with zkSync Era.
* A `Provider` extends {@link ethers.providers.JsonRpcProvider} and includes additional features for interacting with ZKsync Era.
* It supports RPC endpoints within the `zks` namespace.
*/
export class Provider extends ethers.providers.JsonRpcProvider {
Expand Down Expand Up @@ -527,7 +527,7 @@ export class Provider extends ethers.providers.JsonRpcProvider {
* Returns the L2 token address equivalent for a L1 token address as they are not equal.
* ETH address is set to zero address.
*
* @remarks Only works for tokens bridged on default zkSync Era bridges.
* @remarks Only works for tokens bridged on default ZKsync Era bridges.
*
* @param token The address of the token on L1.
* @param bridgeAddress The address of custom bridge, which will be used to get l2 token address.
Expand Down Expand Up @@ -563,7 +563,7 @@ export class Provider extends ethers.providers.JsonRpcProvider {
* Returns the L1 token address equivalent for a L2 token address as they are not equal.
* ETH address is set to zero address.
*
* @remarks Only works for tokens bridged on default zkSync Era bridges.
* @remarks Only works for tokens bridged on default ZKsync Era bridges.
*
* @param token The address of the token on L2.
*
Expand Down Expand Up @@ -739,7 +739,13 @@ export class Provider extends ethers.providers.JsonRpcProvider {
* console.log(`Fee: ${utils.toJSON(fee)}`);
*/
async estimateFee(transaction: TransactionRequest): Promise<Fee> {
return await this.send('zks_estimateFee', [transaction]);
const fee = await this.send('zks_estimateFee', [transaction]);
return {
gasLimit: BigNumber.from(fee.gas_limit),
gasPerPubdataLimit: BigNumber.from(fee.gas_per_pubdata_limit),
maxPriorityFeePerGas: BigNumber.from(fee.max_priority_fee_per_gas),
maxFeePerGas: BigNumber.from(fee.max_fee_per_gas),
}
}

/**
Expand Down Expand Up @@ -857,7 +863,7 @@ export class Provider extends ethers.providers.JsonRpcProvider {
}

/**
* Returns the main zkSync Era smart contract address.
* Returns the main ZKsync Era smart contract address.
*
* Calls the {@link https://docs.zksync.io/build/api.html#zks-getmaincontract zks_getMainContract} JSON-RPC method.
*
Expand Down Expand Up @@ -954,7 +960,7 @@ export class Provider extends ethers.providers.JsonRpcProvider {
}

/**
* Returns the addresses of the default zkSync Era bridge contracts on both L1 and L2.
* Returns the addresses of the default ZKsync Era bridge contracts on both L1 and L2.
*
* Calls the {@link https://docs.zksync.io/build/api.html#zks-getbridgecontracts zks_getBridgeContracts} JSON-RPC method.
*
Expand Down Expand Up @@ -1000,7 +1006,7 @@ export class Provider extends ethers.providers.JsonRpcProvider {
}

/**
* Returns contract wrapper. If given address is shared bridge addres it returns Il2SharedBridge and if its legacy it returns Il2Bridge.
* Returns contract wrapper. If given address is shared bridge address it returns Il2SharedBridge and if its legacy it returns Il2Bridge.
**
* @param address The bridge address.
*
Expand Down Expand Up @@ -1557,7 +1563,7 @@ export class Provider extends ethers.providers.JsonRpcProvider {
/**
* Creates a new `Provider` from provided URL or network name.
*
* @param zksyncNetwork The type of zkSync network.
* @param zksyncNetwork The type of ZKsync network.
*
* @example
*
Expand Down Expand Up @@ -1803,6 +1809,7 @@ export class Provider extends ethers.providers.JsonRpcProvider {
* @example
*
* import { Provider, types, utils } from "zksync-ethers";
* import { ethers } from "ethers";
*
* const provider = Provider.getDefaultProvider(types.Network.Sepolia);
* const ethProvider = ethers.getDefaultProvider("sepolia");
Expand Down Expand Up @@ -1838,6 +1845,7 @@ export class Provider extends ethers.providers.JsonRpcProvider {
* @example
*
* import { Provider, types, utils } from "zksync-ethers";
* import { ethers } from "ethers";
*
* const provider = Provider.getDefaultProvider(types.Network.Sepolia);
* const ethProvider = ethers.getDefaultProvider("sepolia");
Expand Down Expand Up @@ -1967,7 +1975,6 @@ export class Provider extends ethers.providers.JsonRpcProvider {
* });
* console.log(`Gas L1 to L2: ${gasL1ToL2}`);
*/
// TODO (EVM-3): support refundRecipient for fee estimation
async estimateL1ToL2Execute(transaction: {
contractAddress: Address;
calldata: BytesLike;
Expand Down Expand Up @@ -2003,7 +2010,7 @@ export class Provider extends ethers.providers.JsonRpcProvider {

/* c8 ignore start */
/**
* A `Web3Provider` extends {@link ExternalProvider} and includes additional features for interacting with zkSync Era.
* A `Web3Provider` extends {@link ExternalProvider} and includes additional features for interacting with ZKsync Era.
* It supports RPC endpoints within the `zks` namespace.
* This provider is designed for frontend use in a browser environment and integration for browser wallets
* (e.g., MetaMask, WalletConnect).
Expand Down Expand Up @@ -2250,7 +2257,7 @@ export class Web3Provider extends Provider {
override async getLogProof(
txHash: BytesLike,
index?: number
): Promise<MessageProof | null> {
): Promise<LogProof | null> {
return super.getLogProof(txHash, index);
}

Expand Down Expand Up @@ -2922,6 +2929,13 @@ export class Web3Provider extends Provider {
* @param addressOrIndex The address or index of the account to retrieve the signer for.
*
* @throws {Error} If the account doesn't exist.
*
* @example
*
* import { Web3Provider, utils } from "zksync-ethers";
*
* const provider = new Web3Provider(window.ethereum);
* const signer = await provider.getSigner();
*/
override getSigner(addressOrIndex?: number | string): Signer {
return Signer.from(super.getSigner(addressOrIndex) as any);
Expand Down
68 changes: 64 additions & 4 deletions src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {IBridgehub} from './typechain/IBridgehub';
import {Il2SharedBridge} from './typechain/Il2SharedBridge';

/**
* All typed data conforming to the EIP712 standard within zkSync Era.
* All typed data conforming to the EIP712 standard within ZKsync Era.
*/
export const EIP712_TYPES = {
Transaction: [
Expand All @@ -58,10 +58,22 @@ export const EIP712_TYPES = {
};

/**
* A `EIP712Signer` provides support for signing EIP712-typed zkSync Era transactions.
* A `EIP712Signer` provides support for signing EIP712-typed ZKsync Era transactions.
*/
export class EIP712Signer {
private eip712Domain: Promise<TypedDataDomain>;

/**
* @example
*
* import { Provider, types, EIP712Signer } from "zksync-ethers";
* import { ethers } from "ethers";
*
* const PRIVATE_KEY = "<PRIVATE_KEY>";
*
* const provider = Provider.getDefaultProvider(types.Network.Sepolia);
* const signer = new EIP712Signer(new ethers.Wallet(PRIVATE_KEY, Number(await provider.getNetwork()));
*/
constructor(
private ethSigner: ethers.Signer & TypedDataSigner,
chainId: number | Promise<number>
Expand All @@ -80,12 +92,14 @@ export class EIP712Signer {
*
* @example
*
* import { EIP712Signer } from "zksync-ethers";
*
* const tx = EIP712Signer.getSignInput({
* type: utils.EIP712_TX_TYPE,
* to: "0xa61464658AfeAf65CccaaFD3a512b69A83B77618",
* value: BigNumber.from(7_000_000),
* from: "0x36615Cf349d7F6344891B1e7CA7C72883F5dc049",
* nonce: 0,
* nonce: BigNumber.from(0),
* chainId: 270,
* gasPrice: BigNumber.from(250_000_000),
* gasLimit: BigNumber.from(21_000),
Expand Down Expand Up @@ -126,6 +140,25 @@ export class EIP712Signer {
*
* @param transaction The transaction request that needs to be signed.
* @returns A promise that resolves to the signature of the transaction.
*
* @example
*
* import { Provider, types, EIP712Signer } from "zksync-ethers";
* import { ethers } from "ethers";
*
* const PRIVATE_KEY = "<PRIVATE_KEY>";
*
* const provider = Provider.getDefaultProvider(types.Network.Sepolia);
* const signer = new EIP712Signer(new ethers.Wallet(PRIVATE_KEY, Number(await provider.getNetwork()));
* const signature = signer.sign({
* type: utils.EIP712_TX_TYPE,
* to: "0xa61464658AfeAf65CccaaFD3a512b69A83B77618",
* value: BigNumber.from(7_000_000),
* nonce: BigNumber.from(0),
* chainId: 270,
* gasPrice: BigNumber.from(250_000_000),
* gasLimit: BigNumber.from(21_000),
* });
*/
async sign(transaction: TransactionRequest): Promise<Signature> {
return await this.ethSigner._signTypedData(
Expand All @@ -142,6 +175,22 @@ export class EIP712Signer {
* @returns A hash (digest) of the transaction request.
*
* @throws {Error} If `transaction.chainId` is not set.
*
* @example
*
* import { EIP712Signer } from "zksync-ethers";
*
* const hash = EIP712Signer.getSignedDigest({
* type: utils.EIP712_TX_TYPE,
* to: "0xa61464658AfeAf65CccaaFD3a512b69A83B77618",
* value: BigNumber.from(7_000_000),
* from: "0x36615Cf349d7F6344891B1e7CA7C72883F5dc049",
* nonce: BigNumber.from(0),
* chainId: 270,
* gasPrice: BigNumber.from(250_000_000),
* gasLimit: BigNumber.from(21_000),
* customData: {},
* });
*/
static getSignedDigest(transaction: TransactionRequest): ethers.BytesLike {
if (!transaction.chainId) {
Expand All @@ -160,7 +209,18 @@ export class EIP712Signer {
}

/**
* Returns zkSync Era EIP712 domain.
* Returns ZKsync Era EIP712 domain.
*
* @example
*
* import { Provider, types, EIP712Signer } from "zksync-ethers";
* import { ethers } from "ethers";
*
* const PRIVATE_KEY = "<PRIVATE_KEY>";
*
* const provider = Provider.getDefaultProvider(types.Network.Sepolia);
* const signer = new EIP712Signer(new ethers.Wallet(PRIVATE_KEY, Number(await provider.getNetwork()));
* const domain = await signer.getDomain();
*/
async getDomain(): Promise<ethers.TypedDataDomain> {
return await this.eip712Domain;
Expand Down
Loading

0 comments on commit a419e45

Please sign in to comment.