Skip to content

Commit

Permalink
Merge pull request #1940 from OriginTrail/v6/feature/gas-station
Browse files Browse the repository at this point in the history
add gas station and generalize smart contract method calls
  • Loading branch information
NZT48 authored May 5, 2022
2 parents e8d9297 + 6ce9477 commit bda4b88
Showing 1 changed file with 47 additions and 54 deletions.
101 changes: 47 additions & 54 deletions external/web3-blockchain-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -26,88 +26,79 @@ class Web3BlockchainService {
}

async getGasStationPrice() {
const response = await axios.get(this.gasStationLink)
.catch((err) => {
this.logger.warn(err);
return undefined;
});
if (response) {
return response.data.standard * 1000000000;
const response = await axios.get(this.gasStationLink).catch((err) => {
this.logger.warn(err);
return undefined;
});
try {
return Math.round(response.data.standard.maxFee * 1e9);
} catch(e) {
return undefined;
}
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) {
const gasPrice = await this.getGasStationPrice();

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: gasPrice || this.web3.utils.toWei('20', 'Gwei'),
gas: gasLimit || this.web3.utils.toWei('900', 'Kwei'),
};

const createdTransaction = await this.web3.eth.accounts.signTransaction(
tx,
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 };
}

async registerAsset(uai, type, alsoKnownAs, stateCommitHash, rootHash, tokenAmount) {
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 };
}

async updateAsset(UAI, newStateCommitHash, rootHash) {
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 };
}

Expand All @@ -116,7 +107,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 };
}

Expand Down

0 comments on commit bda4b88

Please sign in to comment.