-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_transfer.js
More file actions
59 lines (52 loc) · 2.54 KB
/
01_transfer.js
File metadata and controls
59 lines (52 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env node
/**
* Example 1 — Native transfer (qweb3.js)
*
* Send TQTOV from one Quantova account to another. Shows connecting, reading a
* balance and nonce, estimating the fee, and the shape of a post-quantum-signed
* transfer broadcast.
*
* Run:
* export QUANTOVA_RPC=http://127.0.0.1:9933 # or https://testnet.quantova.io
* node 01_transfer.js
*
* In a full app with qweb3.js installed:
*
* const { QWeb3, QuantumWallet } = require('qweb3.js');
* const q = new QWeb3(process.env.QUANTOVA_RPC);
* const wallet = new QuantumWallet();
* const sender = wallet.create('dilithium'); // PQ account, address begins with 'Q'
* const txHash = await wallet.buildAndSignTransfer(
* { from: sender.address, to: RECIPIENT, value: 1500000000000000000n }, { rpc: q.rpc });
*
* This runnable version uses raw JSON-RPC (see _common.js) so it works without the
* native @quantova WASM packages installed.
*/
'use strict';
const { rpc, num, toQtov, toPlanck, banner } = require('./_common');
const SENDER = 'Qf2t7p9C5Im4waDJUgrrMqVt3Hs8='; // example Q-address
const RECIPIENT = 'Qe3sJ0p1mK4wQDJUgrrMqVt3Hs8='; // example Q-address
const AMOUNT_QTOV = 1.5;
async function main() {
banner('Quantova example: native TQTOV transfer (qweb3.js)');
const block = num(await rpc('q_blockNumber'));
console.log(`Connected. Current block: ${block}`);
const bal = await rpc('q_getBalance', [SENDER]);
const nonce = num(await rpc('q_getTransactionCount', [SENDER]));
console.log(`Sender: ${SENDER}`);
console.log(`Balance: ${toQtov(bal)} TQTOV nonce: ${nonce}`);
// fee tiers (qweb3.js: await q.fees.estimate())
const gasPrice = await rpc('q_gasPrice');
const tip = await rpc('q_maxPriorityFeePerGas');
console.log(`Fee: gasPrice ${num(gasPrice)} priorityTip ${num(tip)}`);
const value = toPlanck(AMOUNT_QTOV);
console.log(`\nTransfer: ${AMOUNT_QTOV} TQTOV -> ${RECIPIENT} (value ${value} planck)`);
console.log('In qweb3.js this is built and post-quantum-signed by the wallet, then broadcast.');
// Demonstrate the broadcast + receipt round-trip via raw RPC:
const raw = '0x' + Buffer.from(`transfer:${SENDER}->${RECIPIENT}:${value}:nonce=${nonce}`).toString('hex');
const txHash = await rpc('q_sendRawTransaction', [raw]);
console.log(`Broadcast! tx hash: ${txHash}`);
const receipt = await rpc('q_getTransactionReceipt', [txHash]);
console.log(`Included in block: ${num(receipt.blockNumber)} status: ${receipt.status}`);
}
main().catch((e) => { console.error('error:', e.message); process.exit(1); });