Skip to content

Commit

Permalink
nft api updates with bchd
Browse files Browse the repository at this point in the history
  • Loading branch information
jcramer committed Dec 9, 2020
1 parent 411f8cf commit a4e0a8b
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 125 deletions.
38 changes: 17 additions & 21 deletions examples/4-genesis-token-type-NFT1-parent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@
* (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
* NETWORK section and providing valid BCH address.
* (3) - 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.
* (4) - Run `tsc && node <file-name.js>` just before script execution
* (5) - Optional: Use vscode debugger w/ launch.json settings
* (2) - Run `tsc && node <file-name.js>` just before script execution
* (3) - Optional: Use vscode debugger w/ launch.json settings
*
* ************************************************************************************/

import { BigNumber } from "bignumber.js";
import * as BITBOXSDK from "bitbox-sdk";
import { BitboxNetwork, SlpBalancesResult } from "../index";
import { GrpcClient } from "grpc-bchrpc-node";
import { BchdNetwork, GetRawTransactionsAsync, LocalValidator, SlpBalancesResult } from "../index";

const name = "My NFT1 Group";
const ticker = "NFT1 Group";
Expand All @@ -29,28 +25,28 @@ const initialTokenQty = 1000000;
(async () => {

// // NETWORK: FOR MAINNET UNCOMMENT
const BITBOX = new BITBOXSDK.BITBOX({ restURL: "https://rest.bitcoin.com/v2/" });
const BITBOX = new BITBOXSDK.BITBOX();
const fundingAddress = "simpleledger:qrhvcy5xlegs858fjqf8ssl6a4f7wpstaqnt0wauwu"; // <-- must be simpleledger format
const fundingWif = "L3gngkDg1HW5P9v5GdWWiCi3DWwvw5XnzjSPwNwVPN5DSck3AaiF"; // <-- compressed WIF format
const tokenReceiverAddress = "simpleledger:qrhvcy5xlegs858fjqf8ssl6a4f7wpstaqnt0wauwu"; // <-- must be simpleledger format
const bchChangeReceiverAddress = "simpleledger:qrhvcy5xlegs858fjqf8ssl6a4f7wpstaqnt0wauwu"; // <-- cashAddr or slpAddr format
// For unlimited issuance provide a "batonReceiverAddress"
const batonReceiverAddress = "simpleledger:qrhvcy5xlegs858fjqf8ssl6a4f7wpstaqnt0wauwu";

// NETWORK: FOR TESTNET UNCOMMENT
// const BITBOX = new BITBOXSDK.BITBOX({ restURL: 'https://trest.bitcoin.com/v2/' });
// const fundingAddress = "slptest:qpwyc9jnwckntlpuslg7ncmhe2n423304ueqcyw80l";
// const fundingWif = "cVjzvdHGfQDtBEq7oddDRcpzpYuvNtPbWdi8tKQLcZae65G4zGgy";
// const tokenReceiverAddress = "slptest:qpwyc9jnwckntlpuslg7ncmhe2n423304ueqcyw80l";
// const bchChangeReceiverAddress = "slptest:qpwyc9jnwckntlpuslg7ncmhe2n423304ueqcyw80l";
// // For unlimited issuance provide a "batonReceiverAddress"
// const batonReceiverAddress = "slptest:qpwyc9jnwckntlpuslg7ncmhe2n423304ueqcyw80l";
// VALIDATOR SETUP: FOR REMOTE VALIDATION
const client = new GrpcClient(); //{ url: "bchd.ny1.simpleledger.io" });

const bitboxNetwork = new BitboxNetwork(BITBOX);
const getRawTransactions: GetRawTransactionsAsync = async (txids: string[]) => {
const txid = txids[0];
const res = await client.getRawTransaction({ hash: txid, reversedHashOrder: true });
return [Buffer.from(res.getTransaction_asU8()).toString("hex")];
};
const logger = console;
const validator = new LocalValidator(BITBOX, getRawTransactions, logger);
const bchdNetwork = new BchdNetwork({ BITBOX, client, validator, logger });

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

// 2) Calculate the token quantity with decimal precision included
Expand All @@ -60,7 +56,7 @@ const initialTokenQty = 1000000;
balances!.nonSlpUtxos.forEach(txo => txo.wif = fundingWif);

// 4) Use "simpleTokenGenesis()" helper method
const genesisTxid = await bitboxNetwork.simpleNFT1ParentGenesis(
const genesisTxid = await bchdNetwork.simpleNFT1ParentGenesis(
name,
ticker,
initialTokenQtyBN,
Expand Down
77 changes: 31 additions & 46 deletions examples/5-genesis-token-type-NFT1-child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,51 @@
* (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
* NETWORK section and providing valid BCH address.
* (3) - 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.
* (4) - Run `tsc && node <file-name.js>` just before script execution
* (5) - Optional: Use vscode debugger w/ launch.json settings
* (2) - Run `tsc && node <file-name.js>` just before script execution
* (3) - Optional: Use a custom SLP validator by using GrpcClient "url" parameter
* (4) - Optional: Use vscode debugger w/ launch.json settings
*
* ************************************************************************************/

import { BigNumber } from "bignumber.js";
import * as BITBOXSDK from "bitbox-sdk";
import { GrpcClient } from "grpc-bchrpc-node";
import { BitboxNetwork, GetRawTransactionsAsync, LocalValidator, SlpAddressUtxoResult, SlpBalancesResult } from "../index";
import { GetRawTransactionsAsync,
LocalValidator,
SlpAddressUtxoResult,
SlpBalancesResult } from "../index";
import { BchdNetwork } from "../lib/bchdnetwork";

const name = "My NFT1 Child";
const ticker = "NFT1 Child";
const documentUri: string|null = null;
const documentHash: Buffer|null = null;
const NFT1ParentGroupID = "112f967519e18083c8e4bd7ba67ebc04d72aaaa941826d38655c53d677e6a5be";
const NFT1ParentGroupID = "240c44216936e86e624538866934c6f038a6cc4a5a83db232d735f15e400b7ad";

(async () => {

const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));

// // NETWORK: FOR MAINNET UNCOMMENT
const BITBOX = new BITBOXSDK.BITBOX({ restURL: "https://rest.bitcoin.com/v2/" });
const BITBOX = new BITBOXSDK.BITBOX();
const fundingAddress = "simpleledger:qrhvcy5xlegs858fjqf8ssl6a4f7wpstaqnt0wauwu"; // <-- must be simpleledger format
const fundingWif = "L3gngkDg1HW5P9v5GdWWiCi3DWwvw5XnzjSPwNwVPN5DSck3AaiF"; // <-- compressed WIF format
const tokenReceiverAddress = "simpleledger:qrhvcy5xlegs858fjqf8ssl6a4f7wpstaqnt0wauwu"; // <-- must be simpleledger format
const bchChangeReceiverAddress = "simpleledger:qrhvcy5xlegs858fjqf8ssl6a4f7wpstaqnt0wauwu"; // <-- cashAddr or slpAddr format
// For unlimited issuance provide a "batonReceiverAddress"
const batonReceiverAddress = "simpleledger:qrhvcy5xlegs858fjqf8ssl6a4f7wpstaqnt0wauwu";

// NETWORK: FOR TESTNET UNCOMMENT
// const BITBOX = new BITBOXSDK.BITBOX({ restURL: 'https://trest.bitcoin.com/v2/' });
// const fundingAddress = "slptest:qpwyc9jnwckntlpuslg7ncmhe2n423304ueqcyw80l";
// const fundingWif = "cVjzvdHGfQDtBEq7oddDRcpzpYuvNtPbWdi8tKQLcZae65G4zGgy";
// const tokenReceiverAddress = "slptest:qpwyc9jnwckntlpuslg7ncmhe2n423304ueqcyw80l";
// const bchChangeReceiverAddress = "slptest:qpwyc9jnwckntlpuslg7ncmhe2n423304ueqcyw80l";
// // For unlimited issuance provide a "batonReceiverAddress"
// const batonReceiverAddress = "slptest:qpwyc9jnwckntlpuslg7ncmhe2n423304ueqcyw80l";

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

// VALIDATOR: FOR LOCAL VALIDATOR / REMOTE JSON RPC
const grpc = new GrpcClient();
// VALIDATOR SETUP: FOR REMOTE VALIDATION
const client = new GrpcClient();
const getRawTransactions: GetRawTransactionsAsync = async (txids: string[]) => {
const txid = txids[0];
const res = await grpc.getRawTransaction({ hash: txid, reversedHashOrder: true });
const res = await client.getRawTransaction({ hash: txid, reversedHashOrder: true });
return [Buffer.from(res.getTransaction_asU8()).toString("hex")];
//return <string[]>await BITBOX.RawTransactions.getRawTransaction(txids) // <--- alternative to gRPC
};
const logger = console;
const slpValidator = new LocalValidator(BITBOX, getRawTransactions, logger);
const bitboxNetwork = new BitboxNetwork(BITBOX, slpValidator);
const validator = new LocalValidator(BITBOX, getRawTransactions, logger);
const bchdNetwork = new BchdNetwork({ BITBOX, client, validator });

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

// Look at the NFT1 Parent token balance. Make sure its greater than 0.
Expand All @@ -76,18 +60,18 @@ const NFT1ParentGroupID = "112f967519e18083c8e4bd7ba67ebc04d72aaaa941826d38655c5
}

// Try to find an NFT parent that has quantity equal to 1
let utxo: SlpAddressUtxoResult|undefined;
balances.slpTokenUtxos[NFT1ParentGroupID].forEach(txo => {
if (!utxo && txo.slpUtxoJudgementAmount.isEqualTo(1)) {
utxo = txo;
let nftGroupUtxo: SlpAddressUtxoResult|undefined;
balances.slpTokenUtxos[NFT1ParentGroupID].forEach((txo) => {
if (!nftGroupUtxo && txo.slpUtxoJudgementAmount.isEqualTo(1)) {
nftGroupUtxo = txo;
}
});

// If there wasn't any NFT1 parent UTXO with quantity of 1, so we create a TXO w/ qty 1 to be burned.
if (!utxo) {
if (!nftGroupUtxo) {
const inputs = [...balances.nonSlpUtxos, ...balances.slpTokenUtxos[NFT1ParentGroupID]];
inputs.map(txo => txo.wif = fundingWif);
const sendTxid = await bitboxNetwork.simpleTokenSend(
inputs.map((txo) => txo.wif = fundingWif);
const sendTxid = await bchdNetwork.simpleTokenSend(
NFT1ParentGroupID, new BigNumber(1), inputs,
tokenReceiverAddress, tokenReceiverAddress);

Expand All @@ -97,20 +81,20 @@ const NFT1ParentGroupID = "112f967519e18083c8e4bd7ba67ebc04d72aaaa941826d38655c5
await sleep(3000);

// update balances and set the newly created parent TXO.
balances = (await bitboxNetwork.getAllSlpBalancesAndUtxos(fundingAddress) as SlpBalancesResult);
balances = (await bchdNetwork.getAllSlpBalancesAndUtxos(fundingAddress) as SlpBalancesResult);
balances.slpTokenUtxos[NFT1ParentGroupID].forEach(txo => {
if (!utxo && txo.slpUtxoJudgementAmount.isEqualTo(1)) {
utxo = txo;
if (!nftGroupUtxo && txo.slpUtxoJudgementAmount.isEqualTo(1)) {
nftGroupUtxo = txo;
}
});
}

// 3) Set private keys
const inputs = [utxo!, ...balances.nonSlpUtxos];
inputs.map(txo => txo.wif = fundingWif);
const inputs = [nftGroupUtxo!, ...balances.nonSlpUtxos];
inputs.map((txo) => txo.wif = fundingWif);

// 4) Use "simpleNFT1ChildGenesis()" helper method
const genesisTxid = await bitboxNetwork.simpleNFT1ChildGenesis(
const genesisTxid = await bchdNetwork.simpleNFT1ChildGenesis(
NFT1ParentGroupID,
name,
ticker,
Expand All @@ -120,5 +104,6 @@ const NFT1ParentGroupID = "112f967519e18083c8e4bd7ba67ebc04d72aaaa941826d38655c5
bchChangeReceiverAddress,
inputs,
);

console.log("NFT1 Child GENESIS txn complete:", genesisTxid);
})();
Loading

0 comments on commit a4e0a8b

Please sign in to comment.