fix(transaction):remake constructor

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

@ -1,4 +1,4 @@
import { harmony } from './harmony';
import {harmony} from './harmony';
import demoAccounts from '../fixtures/testAccount.json';
@ -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
@ -30,23 +30,23 @@ describe('e2e test blockchain', () => {
expect(harmony.utils.isHex(res.result)).toEqual(true);
});
it('should test hmy_getBlockByNumber', async () => {
const res = await bc.getBlockByNumber({ blockNumber: 'latest' });
const res = await bc.getBlockByNumber({blockNumber: 'latest'});
const size = res.result.size;
expect(res.responseType).toEqual('raw');
expect(harmony.utils.isHex(size)).toEqual(true);
expect(checkBlockData(res.result)).toEqual(true);
const res2 = await bc.getBlockByNumber({ blockNumber: res.result.number });
const res2 = await bc.getBlockByNumber({blockNumber: res.result.number});
expect(res2.responseType).toEqual('raw');
expect(harmony.utils.isHex(res2.result.size)).toEqual(true);
expect(checkBlockData(res2.result)).toEqual(true);
const res3 = await bc.getBlockByNumber({ returnObject: true });
const res3 = await bc.getBlockByNumber({returnObject: true});
expect(res3.responseType).toEqual('raw');
expect(checkBlockData(res3.result)).toEqual(true);
});
it('should test hmy_getBlockByHash', async () => {
const latestBlock = await bc.getBlockByNumber({ blockNumber: 'latest' });
const res = await bc.getBlockByHash({ blockHash: latestBlock.result.hash });
const latestBlock = await bc.getBlockByNumber({blockNumber: 'latest'});
const res = await bc.getBlockByHash({blockHash: latestBlock.result.hash});
expect(res.responseType).toEqual('raw');
expect(latestBlock.result.hash).toEqual(res.result.hash);
expect(harmony.utils.isHex(res.result.size)).toEqual(true);
@ -55,7 +55,7 @@ describe('e2e test blockchain', () => {
// account related
it('should test hmy_getBalance', async () => {
const balance = await bc.getBalance({ address: testAccount.Address });
const balance = await bc.getBalance({address: testAccount.Address});
expect(harmony.utils.isHex(balance.result)).toEqual(true);
});
});
@ -83,6 +83,6 @@ function checkBlockData(data: any) {
transactionsRoot: [harmony.utils.isHash],
uncles: [harmony.utils.isArray],
},
{ transactions: [harmony.utils.isArray] },
{transactions: [harmony.utils.isArray]},
);
}

@ -1,8 +1,8 @@
import { harmony } from './harmony';
import {harmony} from './harmony';
// tslint:disable-next-line: no-implicit-dependencies
import { Transaction, TxStatus } from '@harmony-js/transaction';
import {Transaction, TxStatus} from '@harmony-js/transaction';
// tslint:disable-next-line: no-implicit-dependencies
import { isHash, numberToHex } from '@harmony-js/utils';
import {isHash, numberToHex} from '@harmony-js/utils';
import txnJsons from '../fixtures/transactions.json';
import demoAccounts from '../fixtures/testAccount.json';
@ -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 =

@ -1,15 +1,15 @@
import { BN, Signature } from '@harmony-js/crypto';
import {BN, Signature} from '@harmony-js/crypto';
export interface TxParams {
id: string;
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;

@ -1,5 +1,5 @@
import BN from 'bn.js';
import { isString, isNumber, isHex } from './validators';
import {isString, isNumber, isHex} from './validators';
export const enum Units {
wei = 'wei',
@ -256,8 +256,15 @@ export class Unit {
wei: BN;
unit: BN | string;
constructor(str: BN | string) {
this.unit = str;
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