@gibs/bridge-client 1.29.0 → 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.
- package/block-polling/package.json +6 -0
- package/coinbase-accept-list/package.json +6 -0
- package/dist/block-polling.d.ts +33 -0
- package/dist/block-polling.js +10 -0
- package/dist/cjs/block-polling.d.ts +33 -0
- package/dist/cjs/block-polling.js +15 -0
- package/dist/cjs/client.d.ts +9 -1
- package/dist/cjs/client.js +8 -5
- package/dist/cjs/coinbase-accept-list.d.ts +101 -0
- package/dist/cjs/coinbase-accept-list.js +97 -0
- package/dist/cjs/crossing-decision.d.ts +166 -0
- package/dist/cjs/crossing-decision.js +169 -0
- package/dist/cjs/crossing-vector-guard.d.ts +45 -0
- package/dist/cjs/crossing-vector-guard.js +139 -0
- package/dist/cjs/exchange-deposit-board.d.ts +50 -0
- package/dist/cjs/exchange-deposit-board.js +70 -0
- package/dist/cjs/exchange-deposit-chain.d.ts +146 -0
- package/dist/cjs/exchange-deposit-chain.js +228 -0
- package/dist/cjs/exchange-deposit-constants.d.ts +10 -0
- package/dist/cjs/exchange-deposit-constants.js +6 -0
- package/dist/cjs/exchange-deposit-factory-calls.d.ts +245 -0
- package/dist/cjs/exchange-deposit-factory-calls.js +103 -0
- package/dist/cjs/exchange-deposit-factory.d.ts +87 -0
- package/dist/cjs/exchange-deposit-factory.js +110 -0
- package/dist/cjs/exchange-deposit-fake.d.ts +67 -0
- package/dist/cjs/exchange-deposit-fake.js +179 -0
- package/dist/cjs/exchange-deposit-index.d.ts +77 -0
- package/dist/cjs/exchange-deposit-index.js +40 -0
- package/dist/cjs/exchange-deposit-issuance.d.ts +327 -0
- package/dist/cjs/exchange-deposit-issuance.js +244 -0
- package/dist/cjs/exchange-deposit-path.d.ts +16 -0
- package/dist/cjs/exchange-deposit-path.js +15 -0
- package/dist/cjs/exchange-deposit-pow.d.ts +163 -0
- package/dist/cjs/exchange-deposit-pow.js +101 -0
- package/dist/cjs/exchange-deposit-recipient.d.ts +66 -0
- package/dist/cjs/exchange-deposit-recipient.js +35 -0
- package/dist/cjs/exchange-deposit-self-cross.d.ts +163 -0
- package/dist/cjs/exchange-deposit-self-cross.js +60 -0
- package/dist/cjs/exchange-deposit-signin.d.ts +275 -0
- package/dist/cjs/exchange-deposit-signin.js +166 -0
- package/dist/cjs/exchange-deposit-watch.d.ts +133 -0
- package/dist/cjs/exchange-deposit-watch.js +58 -0
- package/dist/cjs/exchange-deposit.d.ts +440 -0
- package/dist/cjs/exchange-deposit.js +154 -0
- package/dist/cjs/exchange-sender-guides.d.ts +29 -0
- package/dist/cjs/exchange-sender-guides.js +25 -0
- package/dist/cjs/image.d.ts +18 -0
- package/dist/cjs/image.js +11 -9
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.js +32 -1
- package/dist/cjs/kraken-accept-list.d.ts +70 -0
- package/dist/cjs/kraken-accept-list.js +24 -0
- package/dist/cjs/unlisted-token.d.ts +26 -0
- package/dist/cjs/unlisted-token.js +34 -0
- package/dist/client.d.ts +9 -1
- package/dist/client.js +8 -5
- package/dist/coinbase-accept-list.d.ts +101 -0
- package/dist/coinbase-accept-list.js +89 -0
- package/dist/crossing-decision.d.ts +166 -0
- package/dist/crossing-decision.js +162 -0
- package/dist/crossing-vector-guard.d.ts +45 -0
- package/dist/crossing-vector-guard.js +134 -0
- package/dist/exchange-deposit-board.d.ts +50 -0
- package/dist/exchange-deposit-board.js +28 -0
- package/dist/exchange-deposit-chain.d.ts +146 -0
- package/dist/exchange-deposit-chain.js +218 -0
- package/dist/exchange-deposit-constants.d.ts +10 -0
- package/dist/exchange-deposit-constants.js +3 -0
- package/dist/exchange-deposit-factory-calls.d.ts +245 -0
- package/dist/exchange-deposit-factory-calls.js +97 -0
- package/dist/exchange-deposit-factory.d.ts +87 -0
- package/dist/exchange-deposit-factory.js +104 -0
- package/dist/exchange-deposit-fake.d.ts +67 -0
- package/dist/exchange-deposit-fake.js +171 -0
- package/dist/exchange-deposit-index.d.ts +77 -0
- package/dist/exchange-deposit-index.js +34 -0
- package/dist/exchange-deposit-issuance.d.ts +327 -0
- package/dist/exchange-deposit-issuance.js +224 -0
- package/dist/exchange-deposit-path.d.ts +16 -0
- package/dist/exchange-deposit-path.js +11 -0
- package/dist/exchange-deposit-pow.d.ts +163 -0
- package/dist/exchange-deposit-pow.js +93 -0
- package/dist/exchange-deposit-recipient.d.ts +66 -0
- package/dist/exchange-deposit-recipient.js +28 -0
- package/dist/exchange-deposit-self-cross.d.ts +163 -0
- package/dist/exchange-deposit-self-cross.js +46 -0
- package/dist/exchange-deposit-signin.d.ts +275 -0
- package/dist/exchange-deposit-signin.js +147 -0
- package/dist/exchange-deposit-watch.d.ts +133 -0
- package/dist/exchange-deposit-watch.js +44 -0
- package/dist/exchange-deposit.d.ts +440 -0
- package/dist/exchange-deposit.js +138 -0
- package/dist/exchange-sender-guides.d.ts +29 -0
- package/dist/exchange-sender-guides.js +21 -0
- package/dist/image.d.ts +18 -0
- package/dist/image.js +8 -8
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/kraken-accept-list.d.ts +70 -0
- package/dist/kraken-accept-list.js +17 -0
- package/dist/unlisted-token.d.ts +26 -0
- package/dist/unlisted-token.js +30 -0
- package/exchange-deposit/package.json +6 -0
- package/exchange-deposit-board/package.json +6 -0
- package/exchange-deposit-chain/package.json +6 -0
- package/exchange-deposit-factory/package.json +6 -0
- package/exchange-deposit-fake/package.json +6 -0
- package/exchange-deposit-issuance/package.json +6 -0
- package/exchange-deposit-path/package.json +6 -0
- package/exchange-deposit-pow/package.json +6 -0
- package/exchange-deposit-recipient/package.json +6 -0
- package/exchange-deposit-self-cross/package.json +6 -0
- package/exchange-deposit-signin/package.json +6 -0
- package/exchange-deposit-watch/package.json +6 -0
- package/exchange-sender-guides/package.json +6 -0
- package/kraken-accept-list/package.json +6 -0
- package/llms.txt +1 -0
- package/package.json +487 -6
- package/unlisted-token/package.json +6 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { type PublicClient } from 'viem';
|
|
2
|
+
import { type ExchangeDepositCodeKind, type ExchangeDepositSource } from './exchange-deposit.js';
|
|
3
|
+
import { type ExchangeDepositFactoryEntry } from './exchange-deposit-factory.js';
|
|
4
|
+
import type { FakeExchangeDepositScenario } from './exchange-deposit-fake.js';
|
|
5
|
+
export { exchangeDepositFlushRequest, type ExchangeDepositFlushRequest } from './exchange-deposit-factory-calls.js';
|
|
6
|
+
/**
|
|
7
|
+
* THE REAL SOURCE: the deposit addresses of the deployed factory, read from the chain.
|
|
8
|
+
*
|
|
9
|
+
* Every address comes from the factory's own `depositAddress` view. Nothing here derives an address, so a change to
|
|
10
|
+
* the factory's derivation reaches the interface without a change in this file. The balances are read from the
|
|
11
|
+
* address itself. Whether an address was used is read from the factory's events, because a flushed address ends with
|
|
12
|
+
* no balance and no code. A crossing in progress is not read yet, so no address reports one.
|
|
13
|
+
*/
|
|
14
|
+
/** The read calls the real source makes. Any viem public client has them; a test passes a fake. */
|
|
15
|
+
export type ExchangeDepositReadClient = Pick<PublicClient, 'readContract' | 'getBalance' | 'getBlock' | 'estimateFeesPerGas' | 'getLogs' | 'getCode'>;
|
|
16
|
+
/** The one read the recipient check makes, on the destination chain. Any viem public client has it. */
|
|
17
|
+
export type ExchangeDepositDestinationClient = Pick<PublicClient, 'getCode'>;
|
|
18
|
+
/** The read client of a chain, by chain id. The real source passes {@link readClientOfChain}; a test passes a fake. */
|
|
19
|
+
export type ExchangeDepositClientOfChain = (chainId: bigint) => ExchangeDepositDestinationClient;
|
|
20
|
+
/**
|
|
21
|
+
* The longest one remote procedure call endpoint may take to answer a deposit read before the next endpoint is asked, in milliseconds.
|
|
22
|
+
* A dead endpoint otherwise holds each read that the rotation sends to it for the transport's full timeout, and the
|
|
23
|
+
* deposit reads run one after another, so the wait adds up. The bound is set well above a normal answer.
|
|
24
|
+
*/
|
|
25
|
+
export declare const EXCHANGE_DEPOSIT_READ_ATTEMPT_TIMEOUT_MS = 4000;
|
|
26
|
+
/**
|
|
27
|
+
* What the code read at an address is. A delegated ordinary key still signs for itself, so it is `delegation`, not
|
|
28
|
+
* `contract`. Any other code is `contract`.
|
|
29
|
+
*
|
|
30
|
+
* @param code - the code read at the address, or undefined when the read gave none
|
|
31
|
+
*/
|
|
32
|
+
export declare const codeKindOf: (code: `0x${string}` | undefined) => ExchangeDepositCodeKind;
|
|
33
|
+
/**
|
|
34
|
+
* The real source of one chain with a deployed factory.
|
|
35
|
+
*
|
|
36
|
+
* @param input.chainId - the chain of the factory. Its gas table and prices come from the same chain entry.
|
|
37
|
+
* @param input.client - the read client of the factory's chain
|
|
38
|
+
* @param input.clientOfChain - the read client of any chain, by id. The recipient check reads the destination chain
|
|
39
|
+
* of the factory (where the funds land) through it, and never the factory's own chain.
|
|
40
|
+
* @throws when the chain has no factory in this repository
|
|
41
|
+
*/
|
|
42
|
+
export declare const chainExchangeDepositSource: ({ chainId, client, clientOfChain, }: {
|
|
43
|
+
chainId: bigint;
|
|
44
|
+
client: ExchangeDepositReadClient;
|
|
45
|
+
clientOfChain: ExchangeDepositClientOfChain;
|
|
46
|
+
}) => ExchangeDepositSource;
|
|
47
|
+
/** The real source of Sepolia, read through the public client of the chain. */
|
|
48
|
+
export declare const sepoliaExchangeDepositSource: () => ExchangeDepositSource;
|
|
49
|
+
/**
|
|
50
|
+
* The name the address bar gives the fake source. A release build offers no fake source, so it names nothing there.
|
|
51
|
+
*/
|
|
52
|
+
export declare const FAKE_EXCHANGE_DEPOSIT_NETWORK_NAME = "fake";
|
|
53
|
+
/**
|
|
54
|
+
* Where the interface reads deposit addresses. `factory` is a deployed deposit factory on a chain, and its addresses
|
|
55
|
+
* are real. `fake` is the development-only stand-in. `unavailable` is no source at all: the interface shows no
|
|
56
|
+
* address, no code and nothing to copy.
|
|
57
|
+
*/
|
|
58
|
+
export type ExchangeDepositNetwork = {
|
|
59
|
+
readonly kind: 'factory';
|
|
60
|
+
readonly chainId: bigint;
|
|
61
|
+
} | {
|
|
62
|
+
readonly kind: 'fake';
|
|
63
|
+
} | {
|
|
64
|
+
readonly kind: 'unavailable';
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Whether the fake source is on offer. Only a development build offers it, so a release build can never show a fake
|
|
68
|
+
* address to a reader.
|
|
69
|
+
*/
|
|
70
|
+
export type FakeSourceOffer = 'offered' | 'withheld';
|
|
71
|
+
/** The options of {@link exchangeDepositEntryOf}. */
|
|
72
|
+
export interface ExchangeDepositNetworkOptions {
|
|
73
|
+
/** The name from the address bar (`?network=`), or null when it names none. */
|
|
74
|
+
readonly requestedNetwork: string | null;
|
|
75
|
+
/** The chain of the connected wallet, or null when no wallet is connected. */
|
|
76
|
+
readonly connectedChainId: number | null;
|
|
77
|
+
/** Whether the fake source is on offer. */
|
|
78
|
+
readonly fakeSource: FakeSourceOffer;
|
|
79
|
+
/** The factories to read. Defaults to the deployed ones. */
|
|
80
|
+
readonly factories?: ReadonlyMap<bigint, ExchangeDepositFactoryEntry>;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* What the page does before it reads any address. `ready` reads the network it names. `needs-connect` asks the reader to
|
|
84
|
+
* connect a wallet first. `needs-switch` asks the reader to move the connected wallet to `chainId` first. Neither
|
|
85
|
+
* `needs` state carries a network, so the page cannot show an address before the wallet is on that chain.
|
|
86
|
+
*/
|
|
87
|
+
export type ExchangeDepositEntry = {
|
|
88
|
+
readonly kind: 'ready';
|
|
89
|
+
readonly network: ExchangeDepositNetwork;
|
|
90
|
+
} | {
|
|
91
|
+
readonly kind: 'needs-connect';
|
|
92
|
+
readonly chainId: bigint;
|
|
93
|
+
} | {
|
|
94
|
+
readonly kind: 'needs-switch';
|
|
95
|
+
readonly chainId: bigint;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* The page's entry state, decided in this order:
|
|
99
|
+
*
|
|
100
|
+
* 1. The address bar names `fake`: the fake source, where it is on offer. Otherwise nothing.
|
|
101
|
+
* 2. The address bar names a test network with a factory (`sepolia`): that factory's real addresses while the connected
|
|
102
|
+
* wallet is on that chain. Any other wallet, or no wallet, reads nothing. The test network is never offered by default.
|
|
103
|
+
* 3. The development fake source is on offer and no wallet is connected: the fake source. A release build never gets this.
|
|
104
|
+
* 4. No wallet is connected: `needs-connect` for Ethereum mainnet, the default network.
|
|
105
|
+
* 5. The connected wallet is on another chain: `needs-switch` to Ethereum mainnet.
|
|
106
|
+
* 6. The connected wallet is on Ethereum mainnet: that factory's real addresses.
|
|
107
|
+
*
|
|
108
|
+
* Ethereum mainnet is the default. A name in the address bar that names mainnet (`ethereum`) or nothing at all leads
|
|
109
|
+
* to rules 3 to 6, and an unknown name is ignored. A chain with no factory never gets a real address.
|
|
110
|
+
*
|
|
111
|
+
* @param options - the address bar name, the wallet chain, the fake offer and the factories
|
|
112
|
+
* @returns the entry state the page shows
|
|
113
|
+
*/
|
|
114
|
+
export declare const exchangeDepositEntryOf: (options: ExchangeDepositNetworkOptions) => ExchangeDepositEntry;
|
|
115
|
+
/**
|
|
116
|
+
* The source of a network. The fake source is handed in, so the caller decides whether the fake source exists in the
|
|
117
|
+
* build at all. A release build hands in none, and its fake source is never loaded.
|
|
118
|
+
*
|
|
119
|
+
* @param input.network - which source to read. An unavailable network has no source, so the caller must not ask.
|
|
120
|
+
* @param input.scenario - the situation the fake source acts out
|
|
121
|
+
* @param input.fakeSourceOf - the fake source, or null in a build that offers none
|
|
122
|
+
* @throws when the network is unavailable, or is the fake source with no fake source handed in
|
|
123
|
+
*/
|
|
124
|
+
export declare const exchangeDepositSourceFor: ({ network, scenario, fakeSourceOf, }: {
|
|
125
|
+
network: ExchangeDepositNetwork;
|
|
126
|
+
scenario: FakeExchangeDepositScenario;
|
|
127
|
+
fakeSourceOf: ((scenario: FakeExchangeDepositScenario) => ExchangeDepositSource) | null;
|
|
128
|
+
}) => ExchangeDepositSource;
|
|
129
|
+
/** What a reader is told about the network an address is for. */
|
|
130
|
+
export interface ExchangeDepositNetworkView {
|
|
131
|
+
/** The chain the addresses are for. */
|
|
132
|
+
readonly chainId: bigint;
|
|
133
|
+
/** The chain's proper name, such as `Sepolia`. It is never translated. */
|
|
134
|
+
readonly chainName: string;
|
|
135
|
+
/** Whether the chain is a test network. */
|
|
136
|
+
readonly isTestNetwork: boolean;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* What the page says about the network in use, or null when there is none to say.
|
|
140
|
+
*
|
|
141
|
+
* The fake source acts out Sepolia, so it reads as a test network. A factory reads its own entry.
|
|
142
|
+
*
|
|
143
|
+
* @param network - the network in use
|
|
144
|
+
* @param factories - the factories to read. Defaults to the deployed ones.
|
|
145
|
+
*/
|
|
146
|
+
export declare const exchangeDepositNetworkViewOf: (network: ExchangeDepositNetwork, factories?: ReadonlyMap<bigint, ExchangeDepositFactoryEntry>) => ExchangeDepositNetworkView | null;
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { erc20Abi } from 'viem';
|
|
2
|
+
import { clientFromChain } from './client.js';
|
|
3
|
+
import { ETHEREUM_MAINNET_CHAIN_ID, EXCHANGE_DEPOSIT_FEE_LIMITS_UNREADABLE, SEPOLIA_CHAIN_ID, assertFeeRatesWithinLimits, exchangeDepositGasTableOf, } from './exchange-deposit.js';
|
|
4
|
+
import { EXCHANGE_DEPOSIT_FACTORY_BY_CHAIN, exchangeDepositFactoryOf } from './exchange-deposit-factory.js';
|
|
5
|
+
import { EXCHANGE_DEPOSIT_FACTORY_ABI, EXCHANGE_DEPOSIT_FACTORY_USAGE_EVENTS, EXCHANGE_DEPOSIT_MEDIATOR_ABI, factorySettingsTupleOf, } from './exchange-deposit-factory-calls.js';
|
|
6
|
+
import { exchangeDepositAddressIsUsed, factoryIndexOf, firstUnusedIndexFrom } from './exchange-deposit-index.js';
|
|
7
|
+
export { exchangeDepositFlushRequest } from './exchange-deposit-factory-calls.js';
|
|
8
|
+
export const EXCHANGE_DEPOSIT_READ_ATTEMPT_TIMEOUT_MS = 4_000;
|
|
9
|
+
const readClientOfChain = (chainId) => clientFromChain(Number(chainId), { attemptTimeoutMs: EXCHANGE_DEPOSIT_READ_ATTEMPT_TIMEOUT_MS });
|
|
10
|
+
const SAFETY_MULTIPLE_THOUSANDTHS = 2000n;
|
|
11
|
+
const DELEGATION_CODE_PATTERN = /^0xef0100[0-9a-f]{40}$/i;
|
|
12
|
+
export const codeKindOf = (code) => {
|
|
13
|
+
if (code === undefined || code === '0x')
|
|
14
|
+
return 'no-code';
|
|
15
|
+
if (DELEGATION_CODE_PATTERN.test(code))
|
|
16
|
+
return 'delegation';
|
|
17
|
+
return 'contract';
|
|
18
|
+
};
|
|
19
|
+
export const chainExchangeDepositSource = ({ chainId, client, clientOfChain, }) => {
|
|
20
|
+
const factory = exchangeDepositFactoryOf(chainId);
|
|
21
|
+
const tokenOf = (tokenAddress) => {
|
|
22
|
+
const found = factory.tokens.find((candidate) => candidate.address.toLowerCase() === tokenAddress.toLowerCase());
|
|
23
|
+
if (found === undefined)
|
|
24
|
+
throw new Error(`token ${tokenAddress} is not offered on chain ${chainId}`);
|
|
25
|
+
return found;
|
|
26
|
+
};
|
|
27
|
+
const feeLimits = async () => {
|
|
28
|
+
try {
|
|
29
|
+
const [maximumFeeRateE18, maximumPartnerRateE18] = await Promise.all([
|
|
30
|
+
client.readContract({
|
|
31
|
+
address: factory.address,
|
|
32
|
+
abi: EXCHANGE_DEPOSIT_FACTORY_ABI,
|
|
33
|
+
functionName: 'MAXIMUM_FEE_RATE_E18',
|
|
34
|
+
}),
|
|
35
|
+
client.readContract({
|
|
36
|
+
address: factory.address,
|
|
37
|
+
abi: EXCHANGE_DEPOSIT_FACTORY_ABI,
|
|
38
|
+
functionName: 'MAXIMUM_PARTNER_RATE_E18',
|
|
39
|
+
}),
|
|
40
|
+
]);
|
|
41
|
+
return { maximumFeeRateE18, maximumPartnerRateE18 };
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
throw new Error(`${EXCHANGE_DEPOSIT_FEE_LIMITS_UNREADABLE} Cause: ${error.message}`);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const minimumKeyOf = (token) => token.contractGroup === 'ether' ? factory.wrappedEtherAddress : token.address;
|
|
48
|
+
const minimumPerTxByAddressOf = async () => {
|
|
49
|
+
const mediator = await client.readContract({
|
|
50
|
+
address: factory.address,
|
|
51
|
+
abi: EXCHANGE_DEPOSIT_FACTORY_ABI,
|
|
52
|
+
functionName: 'mediator',
|
|
53
|
+
});
|
|
54
|
+
const minimums = await Promise.all(factory.tokens.map((token) => client.readContract({
|
|
55
|
+
address: mediator,
|
|
56
|
+
abi: EXCHANGE_DEPOSIT_MEDIATOR_ABI,
|
|
57
|
+
functionName: 'minPerTx',
|
|
58
|
+
args: [minimumKeyOf(token)],
|
|
59
|
+
})));
|
|
60
|
+
return Object.fromEntries(factory.tokens.map((token, index) => [token.address.toLowerCase(), minimums[index]]));
|
|
61
|
+
};
|
|
62
|
+
const readBalance = (token, address) => token.contractGroup === 'ether'
|
|
63
|
+
? client.getBalance({ address })
|
|
64
|
+
: client.readContract({ address: token.address, abi: erc20Abi, functionName: 'balanceOf', args: [address] });
|
|
65
|
+
const hasFactoryEventOf = async (factoryEntry, address) => {
|
|
66
|
+
const logs = await Promise.all(EXCHANGE_DEPOSIT_FACTORY_USAGE_EVENTS.map((event) => client.getLogs({
|
|
67
|
+
address: factoryEntry.address,
|
|
68
|
+
event,
|
|
69
|
+
args: { depositAddress: address },
|
|
70
|
+
fromBlock: factoryEntry.deployedBlock,
|
|
71
|
+
toBlock: 'latest',
|
|
72
|
+
})));
|
|
73
|
+
return logs.some((found) => found.length > 0);
|
|
74
|
+
};
|
|
75
|
+
const probeWithLimits = async (request, limits) => {
|
|
76
|
+
const token = tokenOf(request.tokenAddress);
|
|
77
|
+
assertFeeRatesWithinLimits({
|
|
78
|
+
feeRateE18: request.feeRateE18,
|
|
79
|
+
partnerRateE18: 0n,
|
|
80
|
+
limits,
|
|
81
|
+
});
|
|
82
|
+
const settings = {
|
|
83
|
+
recipient: request.recipient,
|
|
84
|
+
tokenAddress: request.tokenAddress,
|
|
85
|
+
feeRateE18: request.feeRateE18,
|
|
86
|
+
unwrapToNativePls: false,
|
|
87
|
+
counter: request.counter,
|
|
88
|
+
index: factoryIndexOf({
|
|
89
|
+
seed: request.seed,
|
|
90
|
+
tokenAddress: request.tokenAddress,
|
|
91
|
+
feeRateE18: request.feeRateE18,
|
|
92
|
+
counter: request.counter,
|
|
93
|
+
}),
|
|
94
|
+
};
|
|
95
|
+
const address = await client.readContract({
|
|
96
|
+
address: factory.address,
|
|
97
|
+
abi: EXCHANGE_DEPOSIT_FACTORY_ABI,
|
|
98
|
+
functionName: 'depositAddress',
|
|
99
|
+
args: [factorySettingsTupleOf({ settings, token })],
|
|
100
|
+
});
|
|
101
|
+
const balance = await readBalance(token, address);
|
|
102
|
+
return { address, token, balance, settings };
|
|
103
|
+
};
|
|
104
|
+
const probe = async (request) => probeWithLimits(request, await feeLimits());
|
|
105
|
+
return {
|
|
106
|
+
listTokens: async () => factory.tokens,
|
|
107
|
+
feeLimits,
|
|
108
|
+
addressFor: async (request) => {
|
|
109
|
+
const { address, token, settings, balance } = await probe(request);
|
|
110
|
+
const result = {
|
|
111
|
+
origin: 'derived',
|
|
112
|
+
address,
|
|
113
|
+
settings,
|
|
114
|
+
token,
|
|
115
|
+
waiting: balance,
|
|
116
|
+
crossing: null,
|
|
117
|
+
};
|
|
118
|
+
return result;
|
|
119
|
+
},
|
|
120
|
+
nextCounterFor: async ({ startCounter, ...request }) => {
|
|
121
|
+
const limits = await feeLimits();
|
|
122
|
+
return firstUnusedIndexFrom({
|
|
123
|
+
start: startCounter,
|
|
124
|
+
isUsed: async (counter) => {
|
|
125
|
+
const { address, balance } = await probeWithLimits({ ...request, counter }, limits);
|
|
126
|
+
const hasFactoryEvent = await hasFactoryEventOf(factory, address);
|
|
127
|
+
return exchangeDepositAddressIsUsed({ balance, hasFactoryEvent });
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
},
|
|
131
|
+
crossingConditions: async () => {
|
|
132
|
+
const [block, fees, minimumPerTxByAddress] = await Promise.all([
|
|
133
|
+
client.getBlock({ blockTag: 'latest' }),
|
|
134
|
+
client.estimateFeesPerGas(),
|
|
135
|
+
minimumPerTxByAddressOf(),
|
|
136
|
+
]);
|
|
137
|
+
if (block.baseFeePerGas === null)
|
|
138
|
+
throw new Error(`chain ${chainId} reports no base fee`);
|
|
139
|
+
return {
|
|
140
|
+
gasTable: exchangeDepositGasTableOf(chainId),
|
|
141
|
+
baseFeeWei: block.baseFeePerGas,
|
|
142
|
+
priorityFeeWei: fees.maxPriorityFeePerGas,
|
|
143
|
+
multipleThousandths: SAFETY_MULTIPLE_THOUSANDTHS,
|
|
144
|
+
weiPerWholeTokenByAddress: factory.weiPerWholeTokenByAddress,
|
|
145
|
+
minimumPerTxByAddress,
|
|
146
|
+
};
|
|
147
|
+
},
|
|
148
|
+
signerCodeKind: async (signer) => codeKindOf(await client.getCode({ address: signer })),
|
|
149
|
+
recipientHasCode: async (recipient) => {
|
|
150
|
+
const destination = clientOfChain(factory.destinationChainId);
|
|
151
|
+
return codeKindOf(await destination.getCode({ address: recipient })) === 'contract';
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
export const sepoliaExchangeDepositSource = () => chainExchangeDepositSource({
|
|
156
|
+
chainId: SEPOLIA_CHAIN_ID,
|
|
157
|
+
client: readClientOfChain(SEPOLIA_CHAIN_ID),
|
|
158
|
+
clientOfChain: readClientOfChain,
|
|
159
|
+
});
|
|
160
|
+
export const FAKE_EXCHANGE_DEPOSIT_NETWORK_NAME = 'fake';
|
|
161
|
+
const chainIdOfAddressBarName = ({ name, factories, }) => {
|
|
162
|
+
if (name === null)
|
|
163
|
+
return null;
|
|
164
|
+
const match = [...factories].find(([, entry]) => entry.addressBarName === name);
|
|
165
|
+
return match === undefined ? null : match[0];
|
|
166
|
+
};
|
|
167
|
+
export const exchangeDepositEntryOf = (options) => {
|
|
168
|
+
const { requestedNetwork, connectedChainId, fakeSource, factories = EXCHANGE_DEPOSIT_FACTORY_BY_CHAIN } = options;
|
|
169
|
+
const fakeOrNothing = fakeSource === 'offered' ? { kind: 'fake' } : { kind: 'unavailable' };
|
|
170
|
+
if (requestedNetwork === FAKE_EXCHANGE_DEPOSIT_NETWORK_NAME)
|
|
171
|
+
return { kind: 'ready', network: fakeOrNothing };
|
|
172
|
+
const namedChainId = chainIdOfAddressBarName({ name: requestedNetwork, factories });
|
|
173
|
+
if (namedChainId !== null && namedChainId !== ETHEREUM_MAINNET_CHAIN_ID) {
|
|
174
|
+
return { kind: 'ready', network: namedChainIdOrNothing({ namedChainId, connectedChainId }) };
|
|
175
|
+
}
|
|
176
|
+
if (namedChainId === null && connectedChainId === null && fakeSource === 'offered') {
|
|
177
|
+
return { kind: 'ready', network: { kind: 'fake' } };
|
|
178
|
+
}
|
|
179
|
+
return mainnetEntryOf({ connectedChainId, factories });
|
|
180
|
+
};
|
|
181
|
+
const mainnetEntryOf = ({ connectedChainId, factories, }) => {
|
|
182
|
+
if (!factories.has(ETHEREUM_MAINNET_CHAIN_ID))
|
|
183
|
+
return { kind: 'ready', network: { kind: 'unavailable' } };
|
|
184
|
+
if (connectedChainId === null)
|
|
185
|
+
return { kind: 'needs-connect', chainId: ETHEREUM_MAINNET_CHAIN_ID };
|
|
186
|
+
if (BigInt(connectedChainId) !== ETHEREUM_MAINNET_CHAIN_ID)
|
|
187
|
+
return { kind: 'needs-switch', chainId: ETHEREUM_MAINNET_CHAIN_ID };
|
|
188
|
+
return { kind: 'ready', network: { kind: 'factory', chainId: ETHEREUM_MAINNET_CHAIN_ID } };
|
|
189
|
+
};
|
|
190
|
+
const namedChainIdOrNothing = ({ namedChainId, connectedChainId, }) => {
|
|
191
|
+
if (connectedChainId === null)
|
|
192
|
+
return { kind: 'unavailable' };
|
|
193
|
+
if (BigInt(connectedChainId) !== namedChainId)
|
|
194
|
+
return { kind: 'unavailable' };
|
|
195
|
+
return { kind: 'factory', chainId: namedChainId };
|
|
196
|
+
};
|
|
197
|
+
export const exchangeDepositSourceFor = ({ network, scenario, fakeSourceOf, }) => {
|
|
198
|
+
if (network.kind === 'factory') {
|
|
199
|
+
return chainExchangeDepositSource({
|
|
200
|
+
chainId: network.chainId,
|
|
201
|
+
client: readClientOfChain(network.chainId),
|
|
202
|
+
clientOfChain: readClientOfChain,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
if (network.kind === 'fake' && fakeSourceOf !== null)
|
|
206
|
+
return fakeSourceOf(scenario);
|
|
207
|
+
throw new Error('no deposit source is available for this network');
|
|
208
|
+
};
|
|
209
|
+
export const exchangeDepositNetworkViewOf = (network, factories = EXCHANGE_DEPOSIT_FACTORY_BY_CHAIN) => {
|
|
210
|
+
if (network.kind === 'unavailable')
|
|
211
|
+
return null;
|
|
212
|
+
if (network.kind === 'fake')
|
|
213
|
+
return { chainId: SEPOLIA_CHAIN_ID, chainName: 'Sepolia', isTestNetwork: true };
|
|
214
|
+
const entry = factories.get(network.chainId);
|
|
215
|
+
if (entry === undefined)
|
|
216
|
+
return null;
|
|
217
|
+
return { chainId: network.chainId, chainName: entry.chainName, isTestNetwork: entry.isTestNetwork };
|
|
218
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two numbers the deposit modules share. They live in their own module, so the factory map and the bridge entry can
|
|
3
|
+
* read them without loading the whole deposit module into the first load. `exchange-deposit.ts` re-exports both.
|
|
4
|
+
*/
|
|
5
|
+
/** Wei in one ether. */
|
|
6
|
+
export declare const WEI_PER_ETHER: bigint;
|
|
7
|
+
/** The chain id of the Sepolia test network. */
|
|
8
|
+
export declare const SEPOLIA_CHAIN_ID = 11155111n;
|
|
9
|
+
/** The chain id of Ethereum mainnet. */
|
|
10
|
+
export declare const ETHEREUM_MAINNET_CHAIN_ID = 1n;
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { type Address, type Hex } from 'viem';
|
|
2
|
+
import { type ExchangeDepositAddress, type ExchangeDepositContractGroup, type ExchangeDepositSettings, type ExchangeDepositToken } from './exchange-deposit.js';
|
|
3
|
+
/**
|
|
4
|
+
* The factory's calls: its contract interface, the events that record a use of an address, and the pure encoding of a flush.
|
|
5
|
+
*
|
|
6
|
+
* Kept apart from `exchange-deposit-factory.ts`, which the entry chunk reads on every load. Only the page's chain
|
|
7
|
+
* reader and its flush hook reach this file, so the contract interface and the event definitions load with the page.
|
|
8
|
+
*/
|
|
9
|
+
/** The group number the factory takes: 0 for ether, 2 for a single token. Group 1 is retired and never sent. */
|
|
10
|
+
export declare const factoryGroupOf: (contractGroup: ExchangeDepositContractGroup) => number;
|
|
11
|
+
/** The settings tuple of the factory's functions, with the field names of its contract interface. */
|
|
12
|
+
export interface FactorySettingsTuple {
|
|
13
|
+
readonly recipient: Address;
|
|
14
|
+
readonly unwrap: boolean;
|
|
15
|
+
readonly feeRateE18: bigint;
|
|
16
|
+
readonly partner: Address;
|
|
17
|
+
readonly partnerRateE18: bigint;
|
|
18
|
+
readonly group: number;
|
|
19
|
+
readonly token: Address;
|
|
20
|
+
readonly index: bigint;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The factory's settings tuple for an address. The interface sends no partner: the partner is the zero address with
|
|
24
|
+
* a zero rate.
|
|
25
|
+
*
|
|
26
|
+
* @param input.settings - the settings that define the address
|
|
27
|
+
* @param input.token - the token of the address, resolved
|
|
28
|
+
*/
|
|
29
|
+
export declare const factorySettingsTupleOf: ({ settings, token, }: {
|
|
30
|
+
settings: ExchangeDepositSettings;
|
|
31
|
+
token: ExchangeDepositToken;
|
|
32
|
+
}) => FactorySettingsTuple;
|
|
33
|
+
export declare const SETTINGS_COMPONENTS: readonly [{
|
|
34
|
+
readonly name: "recipient";
|
|
35
|
+
readonly type: "address";
|
|
36
|
+
}, {
|
|
37
|
+
readonly name: "unwrap";
|
|
38
|
+
readonly type: "bool";
|
|
39
|
+
}, {
|
|
40
|
+
readonly name: "feeRateE18";
|
|
41
|
+
readonly type: "uint256";
|
|
42
|
+
}, {
|
|
43
|
+
readonly name: "partner";
|
|
44
|
+
readonly type: "address";
|
|
45
|
+
}, {
|
|
46
|
+
readonly name: "partnerRateE18";
|
|
47
|
+
readonly type: "uint256";
|
|
48
|
+
}, {
|
|
49
|
+
readonly name: "group";
|
|
50
|
+
readonly type: "uint8";
|
|
51
|
+
}, {
|
|
52
|
+
readonly name: "token";
|
|
53
|
+
readonly type: "address";
|
|
54
|
+
}, {
|
|
55
|
+
readonly name: "index";
|
|
56
|
+
readonly type: "uint256";
|
|
57
|
+
}];
|
|
58
|
+
/**
|
|
59
|
+
* The events the factory emits for a deposit address, from its source. After a flush the address has no balance and
|
|
60
|
+
* no code, so these events are the only record that it was used. Each one names the address as `depositAddress`.
|
|
61
|
+
*/
|
|
62
|
+
export declare const EXCHANGE_DEPOSIT_FACTORY_USAGE_EVENTS: readonly [{
|
|
63
|
+
readonly name: "Flushed";
|
|
64
|
+
readonly type: "event";
|
|
65
|
+
readonly inputs: readonly [{
|
|
66
|
+
readonly type: "bytes32";
|
|
67
|
+
readonly name: "settingsHash";
|
|
68
|
+
readonly indexed: true;
|
|
69
|
+
}, {
|
|
70
|
+
readonly type: "address";
|
|
71
|
+
readonly name: "depositAddress";
|
|
72
|
+
readonly indexed: true;
|
|
73
|
+
}];
|
|
74
|
+
}, {
|
|
75
|
+
readonly name: "Rescued";
|
|
76
|
+
readonly type: "event";
|
|
77
|
+
readonly inputs: readonly [{
|
|
78
|
+
readonly type: "bytes32";
|
|
79
|
+
readonly name: "settingsHash";
|
|
80
|
+
readonly indexed: true;
|
|
81
|
+
}, {
|
|
82
|
+
readonly type: "address";
|
|
83
|
+
readonly name: "depositAddress";
|
|
84
|
+
readonly indexed: true;
|
|
85
|
+
}, {
|
|
86
|
+
readonly type: "address";
|
|
87
|
+
readonly name: "to";
|
|
88
|
+
readonly indexed: true;
|
|
89
|
+
}];
|
|
90
|
+
}, {
|
|
91
|
+
readonly name: "Retired";
|
|
92
|
+
readonly type: "event";
|
|
93
|
+
readonly inputs: readonly [{
|
|
94
|
+
readonly type: "bytes32";
|
|
95
|
+
readonly name: "settingsHash";
|
|
96
|
+
readonly indexed: true;
|
|
97
|
+
}, {
|
|
98
|
+
readonly type: "address";
|
|
99
|
+
readonly name: "depositAddress";
|
|
100
|
+
readonly indexed: true;
|
|
101
|
+
}, {
|
|
102
|
+
readonly type: "address";
|
|
103
|
+
readonly name: "recipient";
|
|
104
|
+
readonly indexed: true;
|
|
105
|
+
}];
|
|
106
|
+
}];
|
|
107
|
+
/** The functions of the deposit factory the interface uses: its two maximums, its two address functions and its mediator. */
|
|
108
|
+
export declare const EXCHANGE_DEPOSIT_FACTORY_ABI: readonly [{
|
|
109
|
+
readonly type: "function";
|
|
110
|
+
readonly name: "mediator";
|
|
111
|
+
readonly stateMutability: "view";
|
|
112
|
+
readonly inputs: readonly [];
|
|
113
|
+
readonly outputs: readonly [{
|
|
114
|
+
readonly name: "";
|
|
115
|
+
readonly type: "address";
|
|
116
|
+
}];
|
|
117
|
+
}, {
|
|
118
|
+
readonly type: "function";
|
|
119
|
+
readonly name: "MAXIMUM_FEE_RATE_E18";
|
|
120
|
+
readonly stateMutability: "view";
|
|
121
|
+
readonly inputs: readonly [];
|
|
122
|
+
readonly outputs: readonly [{
|
|
123
|
+
readonly name: "";
|
|
124
|
+
readonly type: "uint256";
|
|
125
|
+
}];
|
|
126
|
+
}, {
|
|
127
|
+
readonly type: "function";
|
|
128
|
+
readonly name: "MAXIMUM_PARTNER_RATE_E18";
|
|
129
|
+
readonly stateMutability: "view";
|
|
130
|
+
readonly inputs: readonly [];
|
|
131
|
+
readonly outputs: readonly [{
|
|
132
|
+
readonly name: "";
|
|
133
|
+
readonly type: "uint256";
|
|
134
|
+
}];
|
|
135
|
+
}, {
|
|
136
|
+
readonly type: "function";
|
|
137
|
+
readonly name: "depositAddress";
|
|
138
|
+
readonly stateMutability: "view";
|
|
139
|
+
readonly inputs: readonly [{
|
|
140
|
+
readonly name: "settings";
|
|
141
|
+
readonly type: "tuple";
|
|
142
|
+
readonly components: readonly [{
|
|
143
|
+
readonly name: "recipient";
|
|
144
|
+
readonly type: "address";
|
|
145
|
+
}, {
|
|
146
|
+
readonly name: "unwrap";
|
|
147
|
+
readonly type: "bool";
|
|
148
|
+
}, {
|
|
149
|
+
readonly name: "feeRateE18";
|
|
150
|
+
readonly type: "uint256";
|
|
151
|
+
}, {
|
|
152
|
+
readonly name: "partner";
|
|
153
|
+
readonly type: "address";
|
|
154
|
+
}, {
|
|
155
|
+
readonly name: "partnerRateE18";
|
|
156
|
+
readonly type: "uint256";
|
|
157
|
+
}, {
|
|
158
|
+
readonly name: "group";
|
|
159
|
+
readonly type: "uint8";
|
|
160
|
+
}, {
|
|
161
|
+
readonly name: "token";
|
|
162
|
+
readonly type: "address";
|
|
163
|
+
}, {
|
|
164
|
+
readonly name: "index";
|
|
165
|
+
readonly type: "uint256";
|
|
166
|
+
}];
|
|
167
|
+
}];
|
|
168
|
+
readonly outputs: readonly [{
|
|
169
|
+
readonly name: "";
|
|
170
|
+
readonly type: "address";
|
|
171
|
+
}];
|
|
172
|
+
}, {
|
|
173
|
+
readonly type: "function";
|
|
174
|
+
readonly name: "flush";
|
|
175
|
+
readonly stateMutability: "payable";
|
|
176
|
+
readonly inputs: readonly [{
|
|
177
|
+
readonly name: "settings";
|
|
178
|
+
readonly type: "tuple";
|
|
179
|
+
readonly components: readonly [{
|
|
180
|
+
readonly name: "recipient";
|
|
181
|
+
readonly type: "address";
|
|
182
|
+
}, {
|
|
183
|
+
readonly name: "unwrap";
|
|
184
|
+
readonly type: "bool";
|
|
185
|
+
}, {
|
|
186
|
+
readonly name: "feeRateE18";
|
|
187
|
+
readonly type: "uint256";
|
|
188
|
+
}, {
|
|
189
|
+
readonly name: "partner";
|
|
190
|
+
readonly type: "address";
|
|
191
|
+
}, {
|
|
192
|
+
readonly name: "partnerRateE18";
|
|
193
|
+
readonly type: "uint256";
|
|
194
|
+
}, {
|
|
195
|
+
readonly name: "group";
|
|
196
|
+
readonly type: "uint8";
|
|
197
|
+
}, {
|
|
198
|
+
readonly name: "token";
|
|
199
|
+
readonly type: "address";
|
|
200
|
+
}, {
|
|
201
|
+
readonly name: "index";
|
|
202
|
+
readonly type: "uint256";
|
|
203
|
+
}];
|
|
204
|
+
}, {
|
|
205
|
+
readonly name: "tokens";
|
|
206
|
+
readonly type: "address[]";
|
|
207
|
+
}];
|
|
208
|
+
readonly outputs: readonly [];
|
|
209
|
+
}];
|
|
210
|
+
/**
|
|
211
|
+
* The one function of the bridge's mediator the interface reads: the smallest transfer it accepts for a token. The
|
|
212
|
+
* factory's `mediator()` names the mediator, and the mediator keeps the minimum per token.
|
|
213
|
+
*/
|
|
214
|
+
export declare const EXCHANGE_DEPOSIT_MEDIATOR_ABI: readonly [{
|
|
215
|
+
readonly type: "function";
|
|
216
|
+
readonly name: "minPerTx";
|
|
217
|
+
readonly stateMutability: "view";
|
|
218
|
+
readonly inputs: readonly [{
|
|
219
|
+
readonly name: "token";
|
|
220
|
+
readonly type: "address";
|
|
221
|
+
}];
|
|
222
|
+
readonly outputs: readonly [{
|
|
223
|
+
readonly name: "";
|
|
224
|
+
readonly type: "uint256";
|
|
225
|
+
}];
|
|
226
|
+
}];
|
|
227
|
+
/** A transaction a wallet can send. `value` is always zero: a flush moves the address's own funds, not the reader's. */
|
|
228
|
+
export interface ExchangeDepositFlushRequest {
|
|
229
|
+
readonly to: Address;
|
|
230
|
+
readonly data: Hex;
|
|
231
|
+
readonly value: 0n;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* The transaction that finishes the job for one address: a `flush` of the address's token. Anyone may send it, and
|
|
235
|
+
* the funds still reach the recipient the settings name. For ether the token is the zero address, so the flush lists
|
|
236
|
+
* the zero address, as the factory expects.
|
|
237
|
+
*
|
|
238
|
+
* @param input.chainId - the chain the address lives on
|
|
239
|
+
* @param input.address - a derived address. A placeholder has no factory behind it, so it has no flush.
|
|
240
|
+
* @throws when the address is a placeholder, or the chain has no factory
|
|
241
|
+
*/
|
|
242
|
+
export declare const exchangeDepositFlushRequest: ({ chainId, address, }: {
|
|
243
|
+
chainId: bigint;
|
|
244
|
+
address: ExchangeDepositAddress;
|
|
245
|
+
}) => ExchangeDepositFlushRequest;
|