diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-08-24 06:48:15 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-08-28 04:04:54 +0800 |
commit | 260313a6aeab6f64fa274e9aec5c61c6fcaa64e5 (patch) | |
tree | 3635792266474b779ed6981efd242e67657307a0 /packages/contract-wrappers/src/contract_wrappers | |
parent | 1be310cef49da42e4534a945b95b633d5846d864 (diff) | |
download | dexon-sol-tools-260313a6aeab6f64fa274e9aec5c61c6fcaa64e5.tar dexon-sol-tools-260313a6aeab6f64fa274e9aec5c61c6fcaa64e5.tar.gz dexon-sol-tools-260313a6aeab6f64fa274e9aec5c61c6fcaa64e5.tar.bz2 dexon-sol-tools-260313a6aeab6f64fa274e9aec5c61c6fcaa64e5.tar.lz dexon-sol-tools-260313a6aeab6f64fa274e9aec5c61c6fcaa64e5.tar.xz dexon-sol-tools-260313a6aeab6f64fa274e9aec5c61c6fcaa64e5.tar.zst dexon-sol-tools-260313a6aeab6f64fa274e9aec5c61c6fcaa64e5.zip |
Initial OrderValidator wrapper
Diffstat (limited to 'packages/contract-wrappers/src/contract_wrappers')
-rw-r--r-- | packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts new file mode 100644 index 000000000..6857dcd69 --- /dev/null +++ b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts @@ -0,0 +1,72 @@ +import { schemas } from '@0xproject/json-schemas'; +import { SignedOrder } from '@0xproject/types'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import { ContractAbi } from 'ethereum-types'; +import * as _ from 'lodash'; + +import { artifacts } from '../artifacts'; +import { OrdersAndTradersInfo } from '../types'; +import { assert } from '../utils/assert'; + +import { ContractWrapper } from './contract_wrapper'; +import { OrderValidatorContract } from './generated/order_validator'; + +/** + * This class includes the functionality related to interacting with the OrderValidator contract. + */ +export class OrderValidatorWrapper extends ContractWrapper { + public abi: ContractAbi = artifacts.OrderValidator.compilerOutput.abi; + private _orderValidatorContractIfExists?: OrderValidatorContract; + /** + * Instantiate OrderValidatorWrapper + * @param web3Wrapper Web3Wrapper instance to use + * @param networkId Desired networkId + */ + constructor(web3Wrapper: Web3Wrapper, networkId: number) { + super(web3Wrapper, networkId); + } + /** + * Get and object conforming to OrdersAndTradersInfo containing on-chain information of the provided orders and addresses + * @return OrdersAndTradersInfo + */ + public async getOrdersAndTradersInfoAsync( + orders: SignedOrder[], + takerAddresses: string[], + ): Promise<OrdersAndTradersInfo> { + assert.doesConformToSchema('orders', orders, schemas.signedOrdersSchema); + _.forEach(takerAddresses, (takerAddress, index) => + assert.isETHAddressHex(`takerAddresses[${index}]`, takerAddress), + ); + assert.assert(orders.length === takerAddresses.length, 'Expected orders.length to equal takerAddresses.length'); + const OrderValidatorContractInstance = await this._getOrderValidatorContractAsync(); + const ordersAndTradersInfo = await OrderValidatorContractInstance.getOrdersAndTradersInfo.callAsync( + orders, + takerAddresses, + ); + const result = { + ordersInfo: ordersAndTradersInfo[0], + tradersInfo: ordersAndTradersInfo[1], + }; + return result; + } + // HACK: We don't want this method to be visible to the other units within that package but not to the end user. + // TS doesn't give that possibility and therefore we make it private and access it over an any cast. Because of that tslint sees it as unused. + // tslint:disable-next-line:no-unused-variable + private _invalidateContractInstance(): void { + delete this._orderValidatorContractIfExists; + } + private async _getOrderValidatorContractAsync(): Promise<OrderValidatorContract> { + if (!_.isUndefined(this._orderValidatorContractIfExists)) { + return this._orderValidatorContractIfExists; + } + const [abi, address] = await this._getContractAbiAndAddressFromArtifactsAsync(artifacts.OrderValidator); + const contractInstance = new OrderValidatorContract( + abi, + address, + this._web3Wrapper.getProvider(), + this._web3Wrapper.getContractDefaults(), + ); + this._orderValidatorContractIfExists = contractInstance; + return this._orderValidatorContractIfExists; + } +} |