-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpair.ts
65 lines (57 loc) · 1.94 KB
/
pair.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
import BigNumber from "bignumber.js"
import Web3 from "web3"
import { ISwappaPairV1, newISwappaPairV1 } from "../types/web3-v1-contracts/ISwappaPairV1"
export type Address = string
export abstract class Snapshot {}
export type BigNumberString = string
export interface SwapData {
addr: string
extra: string
}
export abstract class Pair {
// pairKey is used to identify conflicting pairs. In a single route, every non-null pairKey must
// be unique. On the otherhand, Pair-s with null pairKey can be used unlimited amount of times in
// a single route.
public pairKey: string | null = null
public tokenA: Address = ""
public tokenB: Address = ""
private swappaPairAddress: Address
private swappaPair: ISwappaPairV1
constructor(web3: Web3, swappaPairAddress: Address) {
this.swappaPairAddress = swappaPairAddress
this.swappaPair = newISwappaPairV1(web3, this.swappaPairAddress)
}
public async init(): Promise<void> {
const r = await this._init()
this.pairKey = r.pairKey
this.tokenA = r.tokenA
this.tokenB = r.tokenB
return this.refresh()
}
protected abstract _init(): Promise<{
pairKey: string | null,
tokenA: Address,
tokenB: Address,
}>;
public abstract refresh(): Promise<void>;
public swapData(inputToken: Address): SwapData {
return {
addr: this.swappaPairAddress,
extra: this.swapExtraData(inputToken),
}
}
protected abstract swapExtraData(inputToken: Address): string;
public abstract outputAmount(inputToken: Address, inputAmount: BigNumber): BigNumber;
public outputAmountAsync = async (inputToken: Address, inputAmount: BigNumber): Promise<BigNumber> => {
const outputToken = inputToken === this.tokenA ? this.tokenB : this.tokenA
const out = await this.swappaPair.methods.getOutputAmount(
inputToken,
outputToken,
inputAmount.toFixed(0),
this.swapExtraData(inputToken),
).call()
return new BigNumber(out)
}
public abstract snapshot(): Snapshot;
public abstract restore(snapshot: Snapshot): void;
}