@proton/wrap-constants 0.2.193 → 0.3.1

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/src/constants.ts DELETED
@@ -1,202 +0,0 @@
1
- import { Coin, CoinWallet, initializeCoins, Wallet } from "./coins";
2
-
3
- export const WRAP_CONTRACT = "proton.wrap";
4
- export const WRAP_AUTH_ACTION = "generateauth";
5
- export const FEE_FACTOR = 10000;
6
-
7
- export const initializeConstants = (chain: string) => {
8
- const {
9
- swapCoins,
10
- bridgeCoins,
11
- allEnabledCoins,
12
- allCoins,
13
- lendCoins,
14
- borrowCoins,
15
- loanCoins,
16
- } = initializeCoins(chain);
17
-
18
- const constants = {
19
- SWAP_CONTRACT: "proton.swaps",
20
- VEST_CONTRACT: "vest",
21
- DEX_CONTRACT: "dex",
22
- FEE_FACTOR,
23
- SWAP_URL:
24
- chain === "proton"
25
- ? "https://otc.protonswap.com"
26
- : "https://otc-test.protonswap.com",
27
- WRAP_SERVER_URL:
28
- chain === "proton"
29
- ? "https://www.api.bloks.io/proton-wrap-public2"
30
- : "https://www.api.bloks.io/proton-wrap-testnet-public2",
31
- WRAP_CONTRACT,
32
- WRAP_ACTIONS: {
33
- SAVE_ADDRESS: "saveaddress2",
34
- DELETE_ADDRESS: "deladdress2",
35
- WRAP_START: "wrapstart2",
36
- WRAP_FINISH: "wrapfinish2",
37
- WRAP_PROCESS: "wrapprocess2",
38
- WRAP_SET_CONF: "wrapsetconf2",
39
- },
40
- WRAP_TABLES: {
41
- ADDRESSES: "addresses2",
42
- WRAPS: "wraps2",
43
- },
44
- ADDRESSES_INDEXES: {
45
- BY_ACCOUNT: 2,
46
- BY_ADDR_HASH: 3,
47
- },
48
- WRAPS_INDEXES: {
49
- BY_ACCOUNT: 2,
50
- BY_STATUS: 3,
51
- BY_WRAP_HASH: 4,
52
- },
53
- WRAP_PROCESS_TIMER: 5000,
54
-
55
- FARM_CONTRACT: "yield.farms",
56
-
57
- WEBHOOK_PERMISSION: "webhook",
58
- PUBLIC_PERMISSION: "public",
59
- LIGHT_PERMISSION: "light",
60
- WRAP_AUTH_ACTION: WRAP_AUTH_ACTION,
61
-
62
- METAL_ENDPOINT:
63
- chain === "proton"
64
- ? "https://cirrus.metalpay.com"
65
- : "https://cirrus-dev.metalpay.com",
66
-
67
- METAL_PROTON_ENDPOINT:
68
- chain === "proton"
69
- ? "https://identity.api.prod.metalx.com"
70
- : "https://identity.api.dev.metalx.com",
71
-
72
- METAL_DEX_HTTP_ENDPOINT:
73
- chain === "proton"
74
- ? "https://dex.api.mainnet.metalx.com/dex"
75
- : "https://dex.api.testnet.metalx.com/dex",
76
-
77
- METAL_DEX_WS_ENDPOINT:
78
- chain === "proton"
79
- ? "wss://dex.api.mainnet.metalx.com/dexws"
80
- : "wss://dex.api.testnet.metalx.com/dexws",
81
-
82
- PROTON_ENDPOINTS:
83
- chain === "proton"
84
- ? [
85
- "https://rpc.api.mainnet.metalx.com",
86
- "https://proton.greymass.com",
87
- "https://proton.eoscafeblock.com",
88
- "https://proton.cryptolions.io",
89
- "https://proton.eosusa.news",
90
- ]
91
- : [
92
- "https://rpc.api.testnet.metalx.com",
93
- "https://testnet.protonchain.com",
94
- ],
95
-
96
- CHAIN_ID:
97
- chain === "proton"
98
- ? "384da888112027f0321850a169f737c33e53b388aad48b5adace4bab97f437e0"
99
- : "71ee83bcf52142d61019d95f9cc5427ba6a0d7ff8accd9e2088ae2abeaf3d3dd",
100
-
101
- COINS_URL:
102
- chain === "proton"
103
- ? "https://app.metalx.com/mainnet-coins.json"
104
- : "https://app.metalx.com/testnet-coins.json",
105
-
106
- DEX_PROCESS_SIZE: 30,
107
- DEX_SLTPPROCESS_SIZE: 10,
108
- MAX_PRICE: "9223372036854775806",
109
- MAX_ITEMS_PER_PAGE_DESKTOP: 100,
110
- MIN_PRICE: 1,
111
- DEFAULT_DEX_SYMBOL: "XPR_XMD",
112
-
113
- CHAIN: chain,
114
-
115
- SWAP_COINS: swapCoins,
116
- BRIDGE_COINS: bridgeCoins,
117
- ALL_ENABLED_COINS: allEnabledCoins,
118
- LEND_COINS: lendCoins,
119
- BORROW_COINS: borrowCoins,
120
- LOAN_COINS: loanCoins,
121
- ALL_COINS: allCoins,
122
-
123
- XMD_CONTRACT: "xmd.treasury",
124
- XMD_TOKEN_CONTRACT: "xmd.token",
125
- XMD_SYMBOL_CODE: "XMD",
126
- XMD_DECIMALS: 6,
127
-
128
- findBridgeCoinWalletByXtokenAndChain(
129
- xtokenSymbol: string,
130
- chain: string
131
- ): CoinWallet | undefined {
132
- const bridgeCoin = constants.BRIDGE_COINS.find(
133
- (bridgeCoin: Coin) => bridgeCoin.xtokenSymbol === xtokenSymbol
134
- );
135
- if (!bridgeCoin) return undefined;
136
- const bridgeWallet = bridgeCoin.wallets.find(
137
- (bridgeWallet: Wallet) => bridgeWallet.chain === chain
138
- );
139
- if (!bridgeWallet) return undefined;
140
-
141
- return {
142
- ...bridgeCoin,
143
- ...bridgeWallet,
144
- };
145
- },
146
-
147
- findBridgeCoinWallet(coin: string, wallet: string): CoinWallet | undefined {
148
- const bridgeCoin = constants.BRIDGE_COINS.find(
149
- (bridgeCoin: Coin) => bridgeCoin.coin === coin
150
- );
151
- if (!bridgeCoin) return undefined;
152
-
153
- const bridgeWallet = bridgeCoin.wallets.find(
154
- (bridgeWallet: Wallet) => bridgeWallet.wallet === wallet
155
- );
156
- if (!bridgeWallet) return undefined;
157
-
158
- return {
159
- ...bridgeCoin,
160
- ...bridgeWallet,
161
- };
162
- },
163
-
164
- findBridgeCoinWalletByWallet(wallet: string): CoinWallet | undefined {
165
- const matches: CoinWallet[] = [];
166
-
167
- constants.BRIDGE_COINS.forEach((bridgeCoin: Coin) => {
168
- bridgeCoin.wallets.forEach((bridgeWallet: Wallet) => {
169
- if (bridgeWallet.wallet === wallet) {
170
- matches.push({
171
- ...bridgeCoin,
172
- ...bridgeWallet,
173
- });
174
- }
175
- });
176
- });
177
-
178
- return matches.length === 1 ? matches[0] : undefined;
179
- },
180
-
181
- findBridgeCoinNetwork(
182
- coin: string,
183
- network: string
184
- ): CoinWallet | undefined {
185
- const bridgeCoin = constants.BRIDGE_COINS.find(
186
- (bridgeCoin: Coin) => bridgeCoin.coin === coin
187
- );
188
- if (!bridgeCoin) return undefined;
189
- const bridgeWallet = bridgeCoin.wallets.find(
190
- (bridgeWallet: Wallet) => bridgeWallet.network === network
191
- );
192
- if (!bridgeWallet) return undefined;
193
-
194
- return {
195
- ...bridgeCoin,
196
- ...bridgeWallet,
197
- };
198
- },
199
- };
200
-
201
- return constants;
202
- };
package/src/graph.ts DELETED
@@ -1,126 +0,0 @@
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 DELETED
@@ -1,6 +0,0 @@
1
- export * from './coins'
2
- export * from './parsers'
3
- export * from './constants'
4
- export * from './utils'
5
- export * from './graph'
6
- export * from './math'
package/src/math.ts DELETED
@@ -1,147 +0,0 @@
1
- import 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 = (
14
- pool1: BN.Instance,
15
- pool2: BN.Instance,
16
- swap: BN.Instance,
17
- exchange_fee: number,
18
- precision: number
19
- ): BN => {
20
- const term1 = pool2;
21
- const term2over = pool1.times(pool2);
22
- const term2under = pool1.plus(swap);
23
- const term2 = term2over.div(term2under).integerValue(BN.ROUND_DOWN);
24
- const term = term1.minus(term2);
25
- const fee = new BN(exchange_fee).div(FEE_FACTOR);
26
- const result = term.minus(term.times(fee));
27
-
28
- return result.integerValue(BN.ROUND_DOWN).shiftedBy(-precision);
29
- };
30
-
31
- export const compute_transfer_curve = (
32
- reserve_in: number,
33
- reserve_out: number,
34
- amount_in: number,
35
- exchange_fee: number,
36
- precision: number,
37
- amplifier: number
38
- ): BN => {
39
- if (amplifier === 0) {
40
- return compute_transfer(
41
- new BN(reserve_in),
42
- new BN(reserve_out),
43
- new BN(amount_in),
44
- exchange_fee,
45
- precision
46
- );
47
- }
48
-
49
- const MAX_ITERATIONS = 10;
50
-
51
- const sum = reserve_in + reserve_out;
52
- let D = sum;
53
- let D_prev = 0;
54
-
55
- const F = (v: number) => {
56
- return (v >= 0 ? 1 : -1) * Math.floor(Math.abs(v));
57
- };
58
-
59
- let i = MAX_ITERATIONS;
60
- while (D != D_prev && i--) {
61
- const prod1 = F((F((D * D) / (reserve_in * 2)) * D) / (reserve_out * 2));
62
- D_prev = D;
63
- D = F(
64
- (2 * D * (amplifier * sum + prod1)) /
65
- ((2 * amplifier - 1) * D + 3 * prod1)
66
- );
67
- }
68
-
69
- const b = reserve_in + amount_in + F(D / (amplifier * 2)) - D;
70
-
71
- const c = F(
72
- (F((D * D) / ((reserve_in + amount_in) * 2)) * D) / (amplifier * 4)
73
- );
74
-
75
- let x = D,
76
- x_prev = 0;
77
- i = MAX_ITERATIONS;
78
- while (x != x_prev && i--) {
79
- x_prev = F(x);
80
- x = +new BN(x * x + c).div(2 * x + b).toFixed(0, BN.ROUND_DOWN);
81
- }
82
-
83
- if (reserve_out <= x) {
84
- throw new Error("curve: insufficient reserve out");
85
- }
86
-
87
- // Amount out + Fee + 1
88
- const amount_out_with_fee = new BN(reserve_out - x);
89
-
90
- // Fee + 1
91
- // +1 covers small precision issues
92
- const fee = amount_out_with_fee
93
- .times(exchange_fee)
94
- .div(FEE_FACTOR)
95
- .plus(1)
96
- .toFixed(0, BN.ROUND_DOWN);
97
-
98
- // Amount out
99
- const amount_out = amount_out_with_fee.minus(fee);
100
-
101
- return amount_out.integerValue(BN.ROUND_DOWN).shiftedBy(-precision);
102
- };
103
-
104
- export const compute_liquidity = (
105
- amount_lt: BN.Instance,
106
- pool: BN.Instance,
107
- lt_supply: BN.Instance,
108
- precision: number
109
- ): number => {
110
- const over = amount_lt.times(pool);
111
- const under = lt_supply;
112
- const result = over.div(under).integerValue(BN.ROUND_DOWN);
113
-
114
- return +result.shiftedBy(-precision).toFixed(precision, BN.ROUND_DOWN);
115
- };
116
-
117
- export const isInputInvalid = (
118
- data: string,
119
- value: string | number,
120
- pool: { xtokenPrecision: number }
121
- ): boolean => {
122
- value = String(value);
123
-
124
- const isBackspace = data === null;
125
- const isNumber = !/[0-9]|\./.test(data);
126
- const isDoubleDot = data === "." && value.indexOf(".") !== value.length - 1;
127
- const isPrecisionTooHigh =
128
- value &&
129
- value.indexOf(".") !== -1 &&
130
- value.split(".")[1].length > pool.xtokenPrecision;
131
- const isInvalid =
132
- !isBackspace && Boolean(isNumber || isDoubleDot || isPrecisionTooHigh);
133
-
134
- return isInvalid;
135
- };
136
-
137
- export const applyDiscountToFee = (fee: number, staked: number): number => {
138
- if (staked >= 10000000) {
139
- return fee * 0;
140
- } else if (staked >= 1000000) {
141
- return fee * 0.33;
142
- } else if (staked >= 100000) {
143
- return fee * 0.66;
144
- } else {
145
- return fee;
146
- }
147
- };
@@ -1,82 +0,0 @@
1
- import * as z from 'zod';
2
- import { accountParser, authorizationsParser } from './base'
3
-
4
- const actionParser = z.object({
5
- account: z.string(),
6
- name: z.string(),
7
- authorization: z.array(
8
- z.object({
9
- actor: z.string(),
10
- permission: z.string(),
11
- })
12
- ),
13
- data: z.any(),
14
- hex_data: z.string().optional()
15
- })
16
-
17
- const transactionExtensionsParser = z.array(z.number().or(z.string())).optional()
18
-
19
- const rawTransactionParser = z.object({
20
- expiration : z.string(),
21
- ref_block_num : z.number(),
22
- ref_block_prefix : z.number(),
23
- max_net_usage_words : z.number(),
24
- max_cpu_usage_ms : z.number(),
25
- delay_sec : z.number(),
26
- context_free_actions : z.array(actionParser),
27
- transaction_extensions: z.array(transactionExtensionsParser)
28
- })
29
-
30
- export const baseActionParser = <T extends z.ZodRawShape>(contract: string, action: string, data: T) => z.object({
31
- account: contract === '*' ? accountParser : z.literal(contract),
32
- name: z.literal(action),
33
- data: z.object(data),
34
- authorization: authorizationsParser
35
- })
36
-
37
- export const baseActionSerializedParser = <T extends z.ZodRawShape>(contract: string, action: string, data: T) => z.object({
38
- account: contract === '*' ? accountParser : z.literal(contract),
39
- name: z.literal(action),
40
- data: z.string().or(z.object(data)),
41
- authorization: authorizationsParser
42
- })
43
-
44
- export const webauthActionParser = <T extends z.ZodRawShape>(contract: string, action: string, data: T) => z.object({
45
- account: z.literal('webauthn'),
46
- name: z.literal('exec'),
47
- data: z.object({
48
- account: accountParser,
49
- key: z.object({
50
- key: z.array(z.string().or(z.object({
51
- modulus: z.string(),
52
- exponent: z.string()
53
- }))),
54
- user_presence: z.number(),
55
- rpid: z.string()
56
- }),
57
- nonce: z.number(),
58
- permissions: authorizationsParser,
59
- signature: z.object({
60
- auth_data: z.string(),
61
- client_json: z.string(),
62
- signature: z.string()
63
- }),
64
- trx: rawTransactionParser.merge(
65
- z.object({
66
- actions: z.array(
67
- baseActionSerializedParser(contract, action, data)
68
- ).length(1)
69
- })
70
- )
71
- }),
72
- authorization: authorizationsParser
73
- })
74
-
75
- export const singleActionTransactionParser = <T extends z.ZodRawShape>(contract: string, action: string, data: T) => {
76
- const baseAction = baseActionParser(contract, action, data)
77
- const webauthAction = webauthActionParser(contract, action, data)
78
-
79
- return rawTransactionParser.merge(z.object({
80
- actions: z.array(baseAction.or(webauthAction)).length(1)
81
- }))
82
- }
@@ -1,27 +0,0 @@
1
- import * as z from 'zod';
2
- import { accountParser } from './base'
3
- import { generateAuthParser } from './auth'
4
-
5
- export const addressParser = z.object({
6
- index : z.number(),
7
- account : accountParser,
8
- chain : z.string(),
9
- address : z.string(),
10
- address_hash: z.string(),
11
- })
12
- export type Address = z.infer<typeof addressParser>
13
-
14
- export const generateAddressBodyParser = z.object({
15
- coin: z.string(),
16
- wallet: z.string(),
17
- }).merge(generateAuthParser)
18
- export type GenerateAddressBody = z.infer<typeof generateAddressBodyParser>
19
-
20
- export const generateAddressResponse = z.object({
21
- id : z.string(), // "2448834505708930950",
22
- address: z.string(), // "0x617C03C1E2Bf81C062A4CEfbE312c070E1d86d18",
23
- coin : z.string(), // "ETH",
24
- wallet : z.string(), // "ETH",
25
- memo : z.string().optional()
26
- })
27
- export type GenerateAddressResponse = z.infer<typeof generateAddressResponse>
@@ -1,36 +0,0 @@
1
- import * as z from "zod";
2
- import { baseActionParser, singleActionTransactionParser } from "./actions";
3
- import { authorizationParser } from "./base";
4
- import { WRAP_CONTRACT, WRAP_AUTH_ACTION } from "../constants";
5
-
6
- export const createAbstractAuthParser = (
7
- contract: string,
8
- action: string,
9
- data: z.ZodRawShape
10
- ) => {
11
- return z.object({
12
- signer: authorizationParser,
13
- signatures: z.array(z.string()).length(1),
14
- transaction: singleActionTransactionParser(contract, action, data),
15
- });
16
- };
17
-
18
- const generateAuthData = {
19
- time: z.string(),
20
- protonAccount: z.string(),
21
- };
22
- const generateAuthActionsBaseParser = baseActionParser(
23
- WRAP_CONTRACT,
24
- WRAP_AUTH_ACTION,
25
- generateAuthData
26
- );
27
- export type GenerateAuthBaseAction = z.infer<
28
- typeof generateAuthActionsBaseParser
29
- >;
30
-
31
- export const generateAuthParser = createAbstractAuthParser(
32
- WRAP_CONTRACT,
33
- WRAP_AUTH_ACTION,
34
- generateAuthData
35
- );
36
- export type GenerateAuth = z.infer<typeof generateAuthParser>;
@@ -1,24 +0,0 @@
1
- import * as z from "zod";
2
-
3
- export const accountParser = z.string().regex(/^[.1-5a-z]{0,12}[.1-5a-j]?$/);
4
-
5
- export const authorizationParser = z.object({
6
- actor: accountParser,
7
- permission: z.string(),
8
- });
9
- export type Authorization = z.infer<typeof authorizationParser>;
10
-
11
- export const authorizationsParser = z.array(authorizationParser).min(1);
12
- export type Authorizations = z.infer<typeof authorizationsParser>;
13
-
14
- export const ExtendedAssetParser = z.object({
15
- quantity: z.string(),
16
- contract: accountParser,
17
- });
18
- export type ExtendedAsset = z.infer<typeof ExtendedAssetParser>;
19
-
20
- export const accountRamDeltaParser = z.object({
21
- account: z.string(),
22
- delta: z.string(),
23
- });
24
- export type HistoryAccountRamDelta = z.infer<typeof accountRamDeltaParser>;