Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
Allow simulation of transactions (#232)
Browse files Browse the repository at this point in the history
* Allow simulation of transactions

* 0.4.8

* always simulate

* fix
  • Loading branch information
AntonioJuliano committed Apr 10, 2019
1 parent 5e6bfeb commit 69eb5cb
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 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",
Expand Down
12 changes: 8 additions & 4 deletions src/lib/Contracts.ts
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions src/modules/operate/AccountOperation.ts
Expand Up @@ -22,6 +22,7 @@ import {
Amount,
Integer,
AccountOperationOptions,
ConfirmationType,
address,
} from '../../types';
import { toBytes } from '../../lib/BytesHelper';
Expand Down Expand Up @@ -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<void>;
Expand All @@ -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,
);
}

Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Expand Up @@ -29,6 +29,7 @@ export enum ConfirmationType {
Hash = 0,
Confirmed = 1,
Both = 2,
Simulate = 3,
}

export interface SoloOptions {
Expand Down Expand Up @@ -64,7 +65,7 @@ export interface LogParsingOptions {
}

export interface TxResult {
transactionHash: string;
transactionHash?: string;
transactionIndex?: number;
blockHash?: string;
blockNumber?: number;
Expand All @@ -79,6 +80,8 @@ export interface TxResult {
};
status?: boolean;
confirmation?: Promise<TransactionReceipt>;
gasEstimate?: number;
gas?: number;
}

export enum AmountDenomination {
Expand Down

0 comments on commit 69eb5cb

Please sign in to comment.