Skip to content

Commit

Permalink
📦 VERSION: 3.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
AricRedemption committed Aug 30, 2024
1 parent 1decbf8 commit 4c2fb29
Show file tree
Hide file tree
Showing 18 changed files with 1,434 additions and 1,475 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@headlessui/tailwindcss": "^0.1.3",
"@headlessui/vue": "^1.7.12",
"@heroicons/vue": "^2.0.16",
"@metalet/utxo-wallet-service": "0.3.18",
"@metalet/utxo-wallet-service": "0.3.19",
"@okxweb3/crypto-lib": "^1.0.1",
"@rollup/plugin-alias": "^4.0.3",
"@scure/bip39": "^1.2.2",
Expand Down Expand Up @@ -109,7 +109,7 @@
"vite-svg-loader": "^5.1.0",
"vitest": "^0.34.3",
"vue-svg-loader": "^0.16.0",
"vue-tsc": "^1.0.24",
"vue-tsc": "^2.1.2",
"webextension-polyfill": "^0.10.0",
"yarn": "^1.22.21"
}
Expand Down
31 changes: 31 additions & 0 deletions src/components/GoToMerge.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script setup lang="ts">
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from '@/components/ui/alert-dialog'
const open = defineModel('open', { type: Boolean })
</script>

<template>
<AlertDialog v-model:open="open">
<AlertDialogContent class="bg-white">
<AlertDialogHeader>
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
<AlertDialogDescription>
This action cannot be undone. This will permanently delete your account and remove your data from our servers.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction>Continue</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</template>
2 changes: 1 addition & 1 deletion src/lib/actions/btc/sign-psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export async function process({
// psbt.signInput(v.index, keyPair, v.sighashTypes)
// })
for (let i = 0; i < options.toSignInputs.length; i++) {
const keyPair = wallet.getSigner(options!.toSignInputs![i].treehash)
const keyPair = wallet.getSigner({ treeHash: options!.toSignInputs![i].treehash })
const v = options.toSignInputs[i]
psbt.signInput(v.index, keyPair, v.sighashTypes)
}
Expand Down
5 changes: 2 additions & 3 deletions src/lib/actions/btc/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ export async function process(params: TransferParameters): Promise<{ txId: strin
const utxos = await getBtcUtxos(wallet.getAddress(), wallet.getScriptType() === ScriptType.P2PKH, true)

const { rawTx } = wallet.signTx(SignType.SEND, {
recipient: params.toAddress,
amount: new Decimal(params.satoshis).toNumber(),
feeRate,
utxos,
feeRate,
outputs: [{ address: params.toAddress, satoshis: new Decimal(params.satoshis).toNumber() }],
})

if (params.options?.noBroadcast) {
Expand Down
69 changes: 53 additions & 16 deletions src/lib/bridge-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,31 +540,60 @@ export async function mintBtc(
try {
const { orderId, bridgeAddress, bridgeRuleServiceAddress, bridgeRuleServiceMintFee } =
await createPrepayOrderMintBtc({
amount: mintAmount,
originTokenId,
addressType: scriptType,
publicKey,
originTokenId,
publicKeySign,
publicKeyReceive,
publicKeyReceiveSign,
addressType: scriptType,
amount: mintAmount.toString(),
})

const outputs = [
{
address: bridgeAddress,
satoshis: Number(mintAmount),
},
]

console.log('bridgeRuleServiceAddress', bridgeRuleServiceAddress)
console.log('bridgeRuleServiceMintFee', bridgeRuleServiceMintFee)

if (bridgeRuleServiceAddress && bridgeRuleServiceMintFee) {
outputs.push({
address: bridgeRuleServiceAddress,
satoshis: bridgeRuleServiceMintFee,
})
}

console.log('outputs', outputs)

const utxos = await getBtcUtxos(btcWallet.getAddress(), btcWallet.getScriptType() === ScriptType.P2PKH, false)

const { rawTx, txId } = btcWallet.signTx(SignType.SEND, {
console.log('utxos', utxos)
console.log('feeRate', feeRate)
console.log('outputs', outputs)

const { rawTx, txId, txInputs, txOutputs } = btcWallet.signTx(SignType.SEND, {
utxos,
amount: mintAmount,
recipient: bridgeAddress,
outputs,
feeRate,
})

console.log('txId', txId)
console.log('rawTx', rawTx)
console.log('txInputs', txInputs)
console.log('txOutputs', txOutputs)

await submitPrepayOrderMintBtc({
orderId,
txHex: rawTx,
})

return { txId, recipient: bridgeAddress }
} catch (error) {
console.log('error', error)

throw new Error((error as any).message || (error as any).msg)
}
}
Expand All @@ -582,16 +611,17 @@ export async function mintMrc20(
const publicKeyReceive = mvcWallet.getPublicKey().toString('hex')
const publicKeyReceiveSign = mvcWallet.signMessage(publicKeyReceive, 'base64')

const createResp = await createPrepayOrderMintMrc20({
amount: new Decimal(mintAmount).div(10 ** selectedPair.decimals).toFixed(),
originTokenId: selectedPair.originTokenId,
addressType: scriptType,
publicKey: publicKey,
publicKeySign: publicKeySign,
publicKeyReceive,
publicKeyReceiveSign: publicKeyReceiveSign,
})
const { orderId, bridgeAddress } = createResp
const { orderId, bridgeAddress, bridgeRuleServiceAddress, bridgeRuleServiceMintFee } =
await createPrepayOrderMintMrc20({
amount: new Decimal(mintAmount).div(10 ** selectedPair.decimals).toFixed(),
originTokenId: selectedPair.originTokenId,
addressType: scriptType,
publicKey: publicKey,
publicKeySign: publicKeySign,
publicKeyReceive,
publicKeyReceiveSign: publicKeyReceiveSign,
})

const needRawTx = btcWallet.getScriptType() === ScriptType.P2PKH
const utxos = await getBtcUtxos(btcWallet.getAddress(), needRawTx, false)
const mrc20Utxos = await getMRC20Utxos(btcWallet.getAddress(), selectedPair.originTokenId, needRawTx)
Expand All @@ -610,6 +640,13 @@ export async function mintMrc20(
},
]),
revealAddr: bridgeAddress,
service:
bridgeRuleServiceAddress && bridgeRuleServiceMintFee
? {
address: bridgeRuleServiceAddress,
satoshis: bridgeRuleServiceMintFee,
}
: undefined,
})

const submitPrepayOrderMintDto = {
Expand Down
12 changes: 1 addition & 11 deletions src/lib/wallets/btc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ import { getBtcNetwork } from '../network'
import { type UTXO } from '@/queries/utxos'
import { getCurrentWallet } from '../wallet'
import { createPayment } from '../bip32-deriver'
import { Account, getAddressType } from '@/lib/account'
import { Chain } from '@metalet/utxo-wallet-service'
import { Payment, Psbt, Transaction } from 'bitcoinjs-lib'
import { Chain, ScriptType } from '@metalet/utxo-wallet-service'
import { fetchBtcTxHex, broadcastBTCTx } from '@/queries/transaction'

// TODO: add safe utxo

function calculateFee(psbt: Psbt, feeRate: number): number {
const tx = psbt.extractTransaction()

Expand Down Expand Up @@ -107,11 +104,6 @@ export const split = async (feeRate: number, utxos: UTXO[]) => {
let psbt = await buildPsbt(utxos, 0)

psbt = await buildPsbt(utxos, Math.ceil(psbt.extractTransaction().virtualSize() * feeRate))
// console.log('virtualSize', psbt.extractTransaction().virtualSize())

// console.log(Math.floor(new Decimal(68971 * 2).div(psbt.extractTransaction().virtualSize()).toNumber()))

// return { txId: psbt.extractTransaction().getId() }

const txId = await broadcastBTCTx(psbt.extractTransaction().toHex())
return { txId }
Expand Down Expand Up @@ -155,6 +147,4 @@ export const merge = async (feeRate: number, utxos: UTXO[]) => {
psbt = await buildPsbt(utxos, total.minus(fee))

return { txId: psbt.extractTransaction().getId(), total, fee, rawTx: psbt.extractTransaction().toHex() }

// return await this.broadcast(psbt)
}
5 changes: 2 additions & 3 deletions src/pages/authorize/BTCTransfer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ watch(
const address = wallet.getAddress()
const utxos = await getBtcUtxos(address, wallet.getScriptType() === ScriptType.P2PKH, true)
const { txInputs, txOutputs } = wallet.signTx(SignType.SEND, {
recipient: props.params.toAddress,
amount: new Decimal(props.params.satoshis).div(1e8).toNumber(),
feeRate: _currentFeeRate,
utxos,
feeRate: _currentFeeRate,
outputs: [{ address: props.params.toAddress, satoshis: new Decimal(props.params.satoshis).toNumber() }],
})
inputs.value = txInputs
outputs.value = txOutputs
Expand Down
81 changes: 31 additions & 50 deletions src/pages/settings/components/BTCMerge.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { getBtcUtxos } from '@/queries/utxos'
import { getBtcUtxos, useBTCUtxosQuery } from '@/queries/utxos'
import { prettifyAddress, prettifyTxId } from '@/lib/formatters'
import { useBTCBalanceQuery } from '@/queries/balance'
import LoadingIcon from '@/components/LoadingIcon.vue'
Expand All @@ -24,9 +24,7 @@ import { broadcastBTCTx } from '@/queries/transaction'
import { sleep } from '@/lib/helpers'
import Copy from '@/components/Copy.vue'
const utxos = ref()
const mergeNum = ref('50')
const isLoading = ref(true)
const feeRate = ref<number>()
const operationLock = ref(false)
const isOpenResultModal = ref(false)
Expand All @@ -49,20 +47,11 @@ const { data: balance } = useBTCBalanceQuery(address, {
enabled: computed(() => !!currentBTCWallet.value && !!address.value),
needRawTx: currentBTCWallet.value?.getScriptType() === ScriptType.P2PKH,
})
watch(
address,
(newAddress) => {
if (newAddress) {
const needRawTx = currentBTCWallet.value!.getScriptType() === ScriptType.P2PKH
getBtcUtxos(newAddress, needRawTx, false).then((_utxos) => {
utxos.value = _utxos
isLoading.value = false
})
}
},
{ immediate: true }
)
const { data: utxos, isLoading } = useBTCUtxosQuery(address, {
useUnconfirmed: false,
enabled: computed(() => !!currentBTCWallet.value && !!address.value),
needRawTx: currentBTCWallet.value?.getScriptType() === ScriptType.P2PKH,
})
const mergeDisabled = computed(() => {
return !utxos.value || utxos.value.length < 5 || !feeRate.value || operationLock.value
Expand Down Expand Up @@ -188,27 +177,29 @@ const broadcast = () => {
</Select>
</div>

<div class="h-80 overflow-y-auto nicer-scrollbar -mr-8 pr-8 w-full overflow-x-hidden" v-if="transactions.length">
<table class="min-w-full table-auto w-full">
<div class="h-80 overflow-y-auto nicer-scrollbar -mr-4 pr-4 w-full overflow-x-hidden" v-if="transactions.length">
<table class="w-full table-fixed">
<thead>
<tr>
<th class="px-4 py-2 text-xs"></th>
<th class="px-4 py-2 text-xs">TxID</th>
<th class="px-4 py-2 text-xs">Fee</th>
<th class="px-4 py-2 text-xs">Total Input</th>
<th class="px-4 py-2 text-xs">Total Output</th>
<th class="text-xs w-6"></th>
<th class="text-xs w-28">TxID</th>
<th class="text-xs">Fee</th>
<th class="text-xs">Total Input</th>
<th class="text-xs">Total Output</th>
</tr>
</thead>
<tbody>
<tr v-for="(transaction, index) in transactions" :key="index">
<td class="border px-4 py-2 text-xs">{{ index + 1 }}</td>
<td class="border px-4 py-2 text-xs flex items-center gap-2">
{{ prettifyTxId(transaction.txId, 4) }}
<Copy title="RawTx Copied" :text="transaction.rawTx" :show-content="false" />
<tr v-for="(transaction, index) in transactions" :key="transaction.txId">
<td class="border text-xs text-center p-1">{{ index + 1 }}</td>
<td class="border text-xs p-1">
<div class="flex items-center justify-center gap-2">
{{ prettifyTxId(transaction.txId, 4) }}
<Copy title="RawTx Copied" :text="transaction.rawTx" :show-content="false" class="size-5" />
</div>
</td>
<td class="border px-4 py-2 text-xs">{{ new Decimal(transaction.fee).div(1e8).toFixed() }} BTC</td>
<td class="border px-4 py-2 text-xs">{{ transaction.total.div(1e8).toFixed() }} BTC</td>
<td class="border px-4 py-2 text-xs">
<td class="border text-xs p-1">{{ new Decimal(transaction.fee).div(1e8).toFixed() }} BTC</td>
<td class="border text-xs p-1">{{ transaction.total.div(1e8).toFixed() }} BTC</td>
<td class="border text-xs p-1">
{{ transaction.total.minus(transaction.fee).div(1e8).toFixed() }} BTC
</td>
</tr>
Expand All @@ -229,27 +220,17 @@ const broadcast = () => {
Split
</button> -->

<button
@click="mergeFn"
v-if="!transactions.length"
:disabled="mergeDisabled"
:class="[
{ 'cursor-not-allowed opacity-50': mergeDisabled },
'bg-blue-primary text-white text-xs w-61.5 rounded-3xl my-12 py-4 mx-auto flex items-center justify-center gap-x-2',
]"
>
<button @click="mergeFn" v-if="!transactions.length" :disabled="mergeDisabled" :class="[
{ 'cursor-not-allowed opacity-50': mergeDisabled },
'bg-blue-primary text-white text-xs w-61.5 rounded-3xl my-12 py-4 mx-auto flex items-center justify-center gap-x-2',
]">
<ArrowPathIcon class="animate-spin w-4 h-4" v-if="operationLock" />
Merge
</button>
<button
v-else
@click="broadcast"
:disabled="!transactions.length"
:class="[
{ 'cursor-not-allowed opacity-50': mergeDisabled },
'bg-blue-primary text-white text-xs w-61.5 rounded-3xl my-12 py-4 mx-auto flex items-center justify-center gap-x-2',
]"
>
<button v-else @click="broadcast" :disabled="!transactions.length" :class="[
{ 'cursor-not-allowed opacity-50': mergeDisabled },
'bg-blue-primary text-white text-xs w-61.5 rounded-3xl my-12 py-4 mx-auto flex items-center justify-center gap-x-2',
]">
<ArrowPathIcon class="animate-spin w-4 h-4" v-if="operationLock" />
Broadcast
</button>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/settings/components/SpaceMerge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FEEB } from '@/data/config'
import { Avatar } from '@/components'
import { getApiHost } from '@/lib/host'
import { getNetwork } from '@/lib/network'
import { useMVCUTXOQuery } from '@/queries/utxos'
import { useMVCUtxosQuery } from '@/queries/utxos'
import { prettifyAddress } from '@/lib/formatters'
import { Chain } from '@metalet/utxo-wallet-service'
import { fetchSpaceBalance } from '@/queries/balance'
Expand Down Expand Up @@ -48,7 +48,7 @@ const merge = async () => {
}
}
const { isLoading, data } = useMVCUTXOQuery(address, { enabled: computed(() => !!address.value) })
const { isLoading, data } = useMVCUtxosQuery(address, { enabled: computed(() => !!address.value) })
</script>

<template>
Expand Down
Loading

0 comments on commit 4c2fb29

Please sign in to comment.