@proton/wrap-constants 0.0.56 → 0.0.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants.d.ts +1 -0
- package/dist/graph.d.ts +30 -0
- package/dist/index.d.ts +2 -0
- package/dist/math.d.ts +17 -0
- package/dist/parsers/pool.d.ts +6 -11
- package/dist/utils.d.ts +1 -0
- package/dist/wrap-constants.cjs.development.js +347 -2
- package/dist/wrap-constants.cjs.development.js.map +1 -1
- package/dist/wrap-constants.cjs.production.min.js +1 -1
- package/dist/wrap-constants.cjs.production.min.js.map +1 -1
- package/dist/wrap-constants.esm.js +336 -3
- package/dist/wrap-constants.esm.js.map +1 -1
- package/package.json +4 -2
- package/src/coins.ts +1 -1
- package/src/constants.ts +2 -1
- package/src/graph.ts +126 -0
- package/src/index.ts +3 -1
- package/src/math.ts +112 -0
- package/src/parsers/pool.ts +7 -6
- package/src/utils.ts +12 -1
package/src/graph.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import cloneDeep from 'lodash.clonedeep'
|
|
2
|
+
|
|
3
|
+
export class INode <T> {
|
|
4
|
+
data: T = {} as T
|
|
5
|
+
constructor (public key: string, data?: T) {
|
|
6
|
+
if (data) {
|
|
7
|
+
this.data = data
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class ILink <T> {
|
|
13
|
+
data: T = {} as T
|
|
14
|
+
constructor (public from: INode<T>, public to: INode<T>, data?: T) {
|
|
15
|
+
if (data) {
|
|
16
|
+
this.data = data
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class Graph <T> {
|
|
22
|
+
nodes: INode<T>[] = []
|
|
23
|
+
links: ILink<T>[] = []
|
|
24
|
+
lastPathFound: INode<T>[] = []
|
|
25
|
+
|
|
26
|
+
isEquals = (a: INode<T>, b: INode<T>) => a.key === b.key
|
|
27
|
+
|
|
28
|
+
addNode (k: string, v?: T): void {
|
|
29
|
+
const node = this.findNode(k)
|
|
30
|
+
if (!node) {
|
|
31
|
+
this.nodes.push(new INode(k, v))
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
addNodes (...nodes: (string | [string, T?])[]): void {
|
|
36
|
+
nodes.forEach(node => typeof node === "string" ? this.addNode(node) : this.addNode(node[0], node[1]))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
findNode (k: string): INode<T> | undefined {
|
|
40
|
+
const inode = new INode<T>(k)
|
|
41
|
+
return this.nodes.find(node => this.isEquals(node, inode))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
findNodes (...keys: string[]): (INode<T> | undefined)[] {
|
|
45
|
+
return keys.map(k => this.findNode(k))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
requireFindNodes (...keys: string[]): INode<T>[] {
|
|
49
|
+
const nodes = this.findNodes(...keys)
|
|
50
|
+
if (nodes.some(n => n === undefined)) {
|
|
51
|
+
throw new Error('One of the nodes does not exist')
|
|
52
|
+
}
|
|
53
|
+
return nodes as INode<T>[]
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
addLink (nodeA: string, nodeB: string, data?: T): void {
|
|
57
|
+
const [inodeA, inodeB] = this.requireFindNodes(nodeA, nodeB)
|
|
58
|
+
this.links.push(new ILink<T>(inodeA, inodeB, data))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
addLinks (...links: [nodeA: string, nodeB: string, data?: T][]): void {
|
|
62
|
+
links.forEach(link => this.addLink(...link))
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Get all nodes linked
|
|
67
|
+
* to from `node`.
|
|
68
|
+
*/
|
|
69
|
+
findLinkedNodes (node: INode<T>) {
|
|
70
|
+
return this.links.reduce((acc, link) => {
|
|
71
|
+
if (this.isEquals(link.from, node)) {
|
|
72
|
+
acc.push(link.to)
|
|
73
|
+
}
|
|
74
|
+
return acc;
|
|
75
|
+
}, [] as INode<T>[]);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
findAllPaths(from: string, to: string) {
|
|
79
|
+
const allPaths: INode<T>[][] = []
|
|
80
|
+
|
|
81
|
+
const [fromNode, toNode] = this.requireFindNodes(from, to)
|
|
82
|
+
|
|
83
|
+
const path: INode<T>[] = [fromNode]
|
|
84
|
+
let seen = new Set()
|
|
85
|
+
|
|
86
|
+
const stuck = (x: INode<T>) => {
|
|
87
|
+
if (this.isEquals(x, toNode)) {
|
|
88
|
+
return false
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
for (const y of this.findLinkedNodes(x)) {
|
|
92
|
+
if (!seen.has(y.key)) {
|
|
93
|
+
seen.add(y.key)
|
|
94
|
+
if (!stuck(y)) {
|
|
95
|
+
return false
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return true
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const search = (x: INode<T>) => {
|
|
104
|
+
if (this.isEquals(x, toNode)) {
|
|
105
|
+
allPaths.push(cloneDeep(path))
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
seen = new Set(path.map(p => p.key))
|
|
109
|
+
if (stuck(x)) {
|
|
110
|
+
return
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
for (const y of this.findLinkedNodes(x)) {
|
|
114
|
+
if (!path.includes(y)) {
|
|
115
|
+
path.push(y)
|
|
116
|
+
search(y)
|
|
117
|
+
path.pop()
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
search(fromNode)
|
|
123
|
+
|
|
124
|
+
return allPaths
|
|
125
|
+
}
|
|
126
|
+
}
|
package/src/index.ts
CHANGED
package/src/math.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { BigNumber as BN } from 'bignumber.js'
|
|
2
|
+
import { FEE_FACTOR } from './constants'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* XPR<>XUSDT (XPR -> XUSDT)
|
|
6
|
+
*
|
|
7
|
+
* pool1 * pool2
|
|
8
|
+
* term = pool2 - --------------
|
|
9
|
+
* pool1 + swap
|
|
10
|
+
*
|
|
11
|
+
* result = term - (term * fee)
|
|
12
|
+
*/
|
|
13
|
+
export const compute_transfer = (pool1: BN.Instance, pool2: BN.Instance, swap: BN.Instance, exchange_fee: number, precision: number): BN => {
|
|
14
|
+
const term1 = pool2
|
|
15
|
+
const term2over = pool1.multipliedBy(pool2)
|
|
16
|
+
const term2under = pool1.plus(swap)
|
|
17
|
+
const term2 = term2over.dividedBy(term2under).integerValue(BN.ROUND_DOWN)
|
|
18
|
+
const term = term1.minus(term2)
|
|
19
|
+
const fee = new BN(exchange_fee).dividedBy(FEE_FACTOR)
|
|
20
|
+
const result = term.minus(term.multipliedBy(fee))
|
|
21
|
+
|
|
22
|
+
return result.integerValue(BN.ROUND_DOWN).shiftedBy(-precision)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const compute_transfer_curve = (
|
|
26
|
+
reserve_in: number,
|
|
27
|
+
reserve_out: number,
|
|
28
|
+
amount_in: number,
|
|
29
|
+
exchange_fee: number,
|
|
30
|
+
precision: number,
|
|
31
|
+
amplifier: number
|
|
32
|
+
): BN => {
|
|
33
|
+
if (amplifier === 0) {
|
|
34
|
+
return compute_transfer(new BN(reserve_in), new BN(reserve_out), new BN(amount_in), exchange_fee, precision)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const MAX_ITERATIONS = 10
|
|
38
|
+
|
|
39
|
+
const sum = reserve_in + reserve_out
|
|
40
|
+
let D = sum;
|
|
41
|
+
let D_prev = 0
|
|
42
|
+
|
|
43
|
+
const F = (v: number) => {
|
|
44
|
+
return (v >= 0 ? 1 : -1) * Math.floor(Math.abs(v));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let i = MAX_ITERATIONS
|
|
48
|
+
while (D != D_prev && i--) {
|
|
49
|
+
const prod1 = F((F((D * D) / (reserve_in * 2)) * D) / (reserve_out * 2));
|
|
50
|
+
D_prev = D
|
|
51
|
+
D = F((2 * D * ((amplifier * sum) + prod1)) / ((2 * amplifier - 1) * D + 3 * prod1));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const b = ((reserve_in + amount_in) + F(D / (amplifier * 2))) - D;
|
|
55
|
+
|
|
56
|
+
const c = F(F((D * D) / ((reserve_in + amount_in) * 2)) * D / (amplifier * 4));
|
|
57
|
+
|
|
58
|
+
let x = D, x_prev = 0;
|
|
59
|
+
i = MAX_ITERATIONS;
|
|
60
|
+
while ( x != x_prev && i--) {
|
|
61
|
+
x_prev = F(x);
|
|
62
|
+
x = +new BN(x * x + c).dividedBy(2 * x + b).toFixed(0, BN.ROUND_DOWN)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (reserve_out <= x) {
|
|
66
|
+
throw new Error("curve: insufficient reserve out")
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Amount out + Fee + 1
|
|
70
|
+
const amount_out_with_fee = new BN(reserve_out - x);
|
|
71
|
+
|
|
72
|
+
// Fee + 1
|
|
73
|
+
// +1 covers small precision issues
|
|
74
|
+
const fee = amount_out_with_fee.multipliedBy(exchange_fee).dividedBy(FEE_FACTOR).plus(1).toFixed(0, BN.ROUND_DOWN);
|
|
75
|
+
|
|
76
|
+
// Amount out
|
|
77
|
+
const amount_out = amount_out_with_fee.minus(fee)
|
|
78
|
+
|
|
79
|
+
return amount_out.integerValue(BN.ROUND_DOWN).shiftedBy(-precision)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export const compute_liquidity = (amount_lt: BN.Instance, pool: BN.Instance, lt_supply: BN.Instance, precision: number): number => {
|
|
83
|
+
const over = amount_lt.multipliedBy(pool)
|
|
84
|
+
const under = lt_supply
|
|
85
|
+
const result = over.dividedBy(under).integerValue(BN.ROUND_DOWN)
|
|
86
|
+
|
|
87
|
+
return +(result.shiftedBy(-precision).toFixed(precision, BN.ROUND_DOWN))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export const isInputInvalid = (data: string, value: string | number, pool: { xtokenPrecision: number }): boolean => {
|
|
91
|
+
value = String(value)
|
|
92
|
+
|
|
93
|
+
const isBackspace = data === null
|
|
94
|
+
const isNumber = !/[0-9]|\./.test(data)
|
|
95
|
+
const isDoubleDot = data === '.' && value.indexOf('.') !== value.length - 1
|
|
96
|
+
const isPrecisionTooHigh = value && value.indexOf('.') !== -1 && value.split('.')[1].length > pool.xtokenPrecision
|
|
97
|
+
const isInvalid = !isBackspace && Boolean(isNumber || isDoubleDot || isPrecisionTooHigh)
|
|
98
|
+
|
|
99
|
+
return isInvalid
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export const applyDiscountToFee = (fee: number, staked: number): number => {
|
|
103
|
+
if (staked >= 10000000) {
|
|
104
|
+
return fee * 0
|
|
105
|
+
} else if (staked >= 1000000) {
|
|
106
|
+
return fee * 0.33
|
|
107
|
+
} else if (staked >= 100000) {
|
|
108
|
+
return fee * 0.66
|
|
109
|
+
} else {
|
|
110
|
+
return fee
|
|
111
|
+
}
|
|
112
|
+
}
|
package/src/parsers/pool.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Coin } from "../coins"
|
|
2
2
|
import { ExtendedAsset } from '@bloks/numbers'
|
|
3
|
+
import { INode } from "../graph";
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
export interface RawPool {
|
|
@@ -27,8 +28,8 @@ export interface Pool extends Omit<RawPool, 'pool1'|'pool2'> {
|
|
|
27
28
|
lt_symbol: string;
|
|
28
29
|
creator: string;
|
|
29
30
|
memo: string;
|
|
30
|
-
pool1:
|
|
31
|
-
pool2:
|
|
31
|
+
pool1: ExtendedTokenWithRate;
|
|
32
|
+
pool2: ExtendedTokenWithRate;
|
|
32
33
|
hash: string;
|
|
33
34
|
fee: {
|
|
34
35
|
exchange_fee: number;
|
|
@@ -42,8 +43,8 @@ export type ExtendedToken = Coin & ExtendedAsset & { key: string; }
|
|
|
42
43
|
export type ExtendedTokenWithRate = ExtendedToken & { rate: number; }
|
|
43
44
|
|
|
44
45
|
export type PoolTable = {
|
|
45
|
-
pool1:
|
|
46
|
-
pool2:
|
|
47
|
-
path:
|
|
48
|
-
getPathString: () => string;
|
|
46
|
+
pool1: ExtendedTokenWithRate;
|
|
47
|
+
pool2: ExtendedTokenWithRate;
|
|
48
|
+
path: INode<Pool>[];
|
|
49
|
+
getPathString: () => string;
|
|
49
50
|
} | undefined
|
package/src/utils.ts
CHANGED
|
@@ -8,4 +8,15 @@ export const isSameToken = (token1: PartialCoin, token2: PartialCoin) => {
|
|
|
8
8
|
return token1.xtokenSymbol === token2.xtokenSymbol &&
|
|
9
9
|
token1.xtokenPrecision === token2.xtokenPrecision &&
|
|
10
10
|
token1.xtokenContract === token2.xtokenContract
|
|
11
|
-
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const median = (numbers: number[]) => {
|
|
14
|
+
const sorted = numbers.slice().sort((a, b) => a - b);
|
|
15
|
+
const middle = Math.floor(sorted.length / 2);
|
|
16
|
+
|
|
17
|
+
if (sorted.length % 2 === 0) {
|
|
18
|
+
return (sorted[middle - 1] + sorted[middle]) / 2;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return sorted[middle];
|
|
22
|
+
}
|