Remove _applyDefaultsToDeployTxDataAsync

This commit is contained in:
Leonid Logvinov 2018-05-10 15:02:32 +02:00
parent ebc296ea31
commit 7eb9444458
No known key found for this signature in database
GPG Key ID: 0DD294BFDE8C95D4
4 changed files with 13 additions and 25 deletions

View File

@ -60,37 +60,22 @@ export class BaseContract {
protected static _bnToBigNumber(type: string, value: any): any { protected static _bnToBigNumber(type: string, value: any): any {
return _.isObject(value) && value._bn ? new BigNumber(value.toString()) : value; return _.isObject(value) && value._bn ? new BigNumber(value.toString()) : value;
} }
protected static async _applyDefaultsToDeployTxDataAsync<T extends Partial<TxData | TxDataPayable>>( protected static async _applyDefaultsToTxDataAsync<T extends Partial<TxData | TxDataPayable>>(
txData: T, txData: T,
defaults: Partial<TxData>, defaults: Partial<TxData>,
estimateGasAsync?: (txData: T) => Promise<number>, estimateGasAsync?: (txData: T) => Promise<number>,
): Promise<TxData> {
const txDataWithDefaults: TxData = {
...defaults,
...(txData as any),
};
if (_.isUndefined(txDataWithDefaults.gas) && !_.isUndefined(estimateGasAsync)) {
const estimatedGas = await estimateGasAsync(txData);
txDataWithDefaults.gas = estimatedGas;
}
return txDataWithDefaults;
}
protected async _applyDefaultsToTxDataAsync<T extends Partial<TxData | TxDataPayable>>(
txData: T,
estimateGasAsync?: (txData: T) => Promise<number>,
): Promise<TxData> { ): Promise<TxData> {
// Gas amount sourced with the following priorities: // Gas amount sourced with the following priorities:
// 1. Optional param passed in to public method call // 1. Optional param passed in to public method call
// 2. Global config passed in at library instantiation // 2. Global config passed in at library instantiation
// 3. Gas estimate calculation + safety margin // 3. Gas estimate calculation + safety margin
const removeUndefinedProperties = _.pickBy; const removeUndefinedProperties = _.pickBy;
const txDataWithDefaults = ({ const txDataWithDefaults: TxData = {
to: this.address, ...removeUndefinedProperties(defaults),
...removeUndefinedProperties(this._web3Wrapper.getContractDefaults()),
...removeUndefinedProperties(txData as any), ...removeUndefinedProperties(txData as any),
// HACK: TS can't prove that T is spreadable. // HACK: TS can't prove that T is spreadable.
// Awaiting https://github.com/Microsoft/TypeScript/pull/13288 to be merged // Awaiting https://github.com/Microsoft/TypeScript/pull/13288 to be merged
} as any) as TxData; } as any;
if (_.isUndefined(txDataWithDefaults.gas) && !_.isUndefined(estimateGasAsync)) { if (_.isUndefined(txDataWithDefaults.gas) && !_.isUndefined(estimateGasAsync)) {
const estimatedGas = await estimateGasAsync(txData); const estimatedGas = await estimateGasAsync(txData);
txDataWithDefaults.gas = estimatedGas; txDataWithDefaults.gas = estimatedGas;

View File

@ -65,7 +65,7 @@ export class {{contractName}}Contract extends BaseContract {
); );
const txData = ethers.Contract.getDeployTransaction(bytecode, abi, {{> params inputs=ctor.inputs}}); const txData = ethers.Contract.getDeployTransaction(bytecode, abi, {{> params inputs=ctor.inputs}});
const web3Wrapper = new Web3Wrapper(provider); const web3Wrapper = new Web3Wrapper(provider);
const txDataWithDefaults = await BaseContract._applyDefaultsToDeployTxDataAsync( const txDataWithDefaults = await BaseContract._applyDefaultsToTxDataAsync(
txData, txData,
defaults, defaults,
web3Wrapper.estimateGasAsync.bind(web3Wrapper), web3Wrapper.estimateGasAsync.bind(web3Wrapper),

View File

@ -12,10 +12,11 @@ async callAsync(
{{> params inputs=inputs}} {{> params inputs=inputs}}
) as ethers.CallDescription; ) as ethers.CallDescription;
const encodedData = ethersFunction.data; const encodedData = ethersFunction.data;
const callDataWithDefaults = await self._applyDefaultsToTxDataAsync( const callDataWithDefaults = await BaseContract._applyDefaultsToTxDataAsync(
{ {
data: encodedData, data: encodedData,
} },
this._web3Wrapper.getContractDefaults(),
) )
const rawCallResult = await self._web3Wrapper.callAsync(callDataWithDefaults, defaultBlock); const rawCallResult = await self._web3Wrapper.callAsync(callDataWithDefaults, defaultBlock);
let resultArray = ethersFunction.parse(rawCallResult); let resultArray = ethersFunction.parse(rawCallResult);

View File

@ -14,11 +14,12 @@ public {{this.tsName}} = {
const encodedData = self._lookupEthersInterface('{{this.functionSignature}}').functions.{{this.name}}( const encodedData = self._lookupEthersInterface('{{this.functionSignature}}').functions.{{this.name}}(
{{> params inputs=inputs}} {{> params inputs=inputs}}
).data; ).data;
const txDataWithDefaults = await self._applyDefaultsToTxDataAsync( const txDataWithDefaults = await BaseContract._applyDefaultsToTxDataAsync(
{ {
...txData, ...txData,
data: encodedData, data: encodedData,
}, },
this._web3Wrapper.getContractDefaults(),
self.{{this.tsName}}.estimateGasAsync.bind( self.{{this.tsName}}.estimateGasAsync.bind(
self, self,
{{> params inputs=inputs}} {{> params inputs=inputs}}
@ -37,11 +38,12 @@ public {{this.tsName}} = {
const encodedData = self._lookupEthersInterface('{{this.functionSignature}}').functions.{{this.name}}( const encodedData = self._lookupEthersInterface('{{this.functionSignature}}').functions.{{this.name}}(
{{> params inputs=inputs}} {{> params inputs=inputs}}
).data; ).data;
const txDataWithDefaults = await self._applyDefaultsToTxDataAsync( const txDataWithDefaults = await BaseContract._applyDefaultsToTxDataAsync(
{ {
...txData, ...txData,
data: encodedData, data: encodedData,
} },
this._web3Wrapper.getContractDefaults(),
); );
const gas = await self._web3Wrapper.estimateGasAsync(txDataWithDefaults); const gas = await self._web3Wrapper.estimateGasAsync(txDataWithDefaults);
return gas; return gas;