/
evm-on-chain-provider.ts
64 lines (57 loc) · 3.04 KB
/
evm-on-chain-provider.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
import { PriceToken, PriceTokenAmount } from 'src/common/tokens';
import { EvmBlockchainName } from 'src/core/blockchain/models/blockchain-name';
import { CHAIN_TYPE } from 'src/core/blockchain/models/chain-type';
import { EvmWeb3Public } from 'src/core/blockchain/web3-public-service/web3-public/evm-web3-public/evm-web3-public';
import { Injector } from 'src/core/injector/injector';
import { getFromWithoutFee } from 'src/features/common/utils/get-from-without-fee';
import {
OnChainCalculationOptions,
RequiredOnChainCalculationOptions
} from 'src/features/on-chain/calculation-manager/providers/common/models/on-chain-calculation-options';
import { OnChainProxyFeeInfo } from 'src/features/on-chain/calculation-manager/providers/common/models/on-chain-proxy-fee-info';
import { OnChainProxyService } from 'src/features/on-chain/calculation-manager/providers/common/on-chain-proxy-service/on-chain-proxy-service';
import { EvmOnChainTrade } from 'src/features/on-chain/calculation-manager/providers/common/on-chain-trade/evm-on-chain-trade/evm-on-chain-trade';
import { getGasPriceInfo } from 'src/features/on-chain/calculation-manager/providers/common/utils/get-gas-price-info';
import { GasPriceInfo } from 'src/features/on-chain/calculation-manager/providers/dexes/common/on-chain-provider/evm-on-chain-provider/models/gas-price-info';
import { OnChainProvider } from 'src/features/on-chain/calculation-manager/providers/dexes/common/on-chain-provider/on-chain-provider';
export abstract class EvmOnChainProvider extends OnChainProvider {
public abstract readonly blockchain: EvmBlockchainName;
protected readonly onChainProxyService = new OnChainProxyService();
protected get walletAddress(): string {
return Injector.web3PrivateService.getWeb3Private(CHAIN_TYPE.EVM).address;
}
protected get web3Public(): EvmWeb3Public {
return Injector.web3PublicService.getWeb3Public(this.blockchain);
}
public abstract calculate(
from: PriceTokenAmount<EvmBlockchainName>,
to: PriceToken<EvmBlockchainName>,
options?: OnChainCalculationOptions
): Promise<EvmOnChainTrade>;
protected async handleProxyContract(
from: PriceTokenAmount<EvmBlockchainName>,
fullOptions: RequiredOnChainCalculationOptions
): Promise<{
fromWithoutFee: PriceTokenAmount<EvmBlockchainName>;
proxyFeeInfo: OnChainProxyFeeInfo | undefined;
}> {
let fromWithoutFee: PriceTokenAmount<EvmBlockchainName>;
let proxyFeeInfo: OnChainProxyFeeInfo | undefined;
if (fullOptions.useProxy) {
proxyFeeInfo = await this.onChainProxyService.getFeeInfo(
from,
fullOptions.providerAddress
);
fromWithoutFee = getFromWithoutFee(from, proxyFeeInfo.platformFee.percent);
} else {
fromWithoutFee = from;
}
return {
fromWithoutFee,
proxyFeeInfo
};
}
protected async getGasPriceInfo(): Promise<GasPriceInfo> {
return getGasPriceInfo(this.blockchain);
}
}