Skip to content

Commit

Permalink
Merge pull request #20 from Bitcoin-com/burn-tokens
Browse files Browse the repository at this point in the history
v1.3.0
  • Loading branch information
cgcardona authored Feb 16, 2019
2 parents 7d97568 + 27962f7 commit ea874d6
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 57 deletions.
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ You can run each example script by entering its directory and executing `npm sta

- [send-token](send-token) - Send tokens from your address to another address.

- [burn-all](burn-all) - Burn all tokens for an address by tokenId.

- [lookup-token](lookup-token) - Lookup token information based on the token `id`.

- [list-all-tokens](list-all-tokens) - List every token in existence.
Expand Down
68 changes: 68 additions & 0 deletions examples/burn-all/burn-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Burn all tokens for an address by tokenId
*/
"use strict"

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

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/v2/` })
else SLP = new SLPSDK({ restURL: `https://trest.bitcoin.com/v2/` })

// Open the wallet generated with create-wallet.
let walletInfo
try {
walletInfo = require(`../create-wallet/wallet.json`)
} catch (err) {
console.log(
`Could not open wallet.json. Generate a wallet with create-wallet first.`
)
process.exit(0)
}

async function burnAll() {
try {
const mnemonic = walletInfo.mnemonic

// root seed buffer
const rootSeed = SLP.Mnemonic.toSeed(mnemonic)
// master HDNode
let masterHDNode
if (NETWORK === `mainnet`) masterHDNode = SLP.HDNode.fromSeed(rootSeed)
else masterHDNode = SLP.HDNode.fromSeed(rootSeed, "testnet") // Testnet

// HDNode of BIP44 account
const account = SLP.HDNode.derivePath(masterHDNode, "m/44'/145'/0'")

const change = SLP.HDNode.derivePath(account, "0/0")

// get the cash address
const cashAddress = SLP.HDNode.toCashAddress(change)

const tokenId = ""

// get token balances
try {
const iBurnAllConfig = {
fundingAddress: cashAddress,
fundingWif: SLP.HDNode.toWIF(change),
tokenId: tokenId,
bchChangeReceiverAddress: cashAddress
}
const burnAll = await SLP.TokenType1.burnAll(iBurnAllConfig)
console.log("TXID: ", burnAll)
} catch (error) {
console.log(error.message)
}
} catch (err) {
console.error(`Error in burnAll: `, err)
console.log(`Error message: ${err.message}`)
throw err
}
}
burnAll()
24 changes: 24 additions & 0 deletions examples/burn-all/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "burn-all",
"version": "1.0.0",
"description": "Burn all tokens for an address by tokenId",
"main": "burn-all.js",
"scripts": {
"test": "echo no tests yet",
"start": "node burn-all.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Bitcoin-com/slp-sdk.git"
},
"keywords": [
"slp-sdk",
"example"
],
"author": "Gabriel Cardona <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/Bitcoin-com/slp-sdk/issues"
},
"homepage": "https://github.com/Bitcoin-com/slp-sdk#readme"
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const repl = require("repl")
const SLP = require("./lib/SLP").default
const clone = require("git-clone")

program.version("1.2.2", "-v, --version")
program.version("1.3.0", "-v, --version")

