chore(transformers):fix Unit

staking
neeboo 5 years ago
parent 8c709e0e1e
commit 299af73fe5
  1. 60
      e2e/fixtures/transactions.json
  2. 31
      e2e/src/transaction.e2e.ts
  3. 81
      e2e/src/txn_rpc.e2e.ts
  4. 2
      package.json
  5. 97
      packages/harmony-transaction/test/testSign.test.ts
  6. 1296
      packages/harmony-transaction/test/transactions.remake.fixture.json
  7. 35
      packages/harmony-utils/src/transformers.ts
  8. 500
      packages/harmony-utils/test/transformer.test.ts
  9. 47
      packages/harmony-utils/test/validators.test.ts
  10. 18
      scripts/jest/jest.e2e.config.js

@ -1,114 +1,114 @@
{ {
"transactions": [ "transactions": [
{ {
"senderAddress": "0x3e881F6C36A3A14a2D1816b0A5471d1caBB16F33", "senderAddress": "0x7c41E0668B551f4f902cFaec05B5Bdca68b124CE",
"receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52", "receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52",
"senderAddressBech32": "one186yp7mpk5ws55tgcz6c223carj4mzmenaal5wz", "senderAddressBech32": "one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7",
"receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8", "receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8",
"nonce": "0x0", "nonce": "0x0",
"value": "0x186a0", "value": "0x186a0",
"gasLimit": "0x5208", "gasLimit": "0x5208",
"gasPrice": "0x174876e800", "gasPrice": "0x174876e800",
"rawTransaction": "0xf8698085174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee52830186a0801ca06d9f530844b9fd5ad649c9151a53e104e9db9bfe41d68c3db7c1ea4e053e0c50a02ebd5d15d84e01232d3d933806c79c09f4df7be959b2883a0f54f085f0737e43" "rawTransaction": "0xf8698085174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee52830186a08027a0ab8229ff5d5240948098f26372eaed9ab2e9be23e8594b08e358ca56a47f8ae9a0084e5c4d1fec496af444423d8a714f65c079260ff01a1be1de7005dd424adf44"
}, },
{ {
"senderAddress": "0x3e881F6C36A3A14a2D1816b0A5471d1caBB16F33", "senderAddress": "0x7c41E0668B551f4f902cFaec05B5Bdca68b124CE",
"receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52", "receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52",
"senderAddressBech32": "one186yp7mpk5ws55tgcz6c223carj4mzmenaal5wz", "senderAddressBech32": "one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7",
"receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8", "receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8",
"nonce": "0x1", "nonce": "0x1",
"value": "0x30d40", "value": "0x30d40",
"gasLimit": "0x5208", "gasLimit": "0x5208",
"gasPrice": "0x174876e800", "gasPrice": "0x174876e800",
"rawTransaction": "0xf8690185174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee5283030d40801ba0ea2ad64ff72be2ea7f40b406a8ded60a74b03cc5209d0d3f9c7779daf1bb576ba01cbd1885f66c1f0bb2bc06c7ca40295b5bb1ec7cf0ebe7f5ec53fbfb29bc0373" "rawTransaction": "0xf8690185174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee5283030d408028a06c6a5900a44c13c4011ecf48d3149f7114e81d168e11422ce157475053083e94a039ce7ff13a2b79a81e98fb035a4068de54477d4bc671f4695fbd42f416a74cc3"
}, },
{ {
"senderAddress": "0x3e881F6C36A3A14a2D1816b0A5471d1caBB16F33", "senderAddress": "0x7c41E0668B551f4f902cFaec05B5Bdca68b124CE",
"receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52", "receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52",
"senderAddressBech32": "one186yp7mpk5ws55tgcz6c223carj4mzmenaal5wz", "senderAddressBech32": "one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7",
"receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8", "receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8",
"nonce": "0x2", "nonce": "0x2",
"value": "0x493e0", "value": "0x493e0",
"gasLimit": "0x5208", "gasLimit": "0x5208",
"gasPrice": "0x174876e800", "gasPrice": "0x174876e800",
"rawTransaction": "0xf8690285174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee52830493e0801ba05e5f8867869b4375a75baa71ed1e5871684cddd0bbe89dbe161e32819585652ea05d4de4b01a93d0c475c21b6f1bfe91509ab74cc86f6a6211dd76116ae0ba0ff3" "rawTransaction": "0xf8690285174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee52830493e08028a0ccc0c2abb00a3133c570cea9281f6a134aa9e3b287b64ec42b608102d70a3f97a02418b19b7b567e6bdfbf49afb6b0980ee16fcf199ef587b62ac2187dd966868f"
}, },
{ {
"senderAddress": "0x3e881F6C36A3A14a2D1816b0A5471d1caBB16F33", "senderAddress": "0x7c41E0668B551f4f902cFaec05B5Bdca68b124CE",
"receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52", "receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52",
"senderAddressBech32": "one186yp7mpk5ws55tgcz6c223carj4mzmenaal5wz", "senderAddressBech32": "one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7",
"receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8", "receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8",
"nonce": "0x3", "nonce": "0x3",
"value": "0x61a80", "value": "0x61a80",
"gasLimit": "0x5208", "gasLimit": "0x5208",
"gasPrice": "0x174876e800", "gasPrice": "0x174876e800",
"rawTransaction": "0xf8690385174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee5283061a80801ba00aedf5e3b2930da417576909e6567f1745fc90de1d0faac043b9853f2108ec6ca04be11e35cf2c0a70fb3b95628f743ca6491b52f8668b030d173bb949570e9965" "rawTransaction": "0xf8690385174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee5283061a808027a054705b19391ca26d6b13d78cd81fd48d172dbd7b3498edb575d0f4afaa67a8aaa044bb3117d943f6ecfff97d3d45df0ff9bdb1899c0b69f93e6e4a81685a4a7ea0"
}, },
{ {
"senderAddress": "0x3e881F6C36A3A14a2D1816b0A5471d1caBB16F33", "senderAddress": "0x7c41E0668B551f4f902cFaec05B5Bdca68b124CE",
"receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52", "receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52",
"senderAddressBech32": "one186yp7mpk5ws55tgcz6c223carj4mzmenaal5wz", "senderAddressBech32": "one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7",
"receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8", "receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8",
"nonce": "0x4", "nonce": "0x4",
"value": "0x7a120", "value": "0x7a120",
"gasLimit": "0x5208", "gasLimit": "0x5208",
"gasPrice": "0x174876e800", "gasPrice": "0x174876e800",
"rawTransaction": "0xf8690485174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee528307a120801ca016b5a3dfa0d41d6c818ff480dda2c947b97be29c4df29de16331cf42f453b52da05df999089b955280a143ce3f1df6542d83c580418aa66ad0fc23061079c9fbf0" "rawTransaction": "0xf8690485174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee528307a1208027a0bf688cfcdc88e07d700ccaab88a1856a33ea74ba10188dbde369cd326b629801a074db255ddf1b9cbf95980ff0433bdbb1f1282cf8a4cb0a02d240fba82992fb0d"
}, },
{ {
"senderAddress": "0x3e881F6C36A3A14a2D1816b0A5471d1caBB16F33", "senderAddress": "0x7c41E0668B551f4f902cFaec05B5Bdca68b124CE",
"receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52", "receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52",
"senderAddressBech32": "one186yp7mpk5ws55tgcz6c223carj4mzmenaal5wz", "senderAddressBech32": "one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7",
"receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8", "receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8",
"nonce": "0x5", "nonce": "0x5",
"value": "0x927c0", "value": "0x927c0",
"gasLimit": "0x5208", "gasLimit": "0x5208",
"gasPrice": "0x174876e800", "gasPrice": "0x174876e800",
"rawTransaction": "0xf8690585174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee52830927c0801ba00944e851189f2f9d69ef17bb767b35c10ab2a302c08e0781d9320ebf4c269391a079eb3547e3426bab7077b81b19d4063d10c3f5c5c7d6fb6c137cb5b3742f38ab" "rawTransaction": "0xf8690585174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee52830927c08028a092648d648460e9ebc56597dbe6cdccad3465f0f81aa47d00d1ae1b4319c72977a0565ea274e20717a3b9eafd4e4adfaba25590ced567087d034253a48098ad9590"
}, },
{ {
"senderAddress": "0x3e881F6C36A3A14a2D1816b0A5471d1caBB16F33", "senderAddress": "0x7c41E0668B551f4f902cFaec05B5Bdca68b124CE",
"receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52", "receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52",
"senderAddressBech32": "one186yp7mpk5ws55tgcz6c223carj4mzmenaal5wz", "senderAddressBech32": "one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7",
"receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8", "receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8",
"nonce": "0x6", "nonce": "0x6",
"value": "0xaae60", "value": "0xaae60",
"gasLimit": "0x5208", "gasLimit": "0x5208",
"gasPrice": "0x174876e800", "gasPrice": "0x174876e800",
"rawTransaction": "0xf8690685174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee52830aae60801ba00b67a57271f2fbb4290c09a4555053d799c0df0211062e19b6aebaa38c1a9619a00c5204b0c8f5ff5a37bf8fccb6592932c157d8621db35114e621e503de32d47e" "rawTransaction": "0xf8690685174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee52830aae608027a0522cee53d8b50d745c748f8d3be146d2c1c96a9b14f036f1fe6ab5e34a20afe0a00f6a048024bb665440b56414c1c3592c91a01287ef79fbf058d0dc10cc48c673"
}, },
{ {
"senderAddress": "0x3e881F6C36A3A14a2D1816b0A5471d1caBB16F33", "senderAddress": "0x7c41E0668B551f4f902cFaec05B5Bdca68b124CE",
"receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52", "receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52",
"senderAddressBech32": "one186yp7mpk5ws55tgcz6c223carj4mzmenaal5wz", "senderAddressBech32": "one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7",
"receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8", "receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8",
"nonce": "0x7", "nonce": "0x7",
"value": "0xc3500", "value": "0xc3500",
"gasLimit": "0x5208", "gasLimit": "0x5208",
"gasPrice": "0x174876e800", "gasPrice": "0x174876e800",
"rawTransaction": "0xf8690785174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee52830c3500801ca0683035ced391473fec92d5eca428c3383f59dcd5a4d3a5c81cab8560ff76d4cda00f01d8078d9f558d872921208d1b3d7698ea88832d6e3a39cd6ccedeb90f738b" "rawTransaction": "0xf8690785174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee52830c35008027a0a45f535b7ca45b8073a9cc5065a2cb58bc5574395a05f10883f71d6184d86d0ca04ef79ebcbef8aa266a51e8ebe2d2c203fd033c8032f2243a0ab6b14f8e45700f"
}, },
{ {
"senderAddress": "0x3e881F6C36A3A14a2D1816b0A5471d1caBB16F33", "senderAddress": "0x7c41E0668B551f4f902cFaec05B5Bdca68b124CE",
"receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52", "receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52",
"senderAddressBech32": "one186yp7mpk5ws55tgcz6c223carj4mzmenaal5wz", "senderAddressBech32": "one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7",
"receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8", "receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8",
"nonce": "0x8", "nonce": "0x8",
"value": "0xdbba0", "value": "0xdbba0",
"gasLimit": "0x5208", "gasLimit": "0x5208",
"gasPrice": "0x174876e800", "gasPrice": "0x174876e800",
"rawTransaction": "0xf8690885174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee52830dbba0801ca010af0f27c1a5663df2f81ee6b824fc2c040b698d386f2a9ff9a2dddfe8ad564aa04b2464808a1ea6bd4d7d3f37e5a189e0f8d5abc3b9c231da2bc0c31e27149388" "rawTransaction": "0xf8690885174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee52830dbba08027a08e63da2b95526b271536e1e53ccb81b5a580da234d59887cda8f69adc1d97dd7a0660db05c1685ba6978fa14c0da92ce19b89e6f973b0590cfed828082b962cdf3"
}, },
{ {
"senderAddress": "0x3e881F6C36A3A14a2D1816b0A5471d1caBB16F33", "senderAddress": "0x7c41E0668B551f4f902cFaec05B5Bdca68b124CE",
"receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52", "receiverAddress": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52",
"senderAddressBech32": "one186yp7mpk5ws55tgcz6c223carj4mzmenaal5wz", "senderAddressBech32": "one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7",
"receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8", "receiverAddressBech32": "one1n4ef3xmgwaap70laduwmq703j2ph8mjjucrkf8",
"nonce": "0x9", "nonce": "0x9",
"value": "0xf4240", "value": "0xf4240",
"gasLimit": "0x5208", "gasLimit": "0x5208",
"gasPrice": "0x174876e800", "gasPrice": "0x174876e800",
"rawTransaction": "0xf8690985174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee52830f4240801ca0ff540fe2800c12ed5bf03a48b33472e4ae0751cb910cb10496545667d70c7a90a0258a23052db64b0c638285405f0781611387791fe083e5df4da5a896a8585d4c" "rawTransaction": "0xf8690985174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee52830f42408028a022efd1decb6e80e88fd33f5e189f48105acc72f7e302931221183f86ca8a7015a02001dee44471a5397a4579f733b3a13a86ba1d6c78ad3312187513d6a424f72b"
} }
] ]
} }

