diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-05-29 17:39:12 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-05-29 17:39:12 +0800 |
commit | 0848fe96cf09679926d307c86414cfb8b6f16d78 (patch) | |
tree | ec1da9d45a01b846de9b45a2bb57fee8bdb0f23e /src/contract_wrappers | |
parent | 62cc3b919c73b7726793808e3b9631dba41cef28 (diff) | |
download | dexon-sol-tools-0848fe96cf09679926d307c86414cfb8b6f16d78.tar dexon-sol-tools-0848fe96cf09679926d307c86414cfb8b6f16d78.tar.gz dexon-sol-tools-0848fe96cf09679926d307c86414cfb8b6f16d78.tar.bz2 dexon-sol-tools-0848fe96cf09679926d307c86414cfb8b6f16d78.tar.lz dexon-sol-tools-0848fe96cf09679926d307c86414cfb8b6f16d78.tar.xz dexon-sol-tools-0848fe96cf09679926d307c86414cfb8b6f16d78.tar.zst dexon-sol-tools-0848fe96cf09679926d307c86414cfb8b6f16d78.zip |
Move files up and remove ts folder
Diffstat (limited to 'src/contract_wrappers')
-rw-r--r-- | src/contract_wrappers/contract_wrapper.ts | 48 | ||||
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 37 |
2 files changed, 85 insertions, 0 deletions
diff --git a/src/contract_wrappers/contract_wrapper.ts b/src/contract_wrappers/contract_wrapper.ts new file mode 100644 index 000000000..9f4cd8039 --- /dev/null +++ b/src/contract_wrappers/contract_wrapper.ts @@ -0,0 +1,48 @@ +import * as _ from 'lodash'; +import contract = require('truffle-contract'); +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; + } + protected async instantiateContractIfExistsAsync(artifact: Artifact, address?: string): Promise<ContractInstance> { + const c = await contract(artifact); + const providerObj = this.web3Wrapper.getCurrentProvider(); + c.setProvider(providerObj); + + const networkIdIfExists = await this.web3Wrapper.getNetworkIdIfExistsAsync(); + const artifactNetworkConfigs = _.isUndefined(networkIdIfExists) ? + undefined : + artifact.networks[networkIdIfExists]; + 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 { + const contractInstance = _.isUndefined(address) ? await c.deployed() : await c.at(address); + return contractInstance; + } catch (err) { + const errMsg = `${err}`; + if (_.includes(errMsg, 'not been deployed to detected network')) { + throw new Error(ZeroExError.CONTRACT_DOES_NOT_EXIST); + } else { + utils.consoleLog(`Notice: Error encountered: ${err} ${err.stack}`); + throw new Error(ZeroExError.UNHANDLED_ERROR); + } + } + } +} diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts new file mode 100644 index 000000000..38043dd55 --- /dev/null +++ b/src/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(dataHex: string, ecSignature: ECSignature, + signerAddressHex: string): Promise<boolean> { + assert.isHexString('dataHex', dataHex); + assert.doesConformToSchema('ecSignature', ecSignature, ECSignatureSchema); + assert.isETHAddressHex('signerAddressHex', signerAddressHex); + + const senderAddressIfExists = await this.web3Wrapper.getSenderAddressIfExistsAsync(); + assert.assert(!_.isUndefined(senderAddressIfExists), ZeroExError.USER_HAS_NO_ASSOCIATED_ADDRESSES); + + const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any)); + const exchangeInstance = contractInstance as ExchangeContract; + + const isValidSignature = await exchangeInstance.isValidSignature.call( + signerAddressHex, + dataHex, + ecSignature.v, + ecSignature.r, + ecSignature.s, + { + from: senderAddressIfExists, + }, + ); + return isValidSignature; + } +} |