@otoplo/wallet-common 0.8.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +65 -52
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +409 -412
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/persistence/repository.ts +7 -12
- package/src/persistence/wallet-db.ts +216 -251
- package/src/services/asset.ts +190 -162
- package/src/services/cache.ts +26 -54
- package/src/types/db.types.ts +11 -18
- package/src/types/wallet.types.ts +10 -0
package/src/services/asset.ts
CHANGED
|
@@ -1,162 +1,190 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { getAssetFileUrl, getAssetMetadataUrl } from "../utils/asset";
|
|
3
|
-
import { balanceTotal, currentTimestamp, tokenIdToHex } from "../utils/common";
|
|
4
|
-
import type { WalletDB } from "../persistence/wallet-db";
|
|
5
|
-
import type { WalletCache } from "./cache";
|
|
6
|
-
import type { AssetInfo, Balance } from "../types/wallet.types";
|
|
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
|
-
this.
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
await this.walletDb.
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
private
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
1
|
+
import type { AssetMetadataEntity } from "../types/db.types";
|
|
2
|
+
import { getAssetFileUrl, getAssetMetadataUrl } from "../utils/asset";
|
|
3
|
+
import { balanceTotal, currentTimestamp, tokenIdToHex } from "../utils/common";
|
|
4
|
+
import type { WalletDB } from "../persistence/wallet-db";
|
|
5
|
+
import type { CachedAsset, WalletCache } from "./cache";
|
|
6
|
+
import type { AccountAsset, AssetInfo, AssetType, Balance } from "../types/wallet.types";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Bump when stored metadata must be fetched again, e.g. the assets API response changed.
|
|
10
|
+
* Rows of an older version are refetched when next used, with no DB migration.
|
|
11
|
+
*/
|
|
12
|
+
export const ASSET_METADATA_VERSION = 1;
|
|
13
|
+
|
|
14
|
+
export class AssetService {
|
|
15
|
+
|
|
16
|
+
private readonly walletDb: WalletDB;
|
|
17
|
+
private readonly walletCache: WalletCache;
|
|
18
|
+
|
|
19
|
+
constructor(walletDb: WalletDB, walletCache: WalletCache) {
|
|
20
|
+
this.walletDb = walletDb;
|
|
21
|
+
this.walletCache = walletCache;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Metadata of any asset, held or not: memory, then DB, then API.
|
|
26
|
+
* Metadata of an older version is refetched, and kept if the fetch fails.
|
|
27
|
+
*/
|
|
28
|
+
public async getAssetInfo(assetId: string): Promise<AssetInfo | undefined> {
|
|
29
|
+
const id = tokenIdToHex(assetId);
|
|
30
|
+
try {
|
|
31
|
+
const cached = this.walletCache.getAsset(id) ?? await this.loadFromDb(id);
|
|
32
|
+
if (cached?.version === ASSET_METADATA_VERSION) {
|
|
33
|
+
return cached.info;
|
|
34
|
+
}
|
|
35
|
+
return await this.fetchMetadata(id) ?? cached?.info;
|
|
36
|
+
} catch (e) {
|
|
37
|
+
console.error(e);
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* One page of the account's assets, straight from the DB.
|
|
44
|
+
* Rows of an older version are returned as they are and refetched in the background,
|
|
45
|
+
* so the next load shows the new metadata.
|
|
46
|
+
*/
|
|
47
|
+
public async getAssets(accountId: number, type: AssetType, pageNum: number, pageSize: number): Promise<AccountAsset[]> {
|
|
48
|
+
const rows = await this.walletDb.getPageAssets(accountId, type, pageNum, pageSize);
|
|
49
|
+
return rows.map(row => {
|
|
50
|
+
const id = row.tokenIdHex;
|
|
51
|
+
const info = parseAssetInfo(row.data);
|
|
52
|
+
if (info && !this.walletCache.getAsset(id)) {
|
|
53
|
+
this.walletCache.setAsset(id, { info, version: row.version });
|
|
54
|
+
}
|
|
55
|
+
if (!info || row.version !== ASSET_METADATA_VERSION) {
|
|
56
|
+
void this.fetchMetadata(id);
|
|
57
|
+
}
|
|
58
|
+
return { tokenIdHex: id, type: row.type, addedTime: row.addedTime, info };
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public countAssets(accountId: number, type: AssetType): Promise<number> {
|
|
63
|
+
return this.walletDb.countAssets(accountId, type);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public async handleNftReceive(accountId: number, tokenIdHex: string, time: number): Promise<void> {
|
|
67
|
+
const nft = await this.getAssetInfo(tokenIdHex);
|
|
68
|
+
if (nft && nft.type == 'nft') {
|
|
69
|
+
await this.saveAsset(accountId, nft, time);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public async syncTokens(accountId: number, tokenBalances: Record<string, Balance>): Promise<void> {
|
|
74
|
+
for (const tokenId in tokenBalances) {
|
|
75
|
+
const balance = tokenBalances[tokenId];
|
|
76
|
+
if (balance && balanceTotal(balance) > 0n) {
|
|
77
|
+
const token = await this.getAssetInfo(tokenId);
|
|
78
|
+
if (token && token.type == 'token') {
|
|
79
|
+
await this.saveAsset(accountId, token);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public async syncNfts(accountId: number, tokensBalance: Record<string, Balance>): Promise<void> {
|
|
86
|
+
const currentAssets = Object.keys(tokensBalance);
|
|
87
|
+
const internalAssets = await this.walletDb.getAssets(accountId, "nft");
|
|
88
|
+
|
|
89
|
+
const staleAssets = internalAssets?.filter(a => !currentAssets.includes(a.tokenIdHex)) ?? [];
|
|
90
|
+
for (const asset of staleAssets) {
|
|
91
|
+
await this.deleteAsset(accountId, asset.tokenIdHex);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
for (const [tokenId, balance] of Object.entries(tokensBalance)) {
|
|
95
|
+
const hasBalance = balance && balanceTotal(balance) > 0n;
|
|
96
|
+
const isAssetExist = internalAssets?.some(a => a.tokenIdHex == tokenId);
|
|
97
|
+
if ((!hasBalance && !isAssetExist) || (hasBalance && isAssetExist)) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!hasBalance && isAssetExist) {
|
|
102
|
+
await this.deleteAsset(accountId, tokenId);
|
|
103
|
+
} else {
|
|
104
|
+
await this.handleNftReceive(accountId, tokenId, currentTimestamp());
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Adds the asset to the account and stores its metadata. */
|
|
110
|
+
public async saveAsset(accountId: number, info: AssetInfo, addedTime = currentTimestamp()): Promise<void> {
|
|
111
|
+
const id = tokenIdToHex(info.tokenIdHex);
|
|
112
|
+
// the version info was fetched under, so an outdated fallback is still refetched later
|
|
113
|
+
const version = this.walletCache.getAsset(id)?.version ?? ASSET_METADATA_VERSION;
|
|
114
|
+
|
|
115
|
+
await this.walletDb.saveAssetMetadata(toMetadataEntity(id, { info, version }));
|
|
116
|
+
await this.walletDb.saveAsset({ accountId, tokenIdHex: id, type: info.type, addedTime });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Removes the asset from the account; its metadata too once no account holds it. */
|
|
120
|
+
public deleteAsset(accountId: number, tokenId: string): Promise<void> {
|
|
121
|
+
return this.walletDb.deleteAsset(accountId, tokenIdToHex(tokenId));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public getAssetFile(token: string, asset: string): Promise<Uint8Array> {
|
|
125
|
+
return this.performGet(getAssetFileUrl(token, asset), 'raw');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private async loadFromDb(id: string): Promise<CachedAsset | undefined> {
|
|
129
|
+
const row = await this.walletDb.getAssetMetadata(id);
|
|
130
|
+
const info = row ? parseAssetInfo(row.data) : undefined;
|
|
131
|
+
if (!row || !info) {
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const cached: CachedAsset = { info, version: row.version };
|
|
136
|
+
this.walletCache.setAsset(id, cached);
|
|
137
|
+
return cached;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Fetches from the API into memory, and into the DB if the asset is held. Never throws. */
|
|
141
|
+
private async fetchMetadata(id: string): Promise<AssetInfo | undefined> {
|
|
142
|
+
try {
|
|
143
|
+
const info: AssetInfo = await this.performGet(getAssetMetadataUrl(id), 'json');
|
|
144
|
+
const cached: CachedAsset = { info, version: ASSET_METADATA_VERSION };
|
|
145
|
+
this.walletCache.setAsset(id, cached);
|
|
146
|
+
if (await this.walletDb.isAssetHeld(id)) {
|
|
147
|
+
await this.walletDb.saveAssetMetadata(toMetadataEntity(id, cached));
|
|
148
|
+
}
|
|
149
|
+
return info;
|
|
150
|
+
} catch (e) {
|
|
151
|
+
console.error(e);
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private async performGet(url: string, responseType?: 'json' | 'raw'): Promise<any> {
|
|
157
|
+
try {
|
|
158
|
+
const response = await fetch(url);
|
|
159
|
+
if (!response.ok) {
|
|
160
|
+
throw new Error(`Failed to perform GET. status: ${response.status}`);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (responseType === 'raw') {
|
|
164
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
165
|
+
return new Uint8Array(arrayBuffer);
|
|
166
|
+
} else {
|
|
167
|
+
return await response.json();
|
|
168
|
+
}
|
|
169
|
+
} catch (e) {
|
|
170
|
+
throw new Error(`Unexpected Error: ${e}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function parseAssetInfo(data: string): AssetInfo | undefined {
|
|
176
|
+
try {
|
|
177
|
+
return JSON.parse(data);
|
|
178
|
+
} catch {
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function toMetadataEntity(id: string, cached: CachedAsset): AssetMetadataEntity {
|
|
184
|
+
return {
|
|
185
|
+
tokenIdHex: id,
|
|
186
|
+
name: cached.info.data.name,
|
|
187
|
+
data: JSON.stringify(cached.info),
|
|
188
|
+
version: cached.version,
|
|
189
|
+
};
|
|
190
|
+
}
|
package/src/services/cache.ts
CHANGED
|
@@ -1,54 +1,26 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { tokenIdToHex } from "../utils/common";
|
|
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
|
-
const token = await this.walletDb.getToken(id);
|
|
28
|
-
if (token) {
|
|
29
|
-
this.tokens.set(id, token);
|
|
30
|
-
}
|
|
31
|
-
return token;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public async getNft(id: string): Promise<NftEntity | null | undefined> {
|
|
35
|
-
id = tokenIdToHex(id);
|
|
36
|
-
if (this.nfts.has(id)) {
|
|
37
|
-
return this.nfts.get(id);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const nft = await this.walletDb.getNft(id);
|
|
41
|
-
if (nft) {
|
|
42
|
-
this.nfts.set(id, nft);
|
|
43
|
-
}
|
|
44
|
-
return nft;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
public removeToken(id: string): void {
|
|
48
|
-
this.tokens.delete(tokenIdToHex(id));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
public removeNft(id: string): void {
|
|
52
|
-
this.nfts.delete(tokenIdToHex(id));
|
|
53
|
-
}
|
|
54
|
-
}
|
|
1
|
+
import type { AssetInfo } from "../types/wallet.types";
|
|
2
|
+
import { tokenIdToHex } from "../utils/common";
|
|
3
|
+
|
|
4
|
+
export interface CachedAsset {
|
|
5
|
+
info: AssetInfo;
|
|
6
|
+
/** The metadata version `info` was fetched under. */
|
|
7
|
+
version: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Asset metadata in memory, for every asset looked up while the wallet is unlocked. */
|
|
11
|
+
export class WalletCache {
|
|
12
|
+
|
|
13
|
+
private readonly assets = new Map<string, CachedAsset>();
|
|
14
|
+
|
|
15
|
+
public clear(): void {
|
|
16
|
+
this.assets.clear();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public getAsset(id: string): CachedAsset | undefined {
|
|
20
|
+
return this.assets.get(tokenIdToHex(id));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public setAsset(id: string, asset: CachedAsset): void {
|
|
24
|
+
this.assets.set(tokenIdToHex(id), asset);
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/types/db.types.ts
CHANGED
|
@@ -50,28 +50,21 @@ export interface AssetEntity {
|
|
|
50
50
|
addedTime: number;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
/** Asset metadata as fetched from the assets API, shared by every account that holds the asset. */
|
|
54
|
+
export interface AssetMetadataEntity {
|
|
55
55
|
tokenIdHex: string;
|
|
56
|
+
/** Copied out of `data`, only to sort lists by it. */
|
|
56
57
|
name: string;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
/** The API response (an `AssetInfo`) as JSON. */
|
|
59
|
+
data: string;
|
|
60
|
+
/** The metadata version that wrote this row; older rows get refetched. */
|
|
61
|
+
version: number;
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
parent: string;
|
|
68
|
-
collection: string;
|
|
69
|
-
series: string;
|
|
70
|
-
author: string;
|
|
71
|
-
public: string;
|
|
72
|
-
front: string;
|
|
73
|
-
back: string;
|
|
74
|
-
thumb: string;
|
|
64
|
+
/** An account's asset joined with its metadata. A query result, not a stored table. */
|
|
65
|
+
export interface AssetDTO extends AssetEntity {
|
|
66
|
+
data: string;
|
|
67
|
+
version: number;
|
|
75
68
|
}
|
|
76
69
|
|
|
77
70
|
export interface SessionEntity {
|
|
@@ -51,6 +51,7 @@ export interface TokenAsset {
|
|
|
51
51
|
name: string;
|
|
52
52
|
ticker: string;
|
|
53
53
|
iconUrl: string;
|
|
54
|
+
icon: string;
|
|
54
55
|
decimals: number;
|
|
55
56
|
}
|
|
56
57
|
|
|
@@ -71,6 +72,15 @@ export type AssetInfo =
|
|
|
71
72
|
| (AssetBase & { type: 'token'; data: TokenAsset })
|
|
72
73
|
| (AssetBase & { type: 'nft'; data: NFTAsset });
|
|
73
74
|
|
|
75
|
+
/** An asset of an account, as listed in the wallet. */
|
|
76
|
+
export interface AccountAsset {
|
|
77
|
+
tokenIdHex: string;
|
|
78
|
+
type: AssetType;
|
|
79
|
+
addedTime: number;
|
|
80
|
+
/** Undefined only if the stored metadata is unreadable; it is refetched in the background. */
|
|
81
|
+
info?: AssetInfo;
|
|
82
|
+
}
|
|
83
|
+
|
|
74
84
|
export interface MarketData {
|
|
75
85
|
id: string;
|
|
76
86
|
name: string;
|