From 3c234ea9ef3b0e65ccb01bc22ddd000e051d8f1b Mon Sep 17 00:00:00 2001 From: vbasiuk Date: Wed, 8 Nov 2023 13:49:58 +0200 Subject: [PATCH 1/5] extend connection config with fee options --- package.json | 4 ++-- src/storage/blockchain/state.ts | 42 +++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index ef7185bd..36c16d99 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", @@ -121,4 +121,4 @@ "engines": { "node": ">=18.16.0" } -} +} \ No newline at end of file diff --git a/src/storage/blockchain/state.ts b/src/storage/blockchain/state.ts index c81cfe77..ad822299 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; @@ -34,6 +36,8 @@ const defaultEthConnectionConfig: EthConnectionConfig = { defaultGasLimit: 600000, minGasPrice: '0', maxGasPrice: '100000000000', + maxFeePerGas: '', //'2000000000', + maxPriorityFeePerGas: '', //'2000000000', confirmationBlockCount: 5, confirmationTimeout: 600000, contractAddress: '', @@ -101,23 +105,41 @@ export class EthStateStorage implements IStateStorage { ], proof.proof.pi_c.slice(0, 2) ]; - - await contract.transitState.estimateGas(...payload); - - const tx = await contract.transitState(...payload); + + const feeData = await this.provider.getFeeData(); + + const maxFeePerGas = defaultEthConnectionConfig.maxFeePerGas ? + BigInt(defaultEthConnectionConfig.maxFeePerGas as string) : + feeData.maxFeePerGas; + const maxPriorityFeePerGas = defaultEthConnectionConfig.maxPriorityFeePerGas ? + BigInt(defaultEthConnectionConfig.maxPriorityFeePerGas as string) : + 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, // defaultGasLimit? + // gasPrice, // TypeError: eip-1559 transaction do not support gasPrice + maxFeePerGas, + maxPriorityFeePerGas + }; + const tx = await signer.sendTransaction(request); const txnReceipt = await tx.wait(); if (!txnReceipt) { throw new Error(`transaction: ${tx.hash} failed to mined`); } - const status: number | null = txnReceipt.status; - const txnHash: string = txnReceipt.hash; + const status: number | undefined | null = txnReceipt?.status; + const txnHash: string | undefined = txnReceipt?.hash; - if (!status) { + if (!status || !txnHash) { throw new Error(`transaction: ${txnHash} failed to mined`); } - return txnHash; + return txnHash as string; } /** {@inheritdoc IStateStorage.getGISTProof} */ From 163593aa02f61b496bae8946dd1d85f2ea9d9782 Mon Sep 17 00:00:00 2001 From: vbasiuk Date: Wed, 8 Nov 2023 13:53:34 +0200 Subject: [PATCH 2/5] txnReceipt type fixes --- src/storage/blockchain/state.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/storage/blockchain/state.ts b/src/storage/blockchain/state.ts index ad822299..691f213b 100644 --- a/src/storage/blockchain/state.ts +++ b/src/storage/blockchain/state.ts @@ -132,10 +132,10 @@ export class EthStateStorage implements IStateStorage { if (!txnReceipt) { throw new Error(`transaction: ${tx.hash} failed to mined`); } - const status: number | undefined | null = txnReceipt?.status; - const txnHash: string | undefined = txnReceipt?.hash; + const status: number | null = txnReceipt!.status; + const txnHash: string = txnReceipt!.hash; - if (!status || !txnHash) { + if (!status) { throw new Error(`transaction: ${txnHash} failed to mined`); } From f80c5b908af1f825699092fc87807a50bb93ddd3 Mon Sep 17 00:00:00 2001 From: vbasiuk Date: Thu, 9 Nov 2023 12:08:24 +0200 Subject: [PATCH 3/5] resolve comments --- src/storage/blockchain/state.ts | 36 ++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/storage/blockchain/state.ts b/src/storage/blockchain/state.ts index 691f213b..20ba2a49 100644 --- a/src/storage/blockchain/state.ts +++ b/src/storage/blockchain/state.ts @@ -17,7 +17,7 @@ export interface EthConnectionConfig { url: string; defaultGasLimit: number; minGasPrice?: string; - maxGasPrice?: string; // eip-1559 transaction do not support gasPrice + maxGasPrice?: string; // eip-1559 transaction do not support gasPrice maxFeePerGas?: string; maxPriorityFeePerGas?: string; confirmationBlockCount: number; @@ -105,26 +105,26 @@ export class EthStateStorage implements IStateStorage { ], proof.proof.pi_c.slice(0, 2) ]; - + const feeData = await this.provider.getFeeData(); - const maxFeePerGas = defaultEthConnectionConfig.maxFeePerGas ? - BigInt(defaultEthConnectionConfig.maxFeePerGas as string) : - feeData.maxFeePerGas; - const maxPriorityFeePerGas = defaultEthConnectionConfig.maxPriorityFeePerGas ? - BigInt(defaultEthConnectionConfig.maxPriorityFeePerGas as string) : - feeData.maxPriorityFeePerGas; + 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, // defaultGasLimit? - // gasPrice, // TypeError: eip-1559 transaction do not support gasPrice - maxFeePerGas, - maxPriorityFeePerGas + to: txData.to, + data: txData.data, + gasLimit, // defaultGasLimit? + // gasPrice, // TypeError: eip-1559 transaction do not support gasPrice + maxFeePerGas, + maxPriorityFeePerGas }; const tx = await signer.sendTransaction(request); @@ -132,14 +132,14 @@ export class EthStateStorage implements IStateStorage { if (!txnReceipt) { throw new Error(`transaction: ${tx.hash} failed to mined`); } - const status: number | null = txnReceipt!.status; - const txnHash: string = txnReceipt!.hash; + const status: number | null = txnReceipt.status; + const txnHash: string = txnReceipt.hash; if (!status) { throw new Error(`transaction: ${txnHash} failed to mined`); } - return txnHash as string; + return txnHash; } /** {@inheritdoc IStateStorage.getGISTProof} */ From 7867b50ab9634f25e1e9194ebbd6e83de0df892b Mon Sep 17 00:00:00 2001 From: vbasiuk Date: Thu, 9 Nov 2023 14:55:58 +0200 Subject: [PATCH 4/5] refactor submitZKPResponse with fee data params --- .../blockchain/onchain-zkp-verifier.ts | 22 +++++++++++++++++-- src/storage/blockchain/state.ts | 5 +---- 2 files changed, 21 insertions(+), 6 deletions(-) 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 20ba2a49..0200a348 100644 --- a/src/storage/blockchain/state.ts +++ b/src/storage/blockchain/state.ts @@ -36,8 +36,6 @@ const defaultEthConnectionConfig: EthConnectionConfig = { defaultGasLimit: 600000, minGasPrice: '0', maxGasPrice: '100000000000', - maxFeePerGas: '', //'2000000000', - maxPriorityFeePerGas: '', //'2000000000', confirmationBlockCount: 5, confirmationTimeout: 600000, contractAddress: '', @@ -121,8 +119,7 @@ export class EthStateStorage implements IStateStorage { const request: TransactionRequest = { to: txData.to, data: txData.data, - gasLimit, // defaultGasLimit? - // gasPrice, // TypeError: eip-1559 transaction do not support gasPrice + gasLimit, maxFeePerGas, maxPriorityFeePerGas }; From 8cb47b64c25c052ed809737300e2ccbfe7d9debb Mon Sep 17 00:00:00 2001 From: vbasiuk Date: Thu, 9 Nov 2023 15:01:58 +0200 Subject: [PATCH 5/5] format --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 36c16d99..7c21fd05 100644 --- a/package.json +++ b/package.json @@ -121,4 +121,4 @@ "engines": { "node": ">=18.16.0" } -} \ No newline at end of file +}