Skip to content

Commit

Permalink
Merge pull request #3 from christroutner/unstable
Browse files Browse the repository at this point in the history
generateBurnOpReturn
  • Loading branch information
christroutner authored Jan 1, 2020
2 parents 086e43e + d6733c4 commit 2e2e695
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/slp/tokentype1.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class TokenType1 {
totalTokens += tokenUtxos[i].tokenQty

const change = totalTokens - sendQty
// console.log(`change: ${change}`)

let script
let outputs = 1
Expand Down Expand Up @@ -104,6 +105,56 @@ class TokenType1 {
}
}

/**
* @api SLP.TokenType1.generateBurnOpReturn() generateBurnOpReturn() - OP_RETURN code for SLP Send tx
* @apiName generateBurnOpReturn
* @apiGroup SLP
* @apiDescription Generate the OP_RETURN value needed to create an SLP Send transaction that burns tokens.
* This is a slight variation of generateSendOpReturn(). It generates an SLP
* SEND transaction designed to burn a select quantity of tokens.
*
* It's assumed all elements in the tokenUtxos array belong to the same token.
* Returns an object with two properties:
* - script: an array of Bufers that is ready to fed into bchjs.Script.encode() to be turned into a transaction output.
* - outputs: an integer with a value of 1. There is no token change to be sent with this transaction.
*/
generateBurnOpReturn(tokenUtxos, burnQty) {
try {
const tokenId = tokenUtxos[0].tokenId
const decimals = tokenUtxos[0].decimals

// Calculate the total amount of tokens owned by the wallet.
let totalTokens = 0
for (let i = 0; i < tokenUtxos.length; i++)
totalTokens += tokenUtxos[i].tokenQty

const remainder = totalTokens - burnQty

let baseQty = new BigNumber(remainder).times(10 ** decimals)
baseQty = baseQty.absoluteValue()
baseQty = Math.floor(baseQty)
let baseQtyHex = baseQty.toString(16)
baseQtyHex = baseQtyHex.padStart(16, "0")

// console.log(`baseQty: ${baseQty.toString()}`)

const script = [
this.Script.opcodes.OP_RETURN,
Buffer.from("534c5000", "hex"),
//BITBOX.Script.opcodes.OP_1,
Buffer.from("01", "hex"),
Buffer.from(`SEND`),
Buffer.from(tokenId, "hex"),
Buffer.from(baseQtyHex, "hex")
]

return script
} catch (err) {
console.log(`Error in generateSendOpReturn()`)
throw err
}
}

/**
* @api SLP.TokenType1.generateGenesisOpReturn() generateGenesisOpReturn() - OP_RETURN code for SLP Genesis tx
* @apiName generateGenesisOpReturn
Expand Down
69 changes: 69 additions & 0 deletions test/unit/slp-tokentype1.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,75 @@ describe("#SLP TokenType1", () => {
assert.hasAllKeys(result, ["script", "outputs"])
assert.isNumber(result.outputs)
})

// it("should handle problematic configuration", async () => {
// // Mock UTXO.
// const tokenUtxos = [
// {
// txid:
// "0bd6b874246202b4cbc2f501419a5ce6f9b01e8ba9521298afd15c7e8eac5951",
// vout: 2,
// value: "546",
// confirmations: 0,
// satoshis: 546,
// utxoType: "token",
// transactionType: "send",
// tokenId:
// "155784a206873c98acc09e8dabcccf6abf13c4c14d8662190534138a16bb93ce",
// tokenTicker: "PSF",
// tokenName: "PSF Testnet Token",
// tokenDocumentUrl: "",
// tokenDocumentHash: "",
// decimals: 8,
// tokenQty: 18004.71169917
// }
// ]
//
// const result = await bchjs.SLP.TokenType1.generateSendOpReturn(
// tokenUtxos,
// 5000
// )
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
//
// // console.log(`decoded: ${result.script[6].toString("ascii")}`)
// // const msg = Buffer.from(result.script[6], "hex").toString("ascii")
// const msg = result.script[6].toString("ascii")
// console.log(`msg: ${msg}`)
// })
})

describe("#generateBurnOpReturn", () => {
it("should generate burn OP_RETURN code", async () => {
// Mock UTXO.
const tokenUtxos = [
{
txid:
"a8eb788b8ddda6faea00e6e2756624b8feb97655363d0400dd66839ea619d36e",
vout: 2,
value: "546",
confirmations: 0,
satoshis: 546,
utxoType: "token",
transactionType: "send",
tokenId:
"497291b8a1dfe69c8daea50677a3d31a5ef0e9484d8bebb610dac64bbc202fb7",
tokenTicker: "TOK-CH",
tokenName: "TokyoCash",
tokenDocumentUrl: "",
tokenDocumentHash: "",
decimals: 8,
tokenQty: 7
}
]

const result = await bchjs.SLP.TokenType1.generateBurnOpReturn(
tokenUtxos,
1
)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)

assert.isArray(result)
})
})

describe("#generateGenesisOpReturn", () => {
Expand Down

0 comments on commit 2e2e695

Please sign in to comment.