Skip to content

Commit

Permalink
Merge pull request #8 from Bitcoin-com/ct-examples
Browse files Browse the repository at this point in the history
Fixing Examples
  • Loading branch information
christroutner authored Feb 11, 2019
2 parents 29b7df4 + 2341294 commit df22b87
Show file tree
Hide file tree
Showing 7 changed files with 2,348 additions and 1,358 deletions.
6 changes: 3 additions & 3 deletions examples/check-balance/check-balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const SLPSDK = require("../../lib/SLP").default
// Instantiate SLP based on the network.
let SLP
if (NETWORK === `mainnet`)
SLP = new SLPSDK({ restURL: `https://rest.bitcoin.com/v1/` })
else SLP = new SLPSDK({ restURL: `https://trest.bitcoin.com/v1/` })
SLP = new SLPSDK({ restURL: `https://rest.bitcoin.com/v2/` })
else SLP = new SLPSDK({ restURL: `https://trest.bitcoin.com/v2/` })

// Open the wallet generated with create-wallet.
let walletInfo
Expand Down Expand Up @@ -44,7 +44,7 @@ async function getBalance() {

// get the cash address
const cashAddress = SLP.HDNode.toCashAddress(change)
const slpAddress = SLP.Utils.toSLPAddress(cashAddress)
const slpAddress = SLP.Address.toSLPAddress(cashAddress)

// first get BCH balance
const balance = await SLP.Address.details(cashAddress)
Expand Down
65 changes: 30 additions & 35 deletions examples/create-token/create-token.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Check the BCH and SLP token balance for the wallet created with the
create-wallet example app.
Create a new SLP token. Requires a wallet created with the create-wallet
example. Also requires that wallet to have a small BCH balance.
*/
"use strict"

Expand All @@ -9,11 +9,15 @@ const NETWORK = `mainnet`

const SLPSDK = require("../../lib/SLP").default

// Used for debugging and investigating JS objects.
const util = require("util")
util.inspect.defaultOptions = { depth: 1 }

// Instantiate SLP based on the network.
let SLP
if (NETWORK === `mainnet`)
SLP = new SLPSDK({ restURL: `https://rest.bitcoin.com/v1/` })
else SLP = new SLPSDK({ restURL: `https://trest.bitcoin.com/v1/` })
SLP = new SLPSDK({ restURL: `https://rest.bitcoin.com/v2/` })
else SLP = new SLPSDK({ restURL: `https://trest.bitcoin.com/v2/` })

// Open the wallet generated with create-wallet.
let walletInfo
Expand Down Expand Up @@ -44,7 +48,7 @@ async function createToken() {

// get the cash address
const cashAddress = SLP.HDNode.toCashAddress(change)
const slpAddress = SLP.Utils.toSLPAddress(cashAddress)
const slpAddress = SLP.Address.toSLPAddress(cashAddress)
const u = await SLP.Address.utxo(cashAddress)
const utxo = u[0]

Expand All @@ -54,38 +58,29 @@ async function createToken() {
const batonReceiverAddress = slpAddress // <-- must be simpleledger format
const bchChangeReceiverAddress = cashAddress // <-- simpleledger or bitcoincash format

const decimals = 9
const initialQty = new SLP.BigNumber(1000).times(10 ** decimals)

const genesisOpReturn = SLP.SlpTokenType1.buildGenesisOpReturn(
"TRUST3",
"Trustafari",
"developer.bitcoin.com",
null,
decimals,
2,
initialQty
)
// Create a config object defining the token to be created.
const createConfig = {
fundingAddress,
fundingWif,
tokenReceiverAddress,
batonReceiverAddress,
bchChangeReceiverAddress,
decimals: 8,
name: "SLP SDK example using BITBOX",
symbol: "SLPSDK",
documentUri: "developer.bitcoin.com",
documentHash: null,
initialTokenQty: 1234
}
//console.log(`createConfig: ${util.inspect(createConfig)}`)

const genesisTxHex = SLP.SlpTokenType1.buildRawGenesisTx({
slpGenesisOpReturn: genesisOpReturn,
mintReceiverAddress: tokenReceiverAddress,
mintReceiverSatoshis: 546,
batonReceiverAddress: batonReceiverAddress,
batonReceiverSatoshis: 546,
bchChangeReceiverAddress: bchChangeReceiverAddress,
input_utxos: [
{
txid: utxo.txid,
vout: utxo.vout,
satoshis: utxo.satoshis,
wif: fundingWif
}
]
})
// Generate, sign, and broadcast a hex-encoded transaction for creating
// the new token.
const genesisTxId = await SLP.TokenType1.create(createConfig)

const res = await SLP.RawTransactions.sendRawTransaction(genesisTxHex)
console.log(res)
console.log(`genesisTxHex: ${util.inspect(genesisTxId)}`)
console.log(`View this transaction on the block explorer:`)
console.log(`https://explorer.bitcoin.com/bch/tx/${genesisTxId}`)
} catch (err) {
console.error(`Error in createToken: `, err)
console.log(`Error message: ${err.message}`)
Expand Down
10 changes: 5 additions & 5 deletions examples/create-wallet/create-wallet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Create an HDNode wallet using SLP SDK. The mnemonic from this wallet
will be used in future examples.
will be used in the other examples.
*/
"use strict"

Expand All @@ -12,8 +12,8 @@ const SLPSDK = require("../../lib/SLP").default
// Instantiate SLP based on the network.
let SLP
if (NETWORK === `mainnet`)
SLP = new SLPSDK({ restURL: `https://rest.bitcoin.com/v1/` })
else SLP = new SLPSDK({ restURL: `https://trest.bitcoin.com/v1/` })
SLP = new SLPSDK({ restURL: `https://rest.bitcoin.com/v2/` })
else SLP = new SLPSDK({ restURL: `https://trest.bitcoin.com/v2/` })

const fs = require("fs")

Expand Down Expand Up @@ -49,8 +49,8 @@ for (let i = 0; i < 10; i++) {

if (i === 0) {
outObj.cashAddress = SLP.HDNode.toCashAddress(childNode)
outObj.slpAddress = SLP.Utils.toSLPAddress(outObj.cashAddress)
outObj.legacyAddress = SLP.Utils.toLegacyAddress(outObj.cashAddress)
outObj.slpAddress = SLP.Address.toSLPAddress(outObj.cashAddress)
outObj.legacyAddress = SLP.Address.toLegacyAddress(outObj.cashAddress)
}
}

Expand Down
12 changes: 7 additions & 5 deletions examples/lookup-token/lookup-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

"use strict"

// EDIT THIS WITH THE TOKEN ID YOU WANT TO LOOK UP
const TOKENID =
"259908ae44f46ef585edef4bcc1e50dc06e4c391ac4be929fae27235b8158cf1"

// Set NETWORK to either testnet or mainnet
const NETWORK = `mainnet`

Expand All @@ -12,14 +16,12 @@ const SLPSDK = require("../../lib/SLP").default
// Instantiate SLP based on the network.
let SLP
if (NETWORK === `mainnet`)
SLP = new SLPSDK({ restURL: `https://rest.bitcoin.com/v1/` })
else SLP = new SLPSDK({ restURL: `https://trest.bitcoin.com/v1/` })
SLP = new SLPSDK({ restURL: `https://rest.bitcoin.com/v2/` })
else SLP = new SLPSDK({ restURL: `https://trest.bitcoin.com/v2/` })

async function lookupToken() {
try {
const properties = await SLP.Utils.list(
"259908ae44f46ef585edef4bcc1e50dc06e4c391ac4be929fae27235b8158cf1"
)
const properties = await SLP.Utils.list(TOKENID)
console.log(properties)
} catch (err) {
console.error(`Error in getTokenInfo: `, err)
Expand Down
Loading

0 comments on commit df22b87

Please sign in to comment.