Skip to content

Commit

Permalink
ts linting
Browse files Browse the repository at this point in the history
  • Loading branch information
jcramer committed Dec 9, 2020
1 parent a4e0a8b commit 883ab9e
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 67 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ const bitboxNetwork = new slpjs.BitboxNetwork(BITBOX);
let balances;
(async function() {
balances = await bitboxNetwork.getAllSlpBalancesAndUtxos(fundingAddress);
console.log("'balances' variable is set.");
console.log('BCH balance:', balances.satoshis_available_bch);
})();

Expand Down Expand Up @@ -228,7 +227,6 @@ const bitboxNetwork = new slpjs.BitboxNetwork(BITBOX);
let balances;
(async function() {
balances = await bitboxNetwork.getAllSlpBalancesAndUtxos(fundingAddress);
console.log("'balances' variable is set.");
if(balances.slpBatonUtxos[tokenIdHexToMint])
console.log("You have the minting baton for this token");
else
Expand Down Expand Up @@ -321,7 +319,6 @@ let tokenDecimals;
let balances;
(async function() {
balances = 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.")
Expand Down Expand Up @@ -386,7 +383,6 @@ const bitboxNetwork = new slpjs.BitboxNetwork(BITBOX);
let balances;
(async function() {
balances = await bitboxNetwork.getAllSlpBalancesAndUtxos(fundingAddress);
console.log("'balances' variable is set.", balances);

if (balances.satoshis_available_bch < sendAmountsInSatoshi) {
throw new Error("You need to fund the addresses provided in this example with BCH.");
Expand Down Expand Up @@ -489,7 +485,6 @@ let tokenDecimals;
let balances;
(async function() {
balances = 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.")
Expand Down Expand Up @@ -580,7 +575,6 @@ let fundingAddress = "simpleledger:pphnuh7dx24rcwjkj0sl6xqfyfzf23aj7udr0837gn";
let balances;
(async function() {
balances = 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.")
Expand Down Expand Up @@ -669,7 +663,6 @@ let tokenDecimals;
let balances;
(async function() {
balances = await bitboxNetwork.getAllSlpBalancesAndUtxos(fundingAddress);
console.log("'balances' variable is set.");
console.log('Token balance:', balances.slpTokenBalances[tokenId].toFixed() / 10**tokenDecimals)
})();

Expand Down
4 changes: 2 additions & 2 deletions examples/1-get-token-balances-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* using SLPDB.
*
* Instructions:
* (1) - Select Network and Address by commenting/uncommenting the desired
* (1) - Select Network and Address by commenting/un-commenting the desired
* TESTNET or MAINNET section and providing valid BCH address.
* (2) - Run `ts-node <file-name.js>` just before script execution,
* or use vscode debugger w/ launch.json settings for "Current TS File"
Expand All @@ -22,7 +22,7 @@ import { BchdNetwork } from "../lib/bchdnetwork";
import { GetRawTransactionsAsync } from "../lib/localvalidator";

// MAINNET NETWORK
const addr = "simpleledger:qp4a73gx6j5u3se0f7263us6rdxygh3rfvk0gu9mfa"; // "simpleledger:qrhvcy5xlegs858fjqf8ssl6a4f7wpstaqnt0wauwu";
const addr = "simpleledger:qrhvcy5xlegs858fjqf8ssl6a4f7wpstaqnt0wauwu";
const testnet = false;

// TESTNET NETWORK
Expand Down
4 changes: 2 additions & 2 deletions examples/1-get-token-balances-trusted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* using BCHD and SLPDB Trusted Validation
*
* Instructions:
* (1) - Select Network and Address by commenting/uncommenting the desired
* (1) - Select Network and Address by commenting/un-commenting the desired
* TESTNET or MAINNET section and providing valid BCH address.
* (2) - Run `ts-node <file-name.js>` just before script execution,
* or use vscode debugger w/ launch.json settings for "Current TS File"
Expand All @@ -16,7 +16,7 @@ const BITBOX = new BITBOXSDK.BITBOX();
import { GrpcClient } from "grpc-bchrpc-node";
import { BchdNetwork, BchdValidator, SlpBalancesResult } from "../index";

const addr = "simpleledger:qpd2crqxr32tqmu72h6dkygryracrwtcwqy0enzf3z";
const addr = "simpleledger:qpcgsyu3c4hd00luwhc5a9x5zcgnlw8kdqmdxyjsta";

const client = new GrpcClient({ url: "bchd.ny1.simpleledger.io" });
const validator = new BchdValidator(client);
Expand Down
55 changes: 28 additions & 27 deletions examples/10-burn-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@
*
* ************************************************************************************/

import * as BITBOXSDK from 'bitbox-sdk';
import { BigNumber } from 'bignumber.js';
import { BitboxNetwork, SlpBalancesResult, LocalValidator } from '../index';
import { BigNumber } from "bignumber.js";
import * as BITBOXSDK from "bitbox-sdk";
import { BitboxNetwork, LocalValidator, SlpBalancesResult } from "../index";

(async () => {

(async function() {

// NETWORK: FOR MAINNET
const BITBOX = new BITBOXSDK.BITBOX({ restURL: 'https://rest.bitcoin.com/v2/' });
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 bchChangeReceiverAddress = "simpleledger:qrhvcy5xlegs858fjqf8ssl6a4f7wpstaqnt0wauwu"; // <-- must be simpleledger format
let tokenId = "d32b4191d3f78909f43a3f5853ba59e9f2d137925f28e7780e717f4b4bfd4a3f";
let burnAmount = 1;
const tokenId = "d32b4191d3f78909f43a3f5853ba59e9f2d137925f28e7780e717f4b4bfd4a3f";
const burnAmount = 1;

// 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 @@ -46,27 +46,27 @@ import { BitboxNetwork, SlpBalancesResult, LocalValidator } from '../index';

// 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 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());
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);
console.log("'balances' variable is set.");
if(balances.slpTokenBalances[tokenId])
console.log('Token balance:', <any>balances.slpTokenBalances[tokenId].toFixed() / 10**tokenDecimals)
else
const balances = <SlpBalancesResult>await bitboxNetwork.getAllSlpBalancesAndUtxos(fundingAddress);
if (balances.slpTokenBalances[tokenId]) {
console.log("Token balance:", <any>balances.slpTokenBalances[tokenId].toFixed() / 10**tokenDecimals)
} else {
console.log("Funding addresss is not holding any tokens with id:", tokenId);
}

// 3) Calculate send amount in "Token Satoshis". In this example we want to just send 1 token unit to someone...
let amount = (new BigNumber(burnAmount)).times(10**tokenDecimals); // Don't forget to account for token precision
const amount = (new BigNumber(burnAmount)).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 @@ -75,14 +75,15 @@ import { BitboxNetwork, SlpBalancesResult, LocalValidator } from '../index';
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.simpleTokenBurn(
tokenId,
amount,
inputUtxos,
const sendTxid = await bitboxNetwork.simpleTokenBurn(
tokenId,
amount,
inputUtxos,
bchChangeReceiverAddress
)
console.log("BURN txn complete:",sendTxid);
);

console.log("BURN txn complete:", sendTxid);
})();
6 changes: 3 additions & 3 deletions examples/2-get-txn-details-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Example 2: Fetch token details for given txid
*
* Instructions:
* (1) - Select Network and Address by commenting/uncommenting the desired
* (1) - Select Network and Address by commenting/un-commenting the desired
* NETWORK section and providing valid BCH address.
* (2) - Select a Validation method by commenting/uncommenting the desired
* (2) - Select a Validation method by commenting/un-commenting the desired
* VALIDATOR section. Chose from remote validator or local validator.
* Both options rely on remote JSON RPC calls to rest.bitcoin.com.
* (3) - Run `tsc && node <file-name.js>` just before script execution
Expand All @@ -14,9 +14,9 @@
* ************************************************************************************/

import * as BITBOXSDK from "bitbox-sdk";
import { GrpcClient } from "grpc-bchrpc-node";
import { BchdNetwork, LocalValidator } from "../index";
import { GetRawTransactionsAsync } from "../lib/localvalidator";
import { GrpcClient } from "grpc-bchrpc-node";

const BITBOX = new BITBOXSDK.BITBOX();
const logger = console;
Expand Down
8 changes: 4 additions & 4 deletions examples/2-get-txn-details-trusted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Example 2: Fetch token details for given txid
*
* Instructions:
* (1) - Select Network and Address by commenting/uncommenting the desired
* (1) - Select Network and Address by commenting/un-commenting the desired
* NETWORK section and providing valid BCH address.
* (2) - Select a Validation method by commenting/uncommenting the desired
* (2) - Select a Validation method by commenting/un-commenting the desired
* VALIDATOR section. Chose from remote validator or local validator.
* Both options rely on remote JSON RPC calls to rest.bitcoin.com.
* (3) - Run `tsc && node <file-name.js>` just before script execution
Expand All @@ -14,16 +14,16 @@
* ************************************************************************************/

import * as BITBOXSDK from "bitbox-sdk";
import { GrpcClient } from "grpc-bchrpc-node";
import { BchdNetwork } from "../index";
import { GetRawTransactionsAsync } from "../lib/localvalidator";
import { GrpcClient } from "grpc-bchrpc-node";
import { TrustedValidator } from "../lib/trustedvalidator";

const BITBOX = new BITBOXSDK.BITBOX();
const logger = console;

// NETWORK: FOR MAINNET COMMENT/UNCOMMENT
let txid = "c67c6423767a86e27c56ad9c04581f4500d88baff12b865611a39602f449b465";
const txid = "c67c6423767a86e27c56ad9c04581f4500d88baff12b865611a39602f449b465";
const testnet = false;

// VALIDATOR: FOR LOCAL VALIDATOR
Expand Down
7 changes: 3 additions & 4 deletions examples/3-genesis-token-type-1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* (1) - Send some BCH to simpleledger:qrhvcy5xlegs858fjqf8ssl6a4f7wpstaqnt0wauwu
* or tBCH to slptest:qpwyc9jnwckntlpuslg7ncmhe2n423304ueqcyw80l
* to fund the example.
* (2) - Select Network and Address by commenting/uncommenting the desired
* (2) - Select Network and Address by commenting/un-commenting the desired
* NETWORK section and providing valid BCH address.
* (3) - Select a Validation method by commenting/uncommenting the desired
* (3) - Select a Validation method by commenting/un-commenting the desired
* VALIDATOR section. Chose from remote validator or local validator.
* Both options rely on remote JSON RPC calls to rest.bitcoin.com.
* (4) - Run `tsc && node <file-name.js>` just before script execution
Expand Down Expand Up @@ -51,14 +51,13 @@ const initialTokenQty = 1000000;

// 1) Get all balances at the funding address.
const balances = await bitboxNetwork.getAllSlpBalancesAndUtxos(fundingAddress) as SlpBalancesResult;
console.log("'balances' variable is set.");
console.log("BCH balance:", balances.satoshis_available_bch);

// 2) Calculate the token quantity with decimal precision included
const initialTokenQtyBN = (new BigNumber(initialTokenQty)).times(10 ** decimals);

// 3) Set private keys
balances!.nonSlpUtxos.forEach(txo => txo.wif = fundingWif);
balances!.nonSlpUtxos.forEach((txo) => txo.wif = fundingWif);

// 4) Use "simpleTokenGenesis()" helper method
const genesisTxid = await bitboxNetwork.simpleTokenGenesis(
Expand Down
20 changes: 10 additions & 10 deletions examples/5-genesis-token-type-NFT1-child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const NFT1ParentGroupID = "240c44216936e86e624538866934c6f038a6cc4a5a83db232d735
const bchChangeReceiverAddress = "simpleledger:qrhvcy5xlegs858fjqf8ssl6a4f7wpstaqnt0wauwu"; // <-- cashAddr or slpAddr format

// VALIDATOR SETUP: FOR REMOTE VALIDATION
const client = new GrpcClient();
const client = new GrpcClient({ url: "bchd.greyh.at:8335" });
const getRawTransactions: GetRawTransactionsAsync = async (txids: string[]) => {
const txid = txids[0];
const res = await client.getRawTransaction({ hash: txid, reversedHashOrder: true });
Expand Down Expand Up @@ -95,15 +95,15 @@ const NFT1ParentGroupID = "240c44216936e86e624538866934c6f038a6cc4a5a83db232d735

// 4) Use "simpleNFT1ChildGenesis()" helper method
const genesisTxid = await bchdNetwork.simpleNFT1ChildGenesis(
NFT1ParentGroupID,
name,
ticker,
documentUri,
documentHash,
tokenReceiverAddress,
bchChangeReceiverAddress,
inputs,
);
NFT1ParentGroupID,
name,
ticker,
documentUri,
documentHash,
tokenReceiverAddress,
bchChangeReceiverAddress,
inputs,
);

console.log("NFT1 Child GENESIS txn complete:", genesisTxid);
})();
9 changes: 4 additions & 5 deletions examples/6-mint-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
*
* ************************************************************************************/

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 { GetRawTransactionRequest, GrpcClient } from "grpc-bchrpc-node";
import { BitboxNetwork, LocalValidator, SlpBalancesResult } from "../index";

(async () => {

Expand Down Expand Up @@ -69,7 +69,6 @@ import { GetRawTransactionRequest, GrpcClient } from "grpc-bchrpc-node";

// 1) Get all balances at the funding address.
const balances = await bitboxNetwork.getAllSlpBalancesAndUtxos(fundingAddress) as SlpBalancesResult;
console.log("'balances' variable is set.");
if (balances.slpBatonUtxos[tokenIdHexToMint]) {
console.log("You have the minting baton for this token");
} else {
Expand All @@ -78,7 +77,7 @@ import { GetRawTransactionRequest, GrpcClient } from "grpc-bchrpc-node";

// 2) Fetch critical token decimals information using bitdb
const tokenInfo = await bitboxNetwork.getTokenInformation(tokenIdHexToMint);
const tokenDecimals = tokenInfo.decimals;
const tokenDecimals = tokenInfo.decimals;
console.log("Token precision: " + tokenDecimals.toString());

// 3) Multiply the specified token quantity by 10^(token decimal precision)
Expand All @@ -92,7 +91,7 @@ import { GetRawTransactionRequest, GrpcClient } from "grpc-bchrpc-node";
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) MINT token using simple function
const mintTxid = await bitboxNetwork.simpleTokenMint(
Expand Down
1 change: 0 additions & 1 deletion examples/7-send-token-p2pkh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ import { BitboxNetwork, SlpBalancesResult, GetRawTransactionsAsync, LocalValidat

// 2) Check that token balance is greater than our desired sendAmount
const balances = await bitboxNetwork.getAllSlpBalancesAndUtxos(fundingAddress) as SlpBalancesResult;
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.")
Expand Down
1 change: 0 additions & 1 deletion examples/8-send-token-p2sh-frozen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ import { BitboxNetwork, SlpBalancesResult, Slp, TransactionHelpers, Utils } from

// 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.")
Expand Down
1 change: 0 additions & 1 deletion examples/9-send-token-p2sh-multisig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import { BitboxNetwork, SlpBalancesResult, Slp, TransactionHelpers } from '../in
// 2) Check that token balance is greater than our desired sendAmount
let fundingAddress = "simpleledger:pphnuh7dx24rcwjkj0sl6xqfyfzf23aj7udr0837gn";
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.")
Expand Down
Loading

0 comments on commit 883ab9e

Please sign in to comment.