@ -1,8 +1,8 @@
import {harmony} from './harmony'; import { harmony } from './harmony';
// tslint:disable-next-line: no-implicit-dependencies // 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 // 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 txnJsons from '../fixtures/transactions.json';
import demoAccounts from '../fixtures/testAccount.json'; import demoAccounts from '../fixtures/testAccount.json';
@ -23,12 +23,8 @@ describe('test Transaction using SDK', () => {
newTxn.recover(txns[i].rawTransaction); newTxn.recover(txns[i].rawTransaction);
expect(newTxn.txParams.from).toEqual(txns[i].senderAddress); expect(newTxn.txParams.from).toEqual(txns[i].senderAddress);
expect(newTxn.txParams.to).toEqual(txns[i].receiverAddress); expect(newTxn.txParams.to).toEqual(txns[i].receiverAddress);
expect(`0x${newTxn.txParams.gasLimit.toString(16)}`).toEqual( expect(`0x${newTxn.txParams.gasLimit.toString(16)}`).toEqual(txns[i].gasLimit);
txns[i].gasLimit, expect(`0x${newTxn.txParams.gasPrice.toString(16)}`).toEqual(txns[i].gasPrice);
);
expect(`0x${newTxn.txParams.gasPrice.toString(16)}`).toEqual(
txns[i].gasPrice,
);
expect(`0x${newTxn.txParams.value.toString(16)}`).toEqual(txns[i].value); expect(`0x${newTxn.txParams.value.toString(16)}`).toEqual(txns[i].value);
expect(`${numberToHex(newTxn.txParams.nonce)}`).toEqual(txns[i].nonce); expect(`${numberToHex(newTxn.txParams.nonce)}`).toEqual(txns[i].nonce);
} }
@ -81,10 +77,9 @@ describe('test Transaction using SDK', () => {
expect(checkTransactionReceipt(receipt)).toEqual(true); expect(checkTransactionReceipt(receipt)).toEqual(true);
}) })
.on('confirmation', (confirmation) => { .on('confirmation', (confirmation) => {
expect( expect(confirmation === TxStatus.REJECTED || confirmation === TxStatus.CONFIRMED).toBe(
confirmation === TxStatus.REJECTED || true,
confirmation === TxStatus.CONFIRMED, );
).toBe(true);
}) })
.on('error', (error) => { .on('error', (error) => {
expect(error).toBeTruthy(); expect(error).toBeTruthy();
@ -104,20 +99,20 @@ function checkTransactionReceipt(data: any) {
blockNumber: [harmony.utils.isHex], blockNumber: [harmony.utils.isHex],
contractAddress: [ contractAddress: [
// tslint:disable-next-line: no-shadowed-variable // tslint:disable-next-line: no-shadowed-variable
(data: any) => data === null || harmony.utils.isAddress, (data: any) => data === null || harmony.utils.isValidAddress,
], ],
cumulativeGasUsed: [harmony.utils.isHex], cumulativeGasUsed: [harmony.utils.isHex],
from: [harmony.utils.isAddress], from: [harmony.utils.isValidAddress],
gasUsed: [harmony.utils.isHex], gasUsed: [harmony.utils.isHex],
logs: [harmony.utils.isArray], logs: [harmony.utils.isArray],
logsBloom: [harmony.utils.isHex], logsBloom: [harmony.utils.isHex],
root: [harmony.utils.isHash],
shardID: [harmony.utils.isNumber], shardID: [harmony.utils.isNumber],
// tslint:disable-next-line: no-shadowed-variable // tslint:disable-next-line: no-shadowed-variable
to: [(data: any) => data === '0x' || harmony.utils.isAddress], to: [(data: any) => data === '0x' || harmony.utils.isValidAddress],
transactionHash: [harmony.utils.isHash], transactionHash: [harmony.utils.isHash],
transactionIndex: [harmony.utils.isHex], transactionIndex: [harmony.utils.isHex],
}, },
{}, { root: [harmony.utils.isHash] },
); );
} }

@ -17,10 +17,7 @@ describe('e2e test transactions by RPC Method', () => {
const { transactions } = txnJsons; const { transactions } = txnJsons;
for (const txn of transactions) { for (const txn of transactions) {
const sent = await messenger.send( const sent = await messenger.send('hmy_sendRawTransaction', txn.rawTransaction);
'hmy_sendRawTransaction',
txn.rawTransaction,
);
expect(harmony.utils.isHash(sent.result)).toEqual(true); expect(harmony.utils.isHash(sent.result)).toEqual(true);
txnHashesFixtures.push(sent.result); txnHashesFixtures.push(sent.result);
} }
@ -46,60 +43,46 @@ describe('e2e test transactions by RPC Method', () => {
it('should test hmy_getTransactionByBlockHashAndIndex', async () => { it('should test hmy_getTransactionByBlockHashAndIndex', async () => {
for (const some of transactionInfoList) { for (const some of transactionInfoList) {
const transactionInfo: TransactionInfo = some; const transactionInfo: TransactionInfo = some;
const txnDetail: any = await harmony.blockchain.getTransactionByBlockHashAndIndex( const txnDetail: any = await harmony.blockchain.getTransactionByBlockHashAndIndex({
{ blockHash: transactionInfo.blockHash,
blockHash: transactionInfo.blockHash, index: transactionInfo.index,
index: transactionInfo.index, });
},
);
if (txnDetail.result !== null) { if (txnDetail.result !== null) {
expect(checkTransactionDetail(txnDetail.result)).toEqual(true); expect(checkTransactionDetail(txnDetail.result)).toEqual(true);
expect(txnDetail.result.blockHash).toEqual(transactionInfo.blockHash); expect(txnDetail.result.blockHash).toEqual(transactionInfo.blockHash);
expect(txnDetail.result.transactionIndex).toEqual( expect(txnDetail.result.transactionIndex).toEqual(transactionInfo.index);
transactionInfo.index,
);
} }
} }
}); });
it('should test hmy_getTransactionByBlockNumberAndIndex', async () => { it('should test hmy_getTransactionByBlockNumberAndIndex', async () => {
for (const some of transactionInfoList) { for (const some of transactionInfoList) {
const transactionInfo: TransactionInfo = some; const transactionInfo: TransactionInfo = some;
const txnDetail: any = await harmony.blockchain.getTransactionByBlockNumberAndIndex( const txnDetail: any = await harmony.blockchain.getTransactionByBlockNumberAndIndex({
{ blockNumber: transactionInfo.blockNumber,
blockNumber: transactionInfo.blockNumber, index: transactionInfo.index,
index: transactionInfo.index, });
},
);
if (txnDetail.result !== null) { if (txnDetail.result !== null) {
expect(checkTransactionDetail(txnDetail.result)).toEqual(true); expect(checkTransactionDetail(txnDetail.result)).toEqual(true);
expect(txnDetail.result.blockNumber).toEqual( expect(txnDetail.result.blockNumber).toEqual(transactionInfo.blockNumber);
transactionInfo.blockNumber, expect(txnDetail.result.transactionIndex).toEqual(transactionInfo.index);
);
expect(txnDetail.result.transactionIndex).toEqual(
transactionInfo.index,
);
} }
} }
}); });
it('should test hmy_getTransactionCountByHash', async () => { it('should test hmy_getTransactionCountByHash', async () => {
for (const some of transactionInfoList) { for (const some of transactionInfoList) {
const transactionInfo: TransactionInfo = some; const transactionInfo: TransactionInfo = some;
const txnCount: any = await harmony.blockchain.getBlockTransactionCountByHash( const txnCount: any = await harmony.blockchain.getBlockTransactionCountByHash({
{ blockHash: transactionInfo.blockHash,
blockHash: transactionInfo.blockHash, });
},
);
expect(harmony.utils.isHex(txnCount.result)).toEqual(true); expect(harmony.utils.isHex(txnCount.result)).toEqual(true);
} }
}); });
it('should test hmy_getTransactionCountByNumber', async () => { it('should test hmy_getTransactionCountByNumber', async () => {
for (const some of transactionInfoList) { for (const some of transactionInfoList) {
const transactionInfo: TransactionInfo = some; const transactionInfo: TransactionInfo = some;
const txnCount: any = await harmony.blockchain.getBlockTransactionCountByNumber( const txnCount: any = await harmony.blockchain.getBlockTransactionCountByNumber({
{ blockNumber: transactionInfo.blockNumber,
blockNumber: transactionInfo.blockNumber, });
},
);
expect(harmony.utils.isHex(txnCount.result)).toEqual(true); expect(harmony.utils.isHex(txnCount.result)).toEqual(true);
} }
}); });
@ -111,6 +94,7 @@ describe('e2e test transactions by RPC Method', () => {
const receipt: any = await harmony.blockchain.getTransactionReceipt({ const receipt: any = await harmony.blockchain.getTransactionReceipt({
txnHash, txnHash,
}); });
if (receipt.result !== null) { if (receipt.result !== null) {
expect(checkTransactionReceipt(receipt.result)).toEqual(true); expect(checkTransactionReceipt(receipt.result)).toEqual(true);
expect(harmony.crypto.getAddress(receipt.result.from).checksum).toEqual( expect(harmony.crypto.getAddress(receipt.result.from).checksum).toEqual(
@ -119,15 +103,9 @@ describe('e2e test transactions by RPC Method', () => {
expect(harmony.crypto.getAddress(receipt.result.to).checksum).toEqual( expect(harmony.crypto.getAddress(receipt.result.to).checksum).toEqual(
transactions[i].receiverAddress, transactions[i].receiverAddress,
); );
expect(receipt.result.blockHash).toEqual( expect(receipt.result.blockHash).toEqual(transactionInfoList[i].blockHash);
transactionInfoList[i].blockHash, expect(receipt.result.blockNumber).toEqual(transactionInfoList[i].blockNumber);
); expect(receipt.result.transactionIndex).toEqual(transactionInfoList[i].index);
expect(receipt.result.blockNumber).toEqual(
transactionInfoList[i].blockNumber,
);
expect(receipt.result.transactionIndex).toEqual(
transactionInfoList[i].index,
);
} }
} }
}); });
@ -151,7 +129,8 @@ function checkTransactionDetail(data: any) {
{ {
blockHash: [harmony.utils.isHash], blockHash: [harmony.utils.isHash],
blockNumber: [harmony.utils.isHex], blockNumber: [harmony.utils.isHex],
from: [harmony.utils.isAddress], // tslint:disable-next-line: no-shadowed-variable
from: [harmony.utils.isValidAddress],
gas: [harmony.utils.isHex], gas: [harmony.utils.isHex],
gasPrice: [harmony.utils.isHex], gasPrice: [harmony.utils.isHex],
hash: [harmony.utils.isHash], hash: [harmony.utils.isHash],
@ -159,7 +138,7 @@ function checkTransactionDetail(data: any) {
input: [(data: any) => data === '0x' || harmony.utils.isHex(data)], input: [(data: any) => data === '0x' || harmony.utils.isHex(data)],
nonce: [harmony.utils.isHex], nonce: [harmony.utils.isHex],
// tslint:disable-next-line: no-shadowed-variable // tslint:disable-next-line: no-shadowed-variable
to: [(data: any) => data === '0x' || harmony.utils.isAddress(data)], to: [(data: any) => data === '0x' || harmony.utils.isValidAddress(data)],
transactionIndex: [harmony.utils.isHex], transactionIndex: [harmony.utils.isHex],
value: [harmony.utils.isHex], value: [harmony.utils.isHex],
v: [harmony.utils.isHex], v: [harmony.utils.isHex],
@ -177,20 +156,20 @@ function checkTransactionReceipt(data: any) {
blockNumber: [harmony.utils.isHex], blockNumber: [harmony.utils.isHex],
contractAddress: [ contractAddress: [
// tslint:disable-next-line: no-shadowed-variable // tslint:disable-next-line: no-shadowed-variable
(data: any) => data === null || harmony.utils.isAddress, (data: any) => data === null || harmony.utils.isValidAddress,
], ],
cumulativeGasUsed: [harmony.utils.isHex], cumulativeGasUsed: [harmony.utils.isHex],
from: [harmony.utils.isAddress], from: [harmony.utils.isValidAddress],
gasUsed: [harmony.utils.isHex], gasUsed: [harmony.utils.isHex],
logs: [harmony.utils.isArray], logs: [harmony.utils.isArray],
logsBloom: [harmony.utils.isHex], logsBloom: [harmony.utils.isHex],
root: [harmony.utils.isHash],
shardID: [harmony.utils.isNumber], shardID: [harmony.utils.isNumber],
// tslint:disable-next-line: no-shadowed-variable // tslint:disable-next-line: no-shadowed-variable
to: [(data: any) => data === '0x' || harmony.utils.isAddress], to: [(data: any) => data === '0x' || harmony.utils.isValidAddress],
transactionHash: [harmony.utils.isHash], transactionHash: [harmony.utils.isHash],
transactionIndex: [harmony.utils.isHex], transactionIndex: [harmony.utils.isHex],
}, },
{ blockHash: [harmony.utils.isHash] }, { blockHash: [harmony.utils.isHash], root: [harmony.utils.isHash] },
); );
} }

@ -24,7 +24,7 @@
"test:src": "cross-env NODE_ENV=test jest --config ./scripts/jest/jest.src.config.js --verbose --runInBand --no-cache", "test:src": "cross-env NODE_ENV=test jest --config ./scripts/jest/jest.src.config.js --verbose --runInBand --no-cache",
"test:build": "cross-env TEST_ENV=unit jest -c jest.build.config.js", "test:build": "cross-env TEST_ENV=unit jest -c jest.build.config.js",
"test:integration": "cross-env TEST_ENV=integration jest -c jest.iconfig.js --runInBand --verbose --collectCoverage=false", "test:integration": "cross-env TEST_ENV=integration jest -c jest.iconfig.js --runInBand --verbose --collectCoverage=false",
"test:e2e": "cross-env TEST_ENV=e2e jest -c ./scripts/jest/jest.e2e.config.js --runInBand --verbose --collectCoverage=false", "test:e2e": "cross-env TEST_ENV=e2e jest -c ./scripts/jest/jest.e2e.config.js --runInBand --verbose",
"docs:vue": "ts-node -P scripts/tsconfig.json scripts/docs.ts vuepress", "docs:vue": "ts-node -P scripts/tsconfig.json scripts/docs.ts vuepress",
"docs:gitbook": "ts-node -P scripts/tsconfig.json scripts/docs.ts gitbook", "docs:gitbook": "ts-node -P scripts/tsconfig.json scripts/docs.ts gitbook",
"docs:docusaurus": "ts-node -P scripts/tsconfig.json scripts/docs.ts docusaurus", "docs:docusaurus": "ts-node -P scripts/tsconfig.json scripts/docs.ts docusaurus",

@ -2,10 +2,12 @@ import { Transaction } from '../src/transaction';
import { RLPSign } from '../src/utils'; import { RLPSign } from '../src/utils';
import { TxStatus } from '../src/types'; import { TxStatus } from '../src/types';
import { HttpProvider, Messenger } from '@harmony-js/network'; import { HttpProvider, Messenger } from '@harmony-js/network';
import { isAddress, ChainType, hexToBN, ChainID } from '@harmony-js/utils'; import { isAddress, ChainType, hexToBN, ChainID, isValidAddress } from '@harmony-js/utils';
import { getAddressFromPrivateKey, BN, getAddress } from '@harmony-js/crypto'; import { getAddressFromPrivateKey, getAddress } from '@harmony-js/crypto';
import txnVectors from './transactions.fixture.json'; import txnVectors from './transactions.fixture.json';
import reTxnVectors from './transactions.remake.fixture.json';
import { TransactionFactory } from '../src';
const provider = new HttpProvider('http://localhost:9500'); const provider = new HttpProvider('http://localhost:9500');
@ -14,7 +16,7 @@ describe('test sign tranction', () => {
const ethMessenger = new Messenger(provider, ChainType.Ethereum, ChainID.Default); const ethMessenger = new Messenger(provider, ChainType.Ethereum, ChainID.Default);
// tslint:disable-next-line: prefer-for-of // tslint:disable-next-line: prefer-for-of
for (let i = 0; i < txnVectors.length; i += 1) { for (let i = 0; i < txnVectors.length; i += 1) {
const vector = txnVectors[i]; const vector: any = txnVectors[i];
const address = getAddressFromPrivateKey(vector.privateKey); const address = getAddressFromPrivateKey(vector.privateKey);
expect(isAddress(address)).toEqual(true); expect(isAddress(address)).toEqual(true);
@ -22,14 +24,12 @@ describe('test sign tranction', () => {
const transaction: Transaction = new Transaction( const transaction: Transaction = new Transaction(
{ {
gasLimit: gasLimit: vector.gasLimit,
vector.gasLimit && vector.gasLimit !== '0x' ? hexToBN(vector.gasLimit) : new BN(0), gasPrice: vector.gasPrice,
gasPrice: to: vector.to ? getAddress(vector.to).checksum : '0x',
vector.gasPrice && vector.gasPrice !== '0x' ? hexToBN(vector.gasPrice) : new BN(0), value: vector.value,
to: vector.to && vector.to !== '0x' ? getAddress(vector.to).checksum : '0x', data: vector.data,
value: vector.value && vector.value !== '0x' ? hexToBN(vector.value) : new BN(0), nonce: vector.nonce,
data: vector.data || '0x',
nonce: vector.nonce && vector.nonce !== '0x' ? hexToBN(vector.nonce).toNumber() : 0,
}, },
ethMessenger, ethMessenger,
TxStatus.INTIALIZED, TxStatus.INTIALIZED,
@ -42,6 +42,38 @@ describe('test sign tranction', () => {
} }
}); });
it('should test sign transaction with Harmony settings', () => {
const hmyMessenger = new Messenger(provider, ChainType.Harmony, ChainID.Default);
// tslint:disable-next-line: prefer-for-of
for (let i = 0; i < reTxnVectors.length; i += 1) {
const vector: any = reTxnVectors[i];
const address = getAddressFromPrivateKey(vector.privateKey);
expect(isValidAddress(address)).toEqual(true);
expect(address).toEqual(vector.accountAddress);
expect(getAddress(address).bech32).toEqual(vector.accountBech32Address);
const transaction: Transaction = new Transaction(
{
gasLimit: vector.gasLimit,
gasPrice: vector.gasPrice,
to: vector.to ? vector.toChecksumAddress : '0x',
value: vector.value,
data: vector.data,
nonce: vector.nonce,
},
hmyMessenger,
TxStatus.INTIALIZED,
);
const unsigned = transaction.getRLPUnsigned();
expect(unsigned[0]).toEqual(vector.unsignedTransaction);
const signed = RLPSign(transaction, vector.privateKey);
expect(signed[1]).toEqual(vector.signedTransaction);
}
});
it('should test recover from ETHSignedtransaction', () => { it('should test recover from ETHSignedtransaction', () => {
const ethMessenger = new Messenger(provider, ChainType.Ethereum, ChainID.Default); const ethMessenger = new Messenger(provider, ChainType.Ethereum, ChainID.Default);
// tslint:disable-next-line: prefer-for-of // tslint:disable-next-line: prefer-for-of
@ -77,4 +109,47 @@ describe('test sign tranction', () => {
expect(transaction.txParams.from.toLowerCase()).toEqual(vector.accountAddress.toLowerCase()); expect(transaction.txParams.from.toLowerCase()).toEqual(vector.accountAddress.toLowerCase());
} }
}); });
it('should test recover from HarmonySignedTransaction', () => {
const hmyMessenger = new Messenger(provider, ChainType.Harmony, ChainID.Default);
// tslint:disable-next-line: prefer-for-of
for (let i = 0; i < reTxnVectors.length; i += 1) {
const vector = reTxnVectors[i];
const transaction: Transaction = new Transaction({});
transaction.setMessenger(hmyMessenger);
transaction.recover(vector.signedTransaction);
if (vector.gasLimit && vector.gasLimit !== '0x') {
expect(transaction.txParams.gasLimit.toString()).toEqual(
hexToBN(vector.gasLimit).toString(),
);
}
if (vector.gasPrice && vector.gasPrice !== '0x') {
expect(transaction.txParams.gasPrice.toString()).toEqual(
hexToBN(vector.gasPrice).toString(),
);
}
if (vector.nonce && vector.nonce !== '0x') {
expect(transaction.txParams.nonce).toEqual(hexToBN(vector.nonce).toNumber());
}
if (vector.data) {
expect(transaction.txParams.data).toEqual(vector.data);
}
if (vector.value && vector.value !== '0x') {
expect(transaction.txParams.value.toString()).toEqual(hexToBN(vector.value).toString());
}
if (vector.to && vector.to !== '0x') {
expect(transaction.txParams.to).toEqual(getAddress(vector.to).checksum);
}
expect(transaction.txParams.from.toLowerCase()).toEqual(vector.accountAddress.toLowerCase());
}
});
it('should test transactionFactory', () => {
const hmyMessenger = new Messenger(provider, ChainType.Harmony, ChainID.Default);
const factory = new TransactionFactory(hmyMessenger);
const txn = factory.newTx({}, false);
expect(txn.getRLPUnsigned()[0]).toBeTruthy();
const txn2 = factory.newTx({}, true);
expect(txn2.getRLPUnsigned()[0]).toBeTruthy();
});
}); });

@ -1,9 +1,9 @@
import BN from 'bn.js'; import BN from 'bn.js';
import {isString, isNumber, isHex} from './validators'; import { isString, isNumber, isHex } from './validators';
export const enum Units { export const enum Units {
wei = 'wei', wei = 'wei',
kwei = 'kwei', Kwei = 'Kwei',
Mwei = 'Mwei', Mwei = 'Mwei',
Gwei = 'Gwei', Gwei = 'Gwei',
szabo = 'szabo', szabo = 'szabo',
@ -17,7 +17,7 @@ export const enum Units {
export const unitMap = new Map([ export const unitMap = new Map([
[Units.wei, '1'], [Units.wei, '1'],
[Units.kwei, '1000'], // 1e3 wei [Units.Kwei, '1000'], // 1e3 wei
[Units.Mwei, '1000000'], // 1e6 wei [Units.Mwei, '1000000'], // 1e6 wei
[Units.Gwei, '1000000000'], // 1e9 wei [Units.Gwei, '1000000000'], // 1e9 wei
[Units.szabo, '1000000000000'], // 1e12 wei [Units.szabo, '1000000000000'], // 1e12 wei
@ -33,10 +33,7 @@ const DEFAULT_OPTIONS = {
pad: false, pad: false,
}; };
export const numberToString = ( export const numberToString = (obj: BN | number | string, radix: number = 10): string => {
obj: BN | number | string,
radix: number = 10,
): string => {
if (BN.isBN(obj)) { if (BN.isBN(obj)) {
return obj.toString(radix); return obj.toString(radix);
} else if (isNumber(obj)) { } else if (isNumber(obj)) {
@ -148,7 +145,7 @@ export const toWei = (input: BN | string, unit: Units): BN => {
fraction = '0'; fraction = '0';
} }
if (fraction.length > baseNumDecimals) { if (fraction.length > baseNumDecimals) {
throw new Error(`Cannot convert ${inputStr} to Qa.`); throw new Error(`Cannot convert ${inputStr} to wei.`);
} }
while (fraction.length < baseNumDecimals) { while (fraction.length < baseNumDecimals) {
@ -169,11 +166,7 @@ export const toWei = (input: BN | string, unit: Units): BN => {
} }
}; };
export const fromWei = ( export const fromWei = (wei: BN | string, unit: Units, options: any = DEFAULT_OPTIONS): string => {
wei: BN | string,
unit: Units,
options: any = DEFAULT_OPTIONS,
): string => {
try { try {
const weiBN: BN = !BN.isBN(wei) ? new BN(wei) : wei; const weiBN: BN = !BN.isBN(wei) ? new BN(wei) : wei;
@ -261,6 +254,8 @@ export class Unit {
this.unit = hexToNumber(str); this.unit = hexToNumber(str);
} else if (!BN.isBN(str) && typeof str === 'number') { } else if (!BN.isBN(str) && typeof str === 'number') {
this.unit = str.toString(); this.unit = str.toString();
} else if (str === '0x') {
this.unit = hexToNumber('0x0');
} else { } else {
this.unit = str; this.unit = str;
} }
@ -273,7 +268,7 @@ export class Unit {
return this; return this;
} }
asKwei() { asKwei() {
this.wei = toWei(this.unit, Units.kwei); this.wei = toWei(this.unit, Units.Kwei);
return this; return this;
} }
asMwei() { asMwei() {
@ -309,17 +304,21 @@ export class Unit {
return this; return this;
} }
asTether() { asTether() {
this.wei = toWei(this.unit, Units.Gether); this.wei = toWei(this.unit, Units.Tether);
return this; return this;
} }
toWei() { toWei() {
return this.wei; if (this.wei) {
return this.wei;
} else {
throw new Error('error transforming');
}
} }
toKwei() { toKwei() {
if (this.wei) { if (this.wei) {
return fromWei(this.wei, Units.kwei); return fromWei(this.wei, Units.Kwei);
} else { } else {
throw new Error('error transforming'); throw new Error('error transforming');
} }
@ -345,7 +344,7 @@ export class Unit {
throw new Error('error transforming'); throw new Error('error transforming');
} }
} }
tofinney() { toFinney() {
if (this.wei) { if (this.wei) {
return fromWei(this.wei, Units.finney); return fromWei(this.wei, Units.finney);
} else { } else {

@ -0,0 +1,500 @@
import { BN } from '@harmony-js/crypto';
import * as transformers from '../src/transformers';
describe('test transformers', () => {
it('should test unit', () => {
const { Unit } = transformers;
const stringConst = '1';
const unit1 = new Unit(stringConst);
const unit2 = new Unit(1);
const unit3 = new Unit('0x1');
const unit4 = new Unit('0x');
expect(unit4.unit).toEqual('0');
const fromWei = new Unit(stringConst).asWei();
const fromKwei = new Unit(stringConst).asKwei();
const fromMwei = new Unit(stringConst).asMwei();
const fromGwei = new Unit(stringConst).asGwei();
const fromSzabo = new Unit(stringConst).asSzabo();
const fromFinney = new Unit(stringConst).asFinney();
const fromEther = new Unit(stringConst).asEther();
const fromKether = new Unit(stringConst).asKether();
const fromMether = new Unit(stringConst).asMether();
const fromGether = new Unit(stringConst).asGether();
const fromTether = new Unit(stringConst).asTether();
expect(fromWei.toWei()).toEqual(new BN(stringConst));
expect(fromWei.toWeiString()).toEqual(stringConst);
expect(fromWei.toKwei()).toEqual(`0.00${stringConst}`);
expect(fromWei.toMwei()).toEqual(`0.00000${stringConst}`);
expect(fromWei.toGwei()).toEqual(`0.00000000${stringConst}`);
expect(fromWei.toSzabo()).toEqual(`0.00000000000${stringConst}`);
expect(fromWei.toFinney()).toEqual(`0.00000000000000${stringConst}`);
expect(fromWei.toEther()).toEqual(`0.00000000000000000${stringConst}`);
expect(fromWei.toKether()).toEqual(`0.00000000000000000000${stringConst}`);
expect(fromWei.toMether()).toEqual(`0.00000000000000000000000${stringConst}`);
expect(fromWei.toGether()).toEqual(`0.00000000000000000000000000${stringConst}`);
expect(fromWei.toTether()).toEqual(`0.00000000000000000000000000000${stringConst}`);
expect(fromWei.toHex()).toEqual(`0x${new BN(stringConst).toString('hex')}`);
expect(fromKwei.toWei()).toEqual(new BN(`${stringConst}000`));
expect(fromKwei.toWeiString()).toEqual(`${stringConst}000`);
expect(fromKwei.toKwei()).toEqual(`${stringConst}`);
expect(fromKwei.toMwei()).toEqual(`0.00${stringConst}`);
expect(fromKwei.toGwei()).toEqual(`0.00000${stringConst}`);
expect(fromKwei.toSzabo()).toEqual(`0.00000000${stringConst}`);
expect(fromKwei.toFinney()).toEqual(`0.00000000000${stringConst}`);
expect(fromKwei.toEther()).toEqual(`0.00000000000000${stringConst}`);
expect(fromKwei.toKether()).toEqual(`0.00000000000000000${stringConst}`);
expect(fromKwei.toMether()).toEqual(`0.00000000000000000000${stringConst}`);
expect(fromKwei.toGether()).toEqual(`0.00000000000000000000000${stringConst}`);
expect(fromKwei.toTether()).toEqual(`0.00000000000000000000000000${stringConst}`);
expect(fromKwei.toHex()).toEqual(`0x${new BN(`${stringConst}000`).toString('hex')}`);
expect(fromMwei.toWei()).toEqual(new BN(`${stringConst}000000`));
expect(fromMwei.toWeiString()).toEqual(`${stringConst}000000`);
expect(fromMwei.toKwei()).toEqual(`${stringConst}000`);
expect(fromMwei.toMwei()).toEqual(`${stringConst}`);
expect(fromMwei.toGwei()).toEqual(`0.00${stringConst}`);
expect(fromMwei.toSzabo()).toEqual(`0.00000${stringConst}`);
expect(fromMwei.toFinney()).toEqual(`0.00000000${stringConst}`);
expect(fromMwei.toEther()).toEqual(`0.00000000000${stringConst}`);
expect(fromMwei.toKether()).toEqual(`0.00000000000000${stringConst}`);
expect(fromMwei.toMether()).toEqual(`0.00000000000000000${stringConst}`);
expect(fromMwei.toGether()).toEqual(`0.00000000000000000000${stringConst}`);
expect(fromMwei.toTether()).toEqual(`0.00000000000000000000000${stringConst}`);
expect(fromMwei.toHex()).toEqual(`0x${new BN(`${stringConst}000000`).toString('hex')}`);
expect(fromGwei.toWei()).toEqual(new BN(`${stringConst}000000000`));
expect(fromGwei.toWeiString()).toEqual(`${stringConst}000000000`);
expect(fromGwei.toKwei()).toEqual(`${stringConst}000000`);
expect(fromGwei.toMwei()).toEqual(`${stringConst}000`);
expect(fromGwei.toGwei()).toEqual(`${stringConst}`);
expect(fromGwei.toSzabo()).toEqual(`0.00${stringConst}`);
expect(fromGwei.toFinney()).toEqual(`0.00000${stringConst}`);
expect(fromGwei.toEther()).toEqual(`0.00000000${stringConst}`);
expect(fromGwei.toKether()).toEqual(`0.00000000000${stringConst}`);
expect(fromGwei.toMether()).toEqual(`0.00000000000000${stringConst}`);
expect(fromGwei.toGether()).toEqual(`0.00000000000000000${stringConst}`);
expect(fromGwei.toTether()).toEqual(`0.00000000000000000000${stringConst}`);
expect(fromGwei.toHex()).toEqual(`0x${new BN(`${stringConst}000000000`).toString('hex')}`);
expect(fromSzabo.toWei()).toEqual(new BN(`${stringConst}000000000000`));
expect(fromSzabo.toWeiString()).toEqual(`${stringConst}000000000000`);
expect(fromSzabo.toKwei()).toEqual(`${stringConst}000000000`);
expect(fromSzabo.toMwei()).toEqual(`${stringConst}000000`);
expect(fromSzabo.toGwei()).toEqual(`${stringConst}000`);
expect(fromSzabo.toSzabo()).toEqual(`${stringConst}`);
expect(fromSzabo.toFinney()).toEqual(`0.00${stringConst}`);
expect(fromSzabo.toEther()).toEqual(`0.00000${stringConst}`);
expect(fromSzabo.toKether()).toEqual(`0.00000000${stringConst}`);
expect(fromSzabo.toMether()).toEqual(`0.00000000000${stringConst}`);
expect(fromSzabo.toGether()).toEqual(`0.00000000000000${stringConst}`);
expect(fromSzabo.toTether()).toEqual(`0.00000000000000000${stringConst}`);
expect(fromSzabo.toHex()).toEqual(`0x${new BN(`${stringConst}000000000000`).toString('hex')}`);
expect(fromFinney.toWei()).toEqual(new BN(`${stringConst}000000000000000`));
expect(fromFinney.toWeiString()).toEqual(`${stringConst}000000000000000`);
expect(fromFinney.toKwei()).toEqual(`${stringConst}000000000000`);
expect(fromFinney.toMwei()).toEqual(`${stringConst}000000000`);
expect(fromFinney.toGwei()).toEqual(`${stringConst}000000`);
expect(fromFinney.toSzabo()).toEqual(`${stringConst}000`);
expect(fromFinney.toFinney()).toEqual(`${stringConst}`);
expect(fromFinney.toEther()).toEqual(`0.00${stringConst}`);
expect(fromFinney.toKether()).toEqual(`0.00000${stringConst}`);
expect(fromFinney.toMether()).toEqual(`0.00000000${stringConst}`);
expect(fromFinney.toGether()).toEqual(`0.00000000000${stringConst}`);
expect(fromFinney.toTether()).toEqual(`0.00000000000000${stringConst}`);
expect(fromFinney.toHex()).toEqual(
`0x${new BN(`${stringConst}000000000000000`).toString('hex')}`,
);
expect(fromEther.toWei()).toEqual(new BN(`${stringConst}000000000000000000`));
expect(fromEther.toWeiString()).toEqual(`${stringConst}000000000000000000`);
expect(fromEther.toKwei()).toEqual(`${stringConst}000000000000000`);
expect(fromEther.toMwei()).toEqual(`${stringConst}000000000000`);
expect(fromEther.toGwei()).toEqual(`${stringConst}000000000`);
expect(fromEther.toSzabo()).toEqual(`${stringConst}000000`);
expect(fromEther.toFinney()).toEqual(`${stringConst}000`);
expect(fromEther.toEther()).toEqual(`${stringConst}`);
expect(fromEther.toKether()).toEqual(`0.00${stringConst}`);
expect(fromEther.toMether()).toEqual(`0.00000${stringConst}`);
expect(fromEther.toGether()).toEqual(`0.00000000${stringConst}`);
expect(fromEther.toTether()).toEqual(`0.00000000000${stringConst}`);
expect(fromEther.toHex()).toEqual(
`0x${new BN(`${stringConst}000000000000000000`).toString('hex')}`,
);
expect(fromKether.toWei()).toEqual(new BN(`${stringConst}000000000000000000000`));
expect(fromKether.toWeiString()).toEqual(`${stringConst}000000000000000000000`);
expect(fromKether.toKwei()).toEqual(`${stringConst}000000000000000000`);
expect(fromKether.toMwei()).toEqual(`${stringConst}000000000000000`);
expect(fromKether.toGwei()).toEqual(`${stringConst}000000000000`);
expect(fromKether.toSzabo()).toEqual(`${stringConst}000000000`);
expect(fromKether.toFinney()).toEqual(`${stringConst}000000`);
expect(fromKether.toEther()).toEqual(`${stringConst}000`);
expect(fromKether.toKether()).toEqual(`${stringConst}`);
expect(fromKether.toMether()).toEqual(`0.00${stringConst}`);
expect(fromKether.toGether()).toEqual(`0.00000${stringConst}`);
expect(fromKether.toTether()).toEqual(`0.00000000${stringConst}`);
expect(fromKether.toHex()).toEqual(
`0x${new BN(`${stringConst}000000000000000000000`).toString('hex')}`,
);
expect(fromMether.toWei()).toEqual(new BN(`${stringConst}000000000000000000000000`));
expect(fromMether.toWeiString()).toEqual(`${stringConst}000000000000000000000000`);
expect(fromMether.toKwei()).toEqual(`${stringConst}000000000000000000000`);
expect(fromMether.toMwei()).toEqual(`${stringConst}000000000000000000`);
expect(fromMether.toGwei()).toEqual(`${stringConst}000000000000000`);
expect(fromMether.toSzabo()).toEqual(`${stringConst}000000000000`);
expect(fromMether.toFinney()).toEqual(`${stringConst}000000000`);
expect(fromMether.toEther()).toEqual(`${stringConst}000000`);
expect(fromMether.toKether()).toEqual(`${stringConst}000`);
expect(fromMether.toMether()).toEqual(`${stringConst}`);
expect(fromMether.toGether()).toEqual(`0.00${stringConst}`);
expect(fromMether.toTether()).toEqual(`0.00000${stringConst}`);
expect(fromMether.toHex()).toEqual(
`0x${new BN(`${stringConst}000000000000000000000000`).toString('hex')}`,
);
expect(fromGether.toWei()).toEqual(new BN(`${stringConst}000000000000000000000000000`));
expect(fromGether.toWeiString()).toEqual(`${stringConst}000000000000000000000000000`);
expect(fromGether.toKwei()).toEqual(`${stringConst}000000000000000000000000`);
expect(fromGether.toMwei()).toEqual(`${stringConst}000000000000000000000`);
expect(fromGether.toGwei()).toEqual(`${stringConst}000000000000000000`);
expect(fromGether.toSzabo()).toEqual(`${stringConst}000000000000000`);
expect(fromGether.toFinney()).toEqual(`${stringConst}000000000000`);
expect(fromGether.toEther()).toEqual(`${stringConst}000000000`);
expect(fromGether.toKether()).toEqual(`${stringConst}000000`);
expect(fromGether.toMether()).toEqual(`${stringConst}000`);
expect(fromGether.toGether()).toEqual(`${stringConst}`);
expect(fromGether.toTether()).toEqual(`0.00${stringConst}`);
expect(fromGether.toHex()).toEqual(
`0x${new BN(`${stringConst}000000000000000000000000000`).toString('hex')}`,
);
expect(fromTether.toWei()).toEqual(new BN(`${stringConst}000000000000000000000000000000`));
expect(fromTether.toWeiString()).toEqual(`${stringConst}000000000000000000000000000000`);
expect(fromTether.toKwei()).toEqual(`${stringConst}000000000000000000000000000`);
expect(fromTether.toMwei()).toEqual(`${stringConst}000000000000000000000000`);
expect(fromTether.toGwei()).toEqual(`${stringConst}000000000000000000000`);
expect(fromTether.toSzabo()).toEqual(`${stringConst}000000000000000000`);
expect(fromTether.toFinney()).toEqual(`${stringConst}000000000000000`);
expect(fromTether.toEther()).toEqual(`${stringConst}000000000000`);
expect(fromTether.toKether()).toEqual(`${stringConst}000000000`);
expect(fromTether.toMether()).toEqual(`${stringConst}000000`);
expect(fromTether.toGether()).toEqual(`${stringConst}000`);
expect(fromTether.toTether()).toEqual(`${stringConst}`);
expect(fromTether.toHex()).toEqual(
`0x${new BN(`${stringConst}000000000000000000000000000000`).toString('hex')}`,
);
expect(unit1.wei).toEqual(unit2.wei);
expect(unit2.wei).toEqual(unit3.wei);
expect(Unit.Wei(stringConst)).toEqual(fromWei);
expect(Unit.Kwei(stringConst)).toEqual(fromKwei);
expect(Unit.Mwei(stringConst)).toEqual(fromMwei);
expect(Unit.Gwei(stringConst)).toEqual(fromGwei);
expect(Unit.Szabo(stringConst)).toEqual(fromSzabo);
expect(Unit.Finney(stringConst)).toEqual(fromFinney);
expect(Unit.Ether(stringConst)).toEqual(fromEther);
expect(Unit.Kether(stringConst)).toEqual(fromKether);
expect(Unit.Mether(stringConst)).toEqual(fromMether);
expect(Unit.Gether(stringConst)).toEqual(fromGether);
expect(Unit.Tether(stringConst)).toEqual(fromTether);
expect(Unit.from(stringConst).wei).toEqual(new Unit(stringConst).wei);
try {
const ttt = new Unit('').asWei();
ttt.wei = <any>undefined;
ttt.toWei();
} catch (error) {
expect(error.message).toEqual('error transforming');
}
try {
const ttts = new Unit('').asWei();
ttts.wei = <any>undefined;
ttts.toWeiString();
} catch (error) {
expect(error.message).toEqual('error transforming');
}
try {
const ttt2 = new Unit('').asWei();
ttt2.wei = <any>undefined;
ttt2.toKwei();
} catch (error) {
expect(error.message).toEqual('error transforming');
}
try {
const ttt3 = new Unit('').asWei();
ttt3.wei = <any>undefined;
ttt3.toMwei();
} catch (error) {
expect(error.message).toEqual('error transforming');
}
try {
const ttt4 = new Unit('').asWei();
ttt4.wei = <any>undefined;
ttt4.toGwei();
} catch (error) {
expect(error.message).toEqual('error transforming');
}
try {
const ttt5 = new Unit('').asWei();
ttt5.wei = <any>undefined;
ttt5.toSzabo();
} catch (error) {
expect(error.message).toEqual('error transforming');
}
try {
const ttt6 = new Unit('').asWei();
ttt6.wei = <any>undefined;
ttt6.toFinney();
} catch (error) {
expect(error.message).toEqual('error transforming');
}
try {
const ttt7 = new Unit('').asWei();
ttt7.wei = <any>undefined;
ttt7.toEther();
} catch (error) {
expect(error.message).toEqual('error transforming');
}
try {
const ttt8 = new Unit('').asWei();
ttt8.wei = <any>undefined;
ttt8.toKether();
} catch (error) {
expect(error.message).toEqual('error transforming');
}
try {
const ttt9 = new Unit('').asWei();
ttt9.wei = <any>undefined;
ttt9.toMether();
} catch (error) {
expect(error.message).toEqual('error transforming');
}
try {
const ttt10 = new Unit('').asWei();
ttt10.wei = <any>undefined;
ttt10.toGether();
} catch (error) {
expect(error.message).toEqual('error transforming');
}
try {
const ttt11 = new Unit('').asWei();
ttt11.wei = <any>undefined;
ttt11.toTether();
} catch (error) {
expect(error.message).toEqual('error transforming');
}
try {
const ttt12 = new Unit('').asWei();
ttt12.wei = <any>undefined;
ttt12.toHex();
} catch (error) {
expect(error.message).toEqual('error transforming');
}
});
it('should test numberToString', () => {
const { numberToString } = transformers;
const str = '123';
const num = 123;
const bn = new BN(123);
expect(numberToString(str)).toEqual(str);
expect(numberToString(num)).toEqual(str);
expect(numberToString(bn)).toEqual(str);
try {
numberToString('abc');
} catch (error) {
expect(error.message).toEqual('cannot parse number:abc to string');
}
});
it('should test numToStr', () => {
const num = 123;
const bigNum = new BN(123);
const str = '123';
const wrongStr = 'ijfkl';
const wrongSome: any[] = [];
expect(transformers.numToStr(num)).toEqual('123');
expect(transformers.numToStr(bigNum)).toEqual('123');
expect(transformers.numToStr(str)).toEqual('123');
try {
transformers.numToStr(wrongStr);
} catch (error) {
expect(error.message).toEqual(
`while converting number to string, invalid number value '${wrongStr}', should be a number matching (^-?[0-9.]+).`,
);
}
try {
transformers.numToStr(wrongSome);
} catch (error) {
expect(error.message).toEqual(
`while converting number to string, invalid number value '${wrongSome}' type ${typeof wrongSome}.`,
);
}
});
it('should test numToStr', () => {
const num = 123;
const bigNum = new BN(123);
const str = '123';
const wrongStr = 'ijfkl';
const wrongSome: any = [];
expect(transformers.numToStr(num)).toEqual('123');
expect(transformers.numToStr(bigNum)).toEqual('123');
expect(transformers.numToStr(str)).toEqual('123');
try {
transformers.numToStr(wrongStr);
} catch (error) {
expect(error.message).toEqual(
`while converting number to string, invalid number value '${wrongStr}', should be a number matching (^-?[0-9.]+).`,
);
}
try {
transformers.numToStr(wrongSome);
} catch (error) {
expect(error.message).toEqual(
`while converting number to string, invalid number value '${wrongSome}' type ${typeof wrongSome}.`,
);
}
});
it('should test add0xToString', () => {
expect(transformers.add0xToString('123')).toEqual('0x123');
expect(transformers.add0xToString('-123')).toEqual('-0x123');
try {
const obj: any | string = 123;
transformers.add0xToString(obj);
} catch (error) {
expect(error.message).toEqual(`123 is not String`);
}
});
it('should test numberToHex', () => {
expect(transformers.numberToHex('1')).toEqual('0x1');
expect(transformers.numberToHex('-1')).toEqual('-0x1');
expect(transformers.numberToHex(1)).toEqual('0x1');
expect(transformers.numberToHex(-1)).toEqual('-0x1');
try {
const obj: any | string = null;
transformers.numberToHex(obj);
} catch (error) {
expect(error.message).toEqual(`cannot parse number:null to string`);
}
});
it('should test hexToNumber', () => {
expect(transformers.hexToNumber('0x1')).toEqual('1');
expect(transformers.hexToNumber('-0x1')).toEqual('-1');
try {
const obj: any | string = null;
transformers.hexToNumber(obj);
} catch (error) {
expect(error.message).toEqual(`null is not string`);
}
try {
const obj2: any | string = '123';
transformers.hexToNumber(obj2);
} catch (error) {
expect(error.message).toEqual(`123 is not hex number`);
}
});
it('should test hexToBN', () => {
expect(transformers.hexToBN('0x1').toString()).toEqual('1');
expect(transformers.hexToBN('-0x1').toString()).toEqual('-1');
try {
const obj: any | string = null;
transformers.hexToBN(obj);
} catch (error) {
expect(error.message).toEqual(`null is not string`);
}
try {
const obj2: any | string = '123';
transformers.hexToBN(obj2);
} catch (error) {
expect(error.message).toEqual(`123 is not hex number`);
}
});
it('should convert Wei to Ether', () => {
const Wei = new BN('1000000000000000000');
const expected = '1';
expect(transformers.fromWei(Wei, transformers.Units.ether)).toEqual(expected);
});
it('should convert Ether to Wei', () => {
const zil = new BN(1);
const expected = new BN('1000000000000000000');
expect(transformers.toWei(zil, transformers.Units.ether).eq(expected)).toBeTruthy();
});
it('fromWei should should work for negative numbers', () => {
const Wei = new BN('-1000000000000000000');
const expected = '-1';
expect(transformers.fromWei(Wei, transformers.Units.ether)).toEqual(expected);
});
it('toWei should should work for negative numbers', () => {
const zil = new BN(-1);
const expected = new BN('-1000000000000000000');
expect(transformers.toWei(zil, transformers.Units.ether)).toEqual(expected);
});
it('fromWei errors', () => {
try {
transformers.fromWei('baba', transformers.Units.wei);
} catch (error) {
expect(error.message).toEqual('Error: Assertion failed of "[object Object]"');
}
try {
const wrongType: any | transformers.Units = 'ba';
transformers.fromWei(new BN(100), wrongType);
} catch (error) {
expect(error.message).toEqual('No unit of type ba exists.');
}
expect(transformers.fromWei(new BN(0), transformers.Units.wei, { pad: false })).toEqual('0');
});
it('toWei errors', () => {
try {
transformers.toWei('-1', transformers.Units.wei);
} catch (error) {
expect(error.message).toEqual('Cannot convert 1 to wei.');
}
try {
const wrongType: any | transformers.Units = 'ba';
transformers.toWei('100000', wrongType);
} catch (error) {
expect(error.message).toEqual('No unit of type ba exists.');
}
try {
transformers.toWei('1.00', transformers.Units.wei);
} catch (error) {
expect(error.message).toEqual('Cannot convert 1.00 to wei.');
}
try {
transformers.toWei('100.00.00', transformers.Units.wei);
} catch (error) {
expect(error.message).toEqual('Cannot convert 100.00.00 to wei.');
}
try {
transformers.toWei('.', transformers.Units.wei);
} catch (error) {
expect(error.message).toEqual('Cannot convert . to wei.');
}
});
});

@ -16,7 +16,7 @@ function mapTest(testObject: any, testTrue: string[], testFunc: any) {
}); });
} }
describe('test transformer', () => { describe('test validator', () => {
it('test isNumber', () => { it('test isNumber', () => {
const beTrue = ['zero', 'float', 'hexNumber']; const beTrue = ['zero', 'float', 'hexNumber'];
mapTest(basicType, beTrue, validators.isNumber); mapTest(basicType, beTrue, validators.isNumber);
@ -98,4 +98,49 @@ describe('test transformer', () => {
expect(error.message).toEqual(`${true} is not valid blockNumber`); expect(error.message).toEqual(`${true} is not valid blockNumber`);
} }
}); });
it('test isHttp', () => {
expect(validators.isHttp('http://www.google.com')).toEqual(true);
expect(validators.isHttp('https://www.google.com')).toEqual(true);
expect(validators.isHttp('ftp://www.google.com')).toEqual(false);
try {
validators.isHttp(123);
} catch (error) {
expect(error.message).toEqual(`123 is not valid url`);
}
});
it('test isWs', () => {
expect(validators.isWs('ws://www.google.com')).toEqual(true);
expect(validators.isWs('wss://www.google.com')).toEqual(true);
expect(validators.isWs('ftp://www.google.com')).toEqual(false);
try {
validators.isWs(123);
} catch (error) {
expect(error.message).toEqual(`123 is not valid url`);
}
});
it('test isBech32Address', () => {
expect(validators.isBech32Address('one1au4f9jectk52k24rnk9hjuygt22q2045wcpfdp')).toEqual(true);
expect(validators.isBech32Address('xxx')).toEqual(false);
});
it('test isBech32Address', () => {
expect(
validators.isBech32TestNetAddress('tone1au4f9jectk52k24rnk9hjuygt22q2045wcpfdp'),
).toEqual(true);
expect(validators.isBech32TestNetAddress('xxx')).toEqual(false);
});
it('test isValidAddress', () => {
expect(validators.isValidAddress('tone1au4f9jectk52k24rnk9hjuygt22q2045wcpfdp')).toEqual(true);
expect(validators.isValidAddress('one1au4f9jectk52k24rnk9hjuygt22q2045wcpfdp')).toEqual(true);
expect(validators.isValidAddress(advanceType.checkSumAddress)).toEqual(true);
expect(validators.isValidAddress(advanceType.address)).toEqual(true);
expect(validators.isValidAddress(advanceType.hexAddress)).toEqual(true);
expect(validators.isValidAddress('888')).toEqual(false);
try {
validators.isValidAddress(<any | string>123);
} catch (error) {
expect(error.message).toEqual(`123 is not string`);
}
});
}); });

@ -4,7 +4,21 @@ module.exports = {
moduleNameMapper: { moduleNameMapper: {
'^@harmony-js/(.*)$': '<rootDir>/packages/harmony-$1/src/index.ts', '^@harmony-js/(.*)$': '<rootDir>/packages/harmony-$1/src/index.ts',
}, },
setupTestFrameworkScriptFile: setupTestFrameworkScriptFile: '<rootDir>/scripts/jest/jest.framework-setup.js',
'<rootDir>/scripts/jest/jest.framework-setup.js',
testMatch: ['<rootDir>/e2e/src/?(*.)+(spec|test|e2e).ts'], testMatch: ['<rootDir>/e2e/src/?(*.)+(spec|test|e2e).ts'],
coverageThreshold: {
global: {
branches: 10,
functions: 10,
lines: 10,
statements: 10,
},
},
collectCoverageFrom: [
// 'packages/!(harmony-core)/src/**/*.ts',
'packages/harmony-core/src/**/*.ts',
'packages/harmony-utils/src/**/*.ts',
'packages/harmony-crypto/src/**/*.ts',
'packages/harmony-transaction/src/**/*.ts',
],
}; };

Loading…
Cancel
Save