Skip to content

Commit

Permalink
feat: Implement redirection to 'Ft Merge' when encountering 'Too many…
Browse files Browse the repository at this point in the history
… token-utxos' error
  • Loading branch information
AricRedemption committed Apr 13, 2024
1 parent 58e03fa commit 54cba85
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/global-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type SuccessResult = {
type FailedResult = {
status: 'failed'
message: string
router?: string
}
type WarningResult = {
status: 'warning'
Expand Down
39 changes: 23 additions & 16 deletions src/pages/settings/components/FTMerge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ const ftManager = ref()
const operation = ref('')
const loading = ref(false)
const isRefresh = ref(true)
const assetLoading = ref(true)
const currentGenesis = ref('')
const currentCodehash = ref('')
const isOpenResultModal = ref(false)
const transactionResult = ref<TransactionResult>()
const ftAsssets = ref<(Asset & { utxoCount: number })[]>([])
const splitCount = 10
const testSplit = false
const NeedToMergeCount = 3
Expand All @@ -33,9 +35,10 @@ type Receiver = {
const split = async (genesis: string, codehash: string, symbol: string, decimal: number) => {
try {
loading.value = true
operation.value = 'split'
currentGenesis.value = genesis
currentCodehash.value = codehash
operation.value = 'split'
const network: API_NET = (await getNetwork()) as API_NET
const purse = await getPrivateKey('mvc')
const apiHost = await getApiHost()
Expand All @@ -47,10 +50,9 @@ const split = async (genesis: string, codehash: string, symbol: string, decimal:
apiHost,
})
let receivers: Receiver[] = []
for (let i = 0; i < 10; i++) {
for (let i = 0; i < splitCount; i++) {
receivers.push({ address: address.value, amount: '1' })
}
loading.value = true
let { txid: splitTxId } = await ftManager
.transfer({
genesis,
Expand All @@ -59,11 +61,6 @@ const split = async (genesis: string, codehash: string, symbol: string, decimal:
senderWif: purse,
})
.catch((e) => {
isOpenResultModal.value = true
transactionResult.value = {
status: 'failed',
message: e,
}
throw e
})
isRefresh.value = true
Expand All @@ -81,12 +78,13 @@ const split = async (genesis: string, codehash: string, symbol: string, decimal:
},
}
} catch (error) {
isOpenResultModal.value = true
transactionResult.value = {
status: 'failed',
message: error as string,
}
} finally {
loading.value = true
loading.value = false
}
}
Expand Down Expand Up @@ -149,19 +147,23 @@ const { isLoading, data: mvcAssets } = useMVCAssetsQuery(address, {
// TODO: Change computed
watch(
[mvcAssets, ftManager, isRefresh],
([assets, manager, _isRefresh]) => {
async ([assets, manager, _isRefresh]) => {
if (manager && assets && _isRefresh) {
ftAsssets.value = []
assetLoading.value = true
const _assets: (Asset & { utxoCount: number })[] = []
for (let asset of assets || []) {
const { codeHash, genesis } = asset
manager.api.getFungibleTokenUnspents(codeHash, genesis, address.value).then((data: any) => {
ftAsssets.value.push({
await manager.api.getFungibleTokenUnspents(codeHash, genesis, address.value).then((data: any) => {
_assets.push({
...asset,
utxoCount: data.length,
})
isRefresh.value = false
})
}
_assets.sort((a, b) => b.tokenName.localeCompare(a.tokenName))
ftAsssets.value = _assets
assetLoading.value = false
}
},
{ immediate: true }
Expand Down Expand Up @@ -200,11 +202,16 @@ onMounted(async () => {
</div>
</div>
<div class="-mx-5 px-5 bg-gray-light py-3">Token</div>
<div class="py-16 text-center" v-if="isLoading">Token List Loading...</div>
<div v-for="(asset, index) in ftAsssets" :key="index" v-else-if="hasMergeToken">
<div class="flex items-center justify-between py-3" v-if="asset.utxoCount > NeedToMergeCount">
<div class="py-16 text-center" v-if="isLoading || assetLoading">Token List Loading...</div>
<div v-for="(asset, index) in ftAsssets" :key="index" v-else-if="hasMergeToken || testSplit">
<div class="flex items-center justify-between py-3" v-if="asset.utxoCount > NeedToMergeCount || testSplit">
<div class="flex items-center gap-3">
<UseImage :src="asset.logo" v-if="asset.logo && asset.codeHash" class="h-10 w-10 rounded-md">
<template #loading>
<div class="h-10 w-10 text-center leading-10 rounded-full text-white text-base bg-[#1E2BFF]">
{{ asset.symbol[0].toLocaleUpperCase() }}
</div>
</template>
<template #error>
<div class="h-10 w-10 text-center leading-10 rounded-full text-white text-base bg-[#1E2BFF]">
{{ asset.symbol[0].toLocaleUpperCase() }}
Expand Down
16 changes: 13 additions & 3 deletions src/pages/wallet/SendToken.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,19 @@ async function send() {
.catch((err) => {
isOpenConfirmModal.value = false
error.value = err.message
transactionResult.value = {
status: 'failed',
message: err.message,
if (err instanceof Error) {
if (err.message === 'Too many token-utxos, should merge them to continue.') {
transactionResult.value = {
router: 'ft-merge',
status: 'failed',
message: 'Please merge token-utxos to continue. Click OK to go to the FT Merge page.',
}
}
} else {
transactionResult.value = {
status: 'failed',
message: err.message,
}
}
isOpenResultModal.value = true
Expand Down
27 changes: 24 additions & 3 deletions src/pages/wallet/components/TransactionResultModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type SuccessTxsResult = {
type FailedResult = {
status: 'failed'
message: string
router?: string
}
type WarningResult = {
status: 'warning'
Expand All @@ -56,7 +57,7 @@ function closeModal() {
function ok() {
closeModal()
if (props.result?.status === 'success') {
router.push('/')
router.replace('/')
}
}
Expand Down Expand Up @@ -154,9 +155,29 @@ const toResultTxs = async (txId: string) => {
</template>

<template #control>
<div class="">
<button class="main-btn-bg w-full rounded-lg py-3 text-sm text-sky-100 outline-none" @click="ok">OK</button>
<div class="flex items-center justify-center gap-4" v-if="result && result.status === 'failed' && result.router">
<button
@click="closeModal"
class="box-border border border-gray-primary w-[133px] rounded-lg py-2.5 text-sm text-black-primary"
>
Cancel
</button>
<button
class="main-btn-bg rounded-lg py-3 text-sm text-sky-100 px-4"
@click="$router.push({ name: (result as FailedResult).router })"
>
Go To
{{
(result as FailedResult)
.router!.split('-')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ')
}}
</button>
</div>
<button v-else class="main-btn-bg w-full rounded-lg py-3 text-sm text-sky-100 outline-none" @click="ok">
OK
</button>
</template>
</Modal>
</template>
Expand Down
1 change: 1 addition & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ const routes = [
},
{
path: '/settings/toolkit/ft-merge',
name: 'ft-merge',
component: () => import('./pages/settings/components/FTMerge.vue'),
meta: {
secondaryHeader: true,
Expand Down

0 comments on commit 54cba85

Please sign in to comment.