forked from polkawallet-io/bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bifrost.ts
292 lines (265 loc) · 6.85 KB
/
bifrost.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { Storage } from "@acala-network/sdk/utils/storage";
import { AnyApi, FixedPointNumber as FN } from "@acala-network/sdk-core";
import { combineLatest, map, Observable } from "rxjs";
import { SubmittableExtrinsic } from "@polkadot/api/types";
import { ISubmittableResult } from "@polkadot/types/types";
import { BalanceAdapter, BalanceAdapterConfigs } from "../balance-adapter";
import { BaseCrossChainAdapter } from "../base-chain-adapter";
import { ChainId, chains } from "../configs";
import { ApiNotFound, InvalidAddress, TokenNotFound } from "../errors";
import { BalanceData, ExtendedToken, TransferParams } from "../types";
import { createRouteConfigs, validateAddress } from "../utils";
export const bifrostKusamaRouteConfigs = createRouteConfigs("bifrost", [
{
to: "karura",
token: "BNC",
xcm: {
fee: { token: "BNC", amount: "5120000000" },
},
},
{
to: "karura",
token: "VSKSM",
xcm: {
fee: { token: "VSKSM", amount: "64000000" },
},
},
{
to: "karura",
token: "KSM",
xcm: {
fee: { token: "KSM", amount: "64000000" },
},
},
{
to: "karura",
token: "KAR",
xcm: {
fee: { token: "KAR", amount: "6400000000" },
},
},
{
to: "karura",
token: "KUSD",
xcm: {
fee: { token: "KUSD", amount: "10011896008" },
},
},
{
to: "kintsugi",
token: "VKSM",
xcm: {
fee: { token: "VKSM", amount: "175000000" },
},
},
]);
export const bifrostPolkadotRoutersConfig = createRouteConfigs(
"bifrostPolkadot",
[
{
to: "interlay",
token: "VDOT",
xcm: {
fee: { token: "VDOT", amount: "20000000" },
},
},
]
);
export const bifrostKusamaTokensConfig: Record<string, ExtendedToken> = {
BNC: {
name: "BNC",
symbol: "BNC",
decimals: 12,
ed: "10000000000",
toRaw: () => ({ Native: "BNC" }),
},
VSKSM: {
name: "VSKSM",
symbol: "VSKSM",
decimals: 12,
ed: "100000000",
toRaw: () => ({ VSToken: "KSM" }),
},
KSM: {
name: "KSM",
symbol: "KSM",
decimals: 12,
ed: "100000000",
toRaw: () => ({ Token: "KSM" }),
},
KAR: {
name: "KAR",
symbol: "KAR",
decimals: 12,
ed: "148000000",
toRaw: () => ({ Token: "KAR" }),
},
KUSD: {
name: "KUSD",
symbol: "KUSD",
decimals: 12,
ed: "100000000",
toRaw: () => ({ Stable: "KUSD" }),
},
VKSM: {
name: "VKSM",
symbol: "VKSM",
decimals: 12,
ed: "100000000",
toRaw: () => ({ VToken: "KSM" }),
},
};
export const bifrostPolkadotTokensConfig: Record<string, ExtendedToken> = {
BNC: {
name: "BNC",
symbol: "BNC",
decimals: 12,
ed: "10000000000",
toRaw: () => ({ Native: "BNC" }),
},
VDOT: {
name: "VDOT",
symbol: "VDOT",
decimals: 10,
ed: "1000000",
toRaw: () => ({ VToken2: 0 }),
},
};
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
const createBalanceStorages = (api: AnyApi) => {
return {
balances: (address: string) =>
Storage.create<any>({
api,
path: "query.system.account",
params: [address],
}),
assets: (address: string, token: unknown) =>
Storage.create<any>({
api,
path: "query.tokens.accounts",
params: [address, token],
}),
};
};
class BifrostBalanceAdapter extends BalanceAdapter {
private storages: ReturnType<typeof createBalanceStorages>;
constructor({ api, chain, tokens }: BalanceAdapterConfigs) {
super({ api, chain, tokens });
this.storages = createBalanceStorages(api);
}
public subscribeBalance(
token: string,
address: string
): Observable<BalanceData> {
if (!validateAddress(address)) throw new InvalidAddress(address);
const storage = this.storages.balances(address);
const tokenData: ExtendedToken = this.getToken(token);
if (token === this.nativeToken) {
return storage.observable.pipe(
map(({ data }) => ({
free: FN.fromInner(data.free.toString(), this.decimals),
locked: FN.fromInner(data.frozen.toString(), this.decimals),
reserved: FN.fromInner(data.reserved.toString(), this.decimals),
available: FN.fromInner(
data.free.sub(data.frozen).toString(),
this.decimals
),
}))
);
}
if (!tokenData) throw new TokenNotFound(token);
return this.storages.assets(address, tokenData.toRaw()).observable.pipe(
map((balance) => {
const amount = FN.fromInner(
balance.free?.toString() || "0",
this.getToken(token).decimals
);
return {
free: amount,
locked: new FN(0),
reserved: new FN(0),
available: amount,
};
})
);
}
}
class BaseBifrostAdapter extends BaseCrossChainAdapter {
private balanceAdapter?: BifrostBalanceAdapter;
public async init(api: AnyApi) {
this.api = api;
await api.isReady;
this.balanceAdapter = new BifrostBalanceAdapter({
chain: this.chain.id as ChainId,
api,
tokens: bifrostKusamaTokensConfig,
});
}
public subscribeTokenBalance(
token: string,
address: string
): Observable<BalanceData> {
if (!this.balanceAdapter) {
throw new ApiNotFound(this.chain.id);
}
return this.balanceAdapter.subscribeBalance(token, address);
}
public subscribeMaxInput(
token: string,
address: string,
to: ChainId
): Observable<FN> {
if (!this.balanceAdapter) {
throw new ApiNotFound(this.chain.id);
}
return combineLatest({
txFee:
token === this.balanceAdapter?.nativeToken
? this.estimateTxFee({
amount: FN.ZERO,
to,
token,
address,
signer: address,
})
: "0",
balance: this.balanceAdapter
.subscribeBalance(token, address)
.pipe(map((i) => i.available)),
}).pipe(
map(({ balance, txFee }) => {
const tokenMeta = this.balanceAdapter?.getToken(token);
const feeFactor = 1.2;
const fee = FN.fromInner(txFee, tokenMeta?.decimals).mul(
new FN(feeFactor)
);
// always minus ed
return balance
.minus(fee)
.minus(FN.fromInner(tokenMeta?.ed || "0", tokenMeta?.decimals));
})
);
}
public createTx(
params: TransferParams
):
| SubmittableExtrinsic<"promise", ISubmittableResult>
| SubmittableExtrinsic<"rxjs", ISubmittableResult> {
return this.createXTokensTx(params);
}
}
export class BifrostAdapter extends BaseBifrostAdapter {
constructor() {
super(chains.bifrost, bifrostKusamaRouteConfigs, bifrostKusamaTokensConfig);
}
}
export class BifrostPolkadotAdapter extends BaseBifrostAdapter {
constructor() {
super(
chains.bifrostPolkadot,
bifrostPolkadotRoutersConfig,
bifrostPolkadotTokensConfig
);
}
}