Skip to content

Commit

Permalink
fix: Catch createAction error and updata inscribe api
Browse files Browse the repository at this point in the history
  • Loading branch information
AricRedemption committed Mar 20, 2024
1 parent 99f4aeb commit cbdbaf9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
11 changes: 7 additions & 4 deletions src/content-script/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export async function createAction(
}
if (event.data?.nonce === nonce) {
window.removeEventListener('message', actionListener)
if (event.data?.res?.error) {
throw new Error(event.data.res.error)
}
// if (event.data?.res?.error) {
// throw new Error(event.data.res.error)
// }
if (callback && typeof callback === 'function') {
callback(event.data)
}
Expand All @@ -51,8 +51,11 @@ export async function createAction(
}
window.addEventListener('message', actionListener)
}
return await new Promise((resolve) => {
return await new Promise((resolve, reject) => {
subscribe((echo: Echo) => {
if (echo.res.error) {
reject(echo.res.error)
}
resolve(echo.res)
})
})
Expand Down
37 changes: 23 additions & 14 deletions src/lib/actions/btc/inscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ export class InscriptionTool {
request.metaidDataList.forEach((metaidData) => {
tool.inscriptionTxCtxDataList.push(createMetaIdTxCtxData(network, metaidData, randomKeyPairs.publicKey))
})


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

const insufficient = tool.buildCommitTx(
network,
Expand All @@ -125,16 +125,14 @@ export class InscriptionTool {
minChangeValue,
keyPairs.privateKey!
)
console.log('buildCommitTx', insufficient)

if (insufficient) {
return tool
}

tool.signCommitTx(request.commitTxPrevOutputList, keyPairs.privateKey!)
console.log('signCommitTx')
tool.completeRevealTx(randomKeyPairs.privateKey!)
console.log('completeRevealTx')

return tool
}

Expand Down Expand Up @@ -217,6 +215,9 @@ export class InscriptionTool {
txForEstimate.outs = txForEstimate.outs.slice(0, txForEstimate.outs.length - 1)
const feeWithoutChange = Math.floor(txForEstimate.virtualSize() * commitFeeRate)
if (totalSenderAmount - totalRevealPrevOutputValue - feeWithoutChange < 0) {
throw new Error(
`Insufficient funds. Sender amount: ${totalSenderAmount} sats, Required amount: ${totalRevealPrevOutputValue + feeWithoutChange} sats`
)
this.mustCommitTxFee = fee
return true
}
Expand Down Expand Up @@ -457,16 +458,24 @@ export async function process({
}))
const signer = (await getSigner('btc')) as BIP32Interface

const { commitTx, revealTxs, commitCost, revealCost } = inscribe(network, { ...data, commitTxPrevOutputList }, signer)
if (commitTx === '') {
throw new Error('Insufficient funds')
}
try {
const { commitTx, revealTxs, commitCost, revealCost } = inscribe(
network,
{ ...data, commitTxPrevOutputList },
signer
)
if (commitTx === '') {
throw new Error('Insufficient funds')
}

if (!options.noBroadcast) {
const commitTxId = await broadcastBTCTx(commitTx)
await sleep(1000)
const [...revealTxIds] = await Promise.all([...revealTxs.map((revealTx) => broadcastBTCTx(revealTx))])
return { commitTxId, revealTxIds, commitCost, revealCost }
if (!options.noBroadcast) {
const commitTxId = await broadcastBTCTx(commitTx)
await sleep(1000)
const [...revealTxIds] = await Promise.all([...revealTxs.map((revealTx) => broadcastBTCTx(revealTx))])
return { commitTxId, revealTxIds, commitCost, revealCost }
}
return { commitTxHex: commitTx, revealTxsHex: revealTxs, commitCost, revealCost }
} catch (error) {
throw error
}
return { commitTxHex: commitTx, revealTxsHex: revealTxs, commitCost, revealCost }
}
1 change: 0 additions & 1 deletion src/pages/authorize/Inscribe.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ actions.Inscribe.process({ ...props.params, options: { noBroadcast: true } })
commitCost: number
revealCost: number
}) => {
console.log('Inscribe', [commitTxHex, ...revealTxsHex])
txHexs.value = [commitTxHex, ...revealTxsHex]
commitCost.value = _commitCost
revealCost.value = _revealCost
Expand Down

0 comments on commit cbdbaf9

Please sign in to comment.