-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathUniswap.ts
92 lines (76 loc) · 3.01 KB
/
Uniswap.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
import Web3 from 'web3';
import { ERC20Asset } from '@burner-wallet/assets';
import Exchange from '../Exchange';
import Pair, { ExchangeParams, ValueTypes } from './Pair';
const { toBN } = Web3.utils;
const UNISWAP_FACTORY_ADDRESS = '0xc0a47dfe034b400b47bdad5fecda2621de6c4d95';
const UNISWAP_NETWORK = '1';
const DEADLINE = 1742680400;
let _abis: any = null;
const getABI = async (contract: string) => {
if (!_abis) {
_abis = await import('./abis');
}
return _abis[contract];
}
export default class Uniswap extends Pair {
private exchangeAddress: string | null;
constructor(asset: string) {
super({ assetA: asset, assetB: 'eth' });
this.exchangeAddress = null;
}
async getContract() {
const web3 = this.getExchange().getWeb3(UNISWAP_NETWORK);
const exchangeAddress = await this.getExchangeAddress();
const contract = new web3.eth.Contract(await getABI('uniswapToken'), exchangeAddress!);
return contract;
}
async getExchangeAddress() {
if (!this.exchangeAddress) {
const web3 = this.getExchange().getWeb3(UNISWAP_NETWORK);
const factoryContract = new web3.eth.Contract(await getABI('uniswapFactory'), UNISWAP_FACTORY_ADDRESS);
const asset = this.getExchange().getAsset(this.assetA) as ERC20Asset;
const exchangeAddress = await factoryContract.methods.getExchange(asset.address).call();
if (!exchangeAddress) {
throw new Error(`Can not find Uniswap exchange for asset ${this.assetA}`);
}
console.log(`Found Uniswap for ${this.assetA} at ${exchangeAddress}`);
this.exchangeAddress = exchangeAddress;
}
return this.exchangeAddress;
}
async exchangeAtoB({ account, value, ether }: ExchangeParams) {
const _value = this._getValue({ value, ether });
const asset = this.getExchange().getAsset(this.assetA) as ERC20Asset;
const contract = await this.getContract();
const uniswapAllowance = await asset.allowance(account, this.exchangeAddress!);
if (toBN(uniswapAllowance).lt(toBN(_value))) {
const allowanceReceipt = await asset.approve(account, this.exchangeAddress!, _value);
console.log(allowanceReceipt);
}
return await contract.methods.tokenToEthSwapInput(_value, 1, DEADLINE)
.send({ from: account });
}
async exchangeBtoA({ account, value, ether }: ExchangeParams) {
const _value = this._getValue({ value, ether });
const contract = await this.getContract();
return contract.methods.ethToTokenSwapInput(1, DEADLINE)
.send({ from: account, value: _value });
}
async estimateAtoB(value: ValueTypes) {
const contract = await this.getContract();
const output = await contract.methods.getTokenToEthInputPrice(this._getValue(value)).call();
return {
estimate: output,
estimateInfo: null
};
}
async estimateBtoA(value: ValueTypes) {
const contract = await this.getContract();
const output = await contract.methods.getEthToTokenInputPrice(this._getValue(value)).call();
return {
estimate: output,
estimateInfo: null
};
}
}