Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
awaitTransactionSuccessAsync(
assetData: string,
from: string,
to: string,
amount: BigNumber,
txData?: Partial,
opts: AwaitTransactionSuccessOpts = { shouldValidate: true },
): PromiseWithTransactionHash {
assert.isString('assetData', assetData);
assert.isString('from', from);
assert.isString('to', to);
assert.isBigNumber('amount', amount);
const self = (this as any) as IAssetProxyContract;
const txHashPromise = self.transferFrom.sendTransactionAsync(
assetData,
from.toLowerCase(),
to.toLowerCase(),
amount,
txData,
opts,
);
return new PromiseWithTransactionHash(
txHashPromise,
(async (): Promise => {
// When the transaction hash resolves, wait for it to be mined.
return self._web3Wrapper.awaitTransactionSuccessAsync(
parse(utf8Data: string): OrdersChannelMessage {
// parse the message
const messageObj = JSON.parse(utf8Data);
// ensure we have a type parameter to switch on
const type: string = _.get(messageObj, 'type');
assert.assert(type !== undefined, `Message is missing a type parameter: ${utf8Data}`);
assert.isString('type', type);
// ensure we have a request id for the resulting message
const requestId: string = _.get(messageObj, 'requestId');
assert.assert(requestId !== undefined, `Message is missing a requestId parameter: ${utf8Data}`);
assert.isString('requestId', requestId);
switch (type) {
case OrdersChannelMessageTypes.Update: {
assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrdersChannelUpdateSchema);
const ordersJson = messageObj.payload;
const orders = relayerResponseJsonParsers.parseAPIOrdersJson(ordersJson);
return _.assign(messageObj, { payload: orders });
}
default: {
return {
type: OrdersChannelMessageTypes.Unknown,
requestId,
payload: undefined,
};
}
}
},
};
public async getTransactionReceiptIfExistsAsync(txHash: string): Promise {
assert.isHexString('txHash', txHash);
const transactionReceiptRpc = await this.sendRawPayloadAsync({
method: 'eth_getTransactionReceipt',
params: [txHash],
});
// HACK Parity can return a pending transaction receipt. We check for a non null
// block number before continuing with returning a fully realised receipt.
// ref: https://github.com/paritytech/parity-ethereum/issues/1180
if (transactionReceiptRpc !== null && transactionReceiptRpc.blockNumber !== null) {
transactionReceiptRpc.status = Web3Wrapper._normalizeTxReceiptStatus(transactionReceiptRpc.status);
const transactionReceipt = marshaller.unmarshalTransactionReceipt(transactionReceiptRpc);
return transactionReceipt;
} else {
return undefined;
}
}
/**
public async signMessageAsync(address: string, message: string): Promise {
assert.isETHAddressHex('address', address);
assert.isString('message', message); // TODO: Should this be stricter? Hex string?
const signData = await this.sendRawPayloadAsync({
method: 'eth_sign',
params: [address, message],
});
return signData;
}
/**
public async getBalanceInWeiAsync(owner: string, defaultBlock?: BlockParam): Promise {
assert.isETHAddressHex('owner', owner);
if (defaultBlock !== undefined) {
Web3Wrapper._assertBlockParam(defaultBlock);
}
const marshalledDefaultBlock = marshaller.marshalBlockParam(defaultBlock);
const encodedOwner = marshaller.marshalAddress(owner);
const balanceInWei = await this.sendRawPayloadAsync({
method: 'eth_getBalance',
params: [encodedOwner, marshalledDefaultBlock],
});
// Rewrap in a new BigNumber
return new BigNumber(balanceInWei);
}
/**
async callAsync(callData: Partial = {}, defaultBlock?: BlockParam): Promise {
assert.doesConformToSchema('callData', callData, schemas.callDataSchema, [
schemas.addressSchema,
schemas.numberSchema,
schemas.jsNumber,
]);
if (defaultBlock !== undefined) {
assert.isBlockParam('defaultBlock', defaultBlock);
}
const self = (this as any) as StaticCallProxyContract;
const encodedData = self._strictEncodeArguments('getProxyId()', []);
const encodedDataBytes = Buffer.from(encodedData.substr(2), 'hex');
let rawCallResult;
try {
rawCallResult = await self._evmExecAsync(encodedDataBytes);
} catch (err) {
BaseContract._throwIfThrownErrorIsRevertError(err);
throw err;
}
BaseContract._throwIfCallResultIsRevertError(rawCallResult);
const abiEncoder = self._lookupAbiEncoder('getProxyId()');
// tslint:disable boolean-naming
public async increaseTimeAsync(timeDelta: number): Promise {
assert.isNumber('timeDelta', timeDelta);
// Detect Geth vs. Ganache and use appropriate endpoint.
const version = await this.getNodeVersionAsync();
if (_.includes(version, uniqueVersionIds.geth)) {
return this.sendRawPayloadAsync({ method: 'debug_increaseTime', params: [timeDelta] });
} else if (_.includes(version, uniqueVersionIds.ganache)) {
return this.sendRawPayloadAsync({ method: 'evm_increaseTime', params: [timeDelta] });
} else {
throw new Error(`Unknown client version: ${version}`);
}
}
/**
public async awaitTransactionMinedAsync(
txHash: string,
pollingIntervalMs: number = 1000,
timeoutMs?: number,
): Promise {
assert.isHexString('txHash', txHash);
assert.isNumber('pollingIntervalMs', pollingIntervalMs);
if (timeoutMs !== undefined) {
assert.isNumber('timeoutMs', timeoutMs);
}
// Immediately check if the transaction has already been mined.
let transactionReceipt = await this.getTransactionReceiptIfExistsAsync(txHash);
if (transactionReceipt !== undefined) {
const logsWithDecodedArgs = _.map(
transactionReceipt.logs,
this.abiDecoder.tryToDecodeLogOrNoop.bind(this.abiDecoder),
);
const transactionReceiptWithDecodedLogArgs: TransactionReceiptWithDecodedLogs = {
...transactionReceipt,
logs: logsWithDecodedArgs,
};
return transactionReceiptWithDecodedLogArgs;
}
// Otherwise, check again every pollingIntervalMs.
public async awaitTransactionMinedAsync(
txHash: string,
pollingIntervalMs: number = 1000,
timeoutMs?: number,
): Promise {
assert.isHexString('txHash', txHash);
assert.isNumber('pollingIntervalMs', pollingIntervalMs);
if (timeoutMs !== undefined) {
assert.isNumber('timeoutMs', timeoutMs);
}
// Immediately check if the transaction has already been mined.
let transactionReceipt = await this.getTransactionReceiptIfExistsAsync(txHash);
if (transactionReceipt !== undefined) {
const logsWithDecodedArgs = _.map(
transactionReceipt.logs,
this.abiDecoder.tryToDecodeLogOrNoop.bind(this.abiDecoder),
);
const transactionReceiptWithDecodedLogArgs: TransactionReceiptWithDecodedLogs = {
...transactionReceipt,
logs: logsWithDecodedArgs,
};
return transactionReceiptWithDecodedLogArgs;
}
parse(utf8Data: string): OrdersChannelMessage {
// parse the message
const messageObj = JSON.parse(utf8Data);
// ensure we have a type parameter to switch on
const type: string = _.get(messageObj, 'type');
assert.assert(type !== undefined, `Message is missing a type parameter: ${utf8Data}`);
assert.isString('type', type);
// ensure we have a request id for the resulting message
const requestId: string = _.get(messageObj, 'requestId');
assert.assert(requestId !== undefined, `Message is missing a requestId parameter: ${utf8Data}`);
assert.isString('requestId', requestId);
switch (type) {
case OrdersChannelMessageTypes.Update: {
assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrdersChannelUpdateSchema);
const ordersJson = messageObj.payload;
const orders = relayerResponseJsonParsers.parseAPIOrdersJson(ordersJson);
return _.assign(messageObj, { payload: orders });
}
default: {
return {
type: OrdersChannelMessageTypes.Unknown,
requestId,
payload: undefined,
};
}
}