fix(transaction):remake constructor

dev
neeboo 5 years ago
parent 79db6325d3
commit db85a67e0f
  1. 6
      e2e/src/blockchain.e2e.ts
  2. 12
      e2e/src/transaction.e2e.ts
  3. 14
      packages/harmony-account/src/hdnode.ts
  4. 38
      packages/harmony-transaction/src/transaction.ts
  5. 6
      packages/harmony-transaction/src/types.ts
  6. 9
      packages/harmony-utils/src/transformers.ts

@ -18,9 +18,9 @@ describe('e2e test blockchain', () => {
const versionNumber = parseInt(netVersion.result as string, 10);
expect(netVersion.result).toEqual(`${versionNumber}`);
});
it('should test hmy_protocalVersion', async () => {
const protocalVersion = await bc.getProtocalVersion();
expect(harmony.utils.isHex(protocalVersion.result)).toEqual(true);
it('should test hmy_protocolVersion', async () => {
const protocolVersion = await bc.getProtocolVersion();
expect(harmony.utils.isHex(protocolVersion.result)).toEqual(true);
});
// block chain info

@ -23,23 +23,21 @@ describe('test Transaction using SDK', () => {
newTxn.recover(txns[i].rawTransaction);
expect(newTxn.txParams.from).toEqual(txns[i].senderAddress);
expect(newTxn.txParams.to).toEqual(txns[i].receiverAddress);
expect(`0x${newTxn.txParams.gasLimit.toString('hex')}`).toEqual(
expect(`0x${newTxn.txParams.gasLimit.toString(16)}`).toEqual(
txns[i].gasLimit,
);
expect(`0x${newTxn.txParams.gasPrice.toString('hex')}`).toEqual(
expect(`0x${newTxn.txParams.gasPrice.toString(16)}`).toEqual(
txns[i].gasPrice,
);
expect(`0x${newTxn.txParams.value.toString('hex')}`).toEqual(
txns[i].value,
);
expect(`0x${newTxn.txParams.value.toString(16)}`).toEqual(txns[i].value);
expect(`${numberToHex(newTxn.txParams.nonce)}`).toEqual(txns[i].nonce);
}
});
it('should test signTransaction', async () => {
const txnObject = {
to: harmony.crypto.getAddress(receiver.Address).bech32,
value: new harmony.utils.Unit('100').asGwei().toWei(),
gasLimit: new harmony.utils.Unit('210000').asWei().toWei(),
value: '0x64',
gasLimit: '210000',
gasPrice: new harmony.utils.Unit('100').asGwei().toWei(),
};

@ -166,15 +166,15 @@ export class HDNode {
if (txParams.gas !== undefined && isHex(txParams.gas)) {
gasLimit = new BN(hexToNumber(txParams.gas)).lt(new BN(this.gasLimit))
? new Unit(hexToNumber(txParams.gas)).asWei().toWei()
: new Unit(this.gasLimit).asWei().toWei();
? txParams.gas
: this.gasLimit;
}
if (txParams.gasLimit !== undefined && isHex(txParams.gasLimit)) {
gasLimit = new BN(hexToNumber(txParams.gasLimit)).lt(
new BN(this.gasLimit),
)
? new Unit(hexToNumber(txParams.gasLimit)).asWei().toWei()
: new Unit(this.gasLimit).asWei().toWei();
? txParams.gasLimit
: this.gasLimit;
}
let gasPrice = new Unit('0').asWei().toWei();
@ -182,14 +182,14 @@ export class HDNode {
gasPrice = new BN(hexToNumber(txParams.gasPrice)).lt(
new BN(this.gasPrice),
)
? new Unit(hexToNumber(txParams.gasPrice)).asWei().toWei()
: new Unit(this.gasPrice).asWei().toWei();
? txParams.gasPrice
: this.gasPrice;
}
const value =
txParams.value !== undefined && isHex(txParams.value)
? txParams.value
: new Unit('0').asWei().toWei();
: '0';
const nonce =
txParams.nonce !== undefined && isHex(txParams.nonce)
? Number.parseInt(hexToNumber(txParams.nonce), 10)

@ -9,7 +9,7 @@ import {
getAddress,
HarmonyAddress,
} from '@harmony-js/crypto';
import {add0xToString, numberToHex, ChainType} from '@harmony-js/utils';
import {add0xToString, numberToHex, ChainType, Unit} from '@harmony-js/utils';
import {
Messenger,
RPCMethod,
@ -67,13 +67,22 @@ class Transaction {
this.id = params && params.id ? params.id : '0x';
this.from = params && params.from ? params.from : '0x';
this.nonce = params && params.nonce ? params.nonce : 0;
this.gasPrice = params && params.gasPrice ? params.gasPrice : new BN(0);
this.gasLimit = params && params.gasLimit ? params.gasLimit : new BN(0);
this.gasPrice =
params && params.gasPrice
? new Unit(params.gasPrice).asWei().toWei()
: new Unit(0).asWei().toWei();
this.gasLimit =
params && params.gasLimit
? new Unit(params.gasLimit).asWei().toWei()
: new Unit(0).asWei().toWei();
this.shardID = params && params.shardID ? params.shardID : 0;
this.toShardID = params && params.toShardID ? params.toShardID : 0;
this.to = params && params.to ? this.normalizeAddress(params.to) : '0x';
this.value = params && params.value ? params.value : new BN(0);
this.value =
params && params.value
? new Unit(params.value).asWei().toWei()
: new Unit(0).asWei().toWei();
this.data = params && params.data ? params.data : '0x';
// chainid should change with different network settings
this.chainId =
@ -201,12 +210,12 @@ class Transaction {
id: this.id || '0x',
from: this.from || '',
nonce: this.nonce || 0,
gasPrice: this.gasPrice || new BN(0),
gasLimit: this.gasLimit || new BN(0),
gasPrice: this.gasPrice || new Unit(0).asWei().toWei(),
gasLimit: this.gasLimit || new Unit(0).asWei().toWei(),
shardID: this.shardID || 0,
toShardID: this.toShardID || 0,
to: this.normalizeAddress(this.to) || '0x',
value: this.value || new BN(0),
value: this.value || new Unit(0).asWei().toWei(),
data: this.data || '0x',
chainId: this.chainId || 0,
rawTransaction: this.rawTransaction || '0x',
@ -218,12 +227,21 @@ class Transaction {
this.id = params && params.id ? params.id : '0x';
this.from = params && params.from ? params.from : '0x';
this.nonce = params && params.nonce ? params.nonce : 0;
this.gasPrice = params && params.gasPrice ? params.gasPrice : new BN(0);
this.gasLimit = params && params.gasLimit ? params.gasLimit : new BN(0);
this.gasPrice =
params && params.gasPrice
? new Unit(params.gasPrice).asWei().toWei()
: new Unit(0).asWei().toWei();
this.gasLimit =
params && params.gasLimit
? new Unit(params.gasLimit).asWei().toWei()
: new Unit(0).asWei().toWei();
this.shardID = params && params.shardID ? params.shardID : 0;
this.toShardID = params && params.toShardID ? params.toShardID : 0;
this.to = params && params.to ? this.normalizeAddress(params.to) : '0x';
this.value = params && params.value ? params.value : new BN(0);
this.value =
params && params.value
? new Unit(params.value).asWei().toWei()
: new Unit(0).asWei().toWei();
this.data = params && params.data ? params.data : '0x';
this.chainId = params && params.chainId ? params.chainId : 0;
this.rawTransaction =

@ -4,12 +4,12 @@ export interface TxParams {
from: string;
to: string;
nonce: number | string;
gasLimit: BN;
gasPrice: BN;
gasLimit: number | string | BN;
gasPrice: number | string | BN;
shardID: number | string;
toShardID: number | string;
data: string;
value: BN;
value: number | string | BN;
chainId: number;
rawTransaction: string;
unsignedRawTransaction: string;

@ -256,8 +256,15 @@ export class Unit {
wei: BN;
unit: BN | string;
constructor(str: BN | string) {
constructor(str: BN | string | number) {
if (!BN.isBN(str) && typeof str !== 'number' && isHex(str)) {
this.unit = hexToNumber(str);
} else if (!BN.isBN(str) && typeof str === 'number') {
this.unit = str.toString();
} else {
this.unit = str;
}
this.wei = new BN(this.unit);
}

Loading…
Cancel
Save