@gibs/bridge-client 1.30.1 → 1.32.0

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.
Files changed (104) hide show
  1. package/coinbase-accept-list/package.json +6 -0
  2. package/dist/cjs/client.d.ts +9 -1
  3. package/dist/cjs/client.js +8 -5
  4. package/dist/cjs/coinbase-accept-list.d.ts +101 -0
  5. package/dist/cjs/coinbase-accept-list.js +97 -0
  6. package/dist/cjs/crossing-decision.d.ts +166 -0
  7. package/dist/cjs/crossing-decision.js +169 -0
  8. package/dist/cjs/crossing-vector-guard.d.ts +45 -0
  9. package/dist/cjs/crossing-vector-guard.js +139 -0
  10. package/dist/cjs/exchange-deposit-board.d.ts +50 -0
  11. package/dist/cjs/exchange-deposit-board.js +70 -0
  12. package/dist/cjs/exchange-deposit-chain.d.ts +146 -0
  13. package/dist/cjs/exchange-deposit-chain.js +228 -0
  14. package/dist/cjs/exchange-deposit-constants.d.ts +10 -0
  15. package/dist/cjs/exchange-deposit-constants.js +6 -0
  16. package/dist/cjs/exchange-deposit-factory-calls.d.ts +245 -0
  17. package/dist/cjs/exchange-deposit-factory-calls.js +103 -0
  18. package/dist/cjs/exchange-deposit-factory.d.ts +87 -0
  19. package/dist/cjs/exchange-deposit-factory.js +110 -0
  20. package/dist/cjs/exchange-deposit-fake.d.ts +67 -0
  21. package/dist/cjs/exchange-deposit-fake.js +179 -0
  22. package/dist/cjs/exchange-deposit-index.d.ts +77 -0
  23. package/dist/cjs/exchange-deposit-index.js +40 -0
  24. package/dist/cjs/exchange-deposit-issuance.d.ts +327 -0
  25. package/dist/cjs/exchange-deposit-issuance.js +244 -0
  26. package/dist/cjs/exchange-deposit-path.d.ts +16 -0
  27. package/dist/cjs/exchange-deposit-path.js +15 -0
  28. package/dist/cjs/exchange-deposit-pow.d.ts +163 -0
  29. package/dist/cjs/exchange-deposit-pow.js +101 -0
  30. package/dist/cjs/exchange-deposit-recipient.d.ts +66 -0
  31. package/dist/cjs/exchange-deposit-recipient.js +35 -0
  32. package/dist/cjs/exchange-deposit-self-cross.d.ts +163 -0
  33. package/dist/cjs/exchange-deposit-self-cross.js +60 -0
  34. package/dist/cjs/exchange-deposit-signin.d.ts +275 -0
  35. package/dist/cjs/exchange-deposit-signin.js +166 -0
  36. package/dist/cjs/exchange-deposit-watch.d.ts +133 -0
  37. package/dist/cjs/exchange-deposit-watch.js +58 -0
  38. package/dist/cjs/exchange-deposit.d.ts +440 -0
  39. package/dist/cjs/exchange-deposit.js +154 -0
  40. package/dist/cjs/exchange-sender-guides.d.ts +29 -0
  41. package/dist/cjs/exchange-sender-guides.js +25 -0
  42. package/dist/cjs/index.d.ts +2 -0
  43. package/dist/cjs/index.js +24 -1
  44. package/dist/cjs/kraken-accept-list.d.ts +70 -0
  45. package/dist/cjs/kraken-accept-list.js +24 -0
  46. package/dist/client.d.ts +9 -1
  47. package/dist/client.js +8 -5
  48. package/dist/coinbase-accept-list.d.ts +101 -0
  49. package/dist/coinbase-accept-list.js +89 -0
  50. package/dist/crossing-decision.d.ts +166 -0
  51. package/dist/crossing-decision.js +162 -0
  52. package/dist/crossing-vector-guard.d.ts +45 -0
  53. package/dist/crossing-vector-guard.js +134 -0
  54. package/dist/exchange-deposit-board.d.ts +50 -0
  55. package/dist/exchange-deposit-board.js +28 -0
  56. package/dist/exchange-deposit-chain.d.ts +146 -0
  57. package/dist/exchange-deposit-chain.js +218 -0
  58. package/dist/exchange-deposit-constants.d.ts +10 -0
  59. package/dist/exchange-deposit-constants.js +3 -0
  60. package/dist/exchange-deposit-factory-calls.d.ts +245 -0
  61. package/dist/exchange-deposit-factory-calls.js +97 -0
  62. package/dist/exchange-deposit-factory.d.ts +87 -0
  63. package/dist/exchange-deposit-factory.js +104 -0
  64. package/dist/exchange-deposit-fake.d.ts +67 -0
  65. package/dist/exchange-deposit-fake.js +171 -0
  66. package/dist/exchange-deposit-index.d.ts +77 -0
  67. package/dist/exchange-deposit-index.js +34 -0
  68. package/dist/exchange-deposit-issuance.d.ts +327 -0
  69. package/dist/exchange-deposit-issuance.js +224 -0
  70. package/dist/exchange-deposit-path.d.ts +16 -0
  71. package/dist/exchange-deposit-path.js +11 -0
  72. package/dist/exchange-deposit-pow.d.ts +163 -0
  73. package/dist/exchange-deposit-pow.js +93 -0
  74. package/dist/exchange-deposit-recipient.d.ts +66 -0
  75. package/dist/exchange-deposit-recipient.js +28 -0
  76. package/dist/exchange-deposit-self-cross.d.ts +163 -0
  77. package/dist/exchange-deposit-self-cross.js +46 -0
  78. package/dist/exchange-deposit-signin.d.ts +275 -0
  79. package/dist/exchange-deposit-signin.js +147 -0
  80. package/dist/exchange-deposit-watch.d.ts +133 -0
  81. package/dist/exchange-deposit-watch.js +44 -0
  82. package/dist/exchange-deposit.d.ts +440 -0
  83. package/dist/exchange-deposit.js +138 -0
  84. package/dist/exchange-sender-guides.d.ts +29 -0
  85. package/dist/exchange-sender-guides.js +21 -0
  86. package/dist/index.d.ts +2 -0
  87. package/dist/index.js +2 -0
  88. package/dist/kraken-accept-list.d.ts +70 -0
  89. package/dist/kraken-accept-list.js +17 -0
  90. package/exchange-deposit/package.json +6 -0
  91. package/exchange-deposit-board/package.json +6 -0
  92. package/exchange-deposit-chain/package.json +6 -0
  93. package/exchange-deposit-factory/package.json +6 -0
  94. package/exchange-deposit-fake/package.json +6 -0
  95. package/exchange-deposit-issuance/package.json +6 -0
  96. package/exchange-deposit-path/package.json +6 -0
  97. package/exchange-deposit-pow/package.json +6 -0
  98. package/exchange-deposit-recipient/package.json +6 -0
  99. package/exchange-deposit-self-cross/package.json +6 -0
  100. package/exchange-deposit-signin/package.json +6 -0
  101. package/exchange-deposit-watch/package.json +6 -0
  102. package/exchange-sender-guides/package.json +6 -0
  103. package/kraken-accept-list/package.json +6 -0
  104. package/package.json +429 -4
