Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logic to handle multiple outputs in ExplorerWallet.buildTXHex() #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions src/modules/wallets/ExplorerWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,28 +175,12 @@ class ExplorerWallet {

inputs.forEach(input => txb.addInput(input.txId, input.vout))

// Check if we are paying to ourself, if so, merge the outputs to just a single output.
// Check if we have two outputs (i.e. pay to and change)
if (outputs.length === 2) {
// If the first input is sending to the from address, and there is a change output,
// then merge the outputs.
if (outputs[0].address === this.p2pkh && !outputs[1].address) {
let totalToSend = outputs[0].value + outputs[1].value
outputs = [{
address: this.p2pkh,
value: totalToSend
}]
} else {
// send the original amount to the first address and send the rest to yourself as change
if (outputs[0].address !== this.p2pkh && !outputs[1].address) {
outputs[1].address = this.p2pkh
}
}
}
// console.log(outputs)

outputs.forEach(output => {
if (!output.address) {
throw new Error(`Missing output address: ${outputs}`)
output.address = this.p2pkh
// throw new Error(`Missing output address: ${outputs}`)
}
txb.addOutput(output.address, output.value)
})
Expand All @@ -209,7 +193,7 @@ class ExplorerWallet {
}

let builtHex

// console.log('txb:', txb)
try {
builtHex = txb.build().toHex()
} catch (err) {
Expand Down
10 changes: 5 additions & 5 deletions src/modules/wallets/RPCWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,10 +734,10 @@ class RPCWallet {

// If we do not already have a loop going to make sure confirmations get fired off, create one
if (!this.onConfirmationInterval) {
this.onConfirmationInterval = setInterval((async () => {
this.onConfirmationInterval = setInterval(async () => {
await this.checkAncestorCount(true)
await this.checkForConfirmations()
}).bind(this), CONFIRMATION_CHECK_LENGTH)
await this.checkForConfirmations()
}, CONFIRMATION_CHECK_LENGTH)
}
}

Expand All @@ -746,13 +746,13 @@ class RPCWallet {
* @return {Promise} Returns a promise that resolves once all of the available confirmation callbacks have been run
*/
async checkForConfirmations () {
if (this.getConfirmationSubscriptionCount() === 0) {
if (this.getConfirmationSubscriptionCount() === 0) {
// Sanity check to clear out the interval if for whatever reason it currently exists.
if (this.onConfirmationInterval) {
clearInterval(this.onConfirmationInterval)
this.onConfirmationInterval = undefined
}
return
return
}

console.log(`[RPC Wallet] Checking ${this.getConfirmationSubscriptionCount()} transations for confirmations...`)
Expand Down
28 changes: 25 additions & 3 deletions test/integration/modules/wallets/ExplorerWallet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ describe(`ExplorerWallet`, () => {
// }
// done()
// }, 250 * 100 * 100)
it.skip('build and broadcast TX hex | sendDataToChain', async () => {
it('build and broadcast TX hex | sendDataToChain', async () => {
let wallet = new ExplorerWallet({ wif, network: 'testnet' })
let txid = await wallet.sendDataToChain(`RC`)
expect(typeof txid === 'string').toBeTruthy()
// console.log(txid)
})
it.skip('flotx w custom output | sendTx', async () => {
let wallet = new ExplorerWallet(wif, 'testnet')
it('flotx w custom output | sendTx', async () => {
let wallet = new ExplorerWallet({ wif, network: 'testnet' })
let output = {
address: 'oNAydz5TjkhdP3RPuu3nEirYQf49Jrzm4S',
value: Math.floor(0.0001 * floTestnet.satPerCoin)
Expand All @@ -73,6 +73,28 @@ describe(`ExplorerWallet`, () => {
expect(txid).toBeDefined()
expect(typeof txid === 'string').toBeTruthy()
})
it('flotx w multiple custom output | sendTx', async () => {
jest.setTimeout(10000)
let wallet = new ExplorerWallet({ wif, network: 'testnet' })
let outputs = [
{
address: 'oNAydz5TjkhdP3RPuu3nEirYQf49Jrzm4S',
value: Math.floor(0.0001 * floTestnet.satPerCoin)
},
{
address: 'ofbB67gqjgaYi45u8Qk2U3hGoCmyZcgbN4',
value: Math.floor(0.0001 * floTestnet.satPerCoin)
},
{
address: 'oV5qwoq9CSaXersHk4DQVHhoMTDjRNWRF2',
value: Math.floor(0.0001 * floTestnet.satPerCoin)
}
]
let txid = await wallet.sendTx(outputs, 'to testnet')
console.log(txid)
expect(txid).toBeDefined()
expect(typeof txid === 'string').toBeTruthy()
})
it('add and remove spent transaction from utxo', async () => {
let wallet = new ExplorerWallet({ wif, network: 'testnet' })
let utxo = await wallet.getUTXO()
Expand Down