Lazily instantiate exchangeContract and store as instance variable instead of re-instantiating on every call
This commit is contained in:
parent
c536114b16
commit
87491e6741
@ -1,4 +1,5 @@
|
|||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
|
import * as Web3 from 'web3';
|
||||||
import {Web3Wrapper} from '../web3_wrapper';
|
import {Web3Wrapper} from '../web3_wrapper';
|
||||||
import {ECSignature, ZeroExError, ExchangeContract} from '../types';
|
import {ECSignature, ZeroExError, ExchangeContract} from '../types';
|
||||||
import {assert} from '../utils/assert';
|
import {assert} from '../utils/assert';
|
||||||
@ -7,9 +8,13 @@ import * as ExchangeArtifacts from '../artifacts/Exchange.json';
|
|||||||
import {ecSignatureSchema} from '../schemas/ec_signature_schema';
|
import {ecSignatureSchema} from '../schemas/ec_signature_schema';
|
||||||
|
|
||||||
export class ExchangeWrapper extends ContractWrapper {
|
export class ExchangeWrapper extends ContractWrapper {
|
||||||
|
private exchangeContractIfExists: ExchangeContract;
|
||||||
constructor(web3Wrapper: Web3Wrapper) {
|
constructor(web3Wrapper: Web3Wrapper) {
|
||||||
super(web3Wrapper);
|
super(web3Wrapper);
|
||||||
}
|
}
|
||||||
|
public invalidateExchangeContract() {
|
||||||
|
delete this.exchangeContractIfExists;
|
||||||
|
}
|
||||||
public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature,
|
public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature,
|
||||||
signerAddressHex: string): Promise<boolean> {
|
signerAddressHex: string): Promise<boolean> {
|
||||||
assert.isHexString('dataHex', dataHex);
|
assert.isHexString('dataHex', dataHex);
|
||||||
@ -19,10 +24,9 @@ export class ExchangeWrapper extends ContractWrapper {
|
|||||||
const senderAddressIfExists = await this.web3Wrapper.getSenderAddressIfExistsAsync();
|
const senderAddressIfExists = await this.web3Wrapper.getSenderAddressIfExistsAsync();
|
||||||
assert.assert(!_.isUndefined(senderAddressIfExists), ZeroExError.USER_HAS_NO_ASSOCIATED_ADDRESSES);
|
assert.assert(!_.isUndefined(senderAddressIfExists), ZeroExError.USER_HAS_NO_ASSOCIATED_ADDRESSES);
|
||||||
|
|
||||||
const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any));
|
await this.instantiateExchangeContractIfDoesntExistAsync();
|
||||||
const exchangeInstance = contractInstance as ExchangeContract;
|
|
||||||
|
|
||||||
const isValidSignature = await exchangeInstance.isValidSignature.call(
|
const isValidSignature = await this.exchangeContractIfExists.isValidSignature.call(
|
||||||
signerAddressHex,
|
signerAddressHex,
|
||||||
dataHex,
|
dataHex,
|
||||||
ecSignature.v,
|
ecSignature.v,
|
||||||
@ -34,4 +38,11 @@ export class ExchangeWrapper extends ContractWrapper {
|
|||||||
);
|
);
|
||||||
return isValidSignature;
|
return isValidSignature;
|
||||||
}
|
}
|
||||||
|
private async instantiateExchangeContractIfDoesntExistAsync() {
|
||||||
|
if (!_.isUndefined(this.exchangeContractIfExists)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any));
|
||||||
|
this.exchangeContractIfExists = contractInstance as ExchangeContract;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
|
import * as Web3 from 'web3';
|
||||||
|
|
||||||
// Utility function to create a K:V from a list of strings
|
// Utility function to create a K:V from a list of strings
|
||||||
// Adapted from: https://basarat.gitbooks.io/typescript/content/docs/types/literal-types.html
|
// Adapted from: https://basarat.gitbooks.io/typescript/content/docs/types/literal-types.html
|
||||||
@ -28,6 +29,8 @@ export interface ECSignature {
|
|||||||
|
|
||||||
export interface ExchangeContract {
|
export interface ExchangeContract {
|
||||||
isValidSignature: any;
|
isValidSignature: any;
|
||||||
|
currentProvider: Web3.Provider;
|
||||||
|
setProvider: (provider: Web3.Provider) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SolidityTypes = strEnum([
|
export const SolidityTypes = strEnum([
|
||||||
|
Loading…
x
Reference in New Issue
Block a user