@@ -0,0 +1,97 @@
1
+ import { encodeFunctionData, parseAbiItem, zeroAddress, } from 'viem';
2
+ import { exchangeDepositFactoryOf } from './exchange-deposit-factory.js';
3
+ export const factoryGroupOf = (contractGroup) => {
4
+ if (contractGroup === 'ether')
5
+ return 0;
6
+ if (contractGroup === 'single')
7
+ return 2;
8
+ throw new Error(`the ${String(contractGroup)} group is not one the factory takes`);
9
+ };
10
+ export const factorySettingsTupleOf = ({ settings, token, }) => ({
11
+ recipient: settings.recipient,
12
+ unwrap: settings.unwrapToNativePls,
13
+ feeRateE18: settings.feeRateE18,
14
+ partner: zeroAddress,
15
+ partnerRateE18: 0n,
16
+ group: factoryGroupOf(token.contractGroup),
17
+ token: token.address,
18
+ index: settings.index,
19
+ });
20
+ export const SETTINGS_COMPONENTS = [
21
+ { name: 'recipient', type: 'address' },
22
+ { name: 'unwrap', type: 'bool' },
23
+ { name: 'feeRateE18', type: 'uint256' },
24
+ { name: 'partner', type: 'address' },
25
+ { name: 'partnerRateE18', type: 'uint256' },
26
+ { name: 'group', type: 'uint8' },
27
+ { name: 'token', type: 'address' },
28
+ { name: 'index', type: 'uint256' },
29
+ ];
30
+ export const EXCHANGE_DEPOSIT_FACTORY_USAGE_EVENTS = [
31
+ parseAbiItem('event Flushed(bytes32 indexed settingsHash, address indexed depositAddress)'),
32
+ parseAbiItem('event Rescued(bytes32 indexed settingsHash, address indexed depositAddress, address indexed to)'),
33
+ parseAbiItem('event Retired(bytes32 indexed settingsHash, address indexed depositAddress, address indexed recipient)'),
34
+ ];
35
+ export const EXCHANGE_DEPOSIT_FACTORY_ABI = [
36
+ {
37
+ type: 'function',
38
+ name: 'mediator',
39
+ stateMutability: 'view',
40
+ inputs: [],
41
+ outputs: [{ name: '', type: 'address' }],
42
+ },
43
+ {
44
+ type: 'function',
45
+ name: 'MAXIMUM_FEE_RATE_E18',
46
+ stateMutability: 'view',
47
+ inputs: [],
48
+ outputs: [{ name: '', type: 'uint256' }],
49
+ },
50
+ {
51
+ type: 'function',
52
+ name: 'MAXIMUM_PARTNER_RATE_E18',
53
+ stateMutability: 'view',
54
+ inputs: [],
55
+ outputs: [{ name: '', type: 'uint256' }],
56
+ },
57
+ {
58
+ type: 'function',
59
+ name: 'depositAddress',
60
+ stateMutability: 'view',
61
+ inputs: [{ name: 'settings', type: 'tuple', components: SETTINGS_COMPONENTS }],
62
+ outputs: [{ name: '', type: 'address' }],
63
+ },
64
+ {
65
+ type: 'function',
66
+ name: 'flush',
67
+ stateMutability: 'payable',
68
+ inputs: [
69
+ { name: 'settings', type: 'tuple', components: SETTINGS_COMPONENTS },
70
+ { name: 'tokens', type: 'address[]' },
71
+ ],
72
+ outputs: [],
73
+ },
74
+ ];
75
+ export const EXCHANGE_DEPOSIT_MEDIATOR_ABI = [
76
+ {
77
+ type: 'function',
78
+ name: 'minPerTx',
79
+ stateMutability: 'view',
80
+ inputs: [{ name: 'token', type: 'address' }],
81
+ outputs: [{ name: '', type: 'uint256' }],
82
+ },
83
+ ];
84
+ export const exchangeDepositFlushRequest = ({ chainId, address, }) => {
85
+ if (address.origin !== 'derived')
86
+ throw new Error('a placeholder address has no flush');
87
+ const { address: factory } = exchangeDepositFactoryOf(chainId);
88
+ return {
89
+ to: factory,
90
+ value: 0n,
91
+ data: encodeFunctionData({
92
+ abi: EXCHANGE_DEPOSIT_FACTORY_ABI,
93
+ functionName: 'flush',
94
+ args: [factorySettingsTupleOf({ settings: address.settings, token: address.token }), [address.token.address]],
95
+ }),
96
+ };
97
+ };
@@ -0,0 +1,87 @@
1
+ import { type Address } from 'viem';
2
+ import type { ExchangeDepositToken } from './exchange-deposit.js';
3
+ /**
4
+ * The deposit factory: its address per chain, and the tokens and prices each chain offers.
5
+ *
6
+ * THE ONE PLACE THE FACTORY ADDRESS LIVES. The factory is redeployed with a simpler constructor. When that happens,
7
+ * change the entry of its chain in {@link EXCHANGE_DEPOSIT_FACTORY_BY_CHAIN} and nothing else. No other file names
8
+ * the address.
9
+ *
10
+ * Nothing here reads the chain. Addresses are derived by calling the factory (`depositAddress`), never by a local
11
+ * CREATE2 copy, so a change to the factory's derivation cannot leave this code behind.
12
+ *
13
+ * THIS FILE IS LIGHT ON PURPOSE. It holds only the map, the route checks and the link names, because the bridge's
14
+ * entry chunk reads it on every load. The contract interface, the events and the flush encoding live in `exchange-deposit-factory-calls.ts`.
15
+ */
16
+ /** One deployment of the deposit factory, and the tokens the interface offers on its chain. */
17
+ export interface ExchangeDepositFactoryEntry {
18
+ /** The factory contract. */
19
+ readonly address: Address;
20
+ /** The chain's proper name as a reader sees it, such as `Sepolia`. It is never translated. */
21
+ readonly chainName: string;
22
+ /** Whether the chain is a test network. A test network's addresses take test funds only, and the page says so. */
23
+ readonly isTestNetwork: boolean;
24
+ /** The PulseChain chain the funds arrive on when a deposit from an exchange crosses. */
25
+ readonly destinationChainId: bigint;
26
+ /** The destination chain's proper name as a reader sees it, such as `PulseChain testnet`. It is never translated. */
27
+ readonly destinationChainName: string;
28
+ /** The name the address bar uses for this chain, such as `sepolia`, in `?network=`. */
29
+ readonly addressBarName: string;
30
+ /** The block the factory was deployed at. Read it before scanning logs for the factory. */
31
+ readonly deployedBlock: bigint;
32
+ /**
33
+ * The wrapped ether token of this chain. The factory wraps ether before it crosses, so the mediator keeps the
34
+ * minimum of ether under this address (crossing spec, section 3.2), and the minimum of ether is read from here.
35
+ */
36
+ readonly wrappedEtherAddress: Address;
37
+ /**
38
+ * Whether the Glamsterdam upgrade is active on this chain. It turns on the warning that a plain ether send of 21,000
39
+ * gas fails (see `exchangeDepositNeedsGasLimitWarning`). It does not choose the gas table, which is a separate choice
40
+ * (`EXCHANGE_DEPOSIT_GAS_TABLE_NAME_BY_CHAIN`).
41
+ */
42
+ readonly glamsterdamActive: boolean;
43
+ /** The tokens the interface offers on this chain, in the order to show them. */
44
+ readonly tokens: readonly ExchangeDepositToken[];
45
+ /**
46
+ * The price the interface assumes for each token, by lower case address, in wei of ether per whole token. A test
47
+ * network has no market, so this is a stated assumption and not a feed. A token with no entry has no threshold.
48
+ */
49
+ readonly weiPerWholeTokenByAddress: Readonly<Record<string, bigint>>;
50
+ }
51
+ /**
52
+ * The factory per chain. There is ONE entry per deployment; a chain without an entry has no real deposit addresses.
53
+ */
54
+ export declare const EXCHANGE_DEPOSIT_FACTORY_BY_CHAIN: ReadonlyMap<bigint, ExchangeDepositFactoryEntry>;
55
+ /**
56
+ * Whether a bridge route is one a deposit from an exchange serves: its origin chain has a factory, and the route ends
57
+ * on the chain that factory's deposits arrive on. The factories are read from the map, never named here, so a factory
58
+ * added to the map turns its route on with no other change.
59
+ *
60
+ * @param input.originChainId - the chain the bridge sends from
61
+ * @param input.destinationChainId - the chain the bridge sends to
62
+ * @param input.factories - the factories to read. Defaults to the deployed ones.
63
+ */
64
+ export declare const isExchangeDepositRoute: ({ originChainId, destinationChainId, factories, }: {
65
+ originChainId: number;
66
+ destinationChainId: number;
67
+ factories?: ReadonlyMap<bigint, ExchangeDepositFactoryEntry>;
68
+ }) => boolean;
69
+ /**
70
+ * The address-bar name of the factory on a chain, for a link that must open that chain's deposits. A chain with no
71
+ * factory has no name, so a link to it carries no network.
72
+ *
73
+ * @param input.chainId - the chain the deposit starts on
74
+ * @param input.factories - the factories to read. Defaults to the deployed ones.
75
+ * @returns the name, or null when the chain has no factory
76
+ */
77
+ export declare const addressBarNetworkOf: ({ chainId, factories, }: {
78
+ chainId: number;
79
+ factories?: ReadonlyMap<bigint, ExchangeDepositFactoryEntry>;
80
+ }) => string | null;
81
+ /**
82
+ * The factory entry of a chain. It never falls back to another chain's factory.
83
+ *
84
+ * @param chainId - the Ethereum chain the deposit runs on
85
+ * @throws when no factory is deployed for the chain in this repository
86
+ */
87
+ export declare const exchangeDepositFactoryOf: (chainId: bigint) => ExchangeDepositFactoryEntry;
@@ -0,0 +1,104 @@
1
+ import { getAddress, zeroAddress } from 'viem';
2
+ import { ETHEREUM_MAINNET_CHAIN_ID, SEPOLIA_CHAIN_ID, WEI_PER_ETHER } from './exchange-deposit-constants.js';
3
+ const SEPOLIA_USDC_ADDRESS = getAddress('0xda9d4f9b69ac6C22e444eD9aF0CfC043b7a7f53f');
4
+ const SEPOLIA_TOKENS = [
5
+ {
6
+ symbol: 'ETH',
7
+ name: 'Sepolia Ether',
8
+ address: zeroAddress,
9
+ decimals: 18,
10
+ contractGroup: 'ether',
11
+ },
12
+ {
13
+ symbol: 'USDC',
14
+ name: 'USD Coin',
15
+ address: SEPOLIA_USDC_ADDRESS,
16
+ decimals: 6,
17
+ contractGroup: 'single',
18
+ },
19
+ ];
20
+ const SEPOLIA_WEI_PER_WHOLE_TOKEN_BY_ADDRESS = {
21
+ [zeroAddress]: WEI_PER_ETHER,
22
+ [SEPOLIA_USDC_ADDRESS.toLowerCase()]: WEI_PER_ETHER / 3200n,
23
+ };
24
+ const MAINNET_USDC_ADDRESS = getAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48');
25
+ const MAINNET_USDT_ADDRESS = getAddress('0xdAC17F958D2ee523a2206206994597C13D831ec7');
26
+ const MAINNET_DAI_ADDRESS = getAddress('0x6B175474E89094C44Da98b954EedeAC495271d0F');
27
+ const MAINNET_TOKENS = [
28
+ {
29
+ symbol: 'ETH',
30
+ name: 'Ether',
31
+ address: zeroAddress,
32
+ decimals: 18,
33
+ contractGroup: 'ether',
34
+ },
35
+ {
36
+ symbol: 'USDC',
37
+ name: 'USD Coin',
38
+ address: MAINNET_USDC_ADDRESS,
39
+ decimals: 6,
40
+ contractGroup: 'single',
41
+ },
42
+ {
43
+ symbol: 'USDT',
44
+ name: 'Tether',
45
+ address: MAINNET_USDT_ADDRESS,
46
+ decimals: 6,
47
+ contractGroup: 'single',
48
+ },
49
+ {
50
+ symbol: 'DAI',
51
+ name: 'Dai',
52
+ address: MAINNET_DAI_ADDRESS,
53
+ decimals: 18,
54
+ contractGroup: 'single',
55
+ },
56
+ ];
57
+ const MAINNET_WEI_PER_WHOLE_TOKEN_BY_ADDRESS = {
58
+ [zeroAddress]: WEI_PER_ETHER,
59
+ };
60
+ export const EXCHANGE_DEPOSIT_FACTORY_BY_CHAIN = new Map([
61
+ [
62
+ SEPOLIA_CHAIN_ID,
63
+ {
64
+ address: getAddress('0xE66877Cfe4CEc10ffBbBB3092B7001321AB5809D'),
65
+ chainName: 'Sepolia',
66
+ isTestNetwork: true,
67
+ destinationChainId: 943n,
68
+ destinationChainName: 'PulseChain testnet',
69
+ addressBarName: 'sepolia',
70
+ deployedBlock: 12144436n,
71
+ wrappedEtherAddress: getAddress('0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9'),
72
+ glamsterdamActive: true,
73
+ tokens: SEPOLIA_TOKENS,
74
+ weiPerWholeTokenByAddress: SEPOLIA_WEI_PER_WHOLE_TOKEN_BY_ADDRESS,
75
+ },
76
+ ],
77
+ [
78
+ ETHEREUM_MAINNET_CHAIN_ID,
79
+ {
80
+ address: getAddress('0x2cf3A381Ae662A06E478491D73bf5D7fD4ebCa0e'),
81
+ chainName: 'Ethereum',
82
+ isTestNetwork: false,
83
+ destinationChainId: 369n,
84
+ destinationChainName: 'PulseChain',
85
+ addressBarName: 'ethereum',
86
+ deployedBlock: 26439236n,
87
+ wrappedEtherAddress: getAddress('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),
88
+ glamsterdamActive: false,
89
+ tokens: MAINNET_TOKENS,
90
+ weiPerWholeTokenByAddress: MAINNET_WEI_PER_WHOLE_TOKEN_BY_ADDRESS,
91
+ },
92
+ ],
93
+ ]);
94
+ export const isExchangeDepositRoute = ({ originChainId, destinationChainId, factories = EXCHANGE_DEPOSIT_FACTORY_BY_CHAIN, }) => {
95
+ const factory = factories.get(BigInt(originChainId));
96
+ return factory !== undefined && factory.destinationChainId === BigInt(destinationChainId);
97
+ };
98
+ export const addressBarNetworkOf = ({ chainId, factories = EXCHANGE_DEPOSIT_FACTORY_BY_CHAIN, }) => factories.get(BigInt(chainId))?.addressBarName ?? null;
99
+ export const exchangeDepositFactoryOf = (chainId) => {
100
+ const entry = EXCHANGE_DEPOSIT_FACTORY_BY_CHAIN.get(chainId);
101
+ if (entry === undefined)
102
+ throw new Error(`no deposit factory is configured for chain ${chainId}`);
103
+ return entry;
104
+ };
@@ -0,0 +1,67 @@
1
+ import { type Address } from 'viem';
2
+ import { type ExchangeDepositAddress, type ExchangeDepositFeeLimits, type ExchangeDepositCrossingConditions, type ExchangeDepositRequest, type ExchangeDepositSettings, type ExchangeDepositSource, type ExchangeDepositToken } from './exchange-deposit.js';
3
+ import { type ExchangeDepositSeed } from './exchange-deposit-index.js';
4
+ /** The tokens the fake source offers, in the order a reader sees them. */
5
+ export declare const FAKE_EXCHANGE_DEPOSIT_TOKENS: readonly ExchangeDepositToken[];
6
+ /** Which situations the fake source can act out. */
7
+ export type FakeExchangeDepositScenario = 'empty' | 'partway' | 'ready' | 'crossing';
8
+ /** Every scenario, for a screen that lets a reviewer pick one. */
9
+ export declare const FAKE_EXCHANGE_DEPOSIT_SCENARIOS: readonly FakeExchangeDepositScenario[];
10
+ /**
11
+ * Whether a string names a scenario.
12
+ *
13
+ * @param value - text from an address bar, or null
14
+ */
15
+ export declare const isFakeExchangeDepositScenario: (value: string | null) => value is FakeExchangeDepositScenario;
16
+ /**
17
+ * One fake address from its settings. NOT the real derivation (TODO-real): it
18
+ * hashes the settings so the same settings give the same address and a new index
19
+ * gives another, which is the only property the screens need. Like the factory,
20
+ * it gives ether one address (the `ether` group) and every token its own address
21
+ * (the `single` group).
22
+ *
23
+ * @param settings - the settings that define the address
24
+ */
25
+ export declare const fakeDeriveExchangeDepositAddress: (settings: ExchangeDepositSettings) => Address;
26
+ /**
27
+ * What the fake executor weighs: a crossing on Ethereum mainnet at the paid gas of its table, at a base fee of
28
+ * 1.2 gwei plus a tip of 0.1 gwei, times 2, with ether at 3,200 dollars.
29
+ */
30
+ export declare const FAKE_EXCHANGE_DEPOSIT_CONDITIONS: ExchangeDepositCrossingConditions;
31
+ /**
32
+ * Builds the fake address of a set of settings, in the state a scenario describes.
33
+ *
34
+ * @param input.settings - the settings that define the address
35
+ * @param input.scenario - the situation to act out
36
+ * @throws when the fake source does not offer the token of the settings
37
+ */
38
+ export declare const fakeExchangeDepositAddress: ({ settings, scenario, }: {
39
+ settings: ExchangeDepositSettings;
40
+ scenario: FakeExchangeDepositScenario;
41
+ }) => ExchangeDepositAddress;
42
+ /**
43
+ * The settings of a request. Wrapped PLS arrives as PLS: the only token that asks for the unwrap. The factory index is
44
+ * derived from the request's seed, the same way the real source derives it.
45
+ *
46
+ * @param request - the recipient, the token, the fee, the counter and the seed
47
+ */
48
+ export declare const settingsOfRequest: (request: ExchangeDepositRequest) => ExchangeDepositSettings;
49
+ /**
50
+ * The seed the fake source uses. A development build that acts out a scenario skips the sign-in, so the fake addresses
51
+ * need a fixed seed rather than a signature. It is not a secret: the fake addresses hold nothing.
52
+ */
53
+ export declare const FAKE_EXCHANGE_DEPOSIT_SEED: ExchangeDepositSeed;
54
+ /** The recipient the fake source reports as a contract: any address ending in `c0de`. */
55
+ export declare const FAKE_CONTRACT_RECIPIENT: Address;
56
+ /**
57
+ * The maximums the fake factory reports: the same 1e16 (1 percent) for each rate as the deployed factory. A test or a
58
+ * draft reads these in place of the chain.
59
+ */
60
+ export declare const FAKE_EXCHANGE_DEPOSIT_FEE_LIMITS: ExchangeDepositFeeLimits;
61
+ /**
62
+ * A source that acts out one scenario. TODO-real: replace with the source the
63
+ * contract work provides.
64
+ *
65
+ * @param scenario - the situation to act out
66
+ */
67
+ export declare const fakeExchangeDepositSource: (scenario: FakeExchangeDepositScenario) => ExchangeDepositSource;
@@ -0,0 +1,171 @@
1
+ import { encodePacked, getAddress, keccak256, parseUnits, toHex, zeroAddress, } from 'viem';
2
+ import { ETHEREUM_MAINNET_CHAIN_ID, EXCHANGE_DEPOSIT_FEE_RATES, assertFeeRatesWithinLimits, WEI_PER_ETHER, exchangeDepositGasTableOf, } from './exchange-deposit.js';
3
+ import { factoryIndexOf, firstUnusedIndexFrom } from './exchange-deposit-index.js';
4
+ const USDC = {
5
+ symbol: 'USDC',
6
+ name: 'USD Coin',
7
+ address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
8
+ decimals: 6,
9
+ contractGroup: 'single',
10
+ };
11
+ const USDT = {
12
+ symbol: 'USDT',
13
+ name: 'Tether',
14
+ address: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
15
+ decimals: 6,
16
+ contractGroup: 'single',
17
+ };
18
+ const DAI = {
19
+ symbol: 'DAI',
20
+ name: 'Dai',
21
+ address: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
22
+ decimals: 18,
23
+ contractGroup: 'single',
24
+ };
25
+ const ETHER = {
26
+ symbol: 'ETH',
27
+ name: 'Ether',
28
+ address: zeroAddress,
29
+ decimals: 18,
30
+ contractGroup: 'ether',
31
+ };
32
+ const WBTC = {
33
+ symbol: 'WBTC',
34
+ name: 'Wrapped Bitcoin',
35
+ address: '0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599',
36
+ decimals: 8,
37
+ contractGroup: 'single',
38
+ };
39
+ const PLS = {
40
+ symbol: 'PLS',
41
+ name: 'Pulse',
42
+ address: '0xA882606494D86804B5514E07e6Bd2D6a6eE6d68A',
43
+ decimals: 18,
44
+ contractGroup: 'single',
45
+ };
46
+ export const FAKE_EXCHANGE_DEPOSIT_TOKENS = [USDC, USDT, DAI, ETHER, WBTC, PLS];
47
+ export const FAKE_EXCHANGE_DEPOSIT_SCENARIOS = [
48
+ 'empty',
49
+ 'partway',
50
+ 'ready',
51
+ 'crossing',
52
+ ];
53
+ export const isFakeExchangeDepositScenario = (value) => FAKE_EXCHANGE_DEPOSIT_SCENARIOS.includes(value);
54
+ const tokenOfSettings = (settings) => FAKE_EXCHANGE_DEPOSIT_TOKENS.find((candidate) => candidate.address.toLowerCase() === settings.tokenAddress.toLowerCase());
55
+ export const fakeDeriveExchangeDepositAddress = (settings) => {
56
+ const token = tokenOfSettings(settings);
57
+ const group = token?.contractGroup ?? 'single';
58
+ const singleToken = group === 'single' ? settings.tokenAddress.toLowerCase() : zeroAddress;
59
+ const hash = keccak256(encodePacked(['address', 'string', 'address', 'uint256', 'bool', 'uint256'], [
60
+ settings.recipient,
61
+ group,
62
+ singleToken,
63
+ settings.feeRateE18,
64
+ settings.unwrapToNativePls,
65
+ settings.index,
66
+ ]));
67
+ return getAddress(`0x${hash.slice(-40)}`);
68
+ };
69
+ export const FAKE_EXCHANGE_DEPOSIT_CONDITIONS = {
70
+ gasTable: exchangeDepositGasTableOf(ETHEREUM_MAINNET_CHAIN_ID),
71
+ baseFeeWei: 1200000000n,
72
+ priorityFeeWei: 100000000n,
73
+ multipleThousandths: 2000n,
74
+ weiPerWholeTokenByAddress: {
75
+ [USDC.address.toLowerCase()]: WEI_PER_ETHER / 3200n,
76
+ [USDT.address.toLowerCase()]: WEI_PER_ETHER / 3200n,
77
+ [DAI.address.toLowerCase()]: WEI_PER_ETHER / 3200n,
78
+ [ETHER.address.toLowerCase()]: WEI_PER_ETHER,
79
+ [WBTC.address.toLowerCase()]: 20n * WEI_PER_ETHER,
80
+ [PLS.address.toLowerCase()]: WEI_PER_ETHER / 300000n,
81
+ },
82
+ minimumPerTxByAddress: {
83
+ [USDC.address.toLowerCase()]: 0n,
84
+ [USDT.address.toLowerCase()]: 0n,
85
+ [DAI.address.toLowerCase()]: 0n,
86
+ [ETHER.address.toLowerCase()]: 0n,
87
+ [WBTC.address.toLowerCase()]: 0n,
88
+ [PLS.address.toLowerCase()]: 0n,
89
+ },
90
+ };
91
+ const situationKey = (symbol, feeRateE18) => `${symbol}:${feeRateE18}`;
92
+ const [, ONE_PERCENT, HALF_PERCENT] = EXCHANGE_DEPOSIT_FEE_RATES;
93
+ const situationsByScenario = {
94
+ empty: {},
95
+ partway: {
96
+ [situationKey('USDC', ONE_PERCENT)]: { balance: '120' },
97
+ [situationKey('USDT', HALF_PERCENT)]: { balance: '90' },
98
+ [situationKey('ETH', ONE_PERCENT)]: { balance: '0.03' },
99
+ },
100
+ ready: {
101
+ [situationKey('USDC', ONE_PERCENT)]: { balance: '300' },
102
+ [situationKey('USDT', HALF_PERCENT)]: { balance: '600' },
103
+ [situationKey('ETH', ONE_PERCENT)]: { balance: '0.09' },
104
+ },
105
+ crossing: {
106
+ [situationKey('USDC', ONE_PERCENT)]: { balance: '300', crossing: true },
107
+ [situationKey('USDT', HALF_PERCENT)]: { balance: '600' },
108
+ [situationKey('ETH', ONE_PERCENT)]: { balance: '0.09' },
109
+ },
110
+ };
111
+ const FAKE_CROSSING_HASH = `0x${'ab'.repeat(32)}`;
112
+ const crossingOf = (situation) => situation?.crossing === true
113
+ ? { transactionHash: FAKE_CROSSING_HASH, startedAtMs: Date.now() - 45_000 }
114
+ : null;
115
+ export const fakeExchangeDepositAddress = ({ settings, scenario, }) => {
116
+ const token = tokenOfSettings(settings);
117
+ if (token === undefined)
118
+ throw new Error(`the fake source has no token ${settings.tokenAddress}`);
119
+ const situation = settings.counter === 0 ? situationsByScenario[scenario][situationKey(token.symbol, settings.feeRateE18)] : undefined;
120
+ return {
121
+ origin: 'placeholder',
122
+ address: fakeDeriveExchangeDepositAddress(settings),
123
+ settings,
124
+ token,
125
+ waiting: situation === undefined ? 0n : parseUnits(situation.balance, token.decimals),
126
+ crossing: crossingOf(situation),
127
+ };
128
+ };
129
+ export const settingsOfRequest = (request) => ({
130
+ recipient: request.recipient,
131
+ tokenAddress: request.tokenAddress,
132
+ feeRateE18: request.feeRateE18,
133
+ counter: request.counter,
134
+ index: factoryIndexOf({
135
+ seed: request.seed,
136
+ tokenAddress: request.tokenAddress,
137
+ feeRateE18: request.feeRateE18,
138
+ counter: request.counter,
139
+ }),
140
+ unwrapToNativePls: request.tokenAddress.toLowerCase() === PLS.address.toLowerCase(),
141
+ });
142
+ export const FAKE_EXCHANGE_DEPOSIT_SEED = keccak256(toHex('fake-exchange-deposit-seed'));
143
+ export const FAKE_CONTRACT_RECIPIENT = getAddress('0x000000000000000000000000000000000000c0de');
144
+ export const FAKE_EXCHANGE_DEPOSIT_FEE_LIMITS = {
145
+ maximumFeeRateE18: 10n ** 16n,
146
+ maximumPartnerRateE18: 10n ** 16n,
147
+ };
148
+ export const fakeExchangeDepositSource = (scenario) => ({
149
+ listTokens: async () => FAKE_EXCHANGE_DEPOSIT_TOKENS,
150
+ feeLimits: async () => FAKE_EXCHANGE_DEPOSIT_FEE_LIMITS,
151
+ addressFor: async (request) => {
152
+ assertFeeRatesWithinLimits({ feeRateE18: request.feeRateE18, partnerRateE18: 0n, limits: FAKE_EXCHANGE_DEPOSIT_FEE_LIMITS });
153
+ return fakeExchangeDepositAddress({ settings: settingsOfRequest(request), scenario });
154
+ },
155
+ nextCounterFor: ({ startCounter, ...request }) => {
156
+ assertFeeRatesWithinLimits({ feeRateE18: request.feeRateE18, partnerRateE18: 0n, limits: FAKE_EXCHANGE_DEPOSIT_FEE_LIMITS });
157
+ return firstUnusedIndexFrom({
158
+ start: startCounter,
159
+ isUsed: async (counter) => {
160
+ const address = await fakeExchangeDepositAddress({
161
+ settings: settingsOfRequest({ ...request, counter }),
162
+ scenario,
163
+ });
164
+ return address.waiting > 0n || address.crossing !== null;
165
+ },
166
+ });
167
+ },
168
+ signerCodeKind: async () => 'no-code',
169
+ crossingConditions: async () => FAKE_EXCHANGE_DEPOSIT_CONDITIONS,
170
+ recipientHasCode: async (recipient) => recipient.toLowerCase().endsWith('c0de'),
171
+ });
@@ -0,0 +1,77 @@
1
+ import { type Address, type Hex } from 'viem';
2
+ /**
3
+ * The index of the next free deposit address.
4
+ *
5
+ * An address is for one deposit (single-use, owner decision 2026-11-13). A new deposit takes the LOWEST index whose
6
+ * address is free: it holds no balance and the factory has not recorded a use of it. The caller decides what
7
+ * "used" means for an address (see {@link exchangeDepositAddressIsUsed}); this file only scans.
8
+ */
9
+ /** How many addresses are read at once. A walk reads two, so the wait stays short on a slow node and few reads are in flight. */
10
+ export declare const EXCHANGE_DEPOSIT_INDEX_SCAN_BATCH_SIZE = 2;
11
+ /** How many indexes a walk checks from its start before it gives up. A reader who uses a thousand addresses needs a new design. */
12
+ export declare const EXCHANGE_DEPOSIT_INDEX_SCAN_LIMIT = 1000;
13
+ /** What the facts of one address say about whether it is in use. */
14
+ export interface ExchangeDepositAddressFacts {
15
+ /** The balance of the address's token, in base units. For ether, the whole ether balance. */
16
+ readonly balance: bigint;
17
+ /**
18
+ * Whether the factory logged a flush, a rescue or a retirement of the address. A flushed address ends with no
19
+ * balance and no code, so this event is the only record of the use.
20
+ */
21
+ readonly hasFactoryEvent: boolean;
22
+ }
23
+ /**
24
+ * Whether an address is used: it holds a balance now, or the factory has logged a use of it. A flushed address
25
+ * with no balance is still used, because its flush is logged.
26
+ *
27
+ * @param facts - the balance of the address and whether the factory logged a use of it
28
+ */
29
+ export declare const exchangeDepositAddressIsUsed: ({ balance, hasFactoryEvent }: ExchangeDepositAddressFacts) => boolean;
30
+ /** The options of {@link firstUnusedIndexFrom}. */
31
+ export interface FirstUnusedIndexOptions {
32
+ /** The first index the walk checks. */
33
+ readonly start: number;
34
+ /** Whether the address at an index is used. Called once per index, in batches. */
35
+ readonly isUsed: (index: number) => Promise<boolean>;
36
+ /** How many indexes are read at once. */
37
+ readonly batchSize?: number;
38
+ /** How many indexes, counted from the start, the walk checks before it gives up. */
39
+ readonly limit?: number;
40
+ }
41
+ /**
42
+ * The first free index at or after a start index, walking forward in small batches. The walk never reads below its
43
+ * start, so it never rescans indexes the caller already knows about. The caller decides where the start is: a
44
+ * recovery walk starts at 0, and a new address starts after the highest address already issued.
45
+ *
46
+ * @param options - the start, the used check, the batch size and the limit
47
+ * @returns the first index at or after the start for which `isUsed` is false
48
+ * @throws when every index in the walk's window is used, or when the start is not a whole number from 0
49
+ */
50
+ export declare const firstUnusedIndexFrom: ({ start, isUsed, batchSize, limit, }: FirstUnusedIndexOptions) => Promise<number>;
51
+ /**
52
+ * The 32-byte seed an address family grows from. It is the same for every address of one recipient on one chain and
53
+ * factory: an ordinary wallet gives it through {@link exchangeDepositSeedOfSignature}, and a smart-contract wallet
54
+ * through a random 32 bytes. Nobody can compute it from public facts.
55
+ */
56
+ export type ExchangeDepositSeed = Hex;
57
+ /**
58
+ * The factory index of one address: the uint256 the factory takes as `index`. It is a keccak256 hash of the seed and
59
+ * the settings that vary, with the counter last. A reader who does not hold the seed cannot compute it, so nobody can
60
+ * guess an address, pre-draw it or burn a scan range (threat analysis, section 6 (ii)).
61
+ *
62
+ * The hash uses `abi.encode`, so every field has a fixed width and no two inputs share an encoding. The counter is
63
+ * the number of the address within the settings: 0 is the first, and each new deposit takes the next counter.
64
+ *
65
+ * @param input.seed - the 32-byte seed of the recipient on this chain and factory
66
+ * @param input.tokenAddress - the token of the address
67
+ * @param input.feeRateE18 - the exact fee of the address, in 1e18 fixed point
68
+ * @param input.counter - the position of the address among those of the same settings, from 0
69
+ * @returns the factory index, a uint256 that is 256 bits wide in practice
70
+ * @throws when the counter is negative or not a safe integer
71
+ */
72
+ export declare const factoryIndexOf: ({ seed, tokenAddress, feeRateE18, counter, }: {
73
+ seed: ExchangeDepositSeed;
74
+ tokenAddress: Address;
75
+ feeRateE18: bigint;
76
+ counter: number;
77
+ }) => bigint;
@@ -0,0 +1,34 @@
1
+ import { encodeAbiParameters, keccak256, toHex, zeroAddress } from 'viem';
2
+ export const EXCHANGE_DEPOSIT_INDEX_SCAN_BATCH_SIZE = 2;
3
+ export const EXCHANGE_DEPOSIT_INDEX_SCAN_LIMIT = 1000;
4
+ export const exchangeDepositAddressIsUsed = ({ balance, hasFactoryEvent }) => balance > 0n || hasFactoryEvent;
5
+ export const firstUnusedIndexFrom = async ({ start, isUsed, batchSize = EXCHANGE_DEPOSIT_INDEX_SCAN_BATCH_SIZE, limit = EXCHANGE_DEPOSIT_INDEX_SCAN_LIMIT, }) => {
6
+ if (!Number.isSafeInteger(start) || start < 0)
7
+ throw new Error(`the walk must start at a whole number from 0, not ${start}`);
8
+ const end = start + limit;
9
+ for (let batchStart = start; batchStart < end; batchStart += batchSize) {
10
+ const indexes = Array.from({ length: Math.min(batchSize, end - batchStart) }, (_, offset) => batchStart + offset);
11
+ const usedFlags = await Promise.all(indexes.map((index) => isUsed(index)));
12
+ const firstFree = usedFlags.indexOf(false);
13
+ if (firstFree !== -1)
14
+ return indexes[firstFree];
15
+ }
16
+ throw new Error(`every deposit address from index ${start} to ${end - 1} is in use`);
17
+ };
18
+ const INDEX_DOMAIN_TAG = keccak256(toHex('gibs-deposit-index-v1'));
19
+ const NO_PARTNER_ADDRESS = zeroAddress;
20
+ const NO_PARTNER_RATE_E18 = 0n;
21
+ export const factoryIndexOf = ({ seed, tokenAddress, feeRateE18, counter, }) => {
22
+ if (!Number.isSafeInteger(counter) || counter < 0)
23
+ throw new Error(`the address counter must be a whole number from 0, not ${counter}`);
24
+ const encoded = encodeAbiParameters([
25
+ { type: 'bytes32' },
26
+ { type: 'bytes32' },
27
+ { type: 'address' },
28
+ { type: 'uint256' },
29
+ { type: 'address' },
30
+ { type: 'uint256' },
31
+ { type: 'uint256' },
32
+ ], [INDEX_DOMAIN_TAG, seed, tokenAddress, feeRateE18, NO_PARTNER_ADDRESS, NO_PARTNER_RATE_E18, BigInt(counter)]);
33
+ return BigInt(keccak256(encoded));
34
+ };