From 58ccdac9dbe7fef8d957659d07c6e6b22cd62b26 Mon Sep 17 00:00:00 2001 From: zeroxbt Date: Fri, 29 Apr 2022 01:33:10 +0200 Subject: [PATCH 1/2] add gas station, generalize contract method call --- external/web3-blockchain-service.js | 88 ++++++++++++----------------- 1 file changed, 37 insertions(+), 51 deletions(-) diff --git a/external/web3-blockchain-service.js b/external/web3-blockchain-service.js index 3c7cc347ba..cee98eb031 100644 --- a/external/web3-blockchain-service.js +++ b/external/web3-blockchain-service.js @@ -26,33 +26,31 @@ class Web3BlockchainService { } async getGasStationPrice() { - const response = await axios.get(this.gasStationLink) - .catch((err) => { - this.logger.warn(err); - return undefined; - }); + const response = await axios.get(this.gasStationLink).catch((err) => { + this.logger.warn(err); + return undefined; + }); if (response) { - return response.data.standard * 1000000000; + return response.data.standard * 1e9; } return undefined; } - async createAssertionRecord(stateCommitHash, rootHash, issuer) { - const contractAddress = await this.getAssertionRegistryAddress(); - const contractInstance = new this.web3.eth.Contract(DKGContract, contractAddress); + async executeContractMethod(contractInstance, method, args) { + let gasPrice = await this.getGasStationPrice(); + gasPrice = Math.round(gasPrice); - let calculatedGas = await this.getGasStationPrice(); - calculatedGas = Math.round(calculatedGas); + const gasLimit = await contractInstance.methods[method](...args).estimateGas({ + from: this.config.publicKey, + }); - const encodedABI = contractInstance.methods.createAssertionRecord(`0x${stateCommitHash}`, `0x${rootHash}`, issuer, - new BigNumber(1), - new BigNumber(1)).encodeABI(); + const encodedABI = contractInstance.methods[method](...args).encodeABI(); const tx = { from: this.config.publicKey, to: contractInstance.options.address, data: encodedABI, - gasPrice: '20000000000', - gas: '500000', + gasPrice, + gas: gasLimit, }; const createdTransaction = await this.web3.eth.accounts.signTransaction( @@ -60,6 +58,20 @@ class Web3BlockchainService { this.config.privateKey, ); const result = await this.web3.eth.sendSignedTransaction(createdTransaction.rawTransaction); + return result + } + + async createAssertionRecord(stateCommitHash, rootHash, issuer) { + const contractAddress = await this.getAssertionRegistryAddress(); + const contractInstance = new this.web3.eth.Contract(DKGContract, contractAddress); + + const result = await this.executeContractMethod(contractInstance, 'createAssertionRecord', [ + `0x${stateCommitHash}`, + `0x${rootHash}`, + issuer, + new BigNumber(1), + new BigNumber(1), + ]); return { transactionHash: result.transactionHash, blockchain: this.config.networkId }; } @@ -67,23 +79,9 @@ class Web3BlockchainService { const contractAddress = this.config.hubContractAddress; const contractInstance = new this.web3.eth.Contract(UAIRegistry, contractAddress); - let calculatedGas = await this.getGasStationPrice(); - calculatedGas = Math.round(calculatedGas); - - const encodedABI = contractInstance.methods.registerAsset(`0x${uai}`, 0, `0x${uai}`, `0x${stateCommitHash}`, `0x${rootHash}`, 1).encodeABI(); - const tx = { - from: this.config.publicKey, - to: contractInstance.options.address, - data: encodedABI, - gasPrice: '20000000000', - gas: '900000', - }; - - const createdTransaction = await this.web3.eth.accounts.signTransaction( - tx, - this.config.privateKey, - ); - const result = await this.web3.eth.sendSignedTransaction(createdTransaction.rawTransaction); + const result = await this.executeContractMethod(contractInstance, 'registerAsset', [ + `0x${uai}`, 0, `0x${uai}`, `0x${stateCommitHash}`, `0x${rootHash}`, 1 + ]); return { transactionHash: result.transactionHash, blockchain: this.config.networkId }; } @@ -91,23 +89,9 @@ class Web3BlockchainService { const contractAddress = this.config.hubContractAddress; const contractInstance = new this.web3.eth.Contract(UAIRegistry, contractAddress); - let calculatedGas = await this.getGasStationPrice(); - calculatedGas = Math.round(calculatedGas); - - const encodedABI = contractInstance.methods.updateAssetState(`0x${UAI}`, `0x${newStateCommitHash}`, `0x${rootHash}`).encodeABI(); - const tx = { - from: this.config.publicKey, - to: contractInstance.options.address, - data: encodedABI, - gasPrice: '20000000000', - gas: '500000', - }; - - const createdTransaction = await this.web3.eth.accounts.signTransaction( - tx, - this.config.privateKey, - ); - const result = await this.web3.eth.sendSignedTransaction(createdTransaction.rawTransaction); + const result = await this.executeContractMethod(contractInstance, 'updateAssetState', [ + `0x${UAI}`, `0x${newStateCommitHash}`, `0x${rootHash}` + ]); return { transactionHash: result.transactionHash, blockchain: this.config.networkId }; } @@ -116,7 +100,9 @@ class Web3BlockchainService { const contractInstance = new this.web3.eth.Contract(DKGContract, contractAddress); const issuer = await contractInstance.methods.getAssertionIssuer(`0x${assertionId}`).call(); - const rootHash = await contractInstance.methods.getAssertionRootHash(`0x${assertionId}`).call(); + const rootHash = await contractInstance.methods + .getAssertionRootHash(`0x${assertionId}`) + .call(); return { issuer, rootHash }; } From 138d595f9c32603e7fbf5c011ab39feece0377a2 Mon Sep 17 00:00:00 2001 From: zeroxbt Date: Wed, 4 May 2022 16:35:15 +0200 Subject: [PATCH 2/2] update gas station link and add default gas values --- external/web3-blockchain-service.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/external/web3-blockchain-service.js b/external/web3-blockchain-service.js index cee98eb031..67ba053553 100644 --- a/external/web3-blockchain-service.js +++ b/external/web3-blockchain-service.js @@ -8,7 +8,7 @@ const constants = require('../modules/constants'); class Web3BlockchainService { constructor(config) { this.config = config; - this.gasStationLink = 'https://gasstation-mumbai.matic.today/'; + this.gasStationLink = 'https://gasstation-mumbai.matic.today/v2'; } initialize(logger) { @@ -30,15 +30,15 @@ class Web3BlockchainService { this.logger.warn(err); return undefined; }); - if (response) { - return response.data.standard * 1e9; + try { + return Math.round(response.data.standard.maxFee * 1e9); + } catch(e) { + return undefined; } - return undefined; } async executeContractMethod(contractInstance, method, args) { - let gasPrice = await this.getGasStationPrice(); - gasPrice = Math.round(gasPrice); + const gasPrice = await this.getGasStationPrice(); const gasLimit = await contractInstance.methods[method](...args).estimateGas({ from: this.config.publicKey, @@ -49,8 +49,8 @@ class Web3BlockchainService { from: this.config.publicKey, to: contractInstance.options.address, data: encodedABI, - gasPrice, - gas: gasLimit, + gasPrice: gasPrice || this.web3.utils.toWei('20', 'Gwei'), + gas: gasLimit || this.web3.utils.toWei('900', 'Kwei'), }; const createdTransaction = await this.web3.eth.accounts.signTransaction( @@ -58,7 +58,7 @@ class Web3BlockchainService { this.config.privateKey, ); const result = await this.web3.eth.sendSignedTransaction(createdTransaction.rawTransaction); - return result + return result; } async createAssertionRecord(stateCommitHash, rootHash, issuer) { @@ -80,7 +80,12 @@ class Web3BlockchainService { const contractInstance = new this.web3.eth.Contract(UAIRegistry, contractAddress); const result = await this.executeContractMethod(contractInstance, 'registerAsset', [ - `0x${uai}`, 0, `0x${uai}`, `0x${stateCommitHash}`, `0x${rootHash}`, 1 + `0x${uai}`, + 0, + `0x${uai}`, + `0x${stateCommitHash}`, + `0x${rootHash}`, + 1, ]); return { transactionHash: result.transactionHash, blockchain: this.config.networkId }; } @@ -90,7 +95,9 @@ class Web3BlockchainService { const contractInstance = new this.web3.eth.Contract(UAIRegistry, contractAddress); const result = await this.executeContractMethod(contractInstance, 'updateAssetState', [ - `0x${UAI}`, `0x${newStateCommitHash}`, `0x${rootHash}` + `0x${UAI}`, + `0x${newStateCommitHash}`, + `0x${rootHash}`, ]); return { transactionHash: result.transactionHash, blockchain: this.config.networkId }; }