@sip-protocol/sdk 0.7.2 → 0.7.4
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/LICENSE +21 -0
- package/README.md +267 -0
- package/dist/{TransportWebUSB-TQ7WZ4LE.mjs → TransportWebUSB-YQMAGJAJ.mjs} +12 -9
- package/dist/browser.d.mts +10 -4
- package/dist/browser.d.ts +10 -4
- package/dist/browser.js +48874 -18336
- package/dist/browser.mjs +674 -48
- package/dist/chunk-4GRJ5MAW.mjs +152 -0
- package/dist/chunk-5D7A3L3W.mjs +717 -0
- package/dist/chunk-64AYA5F5.mjs +7834 -0
- package/dist/chunk-GMDGB22A.mjs +379 -0
- package/dist/chunk-I534WKN7.mjs +328 -0
- package/dist/chunk-IBZVA5Y7.mjs +1003 -0
- package/dist/chunk-PRRZAWJE.mjs +223 -0
- package/dist/{chunk-UJCSKKID.mjs → chunk-XGB3TDIC.mjs} +13 -1
- package/dist/chunk-YWGJ77A2.mjs +33806 -0
- package/dist/{chunk-6WGN57S2.mjs → chunk-Z3K7W5S3.mjs} +48 -0
- package/dist/constants-LHAAUC2T.mjs +51 -0
- package/dist/dist-2OGQ7FED.mjs +3957 -0
- package/dist/dist-IFHPYLDX.mjs +254 -0
- package/dist/fulfillment_proof-ANHVPKTB.mjs +21 -0
- package/dist/funding_proof-ICFZ5LHY.mjs +21 -0
- package/dist/index-DXh2IGkz.d.ts +24681 -0
- package/dist/index-DeE1ZzA4.d.mts +24681 -0
- package/dist/index.d.mts +9 -3
- package/dist/index.d.ts +9 -3
- package/dist/index.js +48676 -17318
- package/dist/index.mjs +583 -19
- package/dist/interface-Bf7w1PLW.d.mts +679 -0
- package/dist/interface-Bf7w1PLW.d.ts +679 -0
- package/dist/{noir-DKfEzWy9.d.mts → noir-kzbLVTei.d.mts} +31 -21
- package/dist/{noir-DKfEzWy9.d.ts → noir-kzbLVTei.d.ts} +31 -21
- package/dist/proofs/halo2.d.mts +151 -0
- package/dist/proofs/halo2.d.ts +151 -0
- package/dist/proofs/halo2.js +350 -0
- package/dist/proofs/halo2.mjs +11 -0
- package/dist/proofs/kimchi.d.mts +160 -0
- package/dist/proofs/kimchi.d.ts +160 -0
- package/dist/proofs/kimchi.js +431 -0
- package/dist/proofs/kimchi.mjs +13 -0
- package/dist/proofs/noir.d.mts +1 -1
- package/dist/proofs/noir.d.ts +1 -1
- package/dist/proofs/noir.js +74 -18
- package/dist/proofs/noir.mjs +84 -24
- package/dist/solana-U3MEGU7W.mjs +280 -0
- package/dist/validity_proof-3POXLPNY.mjs +21 -0
- package/package.json +54 -21
- package/src/adapters/index.ts +41 -0
- package/src/adapters/jupiter.ts +571 -0
- package/src/adapters/near-intents.ts +135 -0
- package/src/advisor/advisor.ts +653 -0
- package/src/advisor/index.ts +54 -0
- package/src/advisor/tools.ts +303 -0
- package/src/advisor/types.ts +164 -0
- package/src/chains/ethereum/announcement.ts +536 -0
- package/src/chains/ethereum/bnb-optimizations.ts +474 -0
- package/src/chains/ethereum/commitment.ts +522 -0
- package/src/chains/ethereum/constants.ts +462 -0
- package/src/chains/ethereum/deployment.ts +596 -0
- package/src/chains/ethereum/gas-estimation.ts +538 -0
- package/src/chains/ethereum/index.ts +268 -0
- package/src/chains/ethereum/optimizations.ts +614 -0
- package/src/chains/ethereum/privacy-adapter.ts +855 -0
- package/src/chains/ethereum/registry.ts +584 -0
- package/src/chains/ethereum/rpc.ts +905 -0
- package/src/chains/ethereum/stealth.ts +491 -0
- package/src/chains/ethereum/token.ts +790 -0
- package/src/chains/ethereum/transfer.ts +637 -0
- package/src/chains/ethereum/types.ts +456 -0
- package/src/chains/ethereum/viewing-key.ts +455 -0
- package/src/chains/near/commitment.ts +608 -0
- package/src/chains/near/constants.ts +284 -0
- package/src/chains/near/function-call.ts +871 -0
- package/src/chains/near/history.ts +654 -0
- package/src/chains/near/implicit-account.ts +840 -0
- package/src/chains/near/index.ts +393 -0
- package/src/chains/near/native-transfer.ts +658 -0
- package/src/chains/near/nep141.ts +775 -0
- package/src/chains/near/privacy-adapter.ts +889 -0
- package/src/chains/near/resolver.ts +971 -0
- package/src/chains/near/rpc.ts +1016 -0
- package/src/chains/near/stealth.ts +419 -0
- package/src/chains/near/types.ts +317 -0
- package/src/chains/near/viewing-key.ts +876 -0
- package/src/chains/solana/anchor-transfer.ts +386 -0
- package/src/chains/solana/commitment.ts +577 -0
- package/src/chains/solana/constants.ts +126 -12
- package/src/chains/solana/ephemeral-keys.ts +543 -0
- package/src/chains/solana/index.ts +276 -1
- package/src/chains/solana/key-derivation.ts +418 -0
- package/src/chains/solana/kit-compat.ts +334 -0
- package/src/chains/solana/optimizations.ts +560 -0
- package/src/chains/solana/privacy-adapter.ts +605 -0
- package/src/chains/solana/providers/generic.ts +201 -0
- package/src/chains/solana/providers/helius-enhanced-types.ts +336 -0
- package/src/chains/solana/providers/helius-enhanced.ts +623 -0
- package/src/chains/solana/providers/helius.ts +402 -0
- package/src/chains/solana/providers/index.ts +85 -0
- package/src/chains/solana/providers/interface.ts +221 -0
- package/src/chains/solana/providers/quicknode.ts +409 -0
- package/src/chains/solana/providers/triton.ts +426 -0
- package/src/chains/solana/providers/webhook.ts +790 -0
- package/src/chains/solana/rpc-client.ts +1150 -0
- package/src/chains/solana/scan.ts +170 -73
- package/src/chains/solana/sol-transfer.ts +732 -0
- package/src/chains/solana/spl-transfer.ts +886 -0
- package/src/chains/solana/stealth-scanner.ts +703 -0
- package/src/chains/solana/sunspot-verifier.ts +453 -0
- package/src/chains/solana/transaction-builder.ts +755 -0
- package/src/chains/solana/transfer.ts +74 -5
- package/src/chains/solana/types.ts +77 -7
- package/src/chains/solana/utils.ts +110 -0
- package/src/chains/solana/viewing-key.ts +807 -0
- package/src/compliance/fireblocks.ts +921 -0
- package/src/compliance/index.ts +37 -0
- package/src/compliance/range-sas.ts +956 -0
- package/src/config/endpoints.ts +100 -0
- package/src/crypto.ts +11 -8
- package/src/errors.ts +82 -0
- package/src/evm/erc4337-relayer.ts +830 -0
- package/src/evm/index.ts +47 -0
- package/src/fees/calculator.ts +396 -0
- package/src/fees/index.ts +87 -0
- package/src/fees/near-contract.ts +429 -0
- package/src/fees/types.ts +268 -0
- package/src/index.ts +785 -1
- package/src/intent.ts +6 -3
- package/src/logger.ts +324 -0
- package/src/network/index.ts +80 -0
- package/src/network/proxy.ts +691 -0
- package/src/optimizations/index.ts +541 -0
- package/src/oracle/types.ts +1 -0
- package/src/privacy-backends/arcium-types.ts +727 -0
- package/src/privacy-backends/arcium.ts +719 -0
- package/src/privacy-backends/combined-privacy.ts +866 -0
- package/src/privacy-backends/cspl-token.ts +595 -0
- package/src/privacy-backends/cspl-types.ts +512 -0
- package/src/privacy-backends/cspl.ts +907 -0
- package/src/privacy-backends/health.ts +488 -0
- package/src/privacy-backends/inco-types.ts +323 -0
- package/src/privacy-backends/inco.ts +616 -0
- package/src/privacy-backends/index.ts +336 -0
- package/src/privacy-backends/interface.ts +906 -0
- package/src/privacy-backends/lru-cache.ts +343 -0
- package/src/privacy-backends/magicblock.ts +458 -0
- package/src/privacy-backends/mock.ts +258 -0
- package/src/privacy-backends/privacycash-types.ts +278 -0
- package/src/privacy-backends/privacycash.ts +456 -0
- package/src/privacy-backends/private-swap.ts +570 -0
- package/src/privacy-backends/rate-limiter.ts +683 -0
- package/src/privacy-backends/registry.ts +690 -0
- package/src/privacy-backends/router.ts +626 -0
- package/src/privacy-backends/shadowwire.ts +449 -0
- package/src/privacy-backends/sip-native.ts +256 -0
- package/src/privacy-logger.ts +191 -0
- package/src/production-safety.ts +373 -0
- package/src/proofs/aggregator.ts +1029 -0
- package/src/proofs/browser-composer.ts +1150 -0
- package/src/proofs/browser.ts +113 -25
- package/src/proofs/cache/index.ts +127 -0
- package/src/proofs/cache/interface.ts +545 -0
- package/src/proofs/cache/key-generator.ts +188 -0
- package/src/proofs/cache/lru-cache.ts +481 -0
- package/src/proofs/cache/multi-tier-cache.ts +575 -0
- package/src/proofs/cache/persistent-cache.ts +788 -0
- package/src/proofs/compliance-proof.ts +872 -0
- package/src/proofs/composer/base.ts +923 -0
- package/src/proofs/composer/index.ts +25 -0
- package/src/proofs/composer/interface.ts +518 -0
- package/src/proofs/composer/types.ts +383 -0
- package/src/proofs/converters/halo2.ts +452 -0
- package/src/proofs/converters/index.ts +208 -0
- package/src/proofs/converters/interface.ts +363 -0
- package/src/proofs/converters/kimchi.ts +462 -0
- package/src/proofs/converters/noir.ts +451 -0
- package/src/proofs/fallback.ts +888 -0
- package/src/proofs/halo2.ts +42 -0
- package/src/proofs/index.ts +471 -0
- package/src/proofs/interface.ts +13 -0
- package/src/proofs/kimchi.ts +42 -0
- package/src/proofs/lazy.ts +1004 -0
- package/src/proofs/mock.ts +25 -1
- package/src/proofs/noir.ts +111 -30
- package/src/proofs/orchestrator.ts +960 -0
- package/src/proofs/parallel/concurrency.ts +297 -0
- package/src/proofs/parallel/dependency-graph.ts +602 -0
- package/src/proofs/parallel/executor.ts +420 -0
- package/src/proofs/parallel/index.ts +131 -0
- package/src/proofs/parallel/interface.ts +685 -0
- package/src/proofs/parallel/worker-pool.ts +644 -0
- package/src/proofs/providers/halo2.ts +560 -0
- package/src/proofs/providers/index.ts +34 -0
- package/src/proofs/providers/kimchi.ts +641 -0
- package/src/proofs/validator.ts +881 -0
- package/src/proofs/verifier.ts +867 -0
- package/src/quantum/index.ts +112 -0
- package/src/quantum/winternitz-vault.ts +639 -0
- package/src/quantum/wots.ts +611 -0
- package/src/settlement/backends/direct-chain.ts +1 -0
- package/src/settlement/index.ts +9 -0
- package/src/settlement/router.ts +732 -46
- package/src/solana/index.ts +72 -0
- package/src/solana/jito-relayer.ts +687 -0
- package/src/solana/noir-verifier-types.ts +430 -0
- package/src/solana/noir-verifier.ts +816 -0
- package/src/stealth/address-derivation.ts +193 -0
- package/src/stealth/ed25519.ts +431 -0
- package/src/stealth/index.ts +233 -0
- package/src/stealth/meta-address.ts +221 -0
- package/src/stealth/secp256k1.ts +368 -0
- package/src/stealth/utils.ts +194 -0
- package/src/stealth.ts +50 -1504
- package/src/surveillance/algorithms/address-reuse.ts +143 -0
- package/src/surveillance/algorithms/cluster.ts +247 -0
- package/src/surveillance/algorithms/exchange.ts +295 -0
- package/src/surveillance/algorithms/temporal.ts +337 -0
- package/src/surveillance/analyzer.ts +442 -0
- package/src/surveillance/index.ts +64 -0
- package/src/surveillance/scoring.ts +372 -0
- package/src/surveillance/types.ts +264 -0
- package/src/sync/index.ts +106 -0
- package/src/sync/manager.ts +504 -0
- package/src/sync/mock-provider.ts +318 -0
- package/src/sync/oblivious.ts +625 -0
- package/src/tokens/index.ts +15 -0
- package/src/tokens/registry.ts +301 -0
- package/src/utils/deprecation.ts +94 -0
- package/src/utils/index.ts +9 -0
- package/src/wallet/ethereum/index.ts +68 -0
- package/src/wallet/ethereum/metamask-privacy.ts +420 -0
- package/src/wallet/ethereum/multi-wallet.ts +646 -0
- package/src/wallet/ethereum/privacy-adapter.ts +700 -0
- package/src/wallet/ethereum/types.ts +3 -1
- package/src/wallet/ethereum/walletconnect-adapter.ts +675 -0
- package/src/wallet/hardware/index.ts +10 -0
- package/src/wallet/hardware/ledger-privacy.ts +414 -0
- package/src/wallet/index.ts +71 -0
- package/src/wallet/near/adapter.ts +626 -0
- package/src/wallet/near/index.ts +86 -0
- package/src/wallet/near/meteor-wallet.ts +1153 -0
- package/src/wallet/near/my-near-wallet.ts +790 -0
- package/src/wallet/near/wallet-selector.ts +702 -0
- package/src/wallet/solana/adapter.ts +6 -4
- package/src/wallet/solana/index.ts +13 -0
- package/src/wallet/solana/privacy-adapter.ts +567 -0
- package/src/wallet/sui/types.ts +6 -4
- package/src/zcash/rpc-client.ts +13 -6
- package/dist/chunk-3INS3PR5.mjs +0 -884
- package/dist/chunk-3OVABDRH.mjs +0 -17096
- package/dist/chunk-DLDWZFYC.mjs +0 -1495
- package/dist/chunk-E6SZWREQ.mjs +0 -57
- package/dist/chunk-G33LB27A.mjs +0 -16166
- package/dist/chunk-HGU6HZRC.mjs +0 -231
- package/dist/chunk-L2K34JCU.mjs +0 -1496
- package/dist/chunk-SN4ZDTVW.mjs +0 -16166
- package/dist/constants-VOI7BSLK.mjs +0 -27
- package/dist/index-BYZbDjal.d.ts +0 -11390
- package/dist/index-CHB3KuOB.d.mts +0 -11859
- package/dist/index-CzWPI6Le.d.ts +0 -11859
- package/dist/index-xbWjohNq.d.mts +0 -11390
- package/dist/solana-5EMCTPTS.mjs +0 -46
- package/dist/solana-Q4NAVBTS.mjs +0 -46
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chain-Specific Optimizations - Auto-Selection Module
|
|
3
|
+
*
|
|
4
|
+
* Provides intelligent chain detection and optimization selection:
|
|
5
|
+
* - Detects chain from context (chain ID, RPC, etc.)
|
|
6
|
+
* - Selects optimal configuration automatically
|
|
7
|
+
* - Unifies optimization APIs across chains
|
|
8
|
+
*
|
|
9
|
+
* @module optimizations
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { ChainId } from '@sip-protocol/types'
|
|
13
|
+
|
|
14
|
+
// Re-export chain-specific modules with namespaces to avoid conflicts
|
|
15
|
+
export * as solanaOptimizations from '../chains/solana/optimizations'
|
|
16
|
+
export * as evmOptimizations from '../chains/ethereum/optimizations'
|
|
17
|
+
export * as bnbOptimizations from '../chains/ethereum/bnb-optimizations'
|
|
18
|
+
|
|
19
|
+
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Chain family for optimization selection
|
|
23
|
+
*/
|
|
24
|
+
export type ChainFamily = 'solana' | 'evm' | 'near' | 'bitcoin' | 'cosmos'
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Unified optimization profile (cross-chain)
|
|
28
|
+
*/
|
|
29
|
+
export type UnifiedOptimizationProfile =
|
|
30
|
+
| 'economy'
|
|
31
|
+
| 'standard'
|
|
32
|
+
| 'fast'
|
|
33
|
+
| 'urgent'
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Chain characteristics for optimization decisions
|
|
37
|
+
*/
|
|
38
|
+
export interface ChainCharacteristics {
|
|
39
|
+
/** Chain family */
|
|
40
|
+
family: ChainFamily
|
|
41
|
+
/** Average block time in seconds */
|
|
42
|
+
blockTime: number
|
|
43
|
+
/** Whether chain has EIP-1559 style gas */
|
|
44
|
+
hasEIP1559: boolean
|
|
45
|
+
/** Whether chain is L2/rollup */
|
|
46
|
+
isL2: boolean
|
|
47
|
+
/** Relative cost tier (1=cheapest, 5=most expensive) */
|
|
48
|
+
costTier: 1 | 2 | 3 | 4 | 5
|
|
49
|
+
/** Whether chain supports blob transactions */
|
|
50
|
+
supportsBlobs: boolean
|
|
51
|
+
/** Native token symbol */
|
|
52
|
+
nativeToken: string
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Unified transaction optimization result
|
|
57
|
+
*/
|
|
58
|
+
export interface UnifiedOptimizationResult {
|
|
59
|
+
/** Chain being optimized for */
|
|
60
|
+
chain: ChainId
|
|
61
|
+
/** Chain family */
|
|
62
|
+
family: ChainFamily
|
|
63
|
+
/** Recommended fee configuration */
|
|
64
|
+
fees: {
|
|
65
|
+
/** For Solana: microlamports per CU, For EVM: wei */
|
|
66
|
+
priorityFee: bigint
|
|
67
|
+
/** For Solana: total lamports, For EVM: max fee per gas (wei) */
|
|
68
|
+
maxFee: bigint
|
|
69
|
+
}
|
|
70
|
+
/** Resource limits */
|
|
71
|
+
limits: {
|
|
72
|
+
/** For Solana: compute units, For EVM: gas limit */
|
|
73
|
+
computeLimit: bigint
|
|
74
|
+
}
|
|
75
|
+
/** Cross-chain recommendations */
|
|
76
|
+
recommendations: string[]
|
|
77
|
+
/** Chain-specific data */
|
|
78
|
+
chainSpecific: Record<string, unknown>
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ─── Chain Detection ──────────────────────────────────────────────────────────
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Chain ID to family mapping
|
|
85
|
+
*/
|
|
86
|
+
const CHAIN_FAMILIES: Record<string, ChainFamily> = {
|
|
87
|
+
// Solana
|
|
88
|
+
solana: 'solana',
|
|
89
|
+
'solana-mainnet': 'solana',
|
|
90
|
+
'solana-devnet': 'solana',
|
|
91
|
+
'solana-testnet': 'solana',
|
|
92
|
+
|
|
93
|
+
// EVM
|
|
94
|
+
ethereum: 'evm',
|
|
95
|
+
arbitrum: 'evm',
|
|
96
|
+
optimism: 'evm',
|
|
97
|
+
base: 'evm',
|
|
98
|
+
polygon: 'evm',
|
|
99
|
+
bsc: 'evm',
|
|
100
|
+
zksync: 'evm',
|
|
101
|
+
scroll: 'evm',
|
|
102
|
+
linea: 'evm',
|
|
103
|
+
mantle: 'evm',
|
|
104
|
+
blast: 'evm',
|
|
105
|
+
|
|
106
|
+
// NEAR
|
|
107
|
+
near: 'near',
|
|
108
|
+
'near-mainnet': 'near',
|
|
109
|
+
'near-testnet': 'near',
|
|
110
|
+
|
|
111
|
+
// Bitcoin
|
|
112
|
+
bitcoin: 'bitcoin',
|
|
113
|
+
'bitcoin-mainnet': 'bitcoin',
|
|
114
|
+
'bitcoin-testnet': 'bitcoin',
|
|
115
|
+
|
|
116
|
+
// Cosmos
|
|
117
|
+
cosmos: 'cosmos',
|
|
118
|
+
osmosis: 'cosmos',
|
|
119
|
+
celestia: 'cosmos',
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Chain characteristics database
|
|
124
|
+
*/
|
|
125
|
+
const CHAIN_CHARACTERISTICS: Record<string, ChainCharacteristics> = {
|
|
126
|
+
solana: {
|
|
127
|
+
family: 'solana',
|
|
128
|
+
blockTime: 0.4,
|
|
129
|
+
hasEIP1559: false,
|
|
130
|
+
isL2: false,
|
|
131
|
+
costTier: 1,
|
|
132
|
+
supportsBlobs: false,
|
|
133
|
+
nativeToken: 'SOL',
|
|
134
|
+
},
|
|
135
|
+
ethereum: {
|
|
136
|
+
family: 'evm',
|
|
137
|
+
blockTime: 12,
|
|
138
|
+
hasEIP1559: true,
|
|
139
|
+
isL2: false,
|
|
140
|
+
costTier: 5,
|
|
141
|
+
supportsBlobs: true,
|
|
142
|
+
nativeToken: 'ETH',
|
|
143
|
+
},
|
|
144
|
+
arbitrum: {
|
|
145
|
+
family: 'evm',
|
|
146
|
+
blockTime: 0.25,
|
|
147
|
+
hasEIP1559: true,
|
|
148
|
+
isL2: true,
|
|
149
|
+
costTier: 2,
|
|
150
|
+
supportsBlobs: true,
|
|
151
|
+
nativeToken: 'ETH',
|
|
152
|
+
},
|
|
153
|
+
optimism: {
|
|
154
|
+
family: 'evm',
|
|
155
|
+
blockTime: 2,
|
|
156
|
+
hasEIP1559: true,
|
|
157
|
+
isL2: true,
|
|
158
|
+
costTier: 2,
|
|
159
|
+
supportsBlobs: true,
|
|
160
|
+
nativeToken: 'ETH',
|
|
161
|
+
},
|
|
162
|
+
base: {
|
|
163
|
+
family: 'evm',
|
|
164
|
+
blockTime: 2,
|
|
165
|
+
hasEIP1559: true,
|
|
166
|
+
isL2: true,
|
|
167
|
+
costTier: 2,
|
|
168
|
+
supportsBlobs: true,
|
|
169
|
+
nativeToken: 'ETH',
|
|
170
|
+
},
|
|
171
|
+
polygon: {
|
|
172
|
+
family: 'evm',
|
|
173
|
+
blockTime: 2,
|
|
174
|
+
hasEIP1559: true,
|
|
175
|
+
isL2: true,
|
|
176
|
+
costTier: 2,
|
|
177
|
+
supportsBlobs: false,
|
|
178
|
+
nativeToken: 'MATIC',
|
|
179
|
+
},
|
|
180
|
+
bsc: {
|
|
181
|
+
family: 'evm',
|
|
182
|
+
blockTime: 3,
|
|
183
|
+
hasEIP1559: false,
|
|
184
|
+
isL2: false,
|
|
185
|
+
costTier: 1,
|
|
186
|
+
supportsBlobs: false,
|
|
187
|
+
nativeToken: 'BNB',
|
|
188
|
+
},
|
|
189
|
+
near: {
|
|
190
|
+
family: 'near',
|
|
191
|
+
blockTime: 1,
|
|
192
|
+
hasEIP1559: false,
|
|
193
|
+
isL2: false,
|
|
194
|
+
costTier: 1,
|
|
195
|
+
supportsBlobs: false,
|
|
196
|
+
nativeToken: 'NEAR',
|
|
197
|
+
},
|
|
198
|
+
bitcoin: {
|
|
199
|
+
family: 'bitcoin',
|
|
200
|
+
blockTime: 600,
|
|
201
|
+
hasEIP1559: false,
|
|
202
|
+
isL2: false,
|
|
203
|
+
costTier: 4,
|
|
204
|
+
supportsBlobs: false,
|
|
205
|
+
nativeToken: 'BTC',
|
|
206
|
+
},
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Detect chain family from chain ID
|
|
211
|
+
*/
|
|
212
|
+
export function detectChainFamily(chainId: ChainId): ChainFamily {
|
|
213
|
+
const normalized = chainId.toLowerCase().replace(/[-_]/g, '-')
|
|
214
|
+
return CHAIN_FAMILIES[normalized] ?? 'evm' // Default to EVM
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Get chain characteristics
|
|
219
|
+
*/
|
|
220
|
+
export function getChainCharacteristics(chainId: ChainId): ChainCharacteristics {
|
|
221
|
+
const normalized = chainId.toLowerCase().replace(/[-_]/g, '-')
|
|
222
|
+
const base = normalized.split('-')[0]
|
|
223
|
+
|
|
224
|
+
return (
|
|
225
|
+
CHAIN_CHARACTERISTICS[normalized] ??
|
|
226
|
+
CHAIN_CHARACTERISTICS[base] ?? {
|
|
227
|
+
family: detectChainFamily(chainId),
|
|
228
|
+
blockTime: 12,
|
|
229
|
+
hasEIP1559: true,
|
|
230
|
+
isL2: false,
|
|
231
|
+
costTier: 3,
|
|
232
|
+
supportsBlobs: false,
|
|
233
|
+
nativeToken: 'ETH',
|
|
234
|
+
}
|
|
235
|
+
)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// ─── Auto-Selection Logic ─────────────────────────────────────────────────────
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Select optimal configuration based on chain
|
|
242
|
+
*
|
|
243
|
+
* @param chainId - Target chain identifier
|
|
244
|
+
* @param profile - Optimization profile
|
|
245
|
+
* @param options - Additional options
|
|
246
|
+
* @returns Unified optimization result
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```typescript
|
|
250
|
+
* // Automatically select optimal settings for Solana
|
|
251
|
+
* const config = selectOptimalConfig('solana', 'standard')
|
|
252
|
+
*
|
|
253
|
+
* // For EVM chain
|
|
254
|
+
* const ethConfig = selectOptimalConfig('ethereum', 'fast', {
|
|
255
|
+
* baseFee: 30_000_000_000n // 30 gwei
|
|
256
|
+
* })
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
export function selectOptimalConfig(
|
|
260
|
+
chainId: ChainId,
|
|
261
|
+
profile: UnifiedOptimizationProfile = 'standard',
|
|
262
|
+
options: {
|
|
263
|
+
/** Current base fee (for EVM chains) */
|
|
264
|
+
baseFee?: bigint
|
|
265
|
+
/** Current priority fee percentiles (for Solana) */
|
|
266
|
+
priorityFeePercentiles?: { p50: number; p75: number; p90: number }
|
|
267
|
+
/** Transaction complexity hint */
|
|
268
|
+
complexityHint?: 'simple' | 'medium' | 'complex'
|
|
269
|
+
} = {}
|
|
270
|
+
): UnifiedOptimizationResult {
|
|
271
|
+
const characteristics = getChainCharacteristics(chainId)
|
|
272
|
+
const recommendations: string[] = []
|
|
273
|
+
|
|
274
|
+
let priorityFee: bigint
|
|
275
|
+
let maxFee: bigint
|
|
276
|
+
let computeLimit: bigint
|
|
277
|
+
const chainSpecific: Record<string, unknown> = {}
|
|
278
|
+
|
|
279
|
+
switch (characteristics.family) {
|
|
280
|
+
case 'solana': {
|
|
281
|
+
// Solana optimization
|
|
282
|
+
const cuMultipliers = { simple: 100000, medium: 200000, complex: 400000 }
|
|
283
|
+
const baseLimit = cuMultipliers[options.complexityHint ?? 'medium']
|
|
284
|
+
|
|
285
|
+
// Profile-based priority fee (microlamports per CU)
|
|
286
|
+
const profileFees = { economy: 500, standard: 1000, fast: 5000, urgent: 20000 }
|
|
287
|
+
const basePriorityFee = options.priorityFeePercentiles?.p50 ?? profileFees[profile]
|
|
288
|
+
|
|
289
|
+
priorityFee = BigInt(basePriorityFee)
|
|
290
|
+
computeLimit = BigInt(baseLimit)
|
|
291
|
+
maxFee = (computeLimit * priorityFee) / 1_000_000n // Convert to lamports
|
|
292
|
+
|
|
293
|
+
chainSpecific.useVersionedTx = options.complexityHint === 'complex'
|
|
294
|
+
chainSpecific.useALT = options.complexityHint === 'complex'
|
|
295
|
+
|
|
296
|
+
recommendations.push('Solana: Use versioned transactions for complex operations')
|
|
297
|
+
if (characteristics.costTier === 1) {
|
|
298
|
+
recommendations.push('Solana: Very low cost - prioritize speed over savings')
|
|
299
|
+
}
|
|
300
|
+
break
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
case 'evm': {
|
|
304
|
+
// EVM optimization
|
|
305
|
+
const baseFee = options.baseFee ?? 30_000_000_000n // 30 gwei default
|
|
306
|
+
|
|
307
|
+
// Profile-based priority fee (wei)
|
|
308
|
+
const profilePriorityFees = {
|
|
309
|
+
economy: 1_000_000_000n, // 1 gwei
|
|
310
|
+
standard: 2_000_000_000n, // 2 gwei
|
|
311
|
+
fast: 5_000_000_000n, // 5 gwei
|
|
312
|
+
urgent: 10_000_000_000n, // 10 gwei
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
priorityFee = profilePriorityFees[profile]
|
|
316
|
+
maxFee = baseFee * 2n + priorityFee
|
|
317
|
+
|
|
318
|
+
// Gas limit based on complexity
|
|
319
|
+
const gasMultipliers = { simple: 50000n, medium: 150000n, complex: 500000n }
|
|
320
|
+
computeLimit = gasMultipliers[options.complexityHint ?? 'medium']
|
|
321
|
+
|
|
322
|
+
if (characteristics.isL2) {
|
|
323
|
+
recommendations.push('L2: Lower fees, optimize calldata for L1 data costs')
|
|
324
|
+
chainSpecific.isL2 = true
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (characteristics.supportsBlobs) {
|
|
328
|
+
recommendations.push('EIP-4844: Use blobs for large data payloads')
|
|
329
|
+
chainSpecific.supportsBlobs = true
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (chainId.toLowerCase() === 'bsc') {
|
|
333
|
+
// BSC-specific: much lower base fee
|
|
334
|
+
maxFee = 5_000_000_000n // 5 gwei typical for BSC
|
|
335
|
+
priorityFee = 1_000_000_000n
|
|
336
|
+
recommendations.push('BSC: Very low gas costs - use standard profile')
|
|
337
|
+
}
|
|
338
|
+
break
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
case 'near': {
|
|
342
|
+
// NEAR uses storage staking, not gas in the same way
|
|
343
|
+
const gasMultipliers = { simple: 5n, medium: 30n, complex: 100n }
|
|
344
|
+
const baseGas = gasMultipliers[options.complexityHint ?? 'medium']
|
|
345
|
+
|
|
346
|
+
computeLimit = baseGas * 10n ** 12n // TGas
|
|
347
|
+
priorityFee = 0n // NEAR doesn't have priority fees
|
|
348
|
+
maxFee = computeLimit // Gas attached
|
|
349
|
+
|
|
350
|
+
chainSpecific.storageDeposit = true
|
|
351
|
+
recommendations.push('NEAR: Attach storage deposit for new accounts/data')
|
|
352
|
+
break
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
case 'bitcoin': {
|
|
356
|
+
// Bitcoin uses sat/vB
|
|
357
|
+
const profileFees = { economy: 5n, standard: 20n, fast: 50n, urgent: 100n }
|
|
358
|
+
priorityFee = profileFees[profile] // sat/vB
|
|
359
|
+
|
|
360
|
+
// Typical privacy tx is ~300 vBytes
|
|
361
|
+
const vSize = options.complexityHint === 'complex' ? 500n : 300n
|
|
362
|
+
computeLimit = vSize
|
|
363
|
+
maxFee = priorityFee * vSize
|
|
364
|
+
|
|
365
|
+
recommendations.push('Bitcoin: Use batching for multiple outputs')
|
|
366
|
+
recommendations.push('Bitcoin: Wait for low fee periods for economy')
|
|
367
|
+
break
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
default: {
|
|
371
|
+
// Fallback to EVM-like
|
|
372
|
+
priorityFee = 2_000_000_000n
|
|
373
|
+
maxFee = 100_000_000_000n
|
|
374
|
+
computeLimit = 100000n
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// Cross-chain cost comparison recommendation
|
|
379
|
+
if (characteristics.costTier >= 4) {
|
|
380
|
+
recommendations.push(
|
|
381
|
+
`High cost chain (tier ${characteristics.costTier}) - consider L2 alternatives`
|
|
382
|
+
)
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
return {
|
|
386
|
+
chain: chainId,
|
|
387
|
+
family: characteristics.family,
|
|
388
|
+
fees: {
|
|
389
|
+
priorityFee,
|
|
390
|
+
maxFee,
|
|
391
|
+
},
|
|
392
|
+
limits: {
|
|
393
|
+
computeLimit,
|
|
394
|
+
},
|
|
395
|
+
recommendations,
|
|
396
|
+
chainSpecific,
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// ─── Cost Comparison ──────────────────────────────────────────────────────────
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Compare costs across multiple chains
|
|
404
|
+
*
|
|
405
|
+
* @param chains - Chains to compare
|
|
406
|
+
* @param _complexityHint - Transaction complexity (reserved for future use)
|
|
407
|
+
* @returns Cost comparison sorted by cost (cheapest first)
|
|
408
|
+
*/
|
|
409
|
+
export function compareCrossChainCosts(
|
|
410
|
+
chains: ChainId[],
|
|
411
|
+
_complexityHint: 'simple' | 'medium' | 'complex' = 'medium'
|
|
412
|
+
): Array<{
|
|
413
|
+
chain: ChainId
|
|
414
|
+
costTier: number
|
|
415
|
+
nativeToken: string
|
|
416
|
+
recommendation: string
|
|
417
|
+
}> {
|
|
418
|
+
return chains
|
|
419
|
+
.map((chain) => {
|
|
420
|
+
const chars = getChainCharacteristics(chain)
|
|
421
|
+
let recommendation: string
|
|
422
|
+
|
|
423
|
+
switch (chars.costTier) {
|
|
424
|
+
case 1:
|
|
425
|
+
recommendation = 'Excellent - very low costs'
|
|
426
|
+
break
|
|
427
|
+
case 2:
|
|
428
|
+
recommendation = 'Good - affordable for frequent use'
|
|
429
|
+
break
|
|
430
|
+
case 3:
|
|
431
|
+
recommendation = 'Moderate - suitable for medium value txs'
|
|
432
|
+
break
|
|
433
|
+
case 4:
|
|
434
|
+
recommendation = 'Expensive - use for high value only'
|
|
435
|
+
break
|
|
436
|
+
case 5:
|
|
437
|
+
recommendation = 'Very expensive - consider alternatives'
|
|
438
|
+
break
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
return {
|
|
442
|
+
chain,
|
|
443
|
+
costTier: chars.costTier,
|
|
444
|
+
nativeToken: chars.nativeToken,
|
|
445
|
+
recommendation,
|
|
446
|
+
}
|
|
447
|
+
})
|
|
448
|
+
.sort((a, b) => a.costTier - b.costTier)
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Recommend cheapest chain for operation
|
|
453
|
+
*
|
|
454
|
+
* @param chains - Available chains
|
|
455
|
+
* @param requirements - Operation requirements
|
|
456
|
+
* @returns Recommended chain
|
|
457
|
+
*/
|
|
458
|
+
export function recommendCheapestChain(
|
|
459
|
+
chains: ChainId[],
|
|
460
|
+
requirements: {
|
|
461
|
+
/** Required block time (seconds) */
|
|
462
|
+
maxBlockTime?: number
|
|
463
|
+
/** Requires blob support */
|
|
464
|
+
needsBlobs?: boolean
|
|
465
|
+
/** Preferred chain family */
|
|
466
|
+
preferredFamily?: ChainFamily
|
|
467
|
+
} = {}
|
|
468
|
+
): ChainId | undefined {
|
|
469
|
+
const viable = chains.filter((chain) => {
|
|
470
|
+
const chars = getChainCharacteristics(chain)
|
|
471
|
+
|
|
472
|
+
if (requirements.maxBlockTime && chars.blockTime > requirements.maxBlockTime) {
|
|
473
|
+
return false
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
if (requirements.needsBlobs && !chars.supportsBlobs) {
|
|
477
|
+
return false
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
if (requirements.preferredFamily && chars.family !== requirements.preferredFamily) {
|
|
481
|
+
return false
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
return true
|
|
485
|
+
})
|
|
486
|
+
|
|
487
|
+
if (viable.length === 0) return undefined
|
|
488
|
+
|
|
489
|
+
// Sort by cost tier, then block time
|
|
490
|
+
return viable.sort((a, b) => {
|
|
491
|
+
const charsA = getChainCharacteristics(a)
|
|
492
|
+
const charsB = getChainCharacteristics(b)
|
|
493
|
+
|
|
494
|
+
if (charsA.costTier !== charsB.costTier) {
|
|
495
|
+
return charsA.costTier - charsB.costTier
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
return charsA.blockTime - charsB.blockTime
|
|
499
|
+
})[0]
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// ─── Profile Selection ────────────────────────────────────────────────────────
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Recommend optimization profile based on urgency and value
|
|
506
|
+
*
|
|
507
|
+
* @param options - Profile selection options
|
|
508
|
+
* @returns Recommended profile
|
|
509
|
+
*/
|
|
510
|
+
export function recommendProfile(options: {
|
|
511
|
+
/** Transaction value in USD */
|
|
512
|
+
valueUsd?: number
|
|
513
|
+
/** Urgency level 1-10 */
|
|
514
|
+
urgency?: number
|
|
515
|
+
/** Chain being used */
|
|
516
|
+
chain?: ChainId
|
|
517
|
+
}): UnifiedOptimizationProfile {
|
|
518
|
+
const { valueUsd = 100, urgency = 5, chain } = options
|
|
519
|
+
|
|
520
|
+
// For cheap chains, always use standard
|
|
521
|
+
if (chain) {
|
|
522
|
+
const chars = getChainCharacteristics(chain)
|
|
523
|
+
if (chars.costTier <= 2) {
|
|
524
|
+
return urgency > 7 ? 'fast' : 'standard'
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// Value-based selection
|
|
529
|
+
if (valueUsd > 10000) {
|
|
530
|
+
// High value: prioritize speed
|
|
531
|
+
return urgency > 5 ? 'urgent' : 'fast'
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
if (valueUsd > 1000) {
|
|
535
|
+
// Medium value: balance cost/speed
|
|
536
|
+
return urgency > 7 ? 'fast' : 'standard'
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// Low value: prioritize cost
|
|
540
|
+
return urgency > 8 ? 'standard' : 'economy'
|
|
541
|
+
}
|
package/src/oracle/types.ts
CHANGED