forked from ZSLP/zslpjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
7-send-token-p2pkh.ts
100 lines (85 loc) · 5.34 KB
/
7-send-token-p2pkh.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/***************************************************************************************
*
* Example 7: Send any type of token.
*
* Instructions:
* (1) - Select Network and Address by commenting/uncommenting the desired
* NETWORK section and providing valid BCH address.
*
* (2) - Select a Validation method by commenting/uncommenting the desired
* VALIDATOR section. Chose from remote validator or local validator.
* Both options rely on remote JSON RPC calls to rest.bitcoin.com.
* - Option 1: REMOTE VALIDATION (rest.bitcoin.com/v2/slp/isTxidValid/)
* - Option 2: LOCAL VALIDATOR / REST JSON RPC
* - Option 3: LOCAL VALIDATOR / LOCAL FULL NODE
*
* (3) - Run `tsc && node <file-name.js>` just before script execution, or for
* debugger just run `tsc` in the console and then use vscode debugger
* with "Launch Current File" mode.
*
* ************************************************************************************/
import * as BITBOXSDK from 'bitbox-sdk';
import { BigNumber } from 'bignumber.js';
import { BitboxNetwork, SlpBalancesResult, GetRawTransactionsAsync, LocalValidator } from '../index';
(async function() {
// FOR MAINNET UNCOMMENT
const BITBOX = new BITBOXSDK.BITBOX({ restURL: 'https://rest.bitcoin.com/v2/' });
const fundingAddress = "simpleledger:qrhvcy5xlegs858fjqf8ssl6a4f7wpstaqnt0wauwu"; // <-- must be simpleledger format
const fundingWif = "L3gngkDg1HW5P9v5GdWWiCi3DWwvw5XnzjSPwNwVPN5DSck3AaiF"; // <-- compressed WIF format
const tokenReceiverAddress = [ "simpleledger:qplrqmjgpug2qrfx4epuknvwaf7vxpnuevyswakrq9" ]; // <-- must be simpleledger format
const bchChangeReceiverAddress = "simpleledger:qrhvcy5xlegs858fjqf8ssl6a4f7wpstaqnt0wauwu"; // <-- must be simpleledger format
let tokenId = "d32b4191d3f78909f43a3f5853ba59e9f2d137925f28e7780e717f4b4bfd4a3f";
let sendAmounts = [ 1 ];
// FOR TESTNET UNCOMMENT
// const BITBOX = new BITBOXSDK.BITBOX({ restURL: 'https://trest.bitcoin.com/v2/' });
// const fundingAddress = "slptest:qpwyc9jnwckntlpuslg7ncmhe2n423304ueqcyw80l"; // <-- must be simpleledger format
// const fundingWif = "cVjzvdHGfQDtBEq7oddDRcpzpYuvNtPbWdi8tKQLcZae65G4zGgy"; // <-- compressed WIF format
// const tokenReceiverAddress = "slptest:qpwyc9jnwckntlpuslg7ncmhe2n423304ueqcyw80l"; // <-- must be simpleledger format
// const bchChangeReceiverAddress = "slptest:qpwyc9jnwckntlpuslg7ncmhe2n423304ueqcyw80l"; // <-- must be simpleledger format
// let tokenId = "78d57a82a0dd9930cc17843d9d06677f267777dd6b25055bad0ae43f1b884091";
// let sendAmounts = [ 10 ];
// VALIDATOR: Option 1: FOR REMOTE VALIDATION
//const bitboxNetwork = new BitboxNetwork(BITBOX);
// VALIDATOR: Option 2: FOR LOCAL VALIDATOR / REMOTE JSON RPC
// const getRawTransactions: GetRawTransactionsAsync = async function(txids: string[]) {
// return <string[]>await BITBOX.RawTransactions.getRawTransaction(txids)
// }
// const logger = console;
// const slpValidator = new LocalValidator(BITBOX, getRawTransactions, logger);
// const bitboxNetwork = new BitboxNetwork(BITBOX, slpValidator);
// VALIDATOR: Option 3: LOCAL VALIDATOR / LOCAL FULL NODE JSON RPC
const logger = console;
const RpcClient = require('bitcoin-rpc-promise');
const connectionString = 'http://bitcoin:password@localhost:8332'
const rpc = new RpcClient(connectionString);
const slpValidator = new LocalValidator(BITBOX, async (txids) => [ await rpc.getRawTransaction(txids[0]) ], logger)
const bitboxNetwork = new BitboxNetwork(BITBOX, slpValidator);
// 1) Fetch critical token information
const tokenInfo = await bitboxNetwork.getTokenInformation(tokenId);
let tokenDecimals = tokenInfo.decimals;
console.log("Token precision: " + tokenDecimals.toString());
// 2) Check that token balance is greater than our desired sendAmount
let balances = <SlpBalancesResult>await bitboxNetwork.getAllSlpBalancesAndUtxos(fundingAddress);
console.log("'balances' variable is set.");
console.log(balances);
if(balances.slpTokenBalances[tokenId] === undefined)
console.log("You need to fund the addresses provided in this example with tokens and BCH. Change the tokenId as required.")
console.log("Token balance:", <any>balances.slpTokenBalances[tokenId].toFixed() / 10**tokenDecimals);
// 3) Calculate send amount in "Token Satoshis". In this example we want to just send 1 token unit to someone...
let sendAmountsBN = sendAmounts.map(a => (new BigNumber(a)).times(10**tokenDecimals)); // Don't forget to account for token precision
// 4) Get all of our token's UTXOs
let inputUtxos = balances.slpTokenUtxos[tokenId];
// 5) Simply sweep our BCH utxos to fuel the transaction
inputUtxos = inputUtxos.concat(balances.nonSlpUtxos);
// 6) Set the proper private key for each Utxo
inputUtxos.forEach(txo => txo.wif = fundingWif);
// 7) Send token
let sendTxid = await bitboxNetwork.simpleTokenSend(
tokenId,
sendAmountsBN,
inputUtxos,
tokenReceiverAddress,
bchChangeReceiverAddress
)
console.log("SEND txn complete:", sendTxid);
})();