Skip to content

Commit

Permalink
816 bug insufficient funds (#847)
Browse files Browse the repository at this point in the history
* Fix blockcyper provider

* Fix hashkoin provider

* Fix bug #846

* Bump versions and update CHANGELOG

* Mitigate 'max amount' error

* Bump package AMM
  • Loading branch information
hippocampus-web3 authored Oct 10, 2023
1 parent 90c21d1 commit d999eda
Show file tree
Hide file tree
Showing 27 changed files with 118 additions and 52 deletions.
6 changes: 6 additions & 0 deletions packages/xchain-bitcoin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.22.4 (2023-10-05)

## Update

- Spend pending UTXO by default

# v0.22.3 (2023-09-11)

## Update
Expand Down
5 changes: 5 additions & 0 deletions packages/xchain-bitcoin/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ describe('Bitcoin Utils Test', () => {
expect(fee).toEqual(1000)
})

it('calc fee', () => {
const fee = Utils.calcFee(50)
expect(fee.amount().toNumber()).toEqual(18800)
})

it('should return default fees of a normal tx', async () => {
const estimates = Utils.getDefaultFees()
expect(estimates.fast).toBeDefined()
Expand Down
6 changes: 3 additions & 3 deletions packages/xchain-bitcoin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-bitcoin",
"version": "0.22.3",
"version": "0.22.4",
"description": "Custom Bitcoin client and utilities used by XChainJS clients",
"keywords": [
"XChain",
Expand Down Expand Up @@ -37,7 +37,7 @@
"@xchainjs/xchain-client": "^0.14.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-utxo-providers": "^0.2.2",
"@xchainjs/xchain-utxo-providers": "^0.2.4",
"axios": "^1.3.6",
"axios-mock-adapter": "^1.20.0",
"bitcoinjs-lib": "5.2.0",
Expand All @@ -48,7 +48,7 @@
"@xchainjs/xchain-client": "^0.14.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-utxo-providers": "^0.2.2",
"@xchainjs/xchain-utxo-providers": "^0.2.4",
"axios": "^1.3.6",
"bitcoinjs-lib": "5.2.0",
"coinselect": "^3.1.12",
Expand Down
9 changes: 1 addition & 8 deletions packages/xchain-bitcoin/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,10 @@ class Client extends UTXOClient {
const feeRate = params.feeRate || (await this.getFeeRates())[FeeOption.Fast]
checkFeeBounds(this.feeBounds, feeRate)

/**
* do not spend pending UTXOs when adding a memo
* https://github.com/xchainjs/xchainjs-lib/issues/330
*/
const spendPendingUTXO = !params.memo

const { psbt } = await this.buildTx({
...params,
feeRate,
sender: this.getAddress(fromAddressIndex),
spendPendingUTXO,
})
const btcKeys = this.getBtcKeys(this.phrase, fromAddressIndex)
psbt.signAllInputs(btcKeys) // Sign all inputs
Expand All @@ -208,7 +201,7 @@ class Client extends UTXOClient {
memo,
feeRate,
sender,
spendPendingUTXO = false, // default: prevent spending uncomfirmed UTXOs
spendPendingUTXO = true, // default: prevent spending uncomfirmed UTXOs
}: TxParams & {
feeRate: FeeRate
sender: Address
Expand Down
7 changes: 5 additions & 2 deletions packages/xchain-bitcoin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ export const compileMemo = (memo: string): Buffer => {
* @returns {number} The fee amount.
*/
export const getFee = (inputs: UTXO[], feeRate: FeeRate, data: Buffer | null = null): number => {
const inputSizeBasedOnInputs =
inputs.length > 0
? inputs.reduce((a, x) => a + inputBytes(x), 0) + inputs.length // +1 byte for each input signature
: (TX_INPUT_BASE + TX_INPUT_PUBKEYHASH) * 2 + 2 // By default 2 UTXOs // Temporal solution until issue addressed https://github.com/xchainjs/xchainjs-lib/issues/850
let sum =
TX_EMPTY_SIZE +
inputs.reduce((a, x) => a + inputBytes(x), 0) +
inputs.length + // +1 byte for each input signature
inputSizeBasedOnInputs +
TX_OUTPUT_BASE +
TX_OUTPUT_PUBKEYHASH +
TX_OUTPUT_BASE +
Expand Down
4 changes: 2 additions & 2 deletions packages/xchain-bitcoincash/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@xchainjs/xchain-client": "^0.14.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-utxo-providers": "^0.2.2",
"@xchainjs/xchain-utxo-providers": "^0.2.4",
"axios": "^1.3.6",
"axios-mock-adapter": "^1.20.0",
"bchaddrjs": "^0.5.2",
Expand All @@ -54,7 +54,7 @@
"@xchainjs/xchain-client": "^0.14.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-utxo-providers": "^0.2.2",
"@xchainjs/xchain-utxo-providers": "^0.2.4",
"axios": "^1.3.6",
"bchaddrjs": "^0.5.2",
"coinselect": "3.1.12"
Expand Down
6 changes: 6 additions & 0 deletions packages/xchain-dash/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.1.3 (2023-06-10)

## Update

- Increase fee estimation

# v0.1.2 (2023-09-11)

## Update
Expand Down
5 changes: 5 additions & 0 deletions packages/xchain-dash/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ describe('Dash Utils Test', () => {
expect(fee).toEqual(3246)
})

it('calc fee', () => {
const fee = utils.calcFee(50)
expect(fee.amount().toNumber()).toEqual(19650)
})

it('should return default fees of a normal tx', async () => {
const estimates = utils.getDefaultFees()
expect(estimates.fast).toBeDefined()
Expand Down
6 changes: 3 additions & 3 deletions packages/xchain-dash/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-dash",
"version": "0.1.2",
"version": "0.1.3",
"description": "Custom Dash client and utilities used by XChainJS clients",
"keywords": [
"XChain",
Expand Down Expand Up @@ -38,7 +38,7 @@
"@xchainjs/xchain-client": "^0.14.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-utxo-providers": "^0.2.2",
"@xchainjs/xchain-utxo-providers": "^0.2.4",
"coinselect": "^3.1.12",
"axios": "^1.3.6",
"axios-mock-adapter": "^1.20.0",
Expand All @@ -50,7 +50,7 @@
"@xchainjs/xchain-client": "^0.14.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-utxo-providers": "^0.2.2",
"@xchainjs/xchain-utxo-providers": "^0.2.4",
"coinselect": "^3.1.12",
"axios": "^1.3.6",
"bitcoinjs-lib": "^5.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/xchain-dash/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const buildTx = async ({

export const calcFee = (feeRate: FeeRate, memo?: string, utxos: UTXO[] = []): BaseAmount => {
const scriptData: Buffer = Buffer.from(`${memo}`, 'utf8')
const fee = getFee(utxos.length, feeRate, scriptData)
const fee = getFee(utxos.length > 0 ? utxos.length : 2, feeRate, scriptData) // By default 2 UTXOs // Temporal solution until issue addressed https://github.com/xchainjs/xchainjs-lib/issues/850
return baseAmount(fee)
}

Expand Down
12 changes: 12 additions & 0 deletions packages/xchain-doge/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# v0.6.5 (2023-06-10)

## Update

- Increase fee estimation

# v0.6.4 (2023-10-05)

## Update

- update deps client & util & utxo-providers

# v0.6.3 (2023-09-13)

## Update
Expand Down
5 changes: 5 additions & 0 deletions packages/xchain-doge/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ describe('Dogecoin Utils Test', () => {
expect(estimates.average).toBeDefined()
})

it('calc fee', () => {
const fee = Utils.calcFee(50)
expect(fee.amount().toNumber()).toEqual(100000)
})

// it('should fetch as many uxtos as are associated with an address', async () => {
// const address = 'DRapidDiBYggT1zdrELnVhNDqyAHn89cRi'
// const utxos: UTXO[] = await Utils.scanUTXOs({
Expand Down
8 changes: 4 additions & 4 deletions packages/xchain-doge/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-doge",
"version": "0.6.3",
"version": "0.6.5",
"description": "Custom Doge client and utilities used by XChain clients",
"keywords": [
"Xchain",
Expand Down Expand Up @@ -36,7 +36,7 @@
"@xchainjs/xchain-client": "^0.14.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-utxo-providers": "^0.2.2",
"@xchainjs/xchain-utxo-providers": "^0.2.4",
"@types/wif": "^2.0.2",
"axios": "^1.3.6",
"axios-mock-adapter": "^1.20.0",
Expand All @@ -49,7 +49,7 @@
"@xchainjs/xchain-client": "^0.14.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-utxo-providers": "^0.2.2",
"@xchainjs/xchain-utxo-providers": "^0.2.4",
"axios": "^1.3.6",
"bitcoinjs-lib": "5.2.0",
"coininfo": "5.1.0",
Expand All @@ -59,4 +59,4 @@
"publishConfig": {
"access": "public"
}
}
}
2 changes: 1 addition & 1 deletion packages/xchain-doge/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class Client extends UTXOClient {
}): Promise<{ psbt: Dogecoin.Psbt; utxos: UTXO[] }> => {
if (!this.validateAddress(recipient)) throw new Error('Invalid address')

const utxos = await this.scanUTXOs(sender)
const utxos = await this.scanUTXOs(sender, false)
if (utxos.length === 0) throw new Error('No utxos to send')
const feeRateWhole = Number(feeRate.toFixed(0))
const compiledMemo = memo ? Utils.compileMemo(memo) : null
Expand Down
9 changes: 5 additions & 4 deletions packages/xchain-doge/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ export const compileMemo = (memo: string): Buffer => {
* @returns {number} The fee amount.
*/
export function getFee(inputs: UTXO[], feeRate: FeeRate, data: Buffer | null = null): number {
const inputSizeBasedOnInputs =
inputs.length > 0
? inputs.reduce((a) => a + inputBytes(), 0) + inputs.length // +1 byte for each input signature
: (TX_INPUT_BASE + TX_INPUT_PUBKEYHASH) * 2 + 2 // By default 2 UTXOs // Temporal solution until issue addressed https://github.com/xchainjs/xchainjs-lib/issues/850
let sum =
TX_EMPTY_SIZE +
inputs.reduce(function (a) {
return a + inputBytes()
}, 0) +
inputs.length + // +1 byte for each input signature
inputSizeBasedOnInputs + // +1 byte for each input signature
TX_OUTPUT_BASE +
TX_OUTPUT_PUBKEYHASH +
TX_OUTPUT_BASE +
Expand Down
6 changes: 6 additions & 0 deletions packages/xchain-litecoin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.12.4 (2023-06-10)

## Update

- Increase fee estimation

# v0.12.3 (2023-09-11)

## Update
Expand Down
9 changes: 7 additions & 2 deletions packages/xchain-litecoin/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ describe('Litecoin Utils Test', () => {

it('get the right vault fee', () => {
const fee = Utils.getFee(utxos, 10, OP_RETURN)
expect(fee).toEqual(1890)
expect(fee).toEqual(1900)
})

it('get the right normal fee', () => {
const fee = Utils.getFee(utxos, 10, null)
expect(fee).toEqual(1640)
expect(fee).toEqual(1650)
})

it('should return a minimum fee of 1000', () => {
Expand Down Expand Up @@ -100,6 +100,11 @@ describe('Litecoin Utils Test', () => {
// expect(utxos?.[1].txHex).toBeUndefined()
})

it('calc fee', () => {
const fee = Utils.calcFee(50)
expect(fee.amount().toNumber()).toEqual(18800)
})

describe('broadcastTx', () => {
it('returns txHash', async () => {
const txHash = await Utils.broadcastTx({
Expand Down
6 changes: 3 additions & 3 deletions packages/xchain-litecoin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-litecoin",
"version": "0.12.3",
"version": "0.12.4",
"description": "Custom Litecoin client and utilities used by XChainJS clients",
"keywords": [
"XChain",
Expand Down Expand Up @@ -37,7 +37,7 @@
"@xchainjs/xchain-client": "^0.14.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-utxo-providers": "^0.2.2",
"@xchainjs/xchain-utxo-providers": "^0.2.4",
"axios": "^1.3.6",
"axios-mock-adapter": "^1.20.0",
"bitcoinjs-lib": "^5.2.0",
Expand All @@ -49,7 +49,7 @@
"@xchainjs/xchain-client": "^0.14.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-utxo-providers": "^0.2.2",
"@xchainjs/xchain-utxo-providers": "^0.2.4",
"axios": "^1.3.6",
"bitcoinjs-lib": "5.2.0",
"coininfo": "^5.1.0",
Expand Down
8 changes: 5 additions & 3 deletions packages/xchain-litecoin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ export const compileMemo = (memo: string): Buffer => {
* @returns {number} The fee amount.
*/
export function getFee(inputs: UTXO[], feeRate: FeeRate, data: Buffer | null = null): number {
const inputSizeBasedOnInputs =
inputs.length > 0
? inputs.reduce((a, x) => a + inputBytes(x), 0) + inputs.length // +1 byte for each input signature
: (TX_INPUT_BASE + TX_INPUT_PUBKEYHASH) * 2 + 2 // By default 2 UTXOs // Temporal solution until issue addressed https://github.com/xchainjs/xchainjs-lib/issues/850
let sum =
TX_EMPTY_SIZE +
inputs.reduce(function (a, x) {
return a + inputBytes(x)
}, 0) +
inputSizeBasedOnInputs +
inputs.length + // +1 byte for each input signature
TX_OUTPUT_BASE +
TX_OUTPUT_PUBKEYHASH +
Expand Down
7 changes: 7 additions & 0 deletions packages/xchain-thorchain-amm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v0.7.4 (2023-10-09)

## Update

- Fix pending UTXOs bug
- Increate default fee estimation

# v0.7.3 (2023-10-06)

## Update
Expand Down
Loading

0 comments on commit d999eda

Please sign in to comment.