Skip to content

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
0xApotheosis committed Dec 13, 2023
2 parents dbbd6c4 + d0b199c commit fc372b8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 21 deletions.
29 changes: 8 additions & 21 deletions packages/chain-adapters/src/utxo/UtxoBaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,38 +383,25 @@ export abstract class UtxoBaseAdapter<T extends UtxoChainId> implements IChainAd
throw new Error('UtxoBaseAdapter: failed to get fee data')
}

// TODO: when does this happen and why?
if (!data.fast?.satsPerKiloByte || data.fast.satsPerKiloByte < 0) {
data.fast = data.average
}
// ensure higher confirmation speeds never have lower fees than lower confirmation speeds
if (data.slow.satsPerKiloByte > data.average.satsPerKiloByte)
data.average.satsPerKiloByte = data.slow.satsPerKiloByte
if (data.average.satsPerKiloByte > data.fast.satsPerKiloByte)
data.fast.satsPerKiloByte = data.average.satsPerKiloByte

const utxos = await this.providers.http.getUtxos({ pubkey })

const utxoSelectInput = { from, to, value, opReturnData, utxos, sendMax }

// We have to round because coinselect library uses sats per byte which cant be decimals
const fastPerByte = String(Math.round(data.fast.satsPerKiloByte / 1024))
const averagePerByte = String(Math.round(data.average.satsPerKiloByte / 1024))
const slowPerByte = String(Math.round(data.slow.satsPerKiloByte / 1024))
const fastPerByte = String(Math.round(data.fast.satsPerKiloByte / 1000))
const averagePerByte = String(Math.round(data.average.satsPerKiloByte / 1000))
const slowPerByte = String(Math.round(data.slow.satsPerKiloByte / 1000))

const { fee: fastFee } = utxoSelect({ ...utxoSelectInput, satoshiPerByte: fastPerByte })
const { fee: averageFee } = utxoSelect({ ...utxoSelectInput, satoshiPerByte: averagePerByte })
const { fee: slowFee } = utxoSelect({ ...utxoSelectInput, satoshiPerByte: slowPerByte })

// Special, temporary case for DOGE to provide a workable fee value when the node is struggling
const isDoge = pubkey.startsWith('dgub')
const allFeesDefined =
fastFee !== undefined && averageFee !== undefined && slowFee !== undefined
if (isDoge && !allFeesDefined) {
const satoshiPerByte = '20000'
const fee = utxoSelect({ ...utxoSelectInput, satoshiPerByte }).fee
return {
fast: { txFee: String(fee), chainSpecific: { satoshiPerByte } },
average: { txFee: String(fee), chainSpecific: { satoshiPerByte } },
slow: { txFee: String(fee), chainSpecific: { satoshiPerByte } },
} as FeeDataEstimate<T>
}

return {
fast: { txFee: String(fastFee), chainSpecific: { satoshiPerByte: fastPerByte } },
average: { txFee: String(averageFee), chainSpecific: { satoshiPerByte: averagePerByte } },
Expand Down
52 changes: 52 additions & 0 deletions packages/chain-adapters/src/utxo/dogecoin/DogecoinChainAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import type { BIP44Params } from '@shapeshiftoss/types'
import { KnownChainIds, UtxoAccountType } from '@shapeshiftoss/types'
import * as unchained from '@shapeshiftoss/unchained-client'

import type { FeeDataEstimate, GetFeeDataInput } from '../../types'
import { ChainAdapterDisplayName } from '../../types'
import type { ChainAdapterArgs } from '../UtxoBaseAdapter'
import { UtxoBaseAdapter } from '../UtxoBaseAdapter'
import { utxoSelect } from '../utxoSelect'

const SUPPORTED_CHAIN_IDS = [KnownChainIds.DogecoinMainnet]
const DEFAULT_CHAIN_ID = KnownChainIds.DogecoinMainnet
Expand Down Expand Up @@ -54,4 +56,54 @@ export class ChainAdapter extends UtxoBaseAdapter<KnownChainIds.DogecoinMainnet>
getFeeAssetId(): AssetId {
return this.assetId
}

async getFeeData({
to,
value,
chainSpecific: { from, pubkey, opReturnData },
sendMax = false,
}: GetFeeDataInput<KnownChainIds.DogecoinMainnet>): Promise<
FeeDataEstimate<KnownChainIds.DogecoinMainnet>
> {
if (!to) throw new Error('to is required')
if (!value) throw new Error('value is required')
if (!pubkey) throw new Error('pubkey is required')

const { fast, average, slow } = await this.providers.http.getNetworkFees()

if (!(fast?.satsPerKiloByte && average?.satsPerKiloByte && slow?.satsPerKiloByte)) {
throw new Error('UtxoBaseAdapter: failed to get fee data')
}

// sane default for invalid fee data from the node
// see: https://github.com/dogecoin/dogecoin/issues/3385
if (fast.satsPerKiloByte <= 0) fast.satsPerKiloByte = 500000000 // 5 DOGE per kB
if (average.satsPerKiloByte <= 0) average.satsPerKiloByte = 100000000 // 1 DOGE per kB
if (slow.satsPerKiloByte <= 0) slow.satsPerKiloByte = 50000000 // .5 DOGE per kB

// ensure higher confirmation speeds never have lower fees than lower confirmation speeds
if (slow.satsPerKiloByte > average.satsPerKiloByte)
average.satsPerKiloByte = slow.satsPerKiloByte
if (average.satsPerKiloByte > fast.satsPerKiloByte)
fast.satsPerKiloByte = average.satsPerKiloByte

const utxos = await this.providers.http.getUtxos({ pubkey })

const utxoSelectInput = { from, to, value, opReturnData, utxos, sendMax }

// We have to round because coinselect library uses sats per byte which cant be decimals
const fastPerByte = String(Math.round(fast.satsPerKiloByte / 1000))
const averagePerByte = String(Math.round(average.satsPerKiloByte / 1000))
const slowPerByte = String(Math.round(slow.satsPerKiloByte / 1000))

const { fee: fastFee } = utxoSelect({ ...utxoSelectInput, satoshiPerByte: fastPerByte })
const { fee: averageFee } = utxoSelect({ ...utxoSelectInput, satoshiPerByte: averagePerByte })
const { fee: slowFee } = utxoSelect({ ...utxoSelectInput, satoshiPerByte: slowPerByte })

return {
fast: { txFee: String(fastFee), chainSpecific: { satoshiPerByte: fastPerByte } },
average: { txFee: String(averageFee), chainSpecific: { satoshiPerByte: averagePerByte } },
slow: { txFee: String(slowFee), chainSpecific: { satoshiPerByte: slowPerByte } },
}
}
}

0 comments on commit fc372b8

Please sign in to comment.