|
|
@ -9,7 +9,7 @@ import { |
|
|
|
getAddress, |
|
|
|
getAddress, |
|
|
|
HarmonyAddress, |
|
|
|
HarmonyAddress, |
|
|
|
} from '@harmony-js/crypto'; |
|
|
|
} from '@harmony-js/crypto'; |
|
|
|
import {add0xToString, numberToHex, ChainType, Unit} from '@harmony-js/utils'; |
|
|
|
import { add0xToString, numberToHex, ChainType, Unit } from '@harmony-js/utils'; |
|
|
|
import { |
|
|
|
import { |
|
|
|
Messenger, |
|
|
|
Messenger, |
|
|
|
RPCMethod, |
|
|
|
RPCMethod, |
|
|
@ -19,7 +19,7 @@ import { |
|
|
|
// SubscribeReturns,
|
|
|
|
// SubscribeReturns,
|
|
|
|
NewHeaders, |
|
|
|
NewHeaders, |
|
|
|
} from '@harmony-js/network'; |
|
|
|
} from '@harmony-js/network'; |
|
|
|
import {TxParams, TxStatus, TransasctionReceipt} from './types'; |
|
|
|
import { TxParams, TxStatus, TransasctionReceipt } from './types'; |
|
|
|
import { |
|
|
|
import { |
|
|
|
recover, |
|
|
|
recover, |
|
|
|
transactionFields, |
|
|
|
transactionFields, |
|
|
@ -80,19 +80,13 @@ class Transaction { |
|
|
|
|
|
|
|
|
|
|
|
this.to = params && params.to ? this.normalizeAddress(params.to) : '0x'; |
|
|
|
this.to = params && params.to ? this.normalizeAddress(params.to) : '0x'; |
|
|
|
this.value = |
|
|
|
this.value = |
|
|
|
params && params.value |
|
|
|
params && params.value ? new Unit(params.value).asWei().toWei() : new Unit(0).asWei().toWei(); |
|
|
|
? new Unit(params.value).asWei().toWei() |
|
|
|
|
|
|
|
: new Unit(0).asWei().toWei(); |
|
|
|
|
|
|
|
this.data = params && params.data ? params.data : '0x'; |
|
|
|
this.data = params && params.data ? params.data : '0x'; |
|
|
|
// chainid should change with different network settings
|
|
|
|
// chainid should change with different network settings
|
|
|
|
this.chainId = |
|
|
|
this.chainId = params && params.chainId ? params.chainId : this.messenger.chainId; |
|
|
|
params && params.chainId ? params.chainId : this.messenger.chainId; |
|
|
|
this.rawTransaction = params && params.rawTransaction ? params.rawTransaction : '0x'; |
|
|
|
this.rawTransaction = |
|
|
|
|
|
|
|
params && params.rawTransaction ? params.rawTransaction : '0x'; |
|
|
|
|
|
|
|
this.unsignedRawTransaction = |
|
|
|
this.unsignedRawTransaction = |
|
|
|
params && params.unsignedRawTransaction |
|
|
|
params && params.unsignedRawTransaction ? params.unsignedRawTransaction : '0x'; |
|
|
|
? params.unsignedRawTransaction |
|
|
|
|
|
|
|
: '0x'; |
|
|
|
|
|
|
|
this.signature = |
|
|
|
this.signature = |
|
|
|
params && params.signature |
|
|
|
params && params.signature |
|
|
|
? params.signature |
|
|
|
? params.signature |
|
|
@ -114,24 +108,15 @@ class Transaction { |
|
|
|
|
|
|
|
|
|
|
|
// temp setting to be compatible with eth
|
|
|
|
// temp setting to be compatible with eth
|
|
|
|
const fields = |
|
|
|
const fields = |
|
|
|
this.messenger.chainType === ChainType.Harmony |
|
|
|
this.messenger.chainType === ChainType.Harmony ? transactionFields : transactionFieldsETH; |
|
|
|
? transactionFields |
|
|
|
|
|
|
|
: transactionFieldsETH; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fields.forEach((field: any) => { |
|
|
|
fields.forEach((field: any) => { |
|
|
|
let value = (<any>this.txParams)[field.name] || []; |
|
|
|
let value = (<any>this.txParams)[field.name] || []; |
|
|
|
value = arrayify( |
|
|
|
value = arrayify( |
|
|
|
hexlify( |
|
|
|
hexlify(field.transform === 'hex' ? add0xToString(value.toString(16)) : value), |
|
|
|
field.transform === 'hex' ? add0xToString(value.toString(16)) : value, |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
// Fixed-width field
|
|
|
|
// Fixed-width field
|
|
|
|
if ( |
|
|
|
if (field.fix === true && field.length && value.length !== field.length && value.length > 0) { |
|
|
|
field.fix === true && |
|
|
|
|
|
|
|
field.length && |
|
|
|
|
|
|
|
value.length !== field.length && |
|
|
|
|
|
|
|
value.length > 0 |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
throw new Error(`invalid length for ${field.name}`); |
|
|
|
throw new Error(`invalid length for ${field.name}`); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -196,9 +181,7 @@ class Transaction { |
|
|
|
shardID: this.txParams.shardID ? numberToHex(this.shardID) : '0x', |
|
|
|
shardID: this.txParams.shardID ? numberToHex(this.shardID) : '0x', |
|
|
|
toShardID: this.txParams.toShardID ? numberToHex(this.toShardID) : '0x', |
|
|
|
toShardID: this.txParams.toShardID ? numberToHex(this.toShardID) : '0x', |
|
|
|
gas: this.txParams.gasLimit ? numberToHex(this.txParams.gasLimit) : '0x', |
|
|
|
gas: this.txParams.gasLimit ? numberToHex(this.txParams.gasLimit) : '0x', |
|
|
|
gasPrice: this.txParams.gasPrice |
|
|
|
gasPrice: this.txParams.gasPrice ? numberToHex(this.txParams.gasPrice) : '0x', |
|
|
|
? numberToHex(this.txParams.gasPrice) |
|
|
|
|
|
|
|
: '0x', |
|
|
|
|
|
|
|
value: this.txParams.value ? numberToHex(this.txParams.value) : '0x', |
|
|
|
value: this.txParams.value ? numberToHex(this.txParams.value) : '0x', |
|
|
|
data: this.txParams.data || '0x', |
|
|
|
data: this.txParams.data || '0x', |
|
|
|
nonce: this.txParams.nonce ? numberToHex(this.nonce) : '0x', |
|
|
|
nonce: this.txParams.nonce ? numberToHex(this.nonce) : '0x', |
|
|
@ -239,17 +222,12 @@ class Transaction { |
|
|
|
this.toShardID = params && params.toShardID ? params.toShardID : 0; |
|
|
|
this.toShardID = params && params.toShardID ? params.toShardID : 0; |
|
|
|
this.to = params && params.to ? this.normalizeAddress(params.to) : '0x'; |
|
|
|
this.to = params && params.to ? this.normalizeAddress(params.to) : '0x'; |
|
|
|
this.value = |
|
|
|
this.value = |
|
|
|
params && params.value |
|
|
|
params && params.value ? new Unit(params.value).asWei().toWei() : new Unit(0).asWei().toWei(); |
|
|
|
? new Unit(params.value).asWei().toWei() |
|
|
|
|
|
|
|
: new Unit(0).asWei().toWei(); |
|
|
|
|
|
|
|
this.data = params && params.data ? params.data : '0x'; |
|
|
|
this.data = params && params.data ? params.data : '0x'; |
|
|
|
this.chainId = params && params.chainId ? params.chainId : 0; |
|
|
|
this.chainId = params && params.chainId ? params.chainId : 0; |
|
|
|
this.rawTransaction = |
|
|
|
this.rawTransaction = params && params.rawTransaction ? params.rawTransaction : '0x'; |
|
|
|
params && params.rawTransaction ? params.rawTransaction : '0x'; |
|
|
|
|
|
|
|
this.unsignedRawTransaction = |
|
|
|
this.unsignedRawTransaction = |
|
|
|
params && params.unsignedRawTransaction |
|
|
|
params && params.unsignedRawTransaction ? params.unsignedRawTransaction : '0x'; |
|
|
|
? params.unsignedRawTransaction |
|
|
|
|
|
|
|
: '0x'; |
|
|
|
|
|
|
|
this.signature = |
|
|
|
this.signature = |
|
|
|
params && params.signature |
|
|
|
params && params.signature |
|
|
|
? params.signature |
|
|
|
? params.signature |
|
|
@ -310,10 +288,7 @@ class Transaction { |
|
|
|
if (!this.messenger) { |
|
|
|
if (!this.messenger) { |
|
|
|
throw new Error('Messenger not found'); |
|
|
|
throw new Error('Messenger not found'); |
|
|
|
} |
|
|
|
} |
|
|
|
const res = await this.messenger.send( |
|
|
|
const res = await this.messenger.send(RPCMethod.SendRawTransaction, this.rawTransaction); |
|
|
|
RPCMethod.SendRawTransaction, |
|
|
|
|
|
|
|
this.rawTransaction, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// temporarilly hard coded
|
|
|
|
// temporarilly hard coded
|
|
|
|
if (res.isResult()) { |
|
|
|
if (res.isResult()) { |
|
|
@ -337,10 +312,7 @@ class Transaction { |
|
|
|
throw new Error('Messenger not found'); |
|
|
|
throw new Error('Messenger not found'); |
|
|
|
} |
|
|
|
} |
|
|
|
// TODO: regex validation for txHash so we don't get garbage
|
|
|
|
// TODO: regex validation for txHash so we don't get garbage
|
|
|
|
const res = await this.messenger.send( |
|
|
|
const res = await this.messenger.send(RPCMethod.GetTransactionReceipt, txHash); |
|
|
|
RPCMethod.GetTransactionReceipt, |
|
|
|
|
|
|
|
txHash, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (res.isResult() && res.result !== null) { |
|
|
|
if (res.isResult() && res.result !== null) { |
|
|
|
this.receipt = res.result; |
|
|
|
this.receipt = res.result; |
|
|
@ -363,20 +335,22 @@ class Transaction { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
this.txStatus = TxStatus.PENDING; |
|
|
|
this.txStatus = TxStatus.PENDING; |
|
|
|
const currentBlock = await this.getBlockNumber(); |
|
|
|
const currentBlock = await this.getBlockNumber(); |
|
|
|
|
|
|
|
|
|
|
|
this.blockNumbers.push('0x' + currentBlock.toString('hex')); |
|
|
|
this.blockNumbers.push('0x' + currentBlock.toString('hex')); |
|
|
|
|
|
|
|
|
|
|
|
this.confirmationCheck += 1; |
|
|
|
this.confirmationCheck += 1; |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
this.txStatus = TxStatus.PENDING; |
|
|
|
|
|
|
|
const currentBlock = await this.getBlockNumber(); |
|
|
|
|
|
|
|
this.blockNumbers.push('0x' + currentBlock.toString('hex')); |
|
|
|
|
|
|
|
this.confirmationCheck += 1; |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async confirm( |
|
|
|
async confirm(txHash: string, maxAttempts: number = 20, interval: number = 1000) { |
|
|
|
txHash: string, |
|
|
|
|
|
|
|
maxAttempts: number = 20, |
|
|
|
|
|
|
|
interval: number = 1000, |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
if (this.messenger.provider instanceof HttpProvider) { |
|
|
|
if (this.messenger.provider instanceof HttpProvider) { |
|
|
|
this.txStatus = TxStatus.PENDING; |
|
|
|
this.txStatus = TxStatus.PENDING; |
|
|
|
const oldBlock = await this.getBlockNumber(); |
|
|
|
const oldBlock = await this.getBlockNumber(); |
|
|
@ -390,6 +364,7 @@ class Transaction { |
|
|
|
|
|
|
|
|
|
|
|
if (newBlock.gte(nextBlock)) { |
|
|
|
if (newBlock.gte(nextBlock)) { |
|
|
|
checkBlock = newBlock; |
|
|
|
checkBlock = newBlock; |
|
|
|
|
|
|
|
|
|
|
|
if (await this.trackTx(txHash)) { |
|
|
|
if (await this.trackTx(txHash)) { |
|
|
|
this.emitConfirm(this.txStatus); |
|
|
|
this.emitConfirm(this.txStatus); |
|
|
|
return this; |
|
|
|
return this; |
|
|
@ -410,9 +385,7 @@ class Transaction { |
|
|
|
} |
|
|
|
} |
|
|
|
this.txStatus = TxStatus.REJECTED; |
|
|
|
this.txStatus = TxStatus.REJECTED; |
|
|
|
this.emitConfirm(this.txStatus); |
|
|
|
this.emitConfirm(this.txStatus); |
|
|
|
throw new Error( |
|
|
|
throw new Error(`The transaction is still not confirmed after ${maxAttempts} attempts.`); |
|
|
|
`The transaction is still not confirmed after ${maxAttempts} attempts.`, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
try { |
|
|
|
try { |
|
|
|
if (await this.trackTx(txHash)) { |
|
|
|
if (await this.trackTx(txHash)) { |
|
|
@ -426,22 +399,18 @@ class Transaction { |
|
|
|
this.txStatus = TxStatus.REJECTED; |
|
|
|
this.txStatus = TxStatus.REJECTED; |
|
|
|
this.emitConfirm(this.txStatus); |
|
|
|
this.emitConfirm(this.txStatus); |
|
|
|
throw new Error( |
|
|
|
throw new Error( |
|
|
|
`The transaction is still not confirmed after ${maxAttempts * |
|
|
|
`The transaction is still not confirmed after ${maxAttempts * interval} mil seconds.`, |
|
|
|
interval} mil seconds.`,
|
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
socketConfirm( |
|
|
|
socketConfirm(txHash: string, maxAttempts: number = 20): Promise<Transaction> { |
|
|
|
txHash: string, |
|
|
|
|
|
|
|
maxAttempts: number = 20, |
|
|
|
|
|
|
|
): Promise<Transaction> { |
|
|
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
const newHeads = Promise.resolve(new NewHeaders(this.messenger)); |
|
|
|
const newHeads = Promise.resolve(new NewHeaders(this.messenger)); |
|
|
|
newHeads.then((p) => { |
|
|
|
newHeads.then((p) => { |
|
|
|
p.onData(async (data: any) => { |
|
|
|
p.onData(async (data: any) => { |
|
|
|
if (!this.blockNumbers.includes(data.number)) { |
|
|
|
if (!this.blockNumbers.includes(data.params.result.number)) { |
|
|
|
if (await this.trackTx(txHash)) { |
|
|
|
if (await this.trackTx(txHash)) { |
|
|
|
this.emitConfirm(this.txStatus); |
|
|
|
this.emitConfirm(this.txStatus); |
|
|
|
await p.unsubscribe(); |
|
|
|
await p.unsubscribe(); |
|
|
@ -492,10 +461,7 @@ class Transaction { |
|
|
|
} |
|
|
|
} |
|
|
|
async getBlockByNumber(blockNumber: string) { |
|
|
|
async getBlockByNumber(blockNumber: string) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
const block = await this.messenger.send(RPCMethod.GetBlockByNumber, [ |
|
|
|
const block = await this.messenger.send(RPCMethod.GetBlockByNumber, [blockNumber, true]); |
|
|
|
blockNumber, |
|
|
|
|
|
|
|
true, |
|
|
|
|
|
|
|
]); |
|
|
|
|
|
|
|
if (block.isError()) { |
|
|
|
if (block.isError()) { |
|
|
|
throw block.message; |
|
|
|
throw block.message; |
|
|
|
} |
|
|
|
} |
|
|
@ -519,4 +485,4 @@ class Transaction { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
export {Transaction}; |
|
|
|
export { Transaction }; |
|
|
|