@provex/utils 1.0.16 → 1.2.3
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/src/chain.d.ts +436 -4
- package/dist/src/chain.js +80 -0
- package/dist/src/chain.test.d.ts +1 -0
- package/dist/src/chain.test.js +112 -0
- package/dist/src/contracts.d.ts +196 -20
- package/dist/src/contracts.js +245 -17
- package/dist/src/contracts.test.d.ts +1 -0
- package/dist/src/contracts.test.js +243 -0
- package/dist/src/conversionRates.d.ts +99 -8
- package/dist/src/conversionRates.js +74 -4
- package/dist/src/conversionRates.test.d.ts +1 -0
- package/dist/src/conversionRates.test.js +473 -0
- package/dist/src/currencies.d.ts +113 -9
- package/dist/src/currencies.js +119 -38
- package/dist/src/currencies.test.d.ts +1 -0
- package/dist/src/currencies.test.js +343 -0
- package/dist/src/data/countries.json +166 -166
- package/dist/src/deposit.d.ts +69 -1
- package/dist/src/deposit.js +52 -1
- package/dist/src/depositKeys.d.ts +154 -0
- package/dist/src/depositKeys.js +104 -0
- package/dist/src/depositKeys.test.d.ts +1 -0
- package/dist/src/depositKeys.test.js +231 -0
- package/dist/src/endpoints.d.ts +51 -0
- package/dist/src/endpoints.js +70 -0
- package/dist/src/endpoints.test.d.ts +1 -0
- package/dist/src/endpoints.test.js +54 -0
- package/dist/src/events.d.ts +40 -2
- package/dist/src/events.js +86 -10
- package/dist/src/events.test.d.ts +1 -0
- package/dist/src/events.test.js +111 -0
- package/dist/src/fees.d.ts +136 -0
- package/dist/src/fees.js +178 -0
- package/dist/src/fees.test.d.ts +1 -0
- package/dist/src/fees.test.js +165 -0
- package/dist/src/ids.d.ts +125 -22
- package/dist/src/ids.js +211 -61
- package/dist/src/ids.test.d.ts +1 -0
- package/dist/src/ids.test.js +607 -0
- package/dist/src/index.d.ts +30 -1
- package/dist/src/index.js +30 -1
- package/dist/src/input.d.ts +23 -4
- package/dist/src/input.js +23 -4
- package/dist/src/intent.d.ts +38 -2
- package/dist/src/intent.js +28 -1
- package/dist/src/intent.test.d.ts +1 -0
- package/dist/src/intent.test.js +119 -0
- package/dist/src/json.d.ts +38 -1
- package/dist/src/json.js +38 -2
- package/dist/src/normalization.d.ts +224 -0
- package/dist/src/normalization.js +119 -0
- package/dist/src/normalization.test.d.ts +1 -0
- package/dist/src/normalization.test.js +284 -0
- package/dist/src/participants.d.ts +19 -4
- package/dist/src/participants.js +19 -4
- package/dist/src/payment.d.ts +333 -16
- package/dist/src/payment.js +768 -58
- package/dist/src/payment.test.d.ts +1 -0
- package/dist/src/payment.test.js +272 -0
- package/dist/src/peer-types.d.ts +327 -0
- package/dist/src/peer-types.js +169 -0
- package/dist/src/profit.d.ts +26 -5
- package/dist/src/profit.js +31 -8
- package/dist/src/profit.test.d.ts +1 -0
- package/dist/src/profit.test.js +315 -0
- package/dist/src/reputation.d.ts +125 -0
- package/dist/src/reputation.js +188 -0
- package/dist/src/reputation.test.d.ts +1 -0
- package/dist/src/reputation.test.js +156 -0
- package/dist/src/sort.d.ts +49 -10
- package/dist/src/sort.js +40 -9
- package/dist/src/sort.test.d.ts +1 -0
- package/dist/src/sort.test.js +216 -0
- package/dist/src/stateKey.d.ts +35 -0
- package/dist/src/stateKey.js +36 -0
- package/dist/src/stats.d.ts +55 -1
- package/dist/src/stats.js +46 -1
- package/dist/src/stats.test.d.ts +1 -0
- package/dist/src/stats.test.js +104 -0
- package/dist/src/time.d.ts +59 -9
- package/dist/src/time.js +71 -15
- package/dist/src/time.test.d.ts +1 -0
- package/dist/src/time.test.js +231 -0
- package/dist/src/tokens.d.ts +11972 -145
- package/dist/src/tokens.js +98 -17
- package/dist/src/tokens.test.d.ts +1 -0
- package/dist/src/tokens.test.js +193 -0
- package/dist/src/units.d.ts +183 -0
- package/dist/src/units.js +206 -6
- package/dist/src/units.test.d.ts +1 -0
- package/dist/src/units.test.js +327 -0
- package/dist/src/zkp2p-types.d.ts +273 -4
- package/dist/src/zkp2p-types.js +169 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +116 -26
- package/scripts/convert-svg.ts +0 -22
- package/src/chain.ts +0 -27
- package/src/contracts.ts +0 -89
- package/src/conversionRates.ts +0 -356
- package/src/currencies.ts +0 -286
- package/src/data/countries.json +0 -1099
- package/src/deposit.ts +0 -2
- package/src/events.ts +0 -21
- package/src/flags/eur.svg +0 -4
- package/src/flags/usd.svg +0 -25
- package/src/flags.ts +0 -4
- package/src/global.d.ts +0 -9
- package/src/ids.ts +0 -137
- package/src/index.ts +0 -16
- package/src/input.ts +0 -35
- package/src/intent.ts +0 -15
- package/src/json.ts +0 -42
- package/src/participants.ts +0 -14
- package/src/payment.ts +0 -573
- package/src/profit.ts +0 -55
- package/src/sort.ts +0 -49
- package/src/stats.ts +0 -10
- package/src/time.ts +0 -123
- package/src/tokens.ts +0 -159
- package/src/units.ts +0 -120
- package/src/zkp2p-types.ts +0 -55
- package/tsconfig.json +0 -18
package/package.json
CHANGED
|
@@ -1,45 +1,135 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@provex/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"main": "dist/src/index.js",
|
|
5
5
|
"types": "dist/src/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/provex-tech/monorepo.git",
|
|
10
|
+
"directory": "packages/utils"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
8
15
|
"exports": {
|
|
9
|
-
".":
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"./
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"./
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"./
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"./
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
16
|
+
".": {
|
|
17
|
+
"import": "./dist/src/index.js",
|
|
18
|
+
"types": "./dist/src/index.d.ts"
|
|
19
|
+
},
|
|
20
|
+
"./contracts": {
|
|
21
|
+
"import": "./dist/src/contracts.js",
|
|
22
|
+
"types": "./dist/src/contracts.d.ts"
|
|
23
|
+
},
|
|
24
|
+
"./currencies": {
|
|
25
|
+
"import": "./dist/src/currencies.js",
|
|
26
|
+
"types": "./dist/src/currencies.d.ts"
|
|
27
|
+
},
|
|
28
|
+
"./units": {
|
|
29
|
+
"import": "./dist/src/units.js",
|
|
30
|
+
"types": "./dist/src/units.d.ts"
|
|
31
|
+
},
|
|
32
|
+
"./payment": {
|
|
33
|
+
"import": "./dist/src/payment.js",
|
|
34
|
+
"types": "./dist/src/payment.d.ts"
|
|
35
|
+
},
|
|
36
|
+
"./sort": {
|
|
37
|
+
"import": "./dist/src/sort.js",
|
|
38
|
+
"types": "./dist/src/sort.d.ts"
|
|
39
|
+
},
|
|
40
|
+
"./time": {
|
|
41
|
+
"import": "./dist/src/time.js",
|
|
42
|
+
"types": "./dist/src/time.d.ts"
|
|
43
|
+
},
|
|
44
|
+
"./tokens": {
|
|
45
|
+
"import": "./dist/src/tokens.js",
|
|
46
|
+
"types": "./dist/src/tokens.d.ts"
|
|
47
|
+
},
|
|
48
|
+
"./intent": {
|
|
49
|
+
"import": "./dist/src/intent.js",
|
|
50
|
+
"types": "./dist/src/intent.d.ts"
|
|
51
|
+
},
|
|
52
|
+
"./conversionRates": {
|
|
53
|
+
"import": "./dist/src/conversionRates.js",
|
|
54
|
+
"types": "./dist/src/conversionRates.d.ts"
|
|
55
|
+
},
|
|
56
|
+
"./json": {
|
|
57
|
+
"import": "./dist/src/json.js",
|
|
58
|
+
"types": "./dist/src/json.d.ts"
|
|
59
|
+
},
|
|
60
|
+
"./profit": {
|
|
61
|
+
"import": "./dist/src/profit.js",
|
|
62
|
+
"types": "./dist/src/profit.d.ts"
|
|
63
|
+
},
|
|
64
|
+
"./chain": {
|
|
65
|
+
"import": "./dist/src/chain.js",
|
|
66
|
+
"types": "./dist/src/chain.d.ts"
|
|
67
|
+
},
|
|
68
|
+
"./participants": {
|
|
69
|
+
"import": "./dist/src/participants.js",
|
|
70
|
+
"types": "./dist/src/participants.d.ts"
|
|
71
|
+
},
|
|
72
|
+
"./input": {
|
|
73
|
+
"import": "./dist/src/input.js",
|
|
74
|
+
"types": "./dist/src/input.d.ts"
|
|
75
|
+
},
|
|
76
|
+
"./stats": {
|
|
77
|
+
"import": "./dist/src/stats.js",
|
|
78
|
+
"types": "./dist/src/stats.d.ts"
|
|
79
|
+
},
|
|
80
|
+
"./stateKey": {
|
|
81
|
+
"import": "./dist/src/stateKey.js",
|
|
82
|
+
"types": "./dist/src/stateKey.d.ts"
|
|
83
|
+
},
|
|
84
|
+
"./ids": {
|
|
85
|
+
"import": "./dist/src/ids.js",
|
|
86
|
+
"types": "./dist/src/ids.d.ts"
|
|
87
|
+
},
|
|
88
|
+
"./events": {
|
|
89
|
+
"import": "./dist/src/events.js",
|
|
90
|
+
"types": "./dist/src/events.d.ts"
|
|
91
|
+
},
|
|
92
|
+
"./peer-types": {
|
|
93
|
+
"import": "./dist/src/peer-types.js",
|
|
94
|
+
"types": "./dist/src/peer-types.d.ts"
|
|
95
|
+
},
|
|
96
|
+
"./deposit": {
|
|
97
|
+
"import": "./dist/src/deposit.js",
|
|
98
|
+
"types": "./dist/src/deposit.d.ts"
|
|
99
|
+
},
|
|
100
|
+
"./fees": {
|
|
101
|
+
"import": "./dist/src/fees.js",
|
|
102
|
+
"types": "./dist/src/fees.d.ts"
|
|
103
|
+
},
|
|
104
|
+
"./reputation": {
|
|
105
|
+
"import": "./dist/src/reputation.js",
|
|
106
|
+
"types": "./dist/src/reputation.d.ts"
|
|
107
|
+
}
|
|
29
108
|
},
|
|
30
109
|
"scripts": {
|
|
31
|
-
"build": "tsx scripts/convert-svg && tsc && cp -r src/data dist/src/"
|
|
110
|
+
"build": "tsx scripts/convert-svg && tsc && cp -r src/data dist/src/",
|
|
111
|
+
"test": "node --import tsx --test 'src/**/*.test.ts'",
|
|
112
|
+
"test:watch": "node --import tsx --test --watch 'src/**/*.test.ts'",
|
|
113
|
+
"test:coverage": "node --import tsx --test --experimental-test-coverage --test-coverage-exclude='src/**/*.test.ts' --test-coverage-exclude='src/data/**' --test-coverage-exclude='src/flags/**' --test-coverage-exclude='src/flags.ts' --test-coverage-exclude='src/global.d.ts' 'src/**/*.test.ts'"
|
|
32
114
|
},
|
|
33
115
|
"keywords": [],
|
|
34
116
|
"author": "",
|
|
35
117
|
"license": "ISC",
|
|
36
118
|
"description": "",
|
|
119
|
+
"files": [
|
|
120
|
+
"dist",
|
|
121
|
+
"README.md"
|
|
122
|
+
],
|
|
37
123
|
"dependencies": {
|
|
124
|
+
"@zkp2p/providers": "^7.8.0"
|
|
125
|
+
},
|
|
126
|
+
"peerDependencies": {
|
|
127
|
+
"viem": "^2.0.0"
|
|
128
|
+
},
|
|
129
|
+
"devDependencies": {
|
|
130
|
+
"@types/node": "^22.0.0",
|
|
38
131
|
"tsx": "^4.20.3",
|
|
39
132
|
"typescript": "^5.9.2",
|
|
40
133
|
"viem": "^2.38.0"
|
|
41
|
-
},
|
|
42
|
-
"devDependencies": {
|
|
43
|
-
"@types/node": "^22.0.0"
|
|
44
134
|
}
|
|
45
135
|
}
|
package/scripts/convert-svg.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
|
|
4
|
-
const __dirname = import.meta.dirname;
|
|
5
|
-
|
|
6
|
-
// Read SVG files and convert to TypeScript constants
|
|
7
|
-
const svgDir = path.join(__dirname, '../src/flags');
|
|
8
|
-
const outputFile = path.join(__dirname, '../src/flags.ts');
|
|
9
|
-
|
|
10
|
-
const svgFiles = ['usd.svg', 'eur.svg'];
|
|
11
|
-
let output = '// Auto-generated from SVG files\n\n';
|
|
12
|
-
|
|
13
|
-
svgFiles.forEach(filename => {
|
|
14
|
-
const svgPath = path.join(svgDir, filename);
|
|
15
|
-
const svgContent = fs.readFileSync(svgPath, 'utf8');
|
|
16
|
-
const cleanSvg = svgContent.replace(/[\r\n]/g, ' ').replace(/\s+/g, ' ').trim();
|
|
17
|
-
const constName = filename.replace('.svg', '').toUpperCase() + '_FLAG_SVG';
|
|
18
|
-
output += `export const ${constName} = '${cleanSvg}';\n`;
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
fs.writeFileSync(outputFile, output);
|
|
22
|
-
console.log('Generated flags.ts from SVG files');
|
package/src/chain.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { base, pulsechain, type Chain } from 'viem/chains'
|
|
2
|
-
|
|
3
|
-
export const chains = [pulsechain, base] as [Chain, ...Chain[]]
|
|
4
|
-
|
|
5
|
-
export const chainIds = chains.map((chain) => chain.id)
|
|
6
|
-
|
|
7
|
-
export type ChainId = (typeof chainIds)[number]
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Get the explorer URL for a given chain, type, and hash
|
|
11
|
-
* @param chain - The chain to get the explorer URL for
|
|
12
|
-
* @param type - The type of explorer to use (tx or address)
|
|
13
|
-
* @param hash - The hash to get the explorer URL for
|
|
14
|
-
* @returns The explorer URL
|
|
15
|
-
*/
|
|
16
|
-
export function getExplorerUrl(chain: Chain, type: 'tx' | 'address', hash: string) {
|
|
17
|
-
return `${chain.blockExplorers?.default.url}/${type}/${hash}`
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const chainIdToChain = new Map<ChainId, Chain>(
|
|
21
|
-
chains.map((chain) => [chain.id, chain] as const)
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
export const chainIdToTargetBlockTime = new Map<number, number>([
|
|
25
|
-
[369, 10_000],
|
|
26
|
-
[8453, 2_000],
|
|
27
|
-
])
|
package/src/contracts.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { getAddress, type Hex } from 'viem'
|
|
2
|
-
import { base, pulsechain } from 'viem/chains'
|
|
3
|
-
import type { ChainId } from './chain.js'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Contracts for the app
|
|
7
|
-
*/
|
|
8
|
-
export type Contracts = {
|
|
9
|
-
gatingService: Hex
|
|
10
|
-
usdc: Hex
|
|
11
|
-
escrow: Hex[]
|
|
12
|
-
venmoReclaimVerifier: Hex[]
|
|
13
|
-
revolutReclaimVerifier: Hex[]
|
|
14
|
-
cashappReclaimVerifier: Hex[]
|
|
15
|
-
wiseReclaimVerifier: Hex[]
|
|
16
|
-
mercadopagoReclaimVerifier: Hex[]
|
|
17
|
-
zelleBaseVerifier: Hex[]
|
|
18
|
-
zelleBoAReclaimVerifier: Hex[]
|
|
19
|
-
zelleChaseReclaimVerifier: Hex[]
|
|
20
|
-
zelleCitiReclaimVerifier: Hex[]
|
|
21
|
-
paypalReclaimVerifier: Hex[]
|
|
22
|
-
monzoReclaimVerifier: Hex[]
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export const reclaimSigners = {
|
|
26
|
-
zkp2pWitnessSigner: '0x0636c417755E3ae25C6c166D181c0607F4C572A3',
|
|
27
|
-
reclaimWitnessSigner: '0x244897572368eadf65bfbc5aec98d8e5443a9072',
|
|
28
|
-
} as const
|
|
29
|
-
|
|
30
|
-
export const baseContracts = {
|
|
31
|
-
gatingService: getAddress('0x396D31055Db28C0C6f36e8b36f18FE7227248a97'),
|
|
32
|
-
usdc: getAddress('0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913'),
|
|
33
|
-
escrow: [getAddress('0xCA38607D85E8F6294Dc10728669605E6664C2D70')],
|
|
34
|
-
venmoReclaimVerifier: [getAddress('0x9a733B55a875D0DB4915c6B36350b24F8AB99dF5')],
|
|
35
|
-
revolutReclaimVerifier: [getAddress('0xAA5A1B62B01781E789C900d616300717CD9A41aB')],
|
|
36
|
-
cashappReclaimVerifier: [getAddress('0x76D33A33068D86016B806dF02376dDBb23Dd3703')],
|
|
37
|
-
wiseReclaimVerifier: [getAddress('0xFF0149799631D7A5bdE2e7eA9b306c42b3d9a9ca')],
|
|
38
|
-
mercadopagoReclaimVerifier: [
|
|
39
|
-
// getAddress('0x00d003c73eab8feaec04bab976235915fe7641e3'),
|
|
40
|
-
getAddress('0xf2AC5be14F32Cbe6A613CFF8931d95460D6c33A3'),
|
|
41
|
-
],
|
|
42
|
-
zelleBaseVerifier: [
|
|
43
|
-
// getAddress('0x1783f040783c0827fb64d128ece548d9b3613ad5'),
|
|
44
|
-
getAddress('0x431a078A5029146aAB239c768A615CD484519aF7'),
|
|
45
|
-
],
|
|
46
|
-
zelleChaseReclaimVerifier: [getAddress('0xBcD7C6BBcA5869fBefe3E322263EE1090221D7A9')],
|
|
47
|
-
zelleBoAReclaimVerifier: [getAddress('0x809c7A20A2dbBB3210fC73Dd7C01Cd7Fa70C195F')],
|
|
48
|
-
zelleCitiReclaimVerifier: [getAddress('0x3128badc46dbe2e37bdd2d64f33fb3b3a639570e')],
|
|
49
|
-
paypalReclaimVerifier: [getAddress('0x03d17E9371C858072E171276979f6B44571C5DeA')],
|
|
50
|
-
monzoReclaimVerifier: [getAddress('0x0dE46433bD251027f73eD8f28E01eF05DA36a2E0')],
|
|
51
|
-
} as Contracts
|
|
52
|
-
|
|
53
|
-
export const pulsechainContracts = {
|
|
54
|
-
gatingService: getAddress('0x4Bd6364FCC4D8A537aBfee33B16E959794fDf9Cf'),
|
|
55
|
-
usdc: getAddress('0x15D38573d2feeb82e7ad5187aB8c1D52810B1f07'),
|
|
56
|
-
escrow: [getAddress('0x2932E08BFD212Ef0732257bDd6463BF55E9b97d7')],
|
|
57
|
-
cashappReclaimVerifier: [getAddress('0x9224433D6AD9a5A774ca74Ca33c01747FA710A9d')],
|
|
58
|
-
mercadopagoReclaimVerifier: [getAddress('0xc4D0cf4DF12e329f9c951d1D9BB815da6138850A')],
|
|
59
|
-
monzoReclaimVerifier: [getAddress('0xBde2c79c1897459e66C0fdF6764EED0c6839C878')],
|
|
60
|
-
nullifierRegistry: [getAddress('0x471D3c6C9E32CE2556d0117aC161Aa82AEac17d8')],
|
|
61
|
-
paypalReclaimVerifier: [getAddress('0x1DdBfd39a141C5A2329d849CADa6a83404E0Ce4F')],
|
|
62
|
-
revolutReclaimVerifier: [getAddress('0x8234AA463C8B94778f58eA0bFEfcBAC3683341A9')],
|
|
63
|
-
venmoReclaimVerifier: [getAddress('0x0Dd524dA03f610480aF3F433B411b6016cFBDeab')],
|
|
64
|
-
wiseReclaimVerifier: [getAddress('0x7B72d7CfAfDfD397E2460c933e84D16dc121845a')],
|
|
65
|
-
zelleBaseVerifier: [getAddress('0x539566771f9c38c6038aafc1946bf11af9cdb201')],
|
|
66
|
-
zelleBoAReclaimVerifier: [getAddress('0x506a746b8228F9f9d4522105b973Dc7223A64EAC')],
|
|
67
|
-
zelleChaseReclaimVerifier: [getAddress('0xB0aA0a58959465bBCf0eCbD62A84B743A5435c99')],
|
|
68
|
-
zelleCitiReclaimVerifier: [getAddress('0x5624E479924eBFb25B51F36e3FDaE605dADb448b')],
|
|
69
|
-
} as Contracts
|
|
70
|
-
|
|
71
|
-
export const contractsByChainId = new Map<ChainId, Contracts>([
|
|
72
|
-
[base.id, baseContracts],
|
|
73
|
-
[pulsechain.id, pulsechainContracts],
|
|
74
|
-
])
|
|
75
|
-
|
|
76
|
-
export const getContracts = (chainId: ChainId) => {
|
|
77
|
-
return contractsByChainId.get(chainId) ?? null
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export const getLatestContract = (chainId: ChainId, key: keyof Contracts) => {
|
|
81
|
-
const contracts = getContracts(chainId)
|
|
82
|
-
if (!contracts) return null
|
|
83
|
-
const target = contracts[key]
|
|
84
|
-
if (!target) return null
|
|
85
|
-
if (Array.isArray(target)) {
|
|
86
|
-
return target[target.length - 1] ?? null
|
|
87
|
-
}
|
|
88
|
-
return target
|
|
89
|
-
}
|
package/src/conversionRates.ts
DELETED
|
@@ -1,356 +0,0 @@
|
|
|
1
|
-
import type { Hex } from 'viem'
|
|
2
|
-
import { Currency, SUPPORTED_CURRENCIES } from './currencies.js'
|
|
3
|
-
import { verifierGetsKey, type ProviderKey } from './payment.js'
|
|
4
|
-
import type { SortDirection } from './sort.js'
|
|
5
|
-
import type { TokenInfo } from './tokens.js'
|
|
6
|
-
import type { ChainId } from './chain.js'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Conversion rate info
|
|
10
|
-
*/
|
|
11
|
-
export interface ConversionRateInfo {
|
|
12
|
-
depositConversionRateUpdatedId: string
|
|
13
|
-
currency: string
|
|
14
|
-
verifier: string
|
|
15
|
-
value: string
|
|
16
|
-
orderId: string
|
|
17
|
-
depositId: string
|
|
18
|
-
transaction: {
|
|
19
|
-
hash: string
|
|
20
|
-
block: {
|
|
21
|
-
timestamp: string
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Conversion rate info with current deposit conversion rate updated
|
|
28
|
-
*/
|
|
29
|
-
export interface ConversionRateInfoWithCurrent extends ConversionRateInfo {
|
|
30
|
-
currentDepositConversionRateUpdatedId: string
|
|
31
|
-
currentDepositConversionRateUpdated: {
|
|
32
|
-
value: string
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Provider rates
|
|
38
|
-
*/
|
|
39
|
-
export type ProviderRates<RateInfo> = Map<ProviderKey, Map<Hex, RateInfo>>
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Process conversion rates options
|
|
43
|
-
*/
|
|
44
|
-
interface ProcessConversionRatesOptions {
|
|
45
|
-
chainId: ChainId
|
|
46
|
-
events: ConversionRateInfoWithCurrent[]
|
|
47
|
-
sortOrder?: SortDirection // Default: 'desc' (most recent first)
|
|
48
|
-
includeHistorical?: boolean // Default: false (only latest rates)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Get conversion rate options
|
|
53
|
-
*/
|
|
54
|
-
interface GetConversionRateOptions {
|
|
55
|
-
events: ConversionRateInfoWithCurrent[]
|
|
56
|
-
currency: Hex
|
|
57
|
-
verifier: string
|
|
58
|
-
chainId: ChainId
|
|
59
|
-
includeHistory?: boolean // Default: false (only latest rate)
|
|
60
|
-
fallbackRate?: { currency: string; value: string; verifier: string } | null
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Extract currencies options
|
|
65
|
-
*/
|
|
66
|
-
interface ExtractCurrenciesOptions {
|
|
67
|
-
providerRates: ProviderRates<ConversionRateInfo[]>
|
|
68
|
-
providersOnly?: ProviderKey[] // Filter to specific providers
|
|
69
|
-
currenciesOnly?: string[] // Filter to specific currencies
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Process deposit currency added events into organized conversion rates
|
|
74
|
-
*/
|
|
75
|
-
export function processConversionRates(options: ProcessConversionRatesOptions): ProviderRates<ConversionRateInfo[]> {
|
|
76
|
-
const {
|
|
77
|
-
events,
|
|
78
|
-
sortOrder = 'asc',
|
|
79
|
-
includeHistorical = false,
|
|
80
|
-
chainId,
|
|
81
|
-
} = options
|
|
82
|
-
|
|
83
|
-
const ratesMap = new Map<ProviderKey, Map<Hex, ConversionRateInfo[]>>()
|
|
84
|
-
|
|
85
|
-
// Sort events by orderId
|
|
86
|
-
const sortedEvents = [...events].sort((a, b) =>
|
|
87
|
-
sortOrder === 'desc'
|
|
88
|
-
? b.orderId.localeCompare(a.orderId)
|
|
89
|
-
: a.orderId.localeCompare(b.orderId)
|
|
90
|
-
)
|
|
91
|
-
|
|
92
|
-
for (const event of sortedEvents) {
|
|
93
|
-
if (!event.currentDepositConversionRateUpdated) continue
|
|
94
|
-
|
|
95
|
-
const providerKey = verifierGetsKey(chainId, event.verifier)
|
|
96
|
-
const currency = event.currency as Hex
|
|
97
|
-
|
|
98
|
-
// Get or create provider rates map
|
|
99
|
-
const providerRates = ratesMap.get(providerKey) ?? new Map<Hex, ConversionRateInfo[]>()
|
|
100
|
-
|
|
101
|
-
// Get or create currency rates array
|
|
102
|
-
const currencyRates = providerRates.get(currency) ?? []
|
|
103
|
-
|
|
104
|
-
const rateInfo: ConversionRateInfo = {
|
|
105
|
-
currency,
|
|
106
|
-
depositConversionRateUpdatedId: event.currentDepositConversionRateUpdatedId,
|
|
107
|
-
value: event.currentDepositConversionRateUpdated.value ?? '',
|
|
108
|
-
verifier: event.verifier,
|
|
109
|
-
orderId: event.orderId,
|
|
110
|
-
depositId: event.depositId,
|
|
111
|
-
transaction: {
|
|
112
|
-
hash: event.transaction!.hash!,
|
|
113
|
-
block: {
|
|
114
|
-
timestamp: event.transaction!.block!.timestamp
|
|
115
|
-
}
|
|
116
|
-
},
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (includeHistorical) {
|
|
120
|
-
// Add all rates chronologically
|
|
121
|
-
currencyRates.push(rateInfo)
|
|
122
|
-
} else {
|
|
123
|
-
// Only keep the latest rate (first in desc order, last in asc order)
|
|
124
|
-
if (currencyRates.length === 0) {
|
|
125
|
-
currencyRates.push(rateInfo)
|
|
126
|
-
} else if (sortOrder === 'asc') {
|
|
127
|
-
// Replace with newer rate
|
|
128
|
-
currencyRates[0] = rateInfo
|
|
129
|
-
}
|
|
130
|
-
// For desc order with includeHistorical=false, we only add the first (most recent) rate
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
providerRates.set(currency, currencyRates)
|
|
134
|
-
ratesMap.set(providerKey, providerRates)
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return ratesMap
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Get conversion rate for a specific currency and verifier combination
|
|
142
|
-
*/
|
|
143
|
-
export function getConversionRate(options: GetConversionRateOptions): ConversionRateInfo | ConversionRateInfo[] | null {
|
|
144
|
-
const {
|
|
145
|
-
events,
|
|
146
|
-
currency,
|
|
147
|
-
verifier,
|
|
148
|
-
chainId,
|
|
149
|
-
includeHistory = false,
|
|
150
|
-
fallbackRate
|
|
151
|
-
} = options
|
|
152
|
-
|
|
153
|
-
// Process rates to get organized data
|
|
154
|
-
const providerRates = processConversionRates({
|
|
155
|
-
events,
|
|
156
|
-
includeHistorical: includeHistory,
|
|
157
|
-
chainId,
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
const providerKey = verifierGetsKey(chainId, verifier)
|
|
161
|
-
const rates = providerRates.get(providerKey)?.get(currency)
|
|
162
|
-
|
|
163
|
-
if (rates && rates.length > 0) {
|
|
164
|
-
return includeHistory ? rates : rates[0]
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// Check fallback rate if provided
|
|
168
|
-
if (fallbackRate &&
|
|
169
|
-
fallbackRate.currency === currency &&
|
|
170
|
-
fallbackRate.verifier === verifier) {
|
|
171
|
-
return {
|
|
172
|
-
depositConversionRateUpdatedId: '0',
|
|
173
|
-
currency: fallbackRate.currency,
|
|
174
|
-
value: fallbackRate.value,
|
|
175
|
-
verifier: fallbackRate.verifier,
|
|
176
|
-
depositId: '0',
|
|
177
|
-
orderId: '0', // Indicate this is a fallback/original rate
|
|
178
|
-
transaction: {
|
|
179
|
-
hash: '0',
|
|
180
|
-
block: {
|
|
181
|
-
timestamp: '0'
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
return null
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Extract unique currencies from processed provider rates
|
|
192
|
-
*/
|
|
193
|
-
export function extractCurrencies(options: ExtractCurrenciesOptions): string[] {
|
|
194
|
-
const { providerRates, providersOnly, currenciesOnly } = options
|
|
195
|
-
|
|
196
|
-
const currencySet = new Set<string>()
|
|
197
|
-
|
|
198
|
-
for (const [providerKey, currencyMap] of providerRates.entries()) {
|
|
199
|
-
// Filter by providers if specified
|
|
200
|
-
if (providersOnly && !providersOnly.includes(providerKey)) {
|
|
201
|
-
continue
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
for (const [currency] of currencyMap.entries()) {
|
|
205
|
-
// Filter by currencies if specified
|
|
206
|
-
if (currenciesOnly && !currenciesOnly.includes(currency)) {
|
|
207
|
-
continue
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
currencySet.add(currency)
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return Array.from(currencySet)
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Get the latest conversion rate across all providers for a currency
|
|
219
|
-
*/
|
|
220
|
-
export function getLatestRateForCurrency(options: {
|
|
221
|
-
providerRates: ProviderRates<ConversionRateInfo>
|
|
222
|
-
currency: Hex
|
|
223
|
-
preferredProvider?: ProviderKey
|
|
224
|
-
}): ConversionRateInfo | null {
|
|
225
|
-
const { providerRates, currency, preferredProvider } = options
|
|
226
|
-
|
|
227
|
-
let latestRate: ConversionRateInfo | null = null
|
|
228
|
-
let latestTimestamp = BigInt(0)
|
|
229
|
-
|
|
230
|
-
// Check preferred provider first
|
|
231
|
-
if (preferredProvider) {
|
|
232
|
-
const providerRate = providerRates.get(preferredProvider)?.get(currency)
|
|
233
|
-
if (providerRate && providerRate.transaction.block.timestamp) {
|
|
234
|
-
const tsmp = BigInt(providerRate.transaction.block.timestamp)
|
|
235
|
-
if (tsmp > latestTimestamp) {
|
|
236
|
-
latestRate = providerRate
|
|
237
|
-
latestTimestamp = tsmp
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// Check all other providers
|
|
243
|
-
for (const [providerKey, currencyMap] of providerRates.entries()) {
|
|
244
|
-
if (providerKey === preferredProvider) continue // Already checked
|
|
245
|
-
|
|
246
|
-
const rate = currencyMap.get(currency)
|
|
247
|
-
if (rate && rate.transaction.block.timestamp) {
|
|
248
|
-
const tsmp = BigInt(rate.transaction.block.timestamp)
|
|
249
|
-
if (tsmp > latestTimestamp) {
|
|
250
|
-
latestRate = rate
|
|
251
|
-
latestTimestamp = tsmp
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
return latestRate
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* Get the primary conversion rate from processed provider rates
|
|
261
|
-
* Returns the first available rate, prioritizing by provider order
|
|
262
|
-
*/
|
|
263
|
-
export function getPrimaryConversionRate(providerRates: ProviderRates<ConversionRateInfo>): ConversionRateInfo | null {
|
|
264
|
-
for (const [, currencyMap] of providerRates) {
|
|
265
|
-
const usd = currencyMap.get(SUPPORTED_CURRENCIES.USD.contractId)
|
|
266
|
-
if (usd) {
|
|
267
|
-
return usd
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
for (const [, currencyMap] of providerRates) {
|
|
271
|
-
for (const [, rate] of currencyMap.entries()) {
|
|
272
|
-
return rate
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
return null
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Get primary conversion rate for a specific currency across all providers
|
|
280
|
-
*/
|
|
281
|
-
export function getPrimaryRateForCurrency(
|
|
282
|
-
providerRates: ProviderRates<ConversionRateInfo>,
|
|
283
|
-
currency: Hex
|
|
284
|
-
): ConversionRateInfo | null {
|
|
285
|
-
for (const [, currencyMap] of providerRates) {
|
|
286
|
-
const rates = currencyMap.get(currency)
|
|
287
|
-
if (rates) {
|
|
288
|
-
return rates
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
return null
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* Get the primary verifier (payment provider) from conversion rates
|
|
296
|
-
*/
|
|
297
|
-
export function getPrimaryVerifier(providerRates: ProviderRates<ConversionRateInfo>): string | null {
|
|
298
|
-
const primaryRate = getPrimaryConversionRate(providerRates)
|
|
299
|
-
return primaryRate?.verifier || null
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* Get the primary currency from conversion rates
|
|
304
|
-
*/
|
|
305
|
-
export function getPrimaryCurrency(providerRates: ProviderRates<ConversionRateInfo>): string | null {
|
|
306
|
-
const primaryRate = getPrimaryConversionRate(providerRates)
|
|
307
|
-
return primaryRate?.currency || null
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* Convert token input to currency
|
|
312
|
-
*/
|
|
313
|
-
export const convertTokenInputToCurrency = ({ token, amountOutInt, rate }: {
|
|
314
|
-
token: TokenInfo
|
|
315
|
-
amountOutInt: bigint
|
|
316
|
-
rate: bigint
|
|
317
|
-
}): bigint => {
|
|
318
|
-
const oneCent = 10n ** BigInt(token.decimals - 2)
|
|
319
|
-
const oneEther = 10n ** 18n
|
|
320
|
-
const amountInInt = amountOutInt * BigInt(rate) / oneEther
|
|
321
|
-
const remainder = amountInInt % oneCent
|
|
322
|
-
const negativeSpace = oneCent - remainder
|
|
323
|
-
const finalAmountInInt = amountInInt + (negativeSpace === oneCent ? 0n : negativeSpace)
|
|
324
|
-
return finalAmountInInt
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* Convert currency amount to token amount out (inverse of convertTokenInputToCurrency)
|
|
329
|
-
* This accounts for the cent-rounding applied in the original function
|
|
330
|
-
*/
|
|
331
|
-
export const convertCurrencyToTokenOutput = ({
|
|
332
|
-
token,
|
|
333
|
-
currency,
|
|
334
|
-
currencyAmountInt,
|
|
335
|
-
rate,
|
|
336
|
-
}: {
|
|
337
|
-
token: TokenInfo
|
|
338
|
-
currency: Currency
|
|
339
|
-
currencyAmountInt: bigint
|
|
340
|
-
rate: bigint
|
|
341
|
-
}): bigint => {
|
|
342
|
-
const smallestUnit = 10n ** BigInt(token.decimals - currency.decimals)
|
|
343
|
-
const oneEther = 10n ** 18n
|
|
344
|
-
|
|
345
|
-
// First, we need to find the original amount before cent-rounding was applied
|
|
346
|
-
// The original function rounds up to the nearest cent, so we need to find
|
|
347
|
-
// the maximum token amount that would result in this currency amount after rounding
|
|
348
|
-
|
|
349
|
-
// Start with the direct inverse calculation
|
|
350
|
-
// console.log('currencyAmountInt', currencyAmountInt, smallestUnit, converter, rate)
|
|
351
|
-
let tokenAmountOut = (currencyAmountInt * smallestUnit * oneEther) / rate
|
|
352
|
-
|
|
353
|
-
const remainder = tokenAmountOut % smallestUnit
|
|
354
|
-
const finalTokenAmountOut = (tokenAmountOut - remainder)
|
|
355
|
-
return finalTokenAmountOut
|
|
356
|
-
}
|