Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 36f6591

Browse files
allow sendTransaction and smart contract calls to process options parameter
1 parent f8188f2 commit 36f6591

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

packages/contract/lib/execute.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const handlers = require("./handlers");
88
const override = require("./override");
99
const reformat = require("./reformat");
1010
const { sendTransactionManual } = require("./manual-send");
11+
const { DEFAULT_RETURN_FORMAT } = require("web3-types");
1112

1213
const execute = {
1314
// ----------------------------------- Helpers --------------------------------------------------
@@ -83,6 +84,7 @@ const execute = {
8384
prepareCall: async function (constructor, methodABI, _arguments, isCall) {
8485
let args = Array.prototype.slice.call(_arguments);
8586
let params = utils.getTxParams.call(constructor, methodABI, args, isCall);
87+
let options = utils.getTxOptions.call(constructor, methodABI, args);
8688

8789
args = utils.convertToEthersBN(args);
8890

@@ -104,7 +106,7 @@ const execute = {
104106
return { args, params };
105107
}
106108
const network = await constructor.detectNetwork();
107-
return { args, params, network };
109+
return { args, params, network, options };
108110
},
109111

110112
/**
@@ -202,7 +204,7 @@ const execute = {
202204
const promiEvent = new PromiEvent(false, constructor.debugger);
203205
execute
204206
.prepareCall(constructor, methodABI, arguments)
205-
.then(async ({ args, params, network }) => {
207+
.then(async ({ args, params, network, options }) => {
206208
const context = {
207209
contract: constructor, // Can't name this field `constructor` or `_constructor`
208210
promiEvent: promiEvent,
@@ -221,15 +223,17 @@ const execute = {
221223
contract: constructor
222224
});
223225

224-
params.gas = await execute.getGasEstimate.call(
225-
constructor,
226-
params,
227-
network.blockLimit,
228-
promiEvent.debug //apply stacktracing mode if promiEvent.debug is true
229-
);
230-
226+
// estimate the gas only if it's not provided
227+
if (!params || !params[0]?.gas) {
228+
params.gas = await execute.getGasEstimate.call(
229+
constructor,
230+
params,
231+
network.blockLimit,
232+
promiEvent.debug //apply stacktracing mode if promiEvent.debug is true
233+
);
234+
}
231235
execute
232-
.sendTransaction(web3, params, promiEvent, context) //the crazy things we do for stacktracing...
236+
.sendTransaction(web3, params, promiEvent, context, options) //the crazy things we do for stacktracing...
233237
.then(receipt => {
234238
if (promiEvent.debug) {
235239
// in this case, we need to manually invoke the handler since it
@@ -561,11 +565,15 @@ const execute = {
561565
//input works the same as input to web3.sendTransaction
562566
//(well, OK, it's lacking some things there too, but again, good enough
563567
//for our purposes)
564-
sendTransaction: async function (web3, params, promiEvent, context) {
568+
sendTransaction: async function (web3, params, promiEvent, context, options) {
565569
//if we don't need the debugger, let's not risk any errors on our part,
566570
//and just have web3 do everything
567571
if (!promiEvent || !promiEvent.debug) {
568-
const deferred = web3.eth.sendTransaction(params);
572+
const deferred = web3.eth.sendTransaction(
573+
params,
574+
DEFAULT_RETURN_FORMAT,
575+
options
576+
);
569577
handlers.setup(deferred, context);
570578
return deferred;
571579
}

packages/contract/lib/utils/index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ const allowedTxParams = new Set([
2222
"overwrite"
2323
]);
2424

25+
const allowedTxOptions = new Set([
26+
"checkRevertBeforeSending",
27+
"ignoreGasPricing",
28+
"transactionResolver",
29+
"contractAbi",
30+
"checkRevertBeforeSending",
31+
"ignoreFillingGasLimit"
32+
]);
33+
2534
const Utils = {
2635
is_object(val) {
2736
return typeof val === "object" && !Array.isArray(val);
@@ -39,6 +48,12 @@ const Utils = {
3948
return Object.keys(val).some(fieldName => allowedTxParams.has(fieldName));
4049
},
4150

51+
isTxOptions(val) {
52+
if (!Utils.is_object(val)) return false;
53+
if (Utils.is_big_number(val)) return false;
54+
return Object.keys(val).some(fieldName => allowedTxOptions.has(fieldName));
55+
},
56+
4257
decodeLogs(_logs, isSingle) {
4358
const constructor = this;
4459
const logs = Utils.toTruffleLog(_logs, isSingle);
@@ -128,6 +143,20 @@ const Utils = {
128143
return bytecode;
129144
},
130145

146+
// Extracts optional tx options from a list of fn arguments
147+
getTxOptions(methodABI, args) {
148+
const expectedArgCount = methodABI ? methodABI.inputs.length : 0;
149+
150+
let txOptions = {};
151+
const lastArg = args[args.length - 1];
152+
153+
if (args.length === expectedArgCount + 1 && Utils.isTxOptions(lastArg)) {
154+
txOptions = args.pop();
155+
}
156+
157+
return txOptions;
158+
},
159+
131160
// Extracts optional tx params from a list of fn arguments
132161
getTxParams(methodABI, args, ignoreDefaultGasPriceParams = false) {
133162
const constructor = this;
@@ -136,9 +165,17 @@ const Utils = {
136165

137166
let txParams = {};
138167
const lastArg = args[args.length - 1];
168+
const beforeLastArg = args.length >= 2 ? args[args.length - 2] : {};
139169

140170
if (args.length === expectedArgCount + 1 && Utils.isTxParams(lastArg)) {
141171
txParams = args.pop();
172+
} else if (
173+
args.length === expectedArgCount + 2 &&
174+
Utils.isTxParams(beforeLastArg)
175+
) {
176+
// remove the item at index - 2 (beforeLastArg) from args
177+
args.splice(args.length - 2, 1);
178+
txParams = beforeLastArg;
142179
}
143180

144181
let defaultParams = constructor.class_defaults;

0 commit comments

Comments
 (0)