Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Commit

Permalink
updated w/latest changes from nightfall_3
Browse files Browse the repository at this point in the history
  • Loading branch information
imagobea committed May 25, 2022
1 parent ee196b9 commit 9c8df11
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 30 deletions.
8 changes: 6 additions & 2 deletions constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ export const DEFAULT_BLOCK_STAKE = 1;
export const WEBSOCKET_PING_TIME = 15000;

export const GAS_MULTIPLIER = Number(process.env.GAS_MULTIPLIER) || 2;
export const GAS = process.env.GAS || 8000000;
export const GAS_PRICE = process.env.GAS_PRICE || '20000000000';
export const GAS_PRICE_MULTIPLIER = Number(process.env.GAS_PRICE_MULTIPLIER) || 2;
export const GAS = process.env.GAS || 4000000;
export const GAS_PRICE = process.env.GAS_PRICE || '10000000000';
export const GAS_ESTIMATE_ENDPOINT =
process.env.GAS_ESTIMATE_ENDPOINT ||
'https://vqxy02tr5e.execute-api.us-east-2.amazonaws.com/production/estimateGas';
83 changes: 55 additions & 28 deletions nf3.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
GAS_MULTIPLIER,
GAS,
GAS_PRICE,
GAS_PRICE_MULTIPLIER,
GAS_ESTIMATE_ENDPOINT,
} from './constants.mjs';

// TODO when SDK is refactored such that these functions are split by user, proposer and challenger,
Expand Down Expand Up @@ -204,47 +206,72 @@ class Nf3 {
return axios.get(`${this.optimistBaseUrl}/block/make-now`);
}

async estimateGas(contractAddress, unsignedTransaction) {
let gasLimit;
try {
// eslint-disable-next-line no-await-in-loop
gasLimit = await this.web3.eth.estimateGas({
from: this.ethereumAddress,
to: contractAddress,
data: unsignedTransaction,
});
} catch (error) {
console.warn(`**********estimateGas >> estimateGas failed. Falling back to constant value`);
gasLimit = GAS; // backup if estimateGas failed
}
return Math.ceil(Number(gasLimit) * GAS_MULTIPLIER); // 50% seems a more than reasonable buffer.
}

async estimateGasPrice() {
let proposedGasPrice;
try {
// Call the endpoint to estimate the gas fee.
const res = (await axios.get(GAS_ESTIMATE_ENDPOINT)).data.result;
proposedGasPrice = Number(res?.ProposeGasPrice) * 10 ** 9;
} catch (error) {
console.warn('**********estimateGasPrice >> Gas Estimation Failed, using previous block gasPrice');
try {
proposedGasPrice = Number(await this.web3.eth.getGasPrice());
} catch (err) {
console.warn('**********estimateGasPrice >> Failed to get previous block gasprice. Falling back to default');
proposedGasPrice = GAS_PRICE;
}
}
return Math.ceil(proposedGasPrice * GAS_PRICE_MULTIPLIER);
}

/**
Method for signing and submitting an Ethereum transaction to the
blockchain.
@method
@async
@param {object} unsignedTransaction - An unsigned web3js transaction object.
@param {string} shieldContractAddress - The address of the Nightfall_3 shield address.
@param {number} fee - the value of the transaction.
This can be found using the getContractAddress convenience function.
@returns {Promise} This will resolve into a transaction receipt.
*/
Method for signing and submitting an Ethereum transaction to the
blockchain.
@method
@async
@param {object} unsignedTransaction - An unsigned web3js transaction object.
@param {string} shieldContractAddress - The address of the Nightfall_3 shield address.
@param {number} fee - the value of the transaction.
This can be found using the getContractAddress convenience function.
@returns {Promise} This will resolve into a transaction receipt.
*/
async submitTransaction(
unsignedTransaction,
contractAddress = this.shieldContractAddress,
fee = this.defaultFee,
) {
// if (!this.nonce)
// this.nonce = await this.web3.eth.getTransactionCount(this.ethereumAddress, 'pending');

// let gasPrice = 200000000000;
/*
const gas = (await this.web3.eth.getBlock('latest')).gasLimit;
const blockGasPrice = GAS_MULTIPLIER * Number(await this.web3.eth.getGasPrice());
if (blockGasPrice > gasPrice) gasPrice = blockGasPrice;
*/
let gasPrice = GAS_PRICE;
const blockGasPrice = GAS_MULTIPLIER * Number(await this.web3.eth.getGasPrice());
if (blockGasPrice > gasPrice) gasPrice = blockGasPrice;
const gas = GAS;

// const gasPrice = GAS_PRICE;
// const gas = GAS;

// estimate the gasPrice
const gasPrice = await this.estimateGasPrice();
// Estimate the gasLimit
const gas = await this.estimateGas(contractAddress, unsignedTransaction);
console.debug(
`**********submitTransaction >> Transaction gasPrice was set at ${Math.ceil(
gasPrice / 10 ** 9,
)} GWei, gas limit was set at ${gas}`,
);
const tx = {
from: this.ethereumAddress,
to: contractAddress,
data: unsignedTransaction,
value: fee,
gas,
gasPrice,
// nonce: this.nonce,
};

// logger.debug(`The nonce for the unsigned transaction ${tx.data} is ${this.nonce}`);
Expand Down

0 comments on commit 9c8df11

Please sign in to comment.