diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/0x.js.ts | 20 | ||||
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 35 | ||||
-rw-r--r-- | src/contract_wrappers/token_wrapper.ts | 11 | ||||
-rw-r--r-- | src/types.ts | 4 | ||||
-rw-r--r-- | src/utils/assert.ts | 12 | ||||
-rw-r--r-- | src/web3_wrapper.ts | 24 |
6 files changed, 38 insertions, 68 deletions
diff --git a/src/0x.js.ts b/src/0x.js.ts index 850827fee..5e2cd9ed9 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -122,17 +122,11 @@ export class ZeroEx { this.token.invalidateContractInstances(); } /** - * Sets default account for sending transactions. + * Gets accounts available for sending transactions. */ - public setTransactionSenderAccount(account: string): void { - this.web3Wrapper.setDefaultAccount(account); - } - /** - * Get the default account set for sending transactions. - */ - public async getTransactionSenderAccountIfExistsAsync(): Promise<string|undefined> { - const senderAccountIfExists = await this.web3Wrapper.getSenderAddressIfExistsAsync(); - return senderAccountIfExists; + public async getAvailableAccountsAsync(): Promise<string[]> { + const availableAccounts = await this.web3Wrapper.getAvailableAccountsAsync(); + return availableAccounts; } /** * Computes the orderHash for a given order and returns it as a hex encoded string. @@ -167,10 +161,10 @@ export class ZeroEx { * Signs an orderHash and returns it's elliptic curve signature * This method currently supports TestRPC, Geth and Parity above and below V1.6.6 */ - public async signOrderHashAsync(orderHashHex: string): Promise<ECSignature> { + public async signOrderHashAsync(orderHashHex: string, senderAccount: string): Promise<ECSignature> { assert.isHexString('orderHashHex', orderHashHex); - - const makerAddress = await this.web3Wrapper.getSenderAddressOrThrowAsync(); + await assert.isSenderAccountHexAsync(this.web3Wrapper, senderAccount); + const makerAddress = senderAccount; let msgHashHex; const nodeVersion = await this.web3Wrapper.getNodeVersionAsync(); diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index ee0b2696f..459e24608 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -57,7 +57,6 @@ export class ExchangeWrapper extends ContractWrapper { assert.doesConformToSchema('ecSignature', ecSignature, ecSignatureSchema); assert.isETHAddressHex('signerAddressHex', signerAddressHex); - const senderAddress = await this.web3Wrapper.getSenderAddressOrThrowAsync(); const exchangeInstance = await this.getExchangeContractAsync(); const isValidSignature = await exchangeInstance.isValidSignature.call( @@ -66,9 +65,6 @@ export class ExchangeWrapper extends ContractWrapper { ecSignature.v, ecSignature.r, ecSignature.s, - { - from: senderAddress, - }, ); return isValidSignature; } @@ -119,16 +115,16 @@ export class ExchangeWrapper extends ContractWrapper { * false forgoes this check and causes the smart contract to throw instead. */ public async fillOrderAsync(signedOrder: SignedOrder, fillTakerAmount: BigNumber.BigNumber, - shouldCheckTransfer: boolean): Promise<void> { + shouldCheckTransfer: boolean, senderAccount: string): Promise<void> { assert.doesConformToSchema('signedOrder', SchemaValidator.convertToJSONSchemaCompatibleObject(signedOrder as object), signedOrderSchema); assert.isBigNumber('fillTakerAmount', fillTakerAmount); assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer); + await assert.isSenderAccountHexAsync(this.web3Wrapper, senderAccount); - const senderAddress = await this.web3Wrapper.getSenderAddressOrThrowAsync(); const exchangeInstance = await this.getExchangeContractAsync(); - await this.validateFillOrderAndThrowIfInvalidAsync(signedOrder, fillTakerAmount, senderAddress); + await this.validateFillOrderAndThrowIfInvalidAsync(signedOrder, fillTakerAmount, senderAccount); const orderAddresses: OrderAddresses = [ signedOrder.maker, @@ -154,7 +150,7 @@ export class ExchangeWrapper extends ContractWrapper { signedOrder.ecSignature.r, signedOrder.ecSignature.s, { - from: senderAddress, + from: senderAccount, }, ); const response: ContractResponse = await exchangeInstance.fill( @@ -166,7 +162,7 @@ export class ExchangeWrapper extends ContractWrapper { signedOrder.ecSignature.r, signedOrder.ecSignature.s, { - from: senderAddress, + from: senderAccount, gas, }, ); @@ -207,11 +203,11 @@ export class ExchangeWrapper extends ContractWrapper { } private async validateFillOrderAndThrowIfInvalidAsync(signedOrder: SignedOrder, fillTakerAmount: BigNumber.BigNumber, - senderAddress: string): Promise<void> { + senderAccount: string): Promise<void> { if (fillTakerAmount.eq(0)) { throw new Error(ExchangeContractErrs.ORDER_REMAINING_FILL_AMOUNT_ZERO); } - if (signedOrder.taker !== constants.NULL_ADDRESS && signedOrder.taker !== senderAddress) { + if (signedOrder.taker !== constants.NULL_ADDRESS && signedOrder.taker !== senderAccount) { throw new Error(ExchangeContractErrs.TRANSACTION_SENDER_IS_NOT_FILL_ORDER_TAKER); } const currentUnixTimestampSec = Date.now() / 1000; @@ -220,7 +216,7 @@ export class ExchangeWrapper extends ContractWrapper { } const zrxTokenAddress = await this.getZRXTokenAddressAsync(); await this.validateFillOrderBalancesAndAllowancesAndThrowIfInvalidAsync(signedOrder, fillTakerAmount, - senderAddress, zrxTokenAddress); + senderAccount, zrxTokenAddress); const wouldRoundingErrorOccur = await this.isRoundingErrorAsync( signedOrder.takerTokenAmount, fillTakerAmount, signedOrder.makerTokenAmount, @@ -241,16 +237,16 @@ export class ExchangeWrapper extends ContractWrapper { */ private async validateFillOrderBalancesAndAllowancesAndThrowIfInvalidAsync(signedOrder: SignedOrder, fillTakerAmount: BigNumber.BigNumber, - senderAddress: string, + senderAccount: string, zrxTokenAddress: string): Promise<void> { const makerBalance = await this.tokenWrapper.getBalanceAsync(signedOrder.makerTokenAddress, signedOrder.maker); - const takerBalance = await this.tokenWrapper.getBalanceAsync(signedOrder.takerTokenAddress, senderAddress); + const takerBalance = await this.tokenWrapper.getBalanceAsync(signedOrder.takerTokenAddress, senderAccount); const makerAllowance = await this.tokenWrapper.getProxyAllowanceAsync(signedOrder.makerTokenAddress, signedOrder.maker); const takerAllowance = await this.tokenWrapper.getProxyAllowanceAsync(signedOrder.takerTokenAddress, - senderAddress); + senderAccount); // exchangeRate is the price of one maker token denominated in taker tokens const exchangeRate = signedOrder.takerTokenAmount.div(signedOrder.makerTokenAmount); @@ -271,11 +267,11 @@ export class ExchangeWrapper extends ContractWrapper { const makerFeeBalance = await this.tokenWrapper.getBalanceAsync(zrxTokenAddress, signedOrder.maker); - const takerFeeBalance = await this.tokenWrapper.getBalanceAsync(zrxTokenAddress, senderAddress); + const takerFeeBalance = await this.tokenWrapper.getBalanceAsync(zrxTokenAddress, senderAccount); const makerFeeAllowance = await this.tokenWrapper.getProxyAllowanceAsync(zrxTokenAddress, signedOrder.maker); const takerFeeAllowance = await this.tokenWrapper.getProxyAllowanceAsync(zrxTokenAddress, - senderAddress); + senderAccount); if (signedOrder.takerFee.greaterThan(takerFeeBalance)) { throw new Error(ExchangeContractErrs.INSUFFICIENT_TAKER_FEE_BALANCE); @@ -302,11 +298,8 @@ export class ExchangeWrapper extends ContractWrapper { fillTakerAmount: BigNumber.BigNumber, makerTokenAmount: BigNumber.BigNumber): Promise<boolean> { const exchangeInstance = await this.getExchangeContractAsync(); - const senderAddress = await this.web3Wrapper.getSenderAddressOrThrowAsync(); const isRoundingError = await exchangeInstance.isRoundingError.call( - takerTokenAmount, fillTakerAmount, makerTokenAmount, { - from: senderAddress, - }, + takerTokenAmount, fillTakerAmount, makerTokenAmount, ); return isRoundingError; } diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index c8b557d0d..55ad9be29 100644 --- a/src/contract_wrappers/token_wrapper.ts +++ b/src/contract_wrappers/token_wrapper.ts @@ -115,21 +115,20 @@ export class TokenWrapper extends ContractWrapper { /** * Transfers `amountInBaseUnits` ERC20 tokens from `fromAddress` to `toAddress`. * Requires the fromAddress to have sufficient funds and to have approved an allowance of - * `amountInBaseUnits` for senderAddress. + * `amountInBaseUnits` for senderAccount. */ public async transferFromAsync(tokenAddress: string, fromAddress: string, toAddress: string, - senderAddress: string, amountInBaseUnits: BigNumber.BigNumber): + senderAccount: string, amountInBaseUnits: BigNumber.BigNumber): Promise<void> { assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isETHAddressHex('fromAddress', fromAddress); assert.isETHAddressHex('toAddress', toAddress); - assert.isETHAddressHex('senderAddress', senderAddress); + await assert.isSenderAccountHexAsync(this.web3Wrapper, senderAccount); assert.isBigNumber('amountInBaseUnits', amountInBaseUnits); - await assert.isSenderAddressAvailableAsync(this.web3Wrapper, senderAddress); const tokenContract = await this.getTokenContractAsync(tokenAddress); - const fromAddressAllowance = await this.getAllowanceAsync(tokenAddress, fromAddress, senderAddress); + const fromAddressAllowance = await this.getAllowanceAsync(tokenAddress, fromAddress, senderAccount); if (fromAddressAllowance.lessThan(amountInBaseUnits)) { throw new Error(ZeroExError.INSUFFICIENT_ALLOWANCE_FOR_TRANSFER); } @@ -140,7 +139,7 @@ export class TokenWrapper extends ContractWrapper { } await tokenContract.transferFrom(fromAddress, toAddress, amountInBaseUnits, { - from: senderAddress, + from: senderAccount, }); } private async getTokenContractAsync(tokenAddress: string): Promise<TokenContract> { diff --git a/src/types.ts b/src/types.ts index 68194f548..a02bd0252 100644 --- a/src/types.ts +++ b/src/types.ts @@ -47,7 +47,7 @@ export type CreateContractEvent = (indexFilterValues: IndexFilterValues, export interface ExchangeContract { isValidSignature: { call: (signerAddressHex: string, dataHex: string, v: number, r: string, s: string, - txOpts: TxOpts) => Promise<boolean>; + txOpts?: TxOpts) => Promise<boolean>; }; LogFill: CreateContractEvent; LogCancel: CreateContractEvent; @@ -60,7 +60,7 @@ export interface ExchangeContract { }; isRoundingError: { call: (takerTokenAmount: BigNumber.BigNumber, fillTakerAmount: BigNumber.BigNumber, - makerTokenAmount: BigNumber.BigNumber, txOpts: TxOpts) => Promise<boolean>; + makerTokenAmount: BigNumber.BigNumber, txOpts?: TxOpts) => Promise<boolean>; }; fill: { (orderAddresses: OrderAddresses, orderValues: OrderValues, fillAmount: BigNumber.BigNumber, diff --git a/src/utils/assert.ts b/src/utils/assert.ts index 5a31e1b16..2396b8534 100644 --- a/src/utils/assert.ts +++ b/src/utils/assert.ts @@ -26,10 +26,14 @@ export const assert = { const web3 = new Web3(); this.assert(web3.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value)); }, - async isSenderAddressAvailableAsync(web3Wrapper: Web3Wrapper, senderAddress: string) { - const isSenderAddressAvailable = await web3Wrapper.isSenderAddressAvailableAsync(senderAddress); - assert.assert(isSenderAddressAvailable, 'Specified senderAddress isn\'t available through the \ - supplied web3 instance'); + async isSenderAccountHexAsync(web3Wrapper: Web3Wrapper, senderAccount: string): Promise<void> { + assert.isETHAddressHex('senderAccount', senderAccount); + await assert.isSenderAccountAvailableAsync(web3Wrapper, senderAccount); + }, + async isSenderAccountAvailableAsync(web3Wrapper: Web3Wrapper, senderAccount: string): Promise<void> { + const isSenderAddressAvailable = await web3Wrapper.isSenderAddressAvailableAsync(senderAccount); + assert.assert(isSenderAddressAvailable, `Specified senderAccount ${senderAccount} isn't available through the \ + supplied web3 instance`); }, isNumber(variableName: string, value: number): void { this.assert(_.isFinite(value), this.typeAssertionMessage(variableName, 'number', value)); diff --git a/src/web3_wrapper.ts b/src/web3_wrapper.ts index 9892abcb8..2508c5116 100644 --- a/src/web3_wrapper.ts +++ b/src/web3_wrapper.ts @@ -23,20 +23,8 @@ export class Web3Wrapper { public setDefaultAccount(address: string): void { this.web3.eth.defaultAccount = address; } - public async getSenderAddressOrThrowAsync(): Promise<string> { - const senderAddressIfExists = await this.getSenderAddressIfExistsAsync(); - assert.assert(!_.isUndefined(senderAddressIfExists), ZeroExError.USER_HAS_NO_ASSOCIATED_ADDRESSES); - return senderAddressIfExists as string; - } - public async getFirstAddressIfExistsAsync(): Promise<string|undefined> { - const addresses = await this.getAvailableSenderAddressesAsync(); - if (_.isEmpty(addresses)) { - return undefined; - } - return addresses[0]; - } public async isSenderAddressAvailableAsync(senderAddress: string): Promise<boolean> { - const addresses = await this.getAvailableSenderAddressesAsync(); + const addresses = await this.getAvailableAccountsAsync(); return _.includes(addresses, senderAddress); } public async getNodeVersionAsync(): Promise<string> { @@ -73,15 +61,7 @@ export class Web3Wrapper { const {timestamp} = await promisify(this.web3.eth.getBlock)(blockHash); return timestamp; } - public async getSenderAddressIfExistsAsync(): Promise<string|undefined> { - const defaultAccount = this.web3.eth.defaultAccount; - if (!_.isUndefined(defaultAccount)) { - return defaultAccount; - } - const firstAccount = await this.getFirstAddressIfExistsAsync(); - return firstAccount; - } - private async getAvailableSenderAddressesAsync(): Promise<string[]> { + public async getAvailableAccountsAsync(): Promise<string[]> { const addresses: string[] = await promisify(this.web3.eth.getAccounts)(); return addresses; } |