Skip to content

Instantly share code, notes, and snippets.

@kermankohli
Created October 28, 2018 06:57
Show Gist options
  • Save kermankohli/824f385a615b08943e4a8983b7346cae to your computer and use it in GitHub Desktop.
Save kermankohli/824f385a615b08943e4a8983b7346cae to your computer and use it in GitHub Desktop.
Base Contract Src
import * as _ from "lodash";
import * as Web3 from "web3";
import { BigNumber } from "bignumber.js";
import { TxData, TxDataPayable } from '@your-project/types';
export const CONTRACT_WRAPPER_ERRORS = {
CONTRACT_NOT_FOUND_ON_NETWORK: (contractName: string, networkId: number) =>
`Unable to find address for contract ${contractName} on network with id ${networkId}`,
};
export class BaseContract {
public address: string;
public abi: any[];
public web3ContractInstance: Web3.ContractInstance;
protected defaults: Partial<TxData>;
constructor(web3ContractInstance: Web3.ContractInstance, defaults: Partial<TxData>) {
this.web3ContractInstance = web3ContractInstance;
this.address = web3ContractInstance.address;
this.abi = web3ContractInstance.abi;
this.defaults = defaults;
}
protected async applyDefaultsToTxDataAsync<T extends TxData | TxDataPayable>(
txData: T,
estimateGasAsync?: (txData: T) => Promise<number>,
): Promise<TxData> {
// Gas amount sourced with the following priorities:
// 1. Optional param passed in to public method call
// 2. Global config passed in at library instantiation
// 3. Gas estimate calculation + safety margin
const removeUndefinedProperties = _.pickBy;
const txDataWithDefaults = {
...removeUndefinedProperties(this.defaults),
...removeUndefinedProperties(txData as any),
// HACK: TS can't prove that T is spreadable.
// Awaiting https://github.com/Microsoft/TypeScript/pull/13288 to be merged
};
if (_.isUndefined(txDataWithDefaults.gas) && !_.isUndefined(estimateGasAsync)) {
const estimatedGas = await estimateGasAsync(txData);
txDataWithDefaults.gas = new BigNumber(estimatedGas);
}
return txDataWithDefaults;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment