From 69eb5cbfd0372e56fb0bc1ab0915863553ccf3d7 Mon Sep 17 00:00:00 2001 From: Antonio Juliano Date: Wed, 10 Apr 2019 16:40:45 -0700 Subject: [PATCH] Allow simulation of transactions (#232) * Allow simulation of transactions * 0.4.8 * always simulate * fix --- package-lock.json | 2 +- package.json | 2 +- src/lib/Contracts.ts | 12 ++++++++---- src/modules/operate/AccountOperation.ts | 7 +++++-- src/types.ts | 5 ++++- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index f7056a5c..19fc0656 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@dydxprotocol/solo", - "version": "0.4.7", + "version": "0.4.8", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index cf016890..39dc2fe4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dydxprotocol/solo", - "version": "0.4.7", + "version": "0.4.8", "description": "Ethereum Smart Contracts and TypeScript library used for the dYdX Solo-Margin Trading Protocol", "main": "dist/js/src/index.js", "types": "dist/types/src/types.d.ts", diff --git a/src/lib/Contracts.ts b/src/lib/Contracts.ts index fb5b0029..91bca96e 100644 --- a/src/lib/Contracts.ts +++ b/src/lib/Contracts.ts @@ -400,12 +400,12 @@ export class Contracts { txOptions.gasPrice = this.defaultGasPrice; } - if (!options.gas) { - if (this.defaultGas) { + if (confirmationType === ConfirmationType.Simulate || !options.gas) { + let gasEstimate: number; + + if (this.defaultGas && confirmationType !== ConfirmationType.Simulate) { txOptions.gas = this.defaultGas; } else { - let gasEstimate: number; - try { gasEstimate = await method.estimateGas(txOptions); } catch (error) { @@ -420,6 +420,10 @@ export class Contracts { const totalGas: number = Math.floor(gasEstimate * multiplier); txOptions.gas = totalGas < this.blockGasLimit ? totalGas : this.blockGasLimit; } + + if (confirmationType === ConfirmationType.Simulate) { + return { gasEstimate, gas: Number(txOptions.gas) }; + } } if (txOptions.value) { diff --git a/src/modules/operate/AccountOperation.ts b/src/modules/operate/AccountOperation.ts index 1873b95e..bba51f03 100644 --- a/src/modules/operate/AccountOperation.ts +++ b/src/modules/operate/AccountOperation.ts @@ -22,6 +22,7 @@ import { Amount, Integer, AccountOperationOptions, + ConfirmationType, address, } from '../../types'; import { toBytes } from '../../lib/BytesHelper'; @@ -209,7 +210,9 @@ export class AccountOperation { throw new Error('No actions have been added to operation'); } - this.committed = true; + if (options && options.confirmationType !== ConfirmationType.Simulate) { + this.committed = true; + } try { let method: TransactionObject; @@ -223,7 +226,7 @@ export class AccountOperation { method = this.contracts.payableProxy.methods.operate( this.accounts, this.actions, - this.sendEthTo || options.from || this.contracts.payableProxy.options.from, + this.sendEthTo || (options && options.from) || this.contracts.payableProxy.options.from, ); } diff --git a/src/types.ts b/src/types.ts index d2eab4ac..738c0069 100644 --- a/src/types.ts +++ b/src/types.ts @@ -29,6 +29,7 @@ export enum ConfirmationType { Hash = 0, Confirmed = 1, Both = 2, + Simulate = 3, } export interface SoloOptions { @@ -64,7 +65,7 @@ export interface LogParsingOptions { } export interface TxResult { - transactionHash: string; + transactionHash?: string; transactionIndex?: number; blockHash?: string; blockNumber?: number; @@ -79,6 +80,8 @@ export interface TxResult { }; status?: boolean; confirmation?: Promise; + gasEstimate?: number; + gas?: number; } export enum AmountDenomination {