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:
+18
-54
@@ -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.
|
* - 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.
|
* - 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 {
|
try {
|
||||||
const tokenId = tokenUtxos[0].tokenId
|
const tokenId = tokenUtxos[0].tokenId
|
||||||
const decimals = tokenUtxos[0].decimals
|
const decimals = tokenUtxos[0].decimals
|
||||||
@@ -128,63 +128,27 @@ class TokenType1 {
|
|||||||
for (let i = 0; i < tokenUtxos.length; i++)
|
for (let i = 0; i < tokenUtxos.length; i++)
|
||||||
totalTokens += tokenUtxos[i].tokenQty
|
totalTokens += tokenUtxos[i].tokenQty
|
||||||
|
|
||||||
const change = totalTokens - sendQty
|
const remainder = totalTokens - burnQty
|
||||||
|
|
||||||
let script
|
let baseQty = new BigNumber(remainder).times(10 ** decimals)
|
||||||
let outputs = 1
|
baseQty = baseQty.absoluteValue()
|
||||||
|
baseQty = Math.floor(baseQty)
|
||||||
|
let baseQtyHex = baseQty.toString(16)
|
||||||
|
baseQtyHex = baseQtyHex.padStart(16, "0")
|
||||||
|
|
||||||
// The normal case, when there is token change to return to sender.
|
// console.log(`baseQty: ${baseQty.toString()}`)
|
||||||
if (change > 0) {
|
|
||||||
outputs = 2
|
|
||||||
|
|
||||||
let baseQty = new BigNumber(sendQty).times(10 ** decimals)
|
const script = [
|
||||||
baseQty = baseQty.absoluteValue()
|
this.Script.opcodes.OP_RETURN,
|
||||||
baseQty = Math.floor(baseQty)
|
Buffer.from("534c5000", "hex"),
|
||||||
let baseQtyHex = baseQty.toString(16)
|
//BITBOX.Script.opcodes.OP_1,
|
||||||
baseQtyHex = baseQtyHex.padStart(16, "0")
|
Buffer.from("01", "hex"),
|
||||||
|
Buffer.from(`SEND`),
|
||||||
|
Buffer.from(tokenId, "hex"),
|
||||||
|
Buffer.from(baseQtyHex, "hex")
|
||||||
|
]
|
||||||
|
|
||||||
let baseChange = new BigNumber(change).times(10 ** decimals)
|
return script
|
||||||
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)
|
|
||||||
baseQty = baseQty.absoluteValue()
|
|
||||||
baseQty = Math.floor(baseQty)
|
|
||||||
let baseQtyHex = baseQty.toString(16)
|
|
||||||
baseQtyHex = baseQtyHex.padStart(16, "0")
|
|
||||||
|
|
||||||
// console.log(`baseQty: ${baseQty.toString()}`)
|
|
||||||
|
|
||||||
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, outputs }
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(`Error in generateSendOpReturn()`)
|
console.log(`Error in generateSendOpReturn()`)
|
||||||
throw err
|
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", () => {
|
describe("#generateGenesisOpReturn", () => {
|
||||||
it("should generate genesis OP_RETURN code", async () => {
|
it("should generate genesis OP_RETURN code", async () => {
|
||||||
const configObj = {
|
const configObj = {
|
||||||
|
|||||||
Reference in New Issue
Block a user