# @harmony-js/transaction

> transaction package for harmony

Latest version **0.1.56** (published 2020-11-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install @harmony-js/transaction
pnpm add @harmony-js/transaction
yarn add @harmony-js/transaction
bun add @harmony-js/transaction
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.56 |
| Published | 2020-11-22 |
| First published | 2019-05-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 369.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | neeboo@firestack.one |
| Maintainers | gupadhyaya, jhwon0820, ghostcorn |

## Links

- npm: https://www.npmjs.com/package/@harmony-js/transaction
- npm.io page: https://npm.io/package/@harmony-js/transaction

## Dependencies (3)

- [@harmony-js/utils](https://npm.io/package/@harmony-js/utils.md) 0.1.56
- [@harmony-js/crypto](https://npm.io/package/@harmony-js/crypto.md) 0.1.56
- [@harmony-js/network](https://npm.io/package/@harmony-js/network.md) 0.1.56

## Recent versions

- 0.1.56 (latest) — 2020-11-22
- 0.1.58 (next) — 2022-07-06
- 0.1.41-alpha.1 (canary) — 2020-05-04
- 0.1.55 — 2020-09-29
- 0.1.54 — 2020-09-07
- 0.1.51 — 2020-08-06
- 0.1.48 — 2020-07-13
- 0.1.45 — 2020-06-06
- 0.1.43 — 2020-05-04
- 0.1.35 — 2019-11-27
- 0.1.34 — 2019-11-18
- 0.1.32 — 2019-10-31
- 0.1.31 — 2019-10-25
- 0.1.30 — 2019-10-22
- 0.1.29 — 2019-10-15
- … 59 more at https://npm.io/package/@harmony-js/transaction/versions

## README

# @harmony-js/transaction

This package provides a collection of apis to create, sign/send transaction, and receive confirm/receipt.

## Installation

```
npm install @harmony-js/transaction
```

## Usage

Create a Harmony instance connecting to testnet

```javascript
const { Harmony } = require('@harmony-js/core');
const {
  ChainID,
  ChainType,
  hexToNumber,
  numberToHex,
  fromWei,
  Units,
  Unit,
} = require('@harmony-js/utils');

const hmy = new Harmony(
    'https://api.s0.b.hmny.io/',
    {
        chainType: ChainType.Harmony,
        chainId: ChainID.HmyTestnet,
    },
);
```

Creating a new transaction using parameters
```javascript
const txn = hmy.transactions.newTx({
  to: 'one166axnkjmghkf3df7xfvd0hn4dft8kemrza4cd2',
  value: new Unit(1).asOne().toWei(),
  // gas limit, you can use string
  gasLimit: '21000',
  // send token from shardID
  shardID: 0,
  // send token to toShardID
  toShardID: 0,
  // gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN
  gasPrice: new hmy.utils.Unit('1').asGwei().toWei(),
});
```

Recovering transaction from raw transaction hash
```javascript
const raw = '0xf86d21843b9aca00825208808094d6ba69da5b45ec98b53e3258d7de756a567b6763880de0b6b3a76400008028a0da8887719f377401963407fc1d82d2ab52404600cf7bea37c27bd2dfd7c86aaaa03c405b0843394442b303256a804bde835821a8a77bd88a2ced9ffdc8b0a409e9';
const tx = hmy.transactions.recover(raw);
```

Getting the RLP encoding of a transaction (rawTransaction), along with raw transaction field values that were encoded
```javascript
const [encoded, raw] = txn.getRLPUnsigned()
```

Sign the transaction using a wallet and send the transaction, wait for confirmation and print receipt
```javascript
// key corresponds to one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7, only has testnet balance
hmy.wallet.addByPrivateKey('45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e');

hmy.wallet.signTransaction(txn).then(signedTxn => {
  signedTxn.sendTransaction().then(([tx, hash]) => {
    console.log('tx hash: ' + hash);
    signedTxn.confirm(hash).then(response => {
      console.log(response.receipt);
    });
  });
});
```

Asynchronous transaction sign, send, and confirm
```javascript
async function transfer() {
  hmy.wallet.addByPrivateKey('45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e');

  const signedTxn = await hmy.wallet.signTransaction(txn);
  signedTxn
    .observed()
    .on('transactionHash', (txnHash) => {
      console.log('');
      console.log('--- hash ---');
      console.log('');
      console.log(txnHash);
      console.log('');
    })
    .on('receipt', (receipt) => {
      console.log('');
      console.log('--- receipt ---');
      console.log('');
      console.log(receipt);
      console.log('');
    })
    .on('cxReceipt', (receipt) => {
      console.log('');
      console.log('--- cxReceipt ---');
      console.log('');
      console.log(receipt);
      console.log('');
    })
    .on('error', (error) => {
      console.log('');
      console.log('--- error ---');
      console.log('');
      console.log(error);
      console.log('');
    });

  const [sentTxn, txnHash] = await signedTxn.sendTransaction();

  const confiremdTxn = await sentTxn.confirm(txnHash);

  // if the transactino is cross-shard transaction
  if (!confiremdTxn.isCrossShard()) {
    if (confiremdTxn.isConfirmed()) {
      console.log('--- Result ---');
      console.log('');
      console.log('Normal transaction');
      console.log(`${txnHash} is confirmed`);
      console.log('');
      console.log('please see detail in explorer:');
      console.log('');
      console.log('https://explorer.testnet.harmony.one/#/tx/' + txnHash);
      console.log('');
      process.exit();
    }
  }
  if (confiremdTxn.isConfirmed() && confiremdTxn.isCxConfirmed()) {
    console.log('--- Result ---');
    console.log('');
    console.log('Cross-Shard transaction');
    console.log(`${txnHash} is confirmed`);
    console.log('');
    console.log('please see detail in explorer:');
    console.log('');
    console.log('https://explorer.testnet.harmony.one/#/tx/' + txnHash);
    console.log('');
    process.exit();
  }
}
transfer();
```

---
_Source: https://npm.io/package/@harmony-js/transaction · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
