-
Notifications
You must be signed in to change notification settings - Fork 5
/
TokenSwapService.ts
44 lines (36 loc) · 1.33 KB
/
TokenSwapService.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
import di from 'a-di';
import { EoAccount } from "@dequanto/models/TAccount";
import { ChainAccountService } from '@dequanto/ChainAccountService';
import { Web3Client } from '@dequanto/clients/Web3Client';
import { IToken } from '@dequanto/models/IToken';
import { ISwapService } from './defi/ISwapService';
import { Paraswap } from './defi/paraswap/Paraswap';
import { $require } from '@dequanto/utils/$require';
export class TokenSwapService {
constructor(
public client: Web3Client,
public provider: ISwapService = di.resolve(Paraswap, client.platform, client)
) {
}
async swap (account: string | EoAccount, params: {
from: string | IToken
to: string | IToken
amount: number | bigint
}) {
let $account: EoAccount;
if (typeof account === 'string') {
let accountsService = di.resolve(ChainAccountService);
let acc = await accountsService.get(account, { platform: this.client.platform });
$account = $require.notNull(acc as EoAccount, `Account ${account} not found`);
} else {
$account = account;
}
let { from, to, amount } = params;
let tx = await this.provider.swap($account, {
from: from,
to: to,
fromAmount: amount,
});
return tx;
}
}