/
endpoint.ts
214 lines (184 loc) · 7.11 KB
/
endpoint.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
/*
* Copyright © 2021 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
import { validator } from '@liskhq/lisk-validator';
import * as cryptography from '@liskhq/lisk-cryptography';
import { NotFoundError } from '../../state_machine';
import { JSONObject, ModuleEndpointContext } from '../../types';
import { ModuleConfig } from './types';
import { BaseEndpoint } from '../base_endpoint';
import { CHAIN_ID_LENGTH, TOKEN_ID_LENGTH } from './constants';
import {
getBalanceRequestSchema,
getBalancesRequestSchema,
hasEscrowAccountRequestSchema,
hasUserAccountRequestSchema,
isSupportedRequestSchema,
} from './schemas';
import { EscrowStore, EscrowStoreData } from './stores/escrow';
import { SupplyStore, SupplyStoreData } from './stores/supply';
import { UserStore, UserStoreData } from './stores/user';
import { SupportedTokensStore } from './stores/supported_tokens';
export class TokenEndpoint extends BaseEndpoint {
private _moduleConfig!: ModuleConfig;
public init(moduleConfig: ModuleConfig) {
this._moduleConfig = moduleConfig;
}
public async getBalances(
context: ModuleEndpointContext,
): Promise<{ balances: JSONObject<UserStoreData & { tokenID: Buffer }>[] }> {
validator.validate<{ address: string }>(getBalancesRequestSchema, context.params);
const address = cryptography.address.getAddressFromLisk32Address(context.params.address);
const userStore = this.stores.get(UserStore);
const userData = await userStore.iterate(context, {
gte: Buffer.concat([address, Buffer.alloc(TOKEN_ID_LENGTH, 0)]),
lte: Buffer.concat([address, Buffer.alloc(TOKEN_ID_LENGTH, 255)]),
});
return {
balances: userData.map(({ key, value: user }) => ({
tokenID: key.subarray(20).toString('hex'),
availableBalance: user.availableBalance.toString(),
lockedBalances: user.lockedBalances.map(b => ({
amount: b.amount.toString(),
module: b.module,
})),
})),
};
}
public async getBalance(context: ModuleEndpointContext): Promise<JSONObject<UserStoreData>> {
validator.validate<{ address: string; tokenID: string }>(
getBalanceRequestSchema,
context.params,
);
const address = cryptography.address.getAddressFromLisk32Address(context.params.address);
const tokenID = Buffer.from(context.params.tokenID, 'hex');
const userStore = this.stores.get(UserStore);
try {
const user = await userStore.get(context, userStore.getKey(address, tokenID));
return {
availableBalance: user.availableBalance.toString(),
lockedBalances: user.lockedBalances.map(b => ({
amount: b.amount.toString(),
module: b.module,
})),
};
} catch (error) {
if (!(error instanceof NotFoundError)) {
throw error;
}
return {
availableBalance: '0',
lockedBalances: [],
};
}
}
public async getTotalSupply(
context: ModuleEndpointContext,
): Promise<{ totalSupply: JSONObject<SupplyStoreData & { tokenID: string }>[] }> {
const supplyStore = this.stores.get(SupplyStore);
const supplyData = await supplyStore.getAll(context);
return {
totalSupply: supplyData.map(({ key: tokenID, value: supply }) => ({
tokenID: tokenID.toString('hex'),
totalSupply: supply.totalSupply.toString(),
})),
};
}
public async getSupportedTokens(
context: ModuleEndpointContext,
): Promise<{ supportedTokens: string[] }> {
const supportedTokensStore = this.stores.get(SupportedTokensStore);
if (await supportedTokensStore.allSupported(context)) {
return {
supportedTokens: ['*'],
};
}
const supportedTokens: string[] = [];
// main chain token
const mainchainTokenID = Buffer.concat([
context.chainID.subarray(0, 1),
Buffer.alloc(TOKEN_ID_LENGTH - 1, 0),
]);
supportedTokens.push(mainchainTokenID.toString('hex'));
// native chain tokens
const supplyStore = this.stores.get(SupplyStore);
const supplyData = await supplyStore.getAll(context);
for (const tokenSupply of supplyData) {
supportedTokens.push(tokenSupply.key.toString('hex'));
}
// foreign chain tokens
const supportedTokensData = await supportedTokensStore.getAll(context);
for (const supportedToken of supportedTokensData) {
if (!supportedToken.value.supportedTokenIDs.length) {
supportedTokens.push(`${supportedToken.key.toString('hex')}${'********'}`); // key in supported token store is 4-byte chain ID
} else {
for (const token of supportedToken.value.supportedTokenIDs) {
supportedTokens.push(token.toString('hex'));
}
}
}
return { supportedTokens };
}
public async isSupported(context: ModuleEndpointContext) {
validator.validate<{ tokenID: string }>(isSupportedRequestSchema, context.params);
const tokenID = Buffer.from(context.params.tokenID, 'hex');
const supportedTokensStore = this.stores.get(SupportedTokensStore);
return { supported: await supportedTokensStore.isSupported(context, tokenID) };
}
public async getEscrowedAmounts(context: ModuleEndpointContext): Promise<{
escrowedAmounts: JSONObject<EscrowStoreData & { escrowChainID: Buffer; tokenID: Buffer }>[];
}> {
const escrowStore = this.stores.get(EscrowStore);
const escrowData = await escrowStore.iterate(context, {
gte: Buffer.concat([Buffer.alloc(TOKEN_ID_LENGTH, 0)]),
lte: Buffer.concat([Buffer.alloc(TOKEN_ID_LENGTH, 255)]),
});
return {
escrowedAmounts: escrowData.map(({ key, value: escrow }) => {
const escrowChainID = key.subarray(0, CHAIN_ID_LENGTH);
const tokenID = key.subarray(CHAIN_ID_LENGTH);
return {
escrowChainID: escrowChainID.toString('hex'),
amount: escrow.amount.toString(),
tokenID: tokenID.toString('hex'),
};
}),
};
}
public getInitializationFees() {
return {
userAccount: this._moduleConfig.userAccountInitializationFee.toString(),
escrowAccount: this._moduleConfig.escrowAccountInitializationFee.toString(),
};
}
public async hasUserAccount(context: ModuleEndpointContext): Promise<{ exists: boolean }> {
validator.validate<{ address: string; tokenID: string }>(
hasUserAccountRequestSchema,
context.params,
);
const address = cryptography.address.getAddressFromLisk32Address(context.params.address);
const tokenID = Buffer.from(context.params.tokenID, 'hex');
const userStore = this.stores.get(UserStore);
return { exists: await userStore.has(context, userStore.getKey(address, tokenID)) };
}
public async hasEscrowAccount(context: ModuleEndpointContext): Promise<{ exists: boolean }> {
validator.validate<{ escrowChainID: string; tokenID: string }>(
hasEscrowAccountRequestSchema,
context.params,
);
const escrowChainID = Buffer.from(context.params.escrowChainID, 'hex');
const tokenID = Buffer.from(context.params.tokenID, 'hex');
const escrowStore = this.stores.get(EscrowStore);
return { exists: await escrowStore.has(context, escrowStore.getKey(escrowChainID, tokenID)) };
}
}