Skip to content

Commit

Permalink
Allow sending up to 19 SLP outputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Cardona committed May 2, 2019
1 parent f2ac2f4 commit d93f1cf
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const repl = require("repl")
const SLP = require("./lib/SLP")
const clone = require("git-clone")

program.version("3.3.2", "-v, --version")
program.version("3.5.0", "-v, --version")

program
.command("new <name>")
Expand Down
30 changes: 24 additions & 6 deletions lib/TokenType1.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ var TokenType1 = /** @class */ (function () {
};
TokenType1.prototype.send = function (sendConfig) {
return __awaiter(this, void 0, void 0, function () {
var tmpBITBOX_3, getRawTransactions, slpValidator, bitboxNetwork, fundingAddress, fundingWif_3, tokenReceiverAddress, bchChangeReceiverAddress, tokenId, amount, tokenInfo, tokenDecimals, balances, inputUtxos, sendTxid, error_3;
var tmpBITBOX_3, getRawTransactions, slpValidator, bitboxNetwork, fundingAddress, fundingWif_3, tokenReceiverAddress, bchChangeReceiverAddress, tokenId, amount_1, tokenInfo, tokenDecimals_1, balances, inputUtxos, sendTxid, error_3;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
Expand All @@ -175,27 +175,36 @@ var TokenType1 = /** @class */ (function () {
bitboxNetwork = new slpjs.BitboxNetwork(tmpBITBOX_3, slpValidator);
fundingAddress = addy.toSLPAddress(sendConfig.fundingAddress);
fundingWif_3 = sendConfig.fundingWif;
tokenReceiverAddress = addy.toSLPAddress(sendConfig.tokenReceiverAddress);
tokenReceiverAddress = sendConfig.tokenReceiverAddress;
bchChangeReceiverAddress = addy.toSLPAddress(sendConfig.bchChangeReceiverAddress);
tokenId = sendConfig.tokenId;
amount = sendConfig.amount;
amount_1 = sendConfig.amount;
return [4 /*yield*/, bitboxNetwork.getTokenInformation(tokenId)];
case 1:
tokenInfo = _a.sent();
tokenDecimals = tokenInfo.decimals;
tokenDecimals_1 = tokenInfo.decimals;
return [4 /*yield*/, bitboxNetwork.getAllSlpBalancesAndUtxos(fundingAddress)
// 3) Calculate send amount in "Token Satoshis". In this example we want to just send 1 token unit to someone...
];
case 2:
balances = _a.sent();
// 3) Calculate send amount in "Token Satoshis". In this example we want to just send 1 token unit to someone...
amount = new BigNumber(amount).times(Math.pow(10, tokenDecimals)); // Don't forget to account for token precision
if (!Array.isArray(amount_1)) {
amount_1 = new BigNumber(amount_1).times(Math.pow(10, tokenDecimals_1)); // Don't forget to account for token precision
}
else {
amount_1.forEach(function (amt, index) {
amount_1[index] = new BigNumber(amt).times(Math.pow(10, tokenDecimals_1)); // Don't forget to account for token precision
});
}
inputUtxos = balances.slpTokenUtxos[tokenId];
// 5) Simply sweep our BCH utxos to fuel the transaction
inputUtxos = inputUtxos.concat(balances.nonSlpUtxos);
// 6) Set the proper private key for each Utxo
inputUtxos.forEach(function (txo) { return (txo.wif = fundingWif_3); });
return [4 /*yield*/, bitboxNetwork.simpleTokenSend(tokenId, amount, inputUtxos, tokenReceiverAddress, bchChangeReceiverAddress)];
console.log("AMOUNT", amount_1.length);
console.log("tokenReceiverAddress", tokenReceiverAddress.length);
return [4 /*yield*/, bitboxNetwork.simpleTokenSend(tokenId, amount_1, inputUtxos, tokenReceiverAddress, bchChangeReceiverAddress)];
case 3:
sendTxid = _a.sent();
return [2 /*return*/, sendTxid];
Expand Down Expand Up @@ -271,9 +280,18 @@ var TokenType1 = /** @class */ (function () {
if (!addy.isSLPAddress(config.fundingAddress))
throw Error("Funding Address must be simpleledger format");
// validate tokenReceiverAddress format
// single tokenReceiverAddress
if (config.tokenReceiverAddress &&
!addy.isSLPAddress(config.tokenReceiverAddress))
throw Error("Token Receiver Address must be simpleledger format");
// bulk tokenReceiverAddress
if (config.tokenReceiverAddress &&
Array.isArray(config.tokenReceiverAddress)) {
config.tokenReceiverAddress.forEach(function (address) {
if (!addy.isSLPAddress(address))
throw Error("Token Receiver Address must be simpleledger format");
});
}
// validate bchChangeReceiverAddress format
if (!addy.isSLPAddress(config.bchChangeReceiverAddress) &&
!addy.isCashAddress(config.bchChangeReceiverAddress)) {
Expand Down
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": "3.4.0",
"version": "3.5.0",
"description": "SLP SDK powered by BITBOX",
"main": "lib/SLP",
"scripts": {
Expand Down
28 changes: 24 additions & 4 deletions src/TokenType1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ class TokenType1 {
sendConfig.fundingAddress
)
const fundingWif: string = sendConfig.fundingWif
const tokenReceiverAddress: string = addy.toSLPAddress(
const tokenReceiverAddress: string | string[] =
sendConfig.tokenReceiverAddress
)

const bchChangeReceiverAddress: string = addy.toSLPAddress(
sendConfig.bchChangeReceiverAddress
)
const tokenId: string = sendConfig.tokenId
let amount: number = sendConfig.amount
let amount: any = sendConfig.amount

const tokenInfo: any = await bitboxNetwork.getTokenInformation(tokenId)
const tokenDecimals: number = tokenInfo.decimals
Expand All @@ -208,7 +208,13 @@ class TokenType1 {
)

// 3) Calculate send amount in "Token Satoshis". In this example we want to just send 1 token unit to someone...
amount = new BigNumber(amount).times(10 ** tokenDecimals) // Don't forget to account for token precision
if (!Array.isArray(amount)) {
amount = new BigNumber(amount).times(10 ** tokenDecimals) // Don't forget to account for token precision
} else {
amount.forEach((amt: number, index: number) => {
amount[index] = new BigNumber(amt).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 @@ -218,6 +224,8 @@ class TokenType1 {

// 6) Set the proper private key for each Utxo
inputUtxos.forEach((txo: any) => (txo.wif = fundingWif))
console.log("AMOUNT", amount.length)
console.log("tokenReceiverAddress", tokenReceiverAddress.length)

const sendTxid = await bitboxNetwork.simpleTokenSend(
tokenId,
Expand Down Expand Up @@ -304,12 +312,24 @@ class TokenType1 {
throw Error("Funding Address must be simpleledger format")

// validate tokenReceiverAddress format
// single tokenReceiverAddress
if (
config.tokenReceiverAddress &&
!addy.isSLPAddress(config.tokenReceiverAddress)
)
throw Error("Token Receiver Address must be simpleledger format")

// bulk tokenReceiverAddress
if (
config.tokenReceiverAddress &&
Array.isArray(config.tokenReceiverAddress)
) {
config.tokenReceiverAddress.forEach((address: string) => {
if (!addy.isSLPAddress(address))
throw Error("Token Receiver Address must be simpleledger format")
})
}

// validate bchChangeReceiverAddress format
if (
!addy.isSLPAddress(config.bchChangeReceiverAddress) &&
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/SLPInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export interface IMintConfig {
export interface ISendConfig {
fundingAddress: string
fundingWif: string
tokenReceiverAddress: string
tokenReceiverAddress: string | string[]
bchChangeReceiverAddress: string
tokenId: string
amount: number
amount: number | number[]
}

export interface IBurnConfig {
Expand Down

0 comments on commit d93f1cf

Please sign in to comment.