mirror of
https://github.com/Permissionless-Software-Foundation/bch-js.git
synced 2026-09-21 16:51:59 -07:00
fix(generateBurnOpReturn): SLP burn functionality
This commit is contained in:
+5
-41
@@ -118,7 +118,7 @@ class TokenType1 {
|
||||
* - 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, sendQty) {
|
||||
generateBurnOpReturn(tokenUtxos, burnQty) {
|
||||
try {
|
||||
const tokenId = tokenUtxos[0].tokenId
|
||||
const decimals = tokenUtxos[0].decimals
|
||||
@@ -128,44 +128,9 @@ class TokenType1 {
|
||||
for (let i = 0; i < tokenUtxos.length; i++)
|
||||
totalTokens += tokenUtxos[i].tokenQty
|
||||
|
||||
const change = totalTokens - sendQty
|
||||
const remainder = totalTokens - burnQty
|
||||
|
||||
let script
|
||||
let outputs = 1
|
||||
|
||||
// The normal case, when there is token change to return to sender.
|
||||
if (change > 0) {
|
||||
outputs = 2
|
||||
|
||||
let baseQty = new BigNumber(sendQty).times(10 ** decimals)
|
||||
baseQty = baseQty.absoluteValue()
|
||||
baseQty = Math.floor(baseQty)
|
||||
let baseQtyHex = baseQty.toString(16)
|
||||
baseQtyHex = baseQtyHex.padStart(16, "0")
|
||||
|
||||
let baseChange = new BigNumber(change).times(10 ** decimals)
|
||||
baseChange = baseChange.absoluteValue()
|
||||
baseChange = Math.floor(baseChange)
|
||||
// console.log(`baseChange: ${baseChange.toString()}`)
|
||||
|
||||
let baseChangeHex = baseChange.toString(16)
|
||||
baseChangeHex = baseChangeHex.padStart(16, "0")
|
||||
// console.log(`baseChangeHex padded: ${baseChangeHex}`)
|
||||
|
||||
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"),
|
||||
Buffer.from(baseChangeHex, "hex")
|
||||
]
|
||||
} else {
|
||||
// Corner case, when there is no token change to send back.
|
||||
|
||||
let baseQty = new BigNumber(sendQty).times(10 ** decimals)
|
||||
let baseQty = new BigNumber(remainder).times(10 ** decimals)
|
||||
baseQty = baseQty.absoluteValue()
|
||||
baseQty = Math.floor(baseQty)
|
||||
let baseQtyHex = baseQty.toString(16)
|
||||
@@ -173,7 +138,7 @@ class TokenType1 {
|
||||
|
||||
// console.log(`baseQty: ${baseQty.toString()}`)
|
||||
|
||||
script = [
|
||||
const script = [
|
||||
this.Script.opcodes.OP_RETURN,
|
||||
Buffer.from("534c5000", "hex"),
|
||||
//BITBOX.Script.opcodes.OP_1,
|
||||
@@ -182,9 +147,8 @@ class TokenType1 {
|
||||
Buffer.from(tokenId, "hex"),
|
||||
Buffer.from(baseQtyHex, "hex")
|
||||
]
|
||||
}
|
||||
|
||||
return { script, outputs }
|
||||
return script
|
||||
} catch (err) {
|
||||
console.log(`Error in generateSendOpReturn()`)
|
||||
throw err
|
||||
|
||||
@@ -107,6 +107,40 @@ describe("#SLP TokenType1", () => {
|
||||
// })
|
||||
})
|
||||
|
||||
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", () => {
|
||||
it("should generate genesis OP_RETURN code", async () => {
|
||||
const configObj = {
|
||||
|
||||
Reference in New Issue
Block a user