fix(burnPsf): Used for writing to P2WDB

This commit is contained in:
Chris Troutner
2021-09-11 15:16:53 -07:00
parent 23a4ee7875
commit fb77ce7a50
3 changed files with 189 additions and 0 deletions
+59
View File
@@ -9,6 +9,9 @@ const BchWallet = require('minimal-slp-wallet/index')
const JsonFiles = require('./json-files')
const WALLET_FILE = `${__dirname.toString()}/../../wallet.json`
const PROOF_OF_BURN_QTY = 0.01
const P2WDB_TOKEN_ID =
'38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0'
class WalletAdapter {
constructor (localConfig = {}) {
@@ -100,6 +103,62 @@ class WalletAdapter {
throw err
}
}
// Burn enough PSF to generate a valide proof-of-burn for writing to the P2WDB.
async burnPsf () {
try {
// TODO: Throw error if this.bchWallet has not been instantiated.
// console.log('walletData: ', walletData)
// console.log(
// `walletData.utxos.utxoStore.slpUtxos: ${JSON.stringify(
// walletData.utxos.utxoStore.slpUtxos,
// null,
// 2,
// )}`,
// )
// Get token UTXOs held by the wallet.
const tokenUtxos = this.bchWallet.utxos.utxoStore.slpUtxos.type1.tokens
// Find a token UTXO that contains PSF with a quantity higher than needed
// to generate a proof-of-burn.
let tokenUtxo = {}
for (let i = 0; i < tokenUtxos.length; i++) {
const thisUtxo = tokenUtxos[i]
// If token ID matches.
if (thisUtxo.tokenId === P2WDB_TOKEN_ID) {
if (parseFloat(thisUtxo.tokenQty) >= PROOF_OF_BURN_QTY) {
tokenUtxo = thisUtxo
break
}
}
}
if (tokenUtxo.tokenId !== P2WDB_TOKEN_ID) {
throw new Error(
`Token UTXO of with ID of ${P2WDB_TOKEN_ID} and quantity greater than ${PROOF_OF_BURN_QTY} could not be found in wallet.`
)
}
const result = await this.bchWallet.burnTokens(
PROOF_OF_BURN_QTY,
P2WDB_TOKEN_ID
)
// console.log('walletData.burnTokens() result: ', result)
return result
// return {
// success: true,
// txid: 'fakeTxid',
// }
} catch (err) {
console.error('Error in burnPsf()')
throw err
}
}
}
module.exports = WalletAdapter
+39
View File
@@ -146,6 +146,45 @@ describe('#wallet', () => {
// }
// })
})
describe('#burnPsf', () => {
it('should burn PSF tokens and return the txid', async () => {
// mock instance of minimal-slp-wallet
uut.bchWallet = new MockBchWallet()
const result = await uut.burnPsf()
// console.log('result: ', result)
assert.equal(result.success, true)
assert.equal(result.txid, 'txid')
})
it('should throw error if no PSF tokens are found', async () => {
try {
// mock instance of minimal-slp-wallet
uut.bchWallet = new MockBchWallet()
// Remove the PSF token from the mock data.
uut.bchWallet.utxos.utxoStore.slpUtxos.type1.tokens.pop()
await uut.burnPsf()
assert.fail('Unexpected code path')
} catch (err) {
assert.include(err.message, 'Token UTXO of with ID of')
}
})
it('should catch and throw an error', async () => {
try {
await uut.burnPsf()
assert.fail('Unexpected code path')
} catch (err) {
assert.include(err.message, 'Cannot read property')
}
})
})
})
const deleteFile = (filepath) => {
+91
View File
@@ -22,6 +22,97 @@ class MockBchWallet {
this.walletInfoPromise = true
this.walletInfo = mockWallet
this.bchjs = new BCHJS()
this.burnTokens = async () => {
return { success: true, txid: 'txid' }
}
// Environment variable is used by wallet-balance.unit.js to force an error.
if (process.env.NO_UTXO) {
this.utxos = {}
} else {
this.utxos = {
utxoStore: {
address: 'bitcoincash:qqetvdnlt0p8g27dr44cx7h057kpzly9xse9huc97z',
bchUtxos: [
{
height: 700685,
tx_hash:
'1fc577caaff5626a8477162581e57bae1b19dc6aa6c10638013c2b1ba14dc654',
tx_pos: 0,
value: 1000,
txid: '1fc577caaff5626a8477162581e57bae1b19dc6aa6c10638013c2b1ba14dc654',
vout: 0,
isValid: false
},
{
height: 700685,
tx_hash:
'1fc577caaff5626a8477162581e57bae1b19dc6aa6c10638013c2b1ba14dc654',
tx_pos: 2,
value: 19406,
txid: '1fc577caaff5626a8477162581e57bae1b19dc6aa6c10638013c2b1ba14dc654',
vout: 2,
isValid: false
}
],
nullUtxos: [],
slpUtxos: {
type1: {
mintBatons: [],
tokens: [
{
height: 700522,
tx_hash:
'bb5691b50930816be78dad76d203a1c97ac94c03f6051b2fa0159c71c43aa3d0',
tx_pos: 1,
value: 546,
txid: 'bb5691b50930816be78dad76d203a1c97ac94c03f6051b2fa0159c71c43aa3d0',
vout: 1,
utxoType: 'token',
transactionType: 'send',
tokenId:
'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2',
tokenTicker: 'TROUT',
tokenName: "Trout's test token",
tokenDocumentUrl: 'troutsblog.com',
tokenDocumentHash: '',
decimals: 2,
tokenType: 1,
isValid: true,
tokenQty: '4.25'
},
{
height: 0,
tx_hash:
'c0ac066ce6efa1fa4763bf85a91c738e57c12b8765731bd07f0d8f5a55ce582f',
tx_pos: 1,
value: 546,
txid: 'c0ac066ce6efa1fa4763bf85a91c738e57c12b8765731bd07f0d8f5a55ce582f',
vout: 1,
utxoType: 'token',
transactionType: 'send',
tokenId:
'38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0',
tokenTicker: 'PSF',
tokenName: 'Permissionless Software Foundation',
tokenDocumentUrl: 'psfoundation.cash',
tokenDocumentHash: '',
decimals: 8,
tokenType: 1,
isValid: true,
tokenQty: '1'
}
]
},
nft: {
groupMintBatons: [],
groupTokens: [],
tokens: []
}
}
}
}
}
}
}