Skip to content

Commit

Permalink
minor version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
jcramer committed Jul 7, 2020
1 parent 0508a9a commit 553bc6e
Show file tree
Hide file tree
Showing 4 changed files with 964 additions and 1,058 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,11 @@ Running the unit tests require node.js v8.15+.


# Change Log
### 0.27.3
- Update packages
- Minor updates to token send example
- Remove extraneous regex checks
- Add flag to disable validator transaction cache in LocalValidator

### 0.27.2
- Fixed false positive case for MINT validation
Expand Down
79 changes: 40 additions & 39 deletions examples/7-send-token-p2pkh.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
/***************************************************************************************
*
*
* 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';
import * as BITBOXSDK from "bitbox-sdk";
import { BigNumber } from "bignumber.js";
import { BitboxNetwork, SlpBalancesResult, GetRawTransactionsAsync, LocalValidator } from "../index";

(async () => {

(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 ];
const BITBOX = new BITBOXSDK.BITBOX({ restURL: "https://rest.bitcoin.com/v2/" });
const fundingAddress = "simpleledger:qzeq626k0w2sg50vrlhthm6988qtp93pnvfeppy4ze"; // <-- must be simpleledger format
const fundingWif = "L56PzQUUt2Cf8rh8GZwv6uEqwm4avxtHUeZaiBAN8ayRCaSrJTNW"; // <-- compressed WIF format
const tokenReceiverAddress = [ "simpleledger:qpq8swcqnmvvaym6az5tlr377ukwem3ykupshhwvz3" ]; // <-- must be simpleledger format
const bchChangeReceiverAddress = "simpleledger:qzeq626k0w2sg50vrlhthm6988qtp93pnvfeppy4ze"; // <-- must be simpleledger format
const tokenId = "d6876f0fce603be43f15d34348bb1de1a8d688e1152596543da033a060cff798";
const sendAmounts = [ 0.000001 ];

// FOR TESTNET UNCOMMENT
// const BITBOX = new BITBOXSDK.BITBOX({ restURL: 'https://trest.bitcoin.com/v2/' });
Expand All @@ -44,7 +44,7 @@ import { BitboxNetwork, SlpBalancesResult, GetRawTransactionsAsync, LocalValidat
// let sendAmounts = [ 10 ];

// VALIDATOR: Option 1: FOR REMOTE VALIDATION
//const bitboxNetwork = new BitboxNetwork(BITBOX);
const bitboxNetwork = new BitboxNetwork(BITBOX);

// VALIDATOR: Option 2: FOR LOCAL VALIDATOR / REMOTE JSON RPC
// const getRawTransactions: GetRawTransactionsAsync = async function(txids: string[]) {
Expand All @@ -55,28 +55,29 @@ import { BitboxNetwork, SlpBalancesResult, GetRawTransactionsAsync, LocalValidat
// 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 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 token information
const tokenInfo = await bitboxNetwork.getTokenInformation(tokenId);
let tokenDecimals = tokenInfo.decimals;
const 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);
const balances = await bitboxNetwork.getAllSlpBalancesAndUtxos(fundingAddress) as SlpBalancesResult;
console.log("'balances' variable is set.");
console.log(balances);
if(balances.slpTokenBalances[tokenId] === undefined)
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);
}
console.log("Token balance:", balances.slpTokenBalances[tokenId].toFixed() as any / 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
const 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];
Expand All @@ -85,16 +86,16 @@ import { BitboxNetwork, SlpBalancesResult, GetRawTransactionsAsync, LocalValidat
inputUtxos = inputUtxos.concat(balances.nonSlpUtxos);

// 6) Set the proper private key for each Utxo
inputUtxos.forEach(txo => txo.wif = fundingWif);
inputUtxos.forEach((txo) => txo.wif = fundingWif);

// 7) Send token
let sendTxid = await bitboxNetwork.simpleTokenSend(
tokenId,
sendAmountsBN,
inputUtxos,
tokenReceiverAddress,
bchChangeReceiverAddress
)
const sendTxid = await bitboxNetwork.simpleTokenSend(
tokenId,
sendAmountsBN,
inputUtxos,
tokenReceiverAddress,
bchChangeReceiverAddress,
);
console.log("SEND txn complete:", sendTxid);

})();
Loading

0 comments on commit 553bc6e

Please sign in to comment.