From 796a9a5bc35c4b690d01f2d35c58933fda121d78 Mon Sep 17 00:00:00 2001 From: volodymyr-basiuk <31999965+volodymyr-basiuk@users.noreply.github.com> Date: Fri, 10 Nov 2023 11:40:57 +0200 Subject: [PATCH] extend connection config with fee options (#151) * extend connection config with fee options * txnReceipt type fixes * refactor submitZKPResponse with fee data params --- package.json | 2 +- .../blockchain/onchain-zkp-verifier.ts | 22 +++++++++++++-- src/storage/blockchain/state.ts | 27 ++++++++++++++++--- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index ef7185bd..7c21fd05 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@0xpolygonid/js-sdk", - "version": "1.4.1", + "version": "1.4.2", "description": "SDK to work with Polygon ID", "main": "dist/node/cjs/index.js", "module": "dist/node/esm/index.js", diff --git a/src/storage/blockchain/onchain-zkp-verifier.ts b/src/storage/blockchain/onchain-zkp-verifier.ts index 56b50c23..2c875b17 100644 --- a/src/storage/blockchain/onchain-zkp-verifier.ts +++ b/src/storage/blockchain/onchain-zkp-verifier.ts @@ -1,4 +1,4 @@ -import { JsonRpcProvider, Signer, Contract } from 'ethers'; +import { JsonRpcProvider, Signer, Contract, TransactionRequest } from 'ethers'; import { EthConnectionConfig } from './state'; import { IOnChainZKPVerifier } from '../interfaces/onchain-zkp-verifier'; import { ContractInvokeTransactionData, ZeroKnowledgeProofResponse } from '../../iden3comm'; @@ -70,7 +70,25 @@ export class OnChainZKPVerifier implements IOnChainZKPVerifier { zkProof.proof.pi_c.slice(0, 2) ]; - const tx = await contract.submitZKPResponse(...payload); + const feeData = await provider.getFeeData(); + const maxFeePerGas = chainConfig.maxFeePerGas + ? BigInt(chainConfig.maxFeePerGas) + : feeData.maxFeePerGas; + const maxPriorityFeePerGas = chainConfig.maxPriorityFeePerGas + ? BigInt(chainConfig.maxPriorityFeePerGas) + : feeData.maxPriorityFeePerGas; + + const gasLimit = await contract.submitZKPResponse.estimateGas(...payload); + const txData = await contract.submitZKPResponse.populateTransaction(...payload); + + const request: TransactionRequest = { + to: txData.to, + data: txData.data, + gasLimit, + maxFeePerGas, + maxPriorityFeePerGas + }; + const tx = await ethSigner.sendTransaction(request); const txnReceipt = await tx.wait(); if (!txnReceipt) { throw new Error(`transaction: ${tx.hash} failed to mined`); diff --git a/src/storage/blockchain/state.ts b/src/storage/blockchain/state.ts index c81cfe77..0200a348 100644 --- a/src/storage/blockchain/state.ts +++ b/src/storage/blockchain/state.ts @@ -1,7 +1,7 @@ import { RootInfo, StateProof } from './../entities/state'; import { ZKProof } from '@iden3/js-jwz'; import { IStateStorage } from '../interfaces/state'; -import { Contract, JsonRpcProvider, Signer } from 'ethers'; +import { Contract, JsonRpcProvider, Signer, TransactionRequest } from 'ethers'; import { StateInfo } from '../entities/state'; import { StateTransitionPubSignals } from '../../circuits'; import { byteEncoder } from '../../utils'; @@ -17,7 +17,9 @@ export interface EthConnectionConfig { url: string; defaultGasLimit: number; minGasPrice?: string; - maxGasPrice?: string; + maxGasPrice?: string; // eip-1559 transaction do not support gasPrice + maxFeePerGas?: string; + maxPriorityFeePerGas?: string; confirmationBlockCount: number; confirmationTimeout: number; contractAddress: string; @@ -102,9 +104,26 @@ export class EthStateStorage implements IStateStorage { proof.proof.pi_c.slice(0, 2) ]; - await contract.transitState.estimateGas(...payload); + const feeData = await this.provider.getFeeData(); - const tx = await contract.transitState(...payload); + const maxFeePerGas = defaultEthConnectionConfig.maxFeePerGas + ? BigInt(defaultEthConnectionConfig.maxFeePerGas) + : feeData.maxFeePerGas; + const maxPriorityFeePerGas = defaultEthConnectionConfig.maxPriorityFeePerGas + ? BigInt(defaultEthConnectionConfig.maxPriorityFeePerGas) + : feeData.maxPriorityFeePerGas; + + const gasLimit = await contract.transitState.estimateGas(...payload); + const txData = await contract.transitState.populateTransaction(...payload); + + const request: TransactionRequest = { + to: txData.to, + data: txData.data, + gasLimit, + maxFeePerGas, + maxPriorityFeePerGas + }; + const tx = await signer.sendTransaction(request); const txnReceipt = await tx.wait(); if (!txnReceipt) {