-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathuseTeleport.ts
165 lines (144 loc) · 3.87 KB
/
useTeleport.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { web3Enable } from '@polkadot/extension-dapp'
import type { SubmittableResult } from '@polkadot/api'
import {
Chain,
allowedTransitions,
chainToPrefixMap,
prefixToChainMap,
} from '@/utils/teleport'
import { getss58AddressByPrefix } from '@/utils/account'
import { txCb } from '@/utils/transactionExecutor'
export type TeleportParams = {
amount: number
from: Chain
to: Chain
fromAddress: string
toAddress: string
currency: string
}
export default function (fetchBalancePeriodically: boolean = false) {
const isError = ref<boolean>(false)
const txId = ref<string | null>(null)
const { isLoading, status, initTransactionLoader, stopLoader }
= useTransactionStatus()
const { accountId } = useAuth()
const { assets } = usePrefix()
const { $i18n, $consola } = useNuxtApp()
const { decimalsOf } = useChain()
const { urlPrefix } = usePrefix()
const { fetchMultipleBalance, chainBalances } = useMultipleBalance(
fetchBalancePeriodically,
)
const chain = computed<Chain | null>(
() => prefixToChainMap[urlPrefix.value] || null,
)
const isAvailable = computed(() =>
Object.keys(allowedTransitions).includes(chain.value || ''),
)
const teleport = async ({
amount,
from,
to,
fromAddress,
toAddress,
currency,
onSuccess,
onError,
}: TeleportParams & {
onSuccess?: (blockHash) => void
onError?: () => void
}) => {
if (!amount || amount < 0) {
return
}
await web3Enable('Kodadot')
let isFirstStatus = true
initTransactionLoader()
isError.value = false
const transactionHandler = txCb(
({ blockHash }) => {
$consola.log(`Transaction finalized at blockHash ${blockHash}`)
status.value = TransactionStatus.Finalized
isLoading.value = false
if (onSuccess) {
onSuccess(blockHash)
}
},
(dispatchError) => {
warningMessage(dispatchError.toString())
stopLoader()
isError.value = true
if (onError) {
onError()
}
},
(submittableResult: SubmittableResult) => {
const { txHash } = submittableResult
if (isFirstStatus) {
$consola.log(`Transaction hash is ${txHash.toHex()}`)
txId.value = txHash.toHex()
status.value = TransactionStatus.Block
isFirstStatus = false
}
},
)
const errorHandler = () => {
warningMessage($i18n.t('general.tx.cancelled'), { reportable: false })
isLoading.value = false
status.value = TransactionStatus.Cancelled
isError.value = true
if (onError) {
onError()
}
}
const promise = await getTransaction({
amount: amount,
from: from,
to: to,
address: toAddress,
currency: currency,
})
if (promise === undefined) {
errorHandler()
return
}
const injector = await getAddress(toDefaultAddress(fromAddress))
promise
.signAndSend(fromAddress, { signer: injector.signer }, transactionHandler)
.catch(errorHandler)
}
const getAddressByChain = (chain: Chain) => {
return (
accountId.value
&& getss58AddressByPrefix(accountId.value, chainToPrefixMap[chain])
)
}
const getChainTokenDecimals = (chain: Chain) => {
switch (chain) {
case Chain.KUSAMA:
case Chain.ASSETHUBKUSAMA:
return assets(5).decimals
case Chain.POLKADOT:
return decimalsOf('dot')
case Chain.ASSETHUBPOLKADOT:
return decimalsOf('ahp')
}
}
const fetchChainsBalances = (chains: Chain[]) => {
const chainPrefixes = chains.map(chain => chainToPrefixMap[chain])
return fetchMultipleBalance(chainPrefixes)
}
return {
chain,
txId,
status,
isError,
isLoading,
isAvailable,
chainBalances,
teleport,
getAddressByChain,
getChainTokenDecimals,
fetchChainsBalances,
}
}