program
.command("new <name>")
Expand Down
2 changes: 1 addition & 1 deletion lib/SLP.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var SLP = /** @class */ (function (_super) {
else
restURL = "https://rest.bitcoin.com/v2/";
_this.Address = new Address_1.default(restURL);
_this.TokenType1 = new TokenType1_1.default();
_this.TokenType1 = new TokenType1_1.default(restURL);
_this.Utils = new Utils_1.default(restURL);
return _this;
}
Expand Down
114 changes: 89 additions & 25 deletions lib/TokenType1.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,17 @@ var slpjs = require("slpjs");
var Address_1 = require("./Address");
var addy = new Address_1.default();
var TokenType1 = /** @class */ (function () {
function TokenType1() {
function TokenType1(restURL) {
this.restURL = restURL;
}
TokenType1.prototype.create = function (createConfig) {
return __awaiter(this, void 0, void 0, function () {
var network, tmpBITBOX, getRawTransactions, slpValidator, bitboxNetwork, fundingAddress, fundingWif, tokenReceiverAddress, batonReceiverAddress, bchChangeReceiverAddress, balances, decimals, name, symbol, documentUri, documentHash, initialTokenQty, genesisTxid;
var tmpBITBOX, getRawTransactions, slpValidator, bitboxNetwork, fundingAddress, fundingWif, tokenReceiverAddress, batonReceiverAddress, bchChangeReceiverAddress, balances, decimals, name, symbol, documentUri, documentHash, initialTokenQty, genesisTxid;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
network = addy.detectAddressNetwork(createConfig.fundingAddress);
if (network === "mainnet") {
tmpBITBOX = new BITBOXSDK({ restURL: "https://rest.bitcoin.com/v2/" });
}
else {
tmpBITBOX = new BITBOXSDK({ restURL: "https://trest.bitcoin.com/v2/" });
}
tmpBITBOX = this.returnBITBOXInstance(createConfig.fundingAddress);
getRawTransactions = function (txids) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
Expand Down Expand Up @@ -102,18 +97,12 @@ var TokenType1 = /** @class */ (function () {
};
TokenType1.prototype.mint = function (mintConfig) {
return __awaiter(this, void 0, void 0, function () {
var network, tmpBITBOX, getRawTransactions, slpValidator, bitboxNetwork, fundingAddress, fundingWif, tokenReceiverAddress, batonReceiverAddress, bchChangeReceiverAddress, tokenId, additionalTokenQty, balances, tokenInfo, tokenDecimals, mintQty, inputUtxos, mintTxid;
var tmpBITBOX, getRawTransactions, slpValidator, bitboxNetwork, fundingAddress, fundingWif, tokenReceiverAddress, batonReceiverAddress, bchChangeReceiverAddress, tokenId, additionalTokenQty, balances, tokenInfo, tokenDecimals, mintQty, inputUtxos, mintTxid;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
network = addy.detectAddressNetwork(mintConfig.fundingAddress);
if (network === "mainnet") {
tmpBITBOX = new BITBOXSDK({ restURL: "https://rest.bitcoin.com/v2/" });
}
else {
tmpBITBOX = new BITBOXSDK({ restURL: "https://trest.bitcoin.com/v2/" });
}
tmpBITBOX = this.returnBITBOXInstance(mintConfig.fundingAddress);
getRawTransactions = function (txids) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
Expand Down Expand Up @@ -156,18 +145,12 @@ var TokenType1 = /** @class */ (function () {
};
TokenType1.prototype.send = function (sendConfig) {
return __awaiter(this, void 0, void 0, function () {
var network, tmpBITBOX, getRawTransactions, slpValidator, bitboxNetwork, fundingAddress, fundingWif, tokenReceiverAddress, bchChangeReceiverAddress, tokenId, amount, tokenInfo, tokenDecimals, balances, inputUtxos, sendTxid;
var tmpBITBOX, getRawTransactions, slpValidator, bitboxNetwork, fundingAddress, fundingWif, tokenReceiverAddress, bchChangeReceiverAddress, tokenId, amount, tokenInfo, tokenDecimals, balances, inputUtxos, sendTxid;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
network = addy.detectAddressNetwork(sendConfig.fundingAddress);
if (network === "mainnet") {
tmpBITBOX = new BITBOXSDK({ restURL: "https://rest.bitcoin.com/v2/" });
}
else {
tmpBITBOX = new BITBOXSDK({ restURL: "https://trest.bitcoin.com/v2/" });
}
tmpBITBOX = this.returnBITBOXInstance(sendConfig.fundingAddress);
getRawTransactions = function (txids) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
Expand Down Expand Up @@ -208,6 +191,87 @@ var TokenType1 = /** @class */ (function () {
});
});
};
TokenType1.prototype.burnAll = function (burnAllConfig) {
return __awaiter(this, void 0, void 0, function () {
var tmpBITBOX_1, getRawTransactions, slpValidator, bitboxNetwork, tokenInfo, tokenDecimals, balances, inputUtxos, network, transactionBuilder_1, originalAmount_1, byteCount, sendAmount, keyPair_1, redeemScript_1, tx, hex, txid, error_1;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 4, , 5]);
tmpBITBOX_1 = this.returnBITBOXInstance(burnAllConfig.fundingAddress);
getRawTransactions = function (txids) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, tmpBITBOX_1.RawTransactions.getRawTransaction(txids)];
case 1: return [2 /*return*/, _a.sent()];
}
});
}); };
slpValidator = new slpjs.LocalValidator(tmpBITBOX_1, getRawTransactions);
bitboxNetwork = new slpjs.BitboxNetwork(tmpBITBOX_1, slpValidator);
return [4 /*yield*/, bitboxNetwork.getTokenInformation(burnAllConfig.tokenId)];
case 1:
tokenInfo = _a.sent();
tokenDecimals = tokenInfo.decimals;
return [4 /*yield*/, bitboxNetwork.getAllSlpBalancesAndUtxos(burnAllConfig.fundingAddress)];
case 2:
balances = _a.sent();
inputUtxos = balances.slpTokenUtxos[burnAllConfig.tokenId];
inputUtxos = inputUtxos.concat(balances.nonSlpUtxos);
inputUtxos.forEach(function (txo) { return (txo.wif = burnAllConfig.fundingWif); });
network = this.returnNetwork(burnAllConfig.fundingAddress);
if (network === "mainnet") {
transactionBuilder_1 = new tmpBITBOX_1.TransactionBuilder("mainnet");
}
else {
transactionBuilder_1 = new tmpBITBOX_1.TransactionBuilder("testnet");
}
originalAmount_1 = 0;
inputUtxos.forEach(function (utxo) {
// index of vout
var vout = utxo.vout;
// txid of vout
var txid = utxo.txid;
// add input with txid and index of vout
transactionBuilder_1.addInput(txid, vout);
originalAmount_1 += utxo.satoshis;
});
byteCount = tmpBITBOX_1.BitcoinCash.getByteCount({ P2PKH: inputUtxos.length }, { P2PKH: 1 });
sendAmount = originalAmount_1 - byteCount;
transactionBuilder_1.addOutput(addy.toCashAddress(burnAllConfig.bchChangeReceiverAddress), sendAmount);
keyPair_1 = tmpBITBOX_1.ECPair.fromWIF(burnAllConfig.fundingWif);
inputUtxos.forEach(function (utxo, index) {
transactionBuilder_1.sign(index, keyPair_1, redeemScript_1, transactionBuilder_1.hashTypes.SIGHASH_ALL, utxo.satoshis);
});
tx = transactionBuilder_1.build();
hex = tx.toHex();
return [4 /*yield*/, tmpBITBOX_1.RawTransactions.sendRawTransaction(hex)];
case 3:
txid = _a.sent();
return [2 /*return*/, txid[0]];
case 4:
error_1 = _a.sent();
return [2 /*return*/, error_1];
case 5: return [2 /*return*/];
}
});
});
};
TokenType1.prototype.returnNetwork = function (address) {
return addy.detectAddressNetwork(address);
};
TokenType1.prototype.returnBITBOXInstance = function (address) {
var network = this.returnNetwork(address);
var tmpBITBOX;
if (network === "mainnet") {
tmpBITBOX = new BITBOXSDK({ restURL: "https://rest.bitcoin.com/v2/" });
}
else {
tmpBITBOX = new BITBOXSDK({ restURL: "https://trest.bitcoin.com/v2/" });
}
return tmpBITBOX;
};
return TokenType1;
}());
exports.default = TokenType1;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slp-sdk",
"version": "1.2.2",
"version": "1.3.0",
"description": "SLP SDK powered by BITBOX",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/SLP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SLP extends BITBOXSDK {
else restURL = "https://rest.bitcoin.com/v2/"

this.Address = new Address(restURL)
this.TokenType1 = new TokenType1()
this.TokenType1 = new TokenType1(restURL)
this.Utils = new Utils(restURL)
}
}
Expand Down
Loading

0 comments on commit ea874d6

Please sign in to comment.