-
Notifications
You must be signed in to change notification settings - Fork 18
/
getPSP22Balances.ts
190 lines (161 loc) · 5.69 KB
/
getPSP22Balances.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { allPSP22Assets } from '@/assets'
import { psp22Abi } from '@/helpers/getAbi'
import { ApiPromise } from '@polkadot/api'
import { ContractPromise } from '@polkadot/api-contract'
import { AccountId } from '@polkadot/types/interfaces'
import { BN } from '@polkadot/util'
import { contractQuery } from './contractCall'
import { decodeOutput } from './decodeOutput'
import { BalanceFormatterOptions, formatBalance } from './formatBalance'
export type PSP22BalanceData = {
tokenSlug: string
tokenDecimals: number
tokenSymbol: string
iconPath: string
balance?: BN
balanceFormatted?: string
}
/**
* Default refresh interval for the PSP-22 token balances.
*/
export const PSP22_TOKEN_BALANCE_SUBSCRIPTION_INTERVAL = 60000
/**
* Returns the PSP-22 token balances of the given `address`.
*/
export const getPSP22Balances = async (
api: ApiPromise,
address: string | AccountId | undefined,
chainId: string,
formatterOptions?: BalanceFormatterOptions,
): Promise<PSP22BalanceData[]> => {
const psp22ContractMap: Record<string, ContractPromise> = {}
Object.entries(allPSP22Assets).forEach(([slug, tokenInfo]) => {
psp22ContractMap[slug] = new ContractPromise(api, psp22Abi, tokenInfo.metadata?.contractAddress)
})
if (!address) {
const result = Object.values(allPSP22Assets)
.filter(({ originChain }) => originChain === chainId)
.map(({ slug, decimals, symbol, iconPath }) => {
return {
tokenSlug: slug,
tokenDecimals: decimals,
tokenSymbol: symbol,
iconPath,
}
})
return result
}
const result = await Promise.all(
Object.values(allPSP22Assets)
.filter(({ originChain }) => originChain === chainId)
.map(async ({ slug, decimals, symbol, iconPath }) => {
let balance = new BN(0)
const contract = psp22ContractMap[slug]
const response = await contractQuery(api, '', contract, 'psp22::balanceOf', {}, [address])
const { isError, decodedOutput } = decodeOutput(response, contract, 'psp22::balanceOf')
if (isError) throw new Error(decodedOutput)
const _balance = response.output?.toPrimitive() as Record<string, any>
balance = new BN(response.output ? (_balance.ok as string) || (_balance.Ok as string) : '0')
if (!balance) throw new Error('Invalid fetched balances')
const data = {
tokenDecimals: decimals,
tokenSymbol: symbol,
balance,
}
const balanceFormatted = parsePSP22Balance(data, formatterOptions)
return {
balanceFormatted,
tokenSlug: slug,
iconPath,
...data,
}
}),
)
return result
}
/**
* Watches the PSP-22 token balances of the given `address` and returns it in a callback.
* The returned void function can be used to unsubscribe.
*/
export const watchPSP22Balances = (
api: ApiPromise,
address: string | AccountId | undefined,
callback: (data: PSP22BalanceData[]) => void,
chainId: string,
formatterOptions?: BalanceFormatterOptions,
): VoidFunction | null => {
const psp22ContractMap: Record<string, ContractPromise> = {}
Object.entries(allPSP22Assets).forEach(([slug, tokenInfo]) => {
psp22ContractMap[slug] = new ContractPromise(api, psp22Abi, tokenInfo.metadata?.contractAddress)
})
if (!address) {
const result = Object.values(allPSP22Assets)
.filter(({ originChain }) => originChain === chainId)
.map(({ slug, decimals, symbol, iconPath }) => {
return {
tokenSlug: slug,
tokenDecimals: decimals,
tokenSymbol: symbol,
iconPath,
}
})
callback(result)
return null
}
// Function to query the chain, parse data, and return promisified data
const fetchTokenBalances = async () =>
callback(
await Promise.all(
Object.values(allPSP22Assets)
.filter(({ originChain }) => originChain === chainId)
.map(async ({ slug, decimals, symbol, iconPath }) => {
let balance = new BN(0)
const contract = psp22ContractMap[slug]
const response = await contractQuery(api, '', contract, 'psp22::balanceOf', {}, [
address,
])
const { isError, decodedOutput } = decodeOutput(response, contract, 'psp22::balanceOf')
if (isError) throw new Error(decodedOutput)
const _balance = response.output?.toPrimitive() as Record<string, any>
balance = new BN(
response.output ? (_balance.ok as string) || (_balance.Ok as string) : '0',
)
if (!balance) throw new Error('Invalid fetched balances')
const data = {
tokenDecimals: decimals,
tokenSymbol: symbol,
balance,
}
const balanceFormatted = parsePSP22Balance(data, formatterOptions)
return {
balanceFormatted,
tokenSlug: slug,
iconPath,
...data,
}
}),
),
)
fetchTokenBalances()
// Create intervalId which can be used to unsubscribe
const intervalId = setInterval(fetchTokenBalances, PSP22_TOKEN_BALANCE_SUBSCRIPTION_INTERVAL)
return () => {
clearInterval(intervalId)
}
}
/**
* Helper to parse the fetched PSP22 token balance data.
*/
export const parsePSP22Balance = (
data: Omit<PSP22BalanceData, 'tokenSlug' | 'iconPath'>,
formatterOptions?: BalanceFormatterOptions,
): string => {
// Destructure necessary fields
const { tokenDecimals, tokenSymbol, balance } = data
// Format the balance
const balanceFormatted: string = formatBalance(undefined, balance, formatterOptions, {
tokenDecimals,
tokenSymbol,
})
return balanceFormatted
}