aboutsummaryrefslogtreecommitdiffstats
path: root/src/ts/contract_wrappers
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-05-26 01:47:11 +0800
committerFabio Berger <me@fabioberger.com>2017-05-26 01:47:11 +0800
commitbc8fc534332b5ea82f881bdd3a75773384714f4d (patch)
tree2e50db69032c0c5e8817db34f5a3db34ee088589 /src/ts/contract_wrappers
parent74e7dc46b4ba8b3931a1cdf4dc7f55670219e324 (diff)
downloaddexon-sol-tools-bc8fc534332b5ea82f881bdd3a75773384714f4d.tar
dexon-sol-tools-bc8fc534332b5ea82f881bdd3a75773384714f4d.tar.gz
dexon-sol-tools-bc8fc534332b5ea82f881bdd3a75773384714f4d.tar.bz2
dexon-sol-tools-bc8fc534332b5ea82f881bdd3a75773384714f4d.tar.lz
dexon-sol-tools-bc8fc534332b5ea82f881bdd3a75773384714f4d.tar.xz
dexon-sol-tools-bc8fc534332b5ea82f881bdd3a75773384714f4d.tar.zst
dexon-sol-tools-bc8fc534332b5ea82f881bdd3a75773384714f4d.zip
Add initial exchange contract function, set up web3Wrapper, added types and utils
Diffstat (limited to 'src/ts/contract_wrappers')
-rw-r--r--src/ts/contract_wrappers/contract_wrapper.ts51
-rw-r--r--src/ts/contract_wrappers/exchange_wrapper.ts37
2 files changed, 88 insertions, 0 deletions
diff --git a/src/ts/contract_wrappers/contract_wrapper.ts b/src/ts/contract_wrappers/contract_wrapper.ts
new file mode 100644
index 000000000..72bfffe95
--- /dev/null
+++ b/src/ts/contract_wrappers/contract_wrapper.ts
@@ -0,0 +1,51 @@
+import * as _ from 'lodash';
+import {Web3Wrapper} from '../web3_wrapper';
+import {ZeroExError} from '../types';
+import {utils} from '../utils/utils';
+
+export class ContractWrapper {
+ public web3Wrapper: Web3Wrapper;
+ constructor(web3Wrapper: Web3Wrapper) {
+ this.web3Wrapper = web3Wrapper;
+ }
+ // this.exchange = await this.instantiateContractIfExistsAsync(ExchangeArtifacts);
+ protected async instantiateContractIfExistsAsync(artifact: Artifact, address?: string): Promise<ContractInstance> {
+ const c = await contract(artifact);
+ const providerObj = this.web3Wrapper.getCurrentProvider();
+ c.setProvider(providerObj);
+
+ const networkId = await this.web3Wrapper.getNetworkIdIfExistsAsync();
+ const artifactNetworkConfigs = _.isUndefined(networkId) ? undefined : artifact.networks[networkId];
+ let contractAddress;
+ if (!_.isUndefined(address)) {
+ contractAddress = address;
+ } else if (!_.isUndefined(artifactNetworkConfigs)) {
+ contractAddress = artifactNetworkConfigs.address;
+ }
+
+ if (!_.isUndefined(contractAddress)) {
+ const doesContractExist = await this.web3Wrapper.doesContractExistAtAddressAsync(contractAddress);
+ if (!doesContractExist) {
+ throw new Error(ZeroExError.CONTRACT_DOES_NOT_EXIST);
+ }
+ }
+
+ try {
+ let contractInstance;
+ if (_.isUndefined(address)) {
+ contractInstance = await c.deployed();
+ } else {
+ contractInstance = await c.at(address);
+ }
+ return contractInstance;
+ } catch (err) {
+ const errMsg = `${err}`;
+ utils.consoleLog(`Notice: Error encountered: ${err} ${err.stack}`);
+ if (_.includes(errMsg, 'not been deployed to detected network')) {
+ throw new Error(ZeroExError.CONTRACT_DOES_NOT_EXIST);
+ } else {
+ throw new Error(ZeroExError.UNHANDLED_ERROR);
+ }
+ }
+ }
+}
diff --git a/src/ts/contract_wrappers/exchange_wrapper.ts b/src/ts/contract_wrappers/exchange_wrapper.ts
new file mode 100644
index 000000000..502a1089b
--- /dev/null
+++ b/src/ts/contract_wrappers/exchange_wrapper.ts
@@ -0,0 +1,37 @@
+import * as _ from 'lodash';
+import {Web3Wrapper} from '../web3_wrapper';
+import {ECSignature, ZeroExError, ExchangeContract} from '../types';
+import {assert} from '../utils/assert';
+import {ContractWrapper} from './contract_wrapper';
+import * as ExchangeArtifacts from '../artifacts/Exchange.json';
+import {ECSignatureSchema} from '../schemas/ec_signature_schema';
+
+export class ExchangeWrapper extends ContractWrapper {
+ constructor(web3Wrapper: Web3Wrapper) {
+ super(web3Wrapper);
+ }
+ public async isValidSignatureAsync(maker: string, ecSignature: ECSignature, dataHex: string) {
+ assert.isString('maker', maker);
+ assert.doesConformToSchema('ecSignature', ecSignature, ECSignatureSchema);
+ assert.isHexString('dataHex', dataHex);
+
+ const senderAddressIfExists = this.web3Wrapper.getSenderAddressIfExistsAsync();
+ assert.assert(!_.isUndefined(senderAddressIfExists), ZeroExError.USER_HAS_NO_ASSOCIATED_ADDRESSES);
+
+ // TODO: remove any here
+ const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any));
+ const exchangeInstance = contractInstance as ExchangeContract;
+
+ const isValidSignature = await exchangeInstance.isValidSignature.call(
+ maker,
+ dataHex,
+ ecSignature.v,
+ ecSignature.r,
+ ecSignature.s,
+ {
+ from: senderAddressIfExists,
+ },
+ );
+ return isValidSignature;
+ }
+}