aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLeonid <logvinov.leon@gmail.com>2017-06-06 21:14:53 +0800
committerGitHub <noreply@github.com>2017-06-06 21:14:53 +0800
commit692a0fd965f7c0cfc0eef79112d2abffdf9e90db (patch)
treea3a97eb6522bc887966da6575e4b2ac73560fdcf /src
parent2eb99f46f53984c1a54315f87059ef18b9d06349 (diff)
parentf54b513935dbba0dd1922566ed2fd4b4acbf6459 (diff)
downloaddexon-sol-tools-692a0fd965f7c0cfc0eef79112d2abffdf9e90db.tar.gz
dexon-sol-tools-692a0fd965f7c0cfc0eef79112d2abffdf9e90db.tar.zst
dexon-sol-tools-692a0fd965f7c0cfc0eef79112d2abffdf9e90db.zip
Merge pull request #39 from 0xProject/senderAccount
Make methods accept senderAccount
Diffstat (limited to 'src')
-rw-r--r--src/0x.js.ts23
-rw-r--r--src/contract_wrappers/exchange_wrapper.ts20
-rw-r--r--src/contract_wrappers/token_wrapper.ts9
-rw-r--r--src/types.ts4
-rw-r--r--src/utils/assert.ts18
-rw-r--r--src/web3_wrapper.ts30
6 files changed, 38 insertions, 66 deletions
diff --git a/src/0x.js.ts b/src/0x.js.ts
index 850827fee..0f437e039 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.
+ * Get addresses via the supplied web3 instance 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 getAvailableAddressesAsync(): Promise<string[]> {
+ const availableAddresses = await this.web3Wrapper.getAvailableAddressesAsync();
+ return availableAddresses;
}
/**
* Computes the orderHash for a given order and returns it as a hex encoded string.
@@ -167,10 +161,9 @@ 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, signerAddress: string): Promise<ECSignature> {
assert.isHexString('orderHashHex', orderHashHex);
-
- const makerAddress = await this.web3Wrapper.getSenderAddressOrThrowAsync();
+ await assert.isSenderAddressAsync('signerAddress', signerAddress, this.web3Wrapper);
let msgHashHex;
const nodeVersion = await this.web3Wrapper.getNodeVersionAsync();
@@ -184,7 +177,7 @@ export class ZeroEx {
msgHashHex = ethUtil.bufferToHex(msgHashBuff);
}
- const signature = await this.web3Wrapper.signTransactionAsync(makerAddress, msgHashHex);
+ const signature = await this.web3Wrapper.signTransactionAsync(signerAddress, msgHashHex);
let signatureData;
const [nodeVersionNumber] = findVersions(nodeVersion);
@@ -214,7 +207,7 @@ export class ZeroEx {
r: ethUtil.bufferToHex(r),
s: ethUtil.bufferToHex(s),
};
- const isValidSignature = ZeroEx.isValidSignature(orderHashHex, ecSignature, makerAddress);
+ const isValidSignature = ZeroEx.isValidSignature(orderHashHex, ecSignature, signerAddress);
if (!isValidSignature) {
throw new Error(ZeroExError.INVALID_SIGNATURE);
}
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts
index ee0b2696f..d3a53a9f7 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, takerAddress: string): Promise<void> {
assert.doesConformToSchema('signedOrder',
SchemaValidator.convertToJSONSchemaCompatibleObject(signedOrder as object),
signedOrderSchema);
assert.isBigNumber('fillTakerAmount', fillTakerAmount);
assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer);
+ await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper);
- const senderAddress = await this.web3Wrapper.getSenderAddressOrThrowAsync();
const exchangeInstance = await this.getExchangeContractAsync();
- await this.validateFillOrderAndThrowIfInvalidAsync(signedOrder, fillTakerAmount, senderAddress);
+ await this.validateFillOrderAndThrowIfInvalidAsync(signedOrder, fillTakerAmount, takerAddress);
const orderAddresses: OrderAddresses = [
signedOrder.maker,
@@ -154,7 +150,7 @@ export class ExchangeWrapper extends ContractWrapper {
signedOrder.ecSignature.r,
signedOrder.ecSignature.s,
{
- from: senderAddress,
+ from: takerAddress,
},
);
const response: ContractResponse = await exchangeInstance.fill(
@@ -166,7 +162,7 @@ export class ExchangeWrapper extends ContractWrapper {
signedOrder.ecSignature.r,
signedOrder.ecSignature.s,
{
- from: senderAddress,
+ from: takerAddress,
gas,
},
);
@@ -301,12 +297,10 @@ export class ExchangeWrapper extends ContractWrapper {
private async isRoundingErrorAsync(takerTokenAmount: BigNumber.BigNumber,
fillTakerAmount: BigNumber.BigNumber,
makerTokenAmount: BigNumber.BigNumber): Promise<boolean> {
+ await assert.isUserAddressAvailableAsync(this.web3Wrapper);
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..4412b1299 100644
--- a/src/contract_wrappers/token_wrapper.ts
+++ b/src/contract_wrappers/token_wrapper.ts
@@ -25,6 +25,7 @@ export class TokenWrapper extends ContractWrapper {
public async getBalanceAsync(tokenAddress: string, ownerAddress: string): Promise<BigNumber.BigNumber> {
assert.isETHAddressHex('ownerAddress', ownerAddress);
assert.isETHAddressHex('tokenAddress', tokenAddress);
+ await assert.isUserAddressAvailableAsync(this.web3Wrapper);
const tokenContract = await this.getTokenContractAsync(tokenAddress);
let balance = await tokenContract.balanceOf.call(ownerAddress);
@@ -38,7 +39,7 @@ export class TokenWrapper extends ContractWrapper {
*/
public async setAllowanceAsync(tokenAddress: string, ownerAddress: string, spenderAddress: string,
amountInBaseUnits: BigNumber.BigNumber): Promise<void> {
- assert.isETHAddressHex('ownerAddress', ownerAddress);
+ await assert.isSenderAddressAsync('ownerAddress', ownerAddress, this.web3Wrapper);
assert.isETHAddressHex('spenderAddress', spenderAddress);
assert.isETHAddressHex('tokenAddress', tokenAddress);
assert.isBigNumber('amountInBaseUnits', amountInBaseUnits);
@@ -60,6 +61,7 @@ export class TokenWrapper extends ContractWrapper {
public async getAllowanceAsync(tokenAddress: string, ownerAddress: string, spenderAddress: string) {
assert.isETHAddressHex('ownerAddress', ownerAddress);
assert.isETHAddressHex('tokenAddress', tokenAddress);
+ await assert.isUserAddressAvailableAsync(this.web3Wrapper);
const tokenContract = await this.getTokenContractAsync(tokenAddress);
let allowanceInBaseUnits = await tokenContract.allowance.call(ownerAddress, spenderAddress);
@@ -97,7 +99,7 @@ export class TokenWrapper extends ContractWrapper {
public async transferAsync(tokenAddress: string, fromAddress: string, toAddress: string,
amountInBaseUnits: BigNumber.BigNumber): Promise<void> {
assert.isETHAddressHex('tokenAddress', tokenAddress);
- assert.isETHAddressHex('fromAddress', fromAddress);
+ await assert.isSenderAddressAsync('fromAddress', fromAddress, this.web3Wrapper);
assert.isETHAddressHex('toAddress', toAddress);
assert.isBigNumber('amountInBaseUnits', amountInBaseUnits);
@@ -123,9 +125,8 @@ export class TokenWrapper extends ContractWrapper {
assert.isETHAddressHex('tokenAddress', tokenAddress);
assert.isETHAddressHex('fromAddress', fromAddress);
assert.isETHAddressHex('toAddress', toAddress);
- assert.isETHAddressHex('senderAddress', senderAddress);
+ await assert.isSenderAddressAsync('senderAddress', senderAddress, this.web3Wrapper);
assert.isBigNumber('amountInBaseUnits', amountInBaseUnits);
- await assert.isSenderAddressAvailableAsync(this.web3Wrapper, senderAddress);
const tokenContract = await this.getTokenContractAsync(tokenAddress);
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..f4d653ffb 100644
--- a/src/utils/assert.ts
+++ b/src/utils/assert.ts
@@ -26,10 +26,20 @@ 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 isSenderAddressAsync(variableName: string, senderAddressHex: string,
+ web3Wrapper: Web3Wrapper): Promise<void> {
+ assert.isETHAddressHex(variableName, senderAddressHex);
+ await assert.isSenderAddressAvailableAsync(web3Wrapper, variableName, senderAddressHex);
+ },
+ async isSenderAddressAvailableAsync(web3Wrapper: Web3Wrapper, variableName: string,
+ senderAddressHex: string): Promise<void> {
+ const isSenderAddressAvailable = await web3Wrapper.isSenderAddressAvailableAsync(senderAddressHex);
+ assert.assert(isSenderAddressAvailable, `Specified ${variableName} ${senderAddressHex} isn't available \
+ through the supplied web3 instance`);
+ },
+ async isUserAddressAvailableAsync(web3Wrapper: Web3Wrapper): Promise<void> {
+ const availableAddresses = await web3Wrapper.getAvailableAddressesAsync();
+ this.assert(!_.isEmpty(availableAddresses), 'No addresses were available on the provided 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..05a8dc063 100644
--- a/src/web3_wrapper.ts
+++ b/src/web3_wrapper.ts
@@ -17,26 +17,8 @@ export class Web3Wrapper {
public isAddress(address: string): boolean {
return this.web3.isAddress(address);
}
- public getDefaultAccount(): string {
- return this.web3.eth.defaultAccount;
- }
- 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.getAvailableAddressesAsync();
return _.includes(addresses, senderAddress);
}
public async getNodeVersionAsync(): Promise<string> {
@@ -73,15 +55,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 getAvailableAddressesAsync(): Promise<string[]> {
const addresses: string[] = await promisify(this.web3.eth.getAccounts)();
return addresses;
}