Skip to content

Commit

Permalink
Merge pull request #24 from Bitcoin-com/burn
Browse files Browse the repository at this point in the history
Burn method.
  • Loading branch information
cgcardona authored Feb 25, 2019
2 parents e35205e + da6d795 commit f196679
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 14 deletions.
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("2.0.0", "-v, --version")
program.version("2.1.0", "-v, --version")

program
.command("new <name>")
Expand Down
44 changes: 44 additions & 0 deletions lib/TokenType1.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,50 @@ var TokenType1 = /** @class */ (function () {
});
});
};
TokenType1.prototype.burn = function (burnConfig) {
return __awaiter(this, void 0, void 0, function () {
var tmpBITBOX_2, getRawTransactions, slpValidator, bitboxNetwork, fundingAddress, bchChangeReceiverAddress, tokenInfo, tokenDecimals, balances, amount, inputUtxos, burnTxid, error_2;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 4, , 5]);
tmpBITBOX_2 = this.returnBITBOXInstance(burnConfig.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_2.RawTransactions.getRawTransaction(txids)];
case 1: return [2 /*return*/, _a.sent()];
}
});
}); };
slpValidator = new slpjs.LocalValidator(tmpBITBOX_2, getRawTransactions);
bitboxNetwork = new slpjs.BitboxNetwork(tmpBITBOX_2, slpValidator);
fundingAddress = addy.toSLPAddress(burnConfig.fundingAddress);
bchChangeReceiverAddress = addy.toSLPAddress(burnConfig.bchChangeReceiverAddress);
return [4 /*yield*/, bitboxNetwork.getTokenInformation(burnConfig.tokenId)];
case 1:
tokenInfo = _a.sent();
tokenDecimals = tokenInfo.decimals;
return [4 /*yield*/, bitboxNetwork.getAllSlpBalancesAndUtxos(fundingAddress)];
case 2:
balances = _a.sent();
amount = new BigNumber(burnConfig.amount).times(Math.pow(10, tokenDecimals));
inputUtxos = balances.slpTokenUtxos[burnConfig.tokenId];
inputUtxos = inputUtxos.concat(balances.nonSlpUtxos);
inputUtxos.forEach(function (txo) { return (txo.wif = burnConfig.fundingWif); });
return [4 /*yield*/, bitboxNetwork.simpleTokenBurn(burnConfig.tokenId, amount, inputUtxos, bchChangeReceiverAddress)];
case 3:
burnTxid = _a.sent();
return [2 /*return*/, burnTxid[0]];
case 4:
error_2 = _a.sent();
return [2 /*return*/, error_2];
case 5: return [2 /*return*/];
}
});
});
};
TokenType1.prototype.returnNetwork = function (address) {
return addy.detectAddressNetwork(address);
};
Expand Down
25 changes: 16 additions & 9 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slp-sdk",
"version": "2.0.0",
"version": "2.1.0",
"description": "SLP SDK powered by BITBOX",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -30,7 +30,7 @@
"dependencies": {
"axios": "^0.17.1",
"babel-register": "^6.26.0",
"bignumber.js": "^7.2.1",
"bignumber.js": "^8.0.2",
"bitbox-sdk": "4.0.0",
"chalk": "^2.3.0",
"clear": "0.1.0",
Expand All @@ -40,7 +40,7 @@
"mkdirp": "^0.5.1",
"node-emoji": "^1.8.1",
"repl.history": "^0.1.4",
"slpjs": "0.13.0",
"slpjs": "0.14.0",
"touch": "^3.1.0"
},
"devDependencies": {
Expand Down
47 changes: 46 additions & 1 deletion src/TokenType1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
ICreateConfig,
IMintConfig,
ISendConfig,
IBurnAllConfig
IBurnAllConfig,
IBurnConfig
} from "./interfaces/SLPInterfaces"

// import classes
Expand Down Expand Up @@ -278,6 +279,50 @@ class TokenType1 {
}
}

async burn(burnConfig: IBurnConfig) {
try {
let tmpBITBOX: any = this.returnBITBOXInstance(burnConfig.fundingAddress)

const getRawTransactions = async (txids: any) => {
return await tmpBITBOX.RawTransactions.getRawTransaction(txids)
}

const slpValidator: any = new slpjs.LocalValidator(
tmpBITBOX,
getRawTransactions
)
const bitboxNetwork = new slpjs.BitboxNetwork(tmpBITBOX, slpValidator)
const fundingAddress: string = addy.toSLPAddress(
burnConfig.fundingAddress
)
const bchChangeReceiverAddress: string = addy.toSLPAddress(
burnConfig.bchChangeReceiverAddress
)
const tokenInfo = await bitboxNetwork.getTokenInformation(
burnConfig.tokenId
)
const tokenDecimals = tokenInfo.decimals
const balances = await bitboxNetwork.getAllSlpBalancesAndUtxos(
fundingAddress
)
let amount = new BigNumber(burnConfig.amount).times(10 ** tokenDecimals)
let inputUtxos = balances.slpTokenUtxos[burnConfig.tokenId]

inputUtxos = inputUtxos.concat(balances.nonSlpUtxos)

inputUtxos.forEach((txo: any) => (txo.wif = burnConfig.fundingWif))
const burnTxid = await bitboxNetwork.simpleTokenBurn(
burnConfig.tokenId,
amount,
inputUtxos,
bchChangeReceiverAddress
)
return burnTxid[0]
} catch (error) {
return error
}
}

returnNetwork(address: string): string {
return addy.detectAddressNetwork(address)
}
Expand Down
8 changes: 8 additions & 0 deletions src/interfaces/SLPInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ export interface IBurnAllConfig {
tokenId: string
bchChangeReceiverAddress: string
}

export interface IBurnConfig {
fundingAddress: string
fundingWif: string
tokenId: string
bchChangeReceiverAddress: string
amount: number
}

0 comments on commit f196679

Please sign in to comment.