Skip to content

Commit

Permalink
fix: inscribe total cost display
Browse files Browse the repository at this point in the history
  • Loading branch information
AricRedemption committed May 13, 2024
1 parent 18039f6 commit 87c1243
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
44 changes: 32 additions & 12 deletions src/lib/actions/btc/inscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export class InscriptionTool {
request.metaidDataList.forEach((metaidData) => {
tool.inscriptionTxCtxDataList.push(createMetaIdTxCtxData(network, metaidData, randomKeyPairs.publicKey))
})


const totalRevealPrevOutputValue = tool.buildEmptyRevealTx(network, revealOutValue, request.feeRate)

Expand Down Expand Up @@ -394,7 +393,12 @@ function createMetaIdTxCtxData(
}
}

export function inscribe(network: bitcoin.Network, request: InscriptionRequest, keyPairs: BIP32Interface) {
export function inscribe(
network: bitcoin.Network,
request: InscriptionRequest,
keyPairs: BIP32Interface,
address: string
) {
const tool = InscriptionTool.newInscriptionTool(network, request, keyPairs)
if (tool.mustCommitTxFee > 0) {
return {
Expand All @@ -405,20 +409,33 @@ export function inscribe(network: bitcoin.Network, request: InscriptionRequest,
commitAddrs: tool.commitAddrs,
commitCost: 0,
revealCost: 0,
totalCost: 0,
}
}

const commitCost =
tool.commitTxPrevOutputFetcher.reduce((accumulator, currentValue) => {
return accumulator + currentValue
}, 0) -
tool.commitTx.outs
.filter((out) => bitcoin.address.fromOutputScript(out.script, network) === address)
.reduce((total, out) => {
return total + out.value
}, 0)

const revealTotalOutput = (request.revealOutValue || defaultRevealOutValue) * tool.revealTxs.length

return {
commitTx: tool.commitTx.toHex(),
revealTxs: tool.revealTxs.map((revealTx) => revealTx.toHex()),
...tool.calculateFee(),
commitAddrs: tool.commitAddrs,
commitCost: tool.commitTxPrevOutputFetcher.reduce((accumulator, currentValue) => {
return accumulator + currentValue
}, 0),
revealCost: tool.revealTxPrevOutputFetcher.reduce((accumulator, currentValue) => {
return accumulator + currentValue
}, 0),
commitCost,
revealCost:
tool.revealTxPrevOutputFetcher.reduce((accumulator, currentValue) => {
return accumulator + currentValue
}, 0) - revealTotalOutput,
totalCost: commitCost - revealTotalOutput,
}
}

Expand All @@ -431,13 +448,15 @@ interface InscribeHexResult {
revealTxsHex: string[]
commitCost: number
revealCost: number
totalCost: number
}

interface InscribeTxIdResult {
commitTxId: string
revealTxIds: string[]
commitCost: number
revealCost: number
totalCost: number
}

export async function process({
Expand All @@ -459,10 +478,11 @@ export async function process({
const signer = (await getSigner('btc')) as BIP32Interface

try {
const { commitTx, revealTxs, commitCost, revealCost } = inscribe(
const { commitTx, revealTxs, commitCost, revealCost, totalCost } = inscribe(
network,
{ ...data, commitTxPrevOutputList },
signer
signer,
address
)
if (commitTx === '') {
throw new Error('Insufficient funds')
Expand All @@ -472,9 +492,9 @@ export async function process({
const commitTxId = await broadcastBTCTx(commitTx)
await sleep(1000)
const [...revealTxIds] = await Promise.all([...revealTxs.map((revealTx) => broadcastBTCTx(revealTx))])
return { commitTxId, revealTxIds, commitCost, revealCost }
return { commitTxId, revealTxIds, commitCost, revealCost, totalCost }
}
return { commitTxHex: commitTx, revealTxsHex: revealTxs, commitCost, revealCost }
return { commitTxHex: commitTx, revealTxsHex: revealTxs, commitCost, revealCost, totalCost }
} catch (error) {
throw error
}
Expand Down
10 changes: 7 additions & 3 deletions src/pages/authorize/Inscribe.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts" setup>
import { ref } from 'vue'
import actions from '@/data/authorize-actions'
import Copy from '@/components/Copy.vue'
import actions from '@/data/authorize-actions'
import LoadingIcon from '@/components/LoadingIcon.vue'
import { MetaidData } from '@/lib/actions/btc/inscribe'
import { ChevronLeftIcon } from '@heroicons/vue/24/outline'
Expand All @@ -22,6 +22,7 @@ const loading = ref(true)
const error = ref<Error>()
const commitCost = ref<number>(0)
const revealCost = ref<number>(0)
const totalCost = ref<number>(0)
const commitTxHex = ref<string>()
const revealTxsHex = ref<string[]>([])
const metaidDataList = props.params.data.metaidDataList
Expand All @@ -43,16 +44,19 @@ actions.Inscribe.process({ ...props.params, options: { noBroadcast: true } })
revealTxsHex: _revealTxsHex,
commitCost: _commitCost,
revealCost: _revealCost,
totalCost: _totalCost,
}: {
commitTxHex: string
revealTxsHex: string[]
commitCost: number
revealCost: number
totalCost: number
}) => {
commitCost.value = _commitCost
revealCost.value = _revealCost
commitTxHex.value = _commitTxHex
revealTxsHex.value = _revealTxsHex
totalCost.value = _totalCost
}
)
.catch((err: Error) => {
Expand Down Expand Up @@ -88,7 +92,7 @@ actions.Inscribe.process({ ...props.params, options: { noBroadcast: true } })
</div>
<div class="flex justify-between">
<div class="label">Total Cost</div>
<div class="text-xs flex gap-2">{{ (commitCost + revealCost) / 1e8 }} BTC</div>
<div class="text-xs flex gap-2">{{ totalCost / 1e8 }} BTC</div>
</div>
<div class="flex justify-between">
<div class="label">Fee Rate</div>
Expand Down Expand Up @@ -133,7 +137,7 @@ actions.Inscribe.process({ ...props.params, options: { noBroadcast: true } })
<div class="flex flex-col w-full gap-y-2">
<div class="flex justify-between">
<div class="label">Total Cost</div>
<div class="text-xs flex gap-2">{{ (commitCost + revealCost) / 1e8 }} BTC</div>
<div class="text-xs flex gap-2">{{ totalCost / 1e8 }} BTC</div>
</div>
<div class="flex justify-between">
<div class="label">Fee Rate</div>
Expand Down
3 changes: 1 addition & 2 deletions src/pages/nfts/InscriptionList.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<script setup lang="ts">
import dayjs from 'dayjs'
import { ref, computed } from 'vue'
import BRCToken from './BRCToken.vue'
import { useRouter } from 'vue-router'
import { getAddress } from '@/lib/account'
import { formatTimestamp } from '@/lib/formatters'
import LoadingIcon from '@/components/LoadingIcon.vue'
import { useInscriptionsInfiniteQuery } from '@/queries/inscribe'
import { formatTimestamp } from '@/lib/formatters'
const size = ref(10)
const address = ref()
Expand Down

0 comments on commit 87c1243

Please sign in to comment.