-
Notifications
You must be signed in to change notification settings - Fork 123
/
client.ts
98 lines (85 loc) · 3.34 KB
/
client.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
import { u8str } from "@manahippo/move-to-ts";
import { AptosClient, HexString } from "aptos";
import { DEFAULT_COIN_LIST, RawCoinInfo } from "./list";
import { App, stdlib, coin_list } from "./src";
import { Simple_map } from "./src/stdlib";
import { TypeInfo } from "./src/stdlib/type_info";
export function coinInfoToRaw(coinInfo: coin_list.Coin_list.CoinInfo): RawCoinInfo {
return {
name: coinInfo.name.str(),
symbol: coinInfo.symbol.str(),
coingecko_id: coinInfo.coingecko_id.str(),
decimals: coinInfo.decimals.toJsNumber(),
logo_url: coinInfo.logo_url.str(),
project_url: coinInfo.project_url.str(),
token_type: {
type: coinInfo.token_type.typeFullname(),
account_address: coinInfo.token_type.account_address.hex(),
module_name: u8str(coinInfo.token_type.module_name),
struct_name: u8str(coinInfo.token_type.struct_name),
},
extensions: {
data: coinInfo.extensions.data.map(element => [
((element as Simple_map.Element).key as stdlib.String.String).str(),
((element as Simple_map.Element).value as stdlib.String.String).str(),
] as [string, string])
},
};
}
export async function fetchUpdatedList(client: AptosClient, owner=coin_list.Coin_list.moduleAddress): Promise<RawCoinInfo[]> {
const app = new App(client);
const list = await app.coin_list.coin_list.query_fetch_full_list({
pubkey: new HexString('0x0000000000000000000000000000000000000000000000000000000000000001'),
address: new HexString('0x0000000000000000000000000000000000000000000000000000000000000001'),
}, owner, []);
return list.coin_info_list.map(coinInfoToRaw);
}
export async function fetchFullRegistry(client: AptosClient): Promise<RawCoinInfo[]> {
const app = new App(client);
const list = await app.coin_list.coin_list.query_fetch_all_registered_coin_info({
pubkey: new HexString('0x1'),
address: new HexString('0x1'),
}, []);
return list.coin_info_list.map(coinInfoToRaw);
}
export class CoinListClient {
fullnameToCoinInfo: Record<string, RawCoinInfo>;
symbolToCoinInfo: Record<string, RawCoinInfo[]>;
coinList: RawCoinInfo[];
constructor(list: RawCoinInfo[] | undefined = undefined) {
this.fullnameToCoinInfo = {};
this.symbolToCoinInfo = {};
this.coinList = list || DEFAULT_COIN_LIST;
this.buildCache();
}
hasTokenType(tokenType: TypeInfo) {
return tokenType.typeFullname() in this.fullnameToCoinInfo;
}
getCoinInfoList() {
return this.coinList;
}
getCoinInfoBySymbol(symbol: string): RawCoinInfo[] {
return this.symbolToCoinInfo[symbol] || [];
}
getCoinInfoByType(tokenType: TypeInfo) {
return this.fullnameToCoinInfo[tokenType.typeFullname()];
}
static async load(client: AptosClient, owner=coin_list.Coin_list.moduleAddress) {
const list = await fetchUpdatedList(client, owner);
return new CoinListClient(list);
}
async update(client: AptosClient, owner=coin_list.Coin_list.moduleAddress) {
this.coinList = await fetchUpdatedList(client, owner);
this.buildCache();
}
private buildCache() {
for (const coinInfo of this.coinList) {
this.fullnameToCoinInfo[coinInfo.token_type.type] = coinInfo;
const symbol = coinInfo.symbol;
if (!this.symbolToCoinInfo[symbol]) {
this.symbolToCoinInfo[symbol] = [];
}
this.symbolToCoinInfo[coinInfo.symbol].push(coinInfo);
}
}
}