-
Notifications
You must be signed in to change notification settings - Fork 394
/
assets.ts
264 lines (244 loc) · 7.15 KB
/
assets.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import { createSlice } from "@reduxjs/toolkit"
import { ethers } from "ethers"
import type { RootState } from "."
import { AddressOnNetwork } from "../accounts"
import {
AnyAsset,
AnyAssetAmount,
AnyAssetMetadata,
isSmartContractFungibleAsset,
SmartContractFungibleAsset,
} from "../assets"
import { ERC20_INTERFACE } from "../lib/erc20"
import logger from "../lib/logger"
import { EVMNetwork, sameNetwork } from "../networks"
import { NormalizedEVMAddress } from "../types"
import { removeAssetReferences, updateAssetReferences } from "./accounts"
import { createBackgroundAsyncThunk } from "./utils"
import { isBaseAssetForNetwork, isSameAsset } from "./utils/asset-utils"
import { getProvider } from "./utils/contract-utils"
export type SingleAssetState = AnyAsset
export type AssetsState = SingleAssetState[]
export const initialState: AssetsState = []
const assetsSlice = createSlice({
name: "assets",
initialState,
reducers: {
assetsLoaded: (
immerState,
{ payload: newAssets }: { payload: AnyAsset[] },
) => {
const mappedAssets: { [sym: string]: SingleAssetState[] } = {}
// bin existing known assets
immerState.forEach((asset) => {
if (mappedAssets[asset.symbol] === undefined) {
mappedAssets[asset.symbol] = []
}
// if an asset is already in state, assume unique checks have been done
// no need to check network, contract address, etc
mappedAssets[asset.symbol].push(asset)
})
// merge in new assets
newAssets.forEach((newAsset) => {
if (mappedAssets[newAsset.symbol] === undefined) {
mappedAssets[newAsset.symbol] = [
{
...newAsset,
},
]
} else {
const duplicateIndexes = mappedAssets[newAsset.symbol].reduce<
number[]
>((acc, existingAsset, id) => {
if (isSameAsset(newAsset, existingAsset)) {
acc.push(id)
}
return acc
}, [])
// if there aren't duplicates, add the asset
if (duplicateIndexes.length === 0) {
mappedAssets[newAsset.symbol].push({
...newAsset,
})
} else {
// TODO if there are duplicates... when should we replace assets?
duplicateIndexes.forEach((id) => {
// Update only the metadata for the duplicate
mappedAssets[newAsset.symbol][id] = {
...mappedAssets[newAsset.symbol][id],
metadata: newAsset.metadata,
}
})
}
}
})
return Object.values(mappedAssets).flat()
},
removeAsset: (
immerState,
{ payload: removedAsset }: { payload: AnyAsset },
) => immerState.filter((asset) => !isSameAsset(asset, removedAsset)),
},
})
export const { assetsLoaded, removeAsset } = assetsSlice.actions
export default assetsSlice.reducer
export const updateAssetMetadata = createBackgroundAsyncThunk(
"assets/updateAssetMetadata",
async (
{
asset,
metadata,
}: {
asset: SmartContractFungibleAsset
metadata: AnyAssetMetadata
},
{ extra: { main } },
) => {
await main.updateAssetMetadata(asset, metadata)
},
)
export const refreshAsset = createBackgroundAsyncThunk(
"assets/refreshAsset",
async (
{
asset,
}: {
asset: SmartContractFungibleAsset
},
{ dispatch },
) => {
// Update assets slice
await dispatch(assetsLoaded([asset]))
// Update accounts slice cached data about this asset
await dispatch(updateAssetReferences(asset))
},
)
export const hideAsset = createBackgroundAsyncThunk(
"assets/hideAsset",
async (
{
asset,
}: {
asset: SmartContractFungibleAsset
},
{ extra: { main } },
) => {
await main.hideAsset(asset)
},
)
/**
* Removes the asset from the user interface.
* The token should be removed from the assets list and all references associated with it.
*/
export const removeAssetData = createBackgroundAsyncThunk(
"assets/removeAssetData",
async (
{
asset,
}: {
asset: SmartContractFungibleAsset
},
{ dispatch },
) => {
await dispatch(removeAsset(asset))
await dispatch(removeAssetReferences(asset))
},
)
/**
* Executes an asset transfer between two addresses, for a set amount. Supports
* an optional fixed gas limit.
*
* If the from address is not a writeable address in the wallet, this signature
* will not be possible.
*/
export const transferAsset = createBackgroundAsyncThunk(
"assets/transferAsset",
async ({
fromAddressNetwork: { address: fromAddress, network: fromNetwork },
toAddressNetwork: { address: toAddress, network: toNetwork },
assetAmount,
gasLimit,
nonce,
}: {
fromAddressNetwork: AddressOnNetwork
toAddressNetwork: AddressOnNetwork
assetAmount: AnyAssetAmount
gasLimit?: bigint
nonce?: number
}) => {
if (!sameNetwork(fromNetwork, toNetwork)) {
throw new Error("Only same-network transfers are supported for now.")
}
const provider = getProvider()
const signer = provider.getSigner()
if (isBaseAssetForNetwork(assetAmount.asset, fromNetwork)) {
logger.debug(
`Sending ${assetAmount.amount} ${assetAmount.asset.symbol} from ` +
`${fromAddress} to ${toAddress} as a base asset transfer.`,
)
await signer.sendTransaction({
from: fromAddress,
to: toAddress,
value: assetAmount.amount,
gasLimit,
nonce,
})
} else if (isSmartContractFungibleAsset(assetAmount.asset)) {
logger.debug(
`Sending ${assetAmount.amount} ${assetAmount.asset.symbol} from ` +
`${fromAddress} to ${toAddress} as an ERC20 transfer.`,
)
const token = new ethers.Contract(
assetAmount.asset.contractAddress,
ERC20_INTERFACE,
signer,
)
const transactionDetails = await token.populateTransaction.transfer(
toAddress,
assetAmount.amount,
)
await signer.sendUncheckedTransaction({
...transactionDetails,
gasLimit: gasLimit ?? transactionDetails.gasLimit,
nonce,
})
} else {
throw new Error(
"Only base and fungible smart contract asset transfers are supported for now.",
)
}
},
)
export const importCustomToken = createBackgroundAsyncThunk(
"assets/importCustomToken",
async (
{
asset,
}: {
asset: SmartContractFungibleAsset
},
{ extra: { main } },
) => ({ success: await main.importCustomToken(asset) }),
)
export const checkTokenContractDetails = createBackgroundAsyncThunk(
"assets/checkTokenContractDetails",
async (
{
contractAddress,
network,
}: { contractAddress: NormalizedEVMAddress; network: EVMNetwork },
{ getState, extra: { main } },
) => {
const state = getState() as RootState
const currentAccount = state.ui.selectedAccount
try {
return await main.queryCustomTokenDetails(contractAddress, {
...currentAccount,
network,
})
} catch (error) {
// FIXME: Rejected thunks return undefined instead of throwing
return null
}
},
)