aboutsummaryrefslogtreecommitdiffstats
path: root/src/contract_wrappers
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-05-30 20:55:43 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2017-05-30 20:55:43 +0800
commit3e65ac018c145d9fb49ece6cb113ae577f84323b (patch)
tree49f3b63f898b78c2b210b56b01e96f62887161b0 /src/contract_wrappers
parentdfcf49464b1a93b6e5df39289e9d14b2e60e62d2 (diff)
parent6b321ca1c70b9dcf188b2a112015386cba0ad5f2 (diff)
downloaddexon-sol-tools-3e65ac018c145d9fb49ece6cb113ae577f84323b.tar
dexon-sol-tools-3e65ac018c145d9fb49ece6cb113ae577f84323b.tar.gz
dexon-sol-tools-3e65ac018c145d9fb49ece6cb113ae577f84323b.tar.bz2
dexon-sol-tools-3e65ac018c145d9fb49ece6cb113ae577f84323b.tar.lz
dexon-sol-tools-3e65ac018c145d9fb49ece6cb113ae577f84323b.tar.xz
dexon-sol-tools-3e65ac018c145d9fb49ece6cb113ae577f84323b.tar.zst
dexon-sol-tools-3e65ac018c145d9fb49ece6cb113ae577f84323b.zip
Merge branch 'master' into fillOrderAsync
Diffstat (limited to 'src/contract_wrappers')
-rw-r--r--src/contract_wrappers/exchange_wrapper.ts14
-rw-r--r--src/contract_wrappers/token_registry_wrapper.ts44
2 files changed, 57 insertions, 1 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts
index 7a80c5800..66e53a7d4 100644
--- a/src/contract_wrappers/exchange_wrapper.ts
+++ b/src/contract_wrappers/exchange_wrapper.ts
@@ -26,9 +26,13 @@ export class ExchangeWrapper extends ContractWrapper {
[ExchangeContractErrs.ERROR_FILL_TRUNCATION]: 'The rounding error was too large when filling this order',
[ExchangeContractErrs.ERROR_FILL_BALANCE_ALLOWANCE]: 'Maker or taker has insufficient balance or allowance',
};
+ private exchangeContractIfExists?: ExchangeContract;
constructor(web3Wrapper: Web3Wrapper) {
super(web3Wrapper);
}
+ public invalidateContractInstance(): void {
+ delete this.exchangeContractIfExists;
+ }
public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature,
signerAddressHex: string): Promise<boolean> {
assert.isHexString('dataHex', dataHex);
@@ -36,7 +40,7 @@ export class ExchangeWrapper extends ContractWrapper {
assert.isETHAddressHex('signerAddressHex', signerAddressHex);
const senderAddress = await this.web3Wrapper.getSenderAddressOrThrowAsync();
- const exchangeInstance = await this.getExchangeInstanceOrThrowAsync();
+ const exchangeInstance = await this.getExchangeContractAsync();
const isValidSignature = await exchangeInstance.isValidSignature.call(
signerAddressHex,
@@ -102,4 +106,12 @@ export class ExchangeWrapper extends ContractWrapper {
throw new Error(humanReadableErrMessage);
}
}
+ private async getExchangeContractAsync(): Promise<ExchangeContract> {
+ if (!_.isUndefined(this.exchangeContractIfExists)) {
+ return this.exchangeContractIfExists;
+ }
+ const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any));
+ this.exchangeContractIfExists = contractInstance as ExchangeContract;
+ return this.exchangeContractIfExists;
+ }
}
diff --git a/src/contract_wrappers/token_registry_wrapper.ts b/src/contract_wrappers/token_registry_wrapper.ts
new file mode 100644
index 000000000..86bea1c5d
--- /dev/null
+++ b/src/contract_wrappers/token_registry_wrapper.ts
@@ -0,0 +1,44 @@
+import * as _ from 'lodash';
+import {Web3Wrapper} from '../web3_wrapper';
+import {Token, TokenRegistryContract, TokenMetadata} from '../types';
+import {assert} from '../utils/assert';
+import {ContractWrapper} from './contract_wrapper';
+import * as TokenRegistryArtifacts from '../artifacts/TokenRegistry.json';
+
+export class TokenRegistryWrapper extends ContractWrapper {
+ private tokenRegistryContractIfExists?: TokenRegistryContract;
+ constructor(web3Wrapper: Web3Wrapper) {
+ super(web3Wrapper);
+ }
+ public invalidateContractInstance(): void {
+ delete this.tokenRegistryContractIfExists;
+ }
+ public async getTokensAsync(): Promise<Token[]> {
+ const tokenRegistryContract = await this.getTokenRegistryContractAsync();
+
+ const addresses = await tokenRegistryContract.getTokenAddresses.call();
+ const tokenMetadataPromises: Array<Promise<TokenMetadata>> = _.map(
+ addresses,
+ (address: string) => (tokenRegistryContract.getTokenMetaData.call(address)),
+ );
+ const tokensMetadata = await Promise.all(tokenMetadataPromises);
+ const tokens = _.map(tokensMetadata, metadata => {
+ return {
+ address: metadata[0],
+ name: metadata[1],
+ symbol: metadata[2],
+ url: metadata[3],
+ decimals: metadata[4].toNumber(),
+ };
+ });
+ return tokens;
+ }
+ private async getTokenRegistryContractAsync(): Promise<TokenRegistryContract> {
+ if (!_.isUndefined(this.tokenRegistryContractIfExists)) {
+ return this.tokenRegistryContractIfExists;
+ }
+ const contractInstance = await this.instantiateContractIfExistsAsync((TokenRegistryArtifacts as any));
+ this.tokenRegistryContractIfExists = contractInstance as TokenRegistryContract;
+ return this.tokenRegistryContractIfExists;
+ }
+}