@forgezero/runtime 0.1.14 → 0.1.16
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/README.md +27 -388
- package/dist/jobs.d.ts +5 -6
- package/dist/notify-templates.js +1 -1
- package/dist/schema-typebox.js +116 -7
- package/dist/schema.d.ts +14 -1
- package/dist/schema.js +116 -7
- package/package.json +3 -62
- package/contracts/foundry.toml +0 -9
- package/contracts/src/ColdVault.sol +0 -206
- package/contracts/src/DepositFactory.sol +0 -202
- package/contracts/src/DepositProxy.sol +0 -72
- package/contracts/src/IERC20.sol +0 -7
- package/contracts/src/MockTokens.sol +0 -32
- package/contracts/src/SafeTransferLib.sol +0 -31
- package/contracts/test/Custody.t.sol +0 -361
- package/contracts/test/Vectors.t.sol +0 -45
- package/dist/compliance.d.ts +0 -172
- package/dist/compliance.js +0 -168
- package/dist/finance/chain-addresses.d.ts +0 -130
- package/dist/finance/chain-addresses.js +0 -462
- package/dist/finance/chain-deposits.d.ts +0 -193
- package/dist/finance/chain-deposits.js +0 -600
- package/dist/finance/chain-reconcile.d.ts +0 -112
- package/dist/finance/chain-reconcile.js +0 -76
- package/dist/finance/chain-withdrawals.d.ts +0 -223
- package/dist/finance/chain-withdrawals.js +0 -635
- package/dist/finance/chain.d.ts +0 -116
- package/dist/finance/chain.js +0 -316
- package/dist/finance/commission.d.ts +0 -155
- package/dist/finance/commission.js +0 -423
- package/dist/finance/custody.d.ts +0 -68
- package/dist/finance/custody.js +0 -107
- package/dist/finance/derive.d.ts +0 -115
- package/dist/finance/derive.js +0 -116
- package/dist/finance/ledger.d.ts +0 -227
- package/dist/finance/ledger.js +0 -313
- package/dist/finance/market.d.ts +0 -209
- package/dist/finance/market.js +0 -112
- package/dist/finance/rates.d.ts +0 -178
- package/dist/finance/rates.js +0 -292
- package/dist/finance/transfers.d.ts +0 -153
- package/dist/finance/transfers.js +0 -292
- package/dist/finance/venues.d.ts +0 -190
- package/dist/finance/venues.js +0 -251
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity ^0.8.24;
|
|
3
|
-
|
|
4
|
-
import { IERC20 } from './IERC20.sol';
|
|
5
|
-
import { SafeTransferLib } from './SafeTransferLib.sol';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Cold storage: M-of-N custodians, and nothing else can move anything.
|
|
9
|
-
*
|
|
10
|
-
* There is no operator role here, no owner who can withdraw, and no key held by
|
|
11
|
-
* any running process. Funds move only when M distinct custodians have each
|
|
12
|
-
* signed the same withdrawal off-chain — on a Ledger, on an air-gapped machine,
|
|
13
|
-
* on whatever they keep their key in. The signatures are collected however the
|
|
14
|
-
* platform likes and submitted by anyone; the submitter has no power beyond
|
|
15
|
-
* paying for gas.
|
|
16
|
-
*
|
|
17
|
-
* That is what makes it cold. Compromising every server the platform runs does
|
|
18
|
-
* not move a single token, because no server holds a key that can.
|
|
19
|
-
*
|
|
20
|
-
* ## EIP-712, so a custodian can read what they are signing
|
|
21
|
-
*
|
|
22
|
-
* A raw hash tells a hardware wallet nothing, and a custodian approving an
|
|
23
|
-
* opaque blob is a custodian who will eventually approve the wrong one. The
|
|
24
|
-
* typed structure shows the token, the destination and the amount on the
|
|
25
|
-
* device's own screen.
|
|
26
|
-
*
|
|
27
|
-
* ## What binds a signature to exactly one withdrawal
|
|
28
|
-
*
|
|
29
|
-
* chainId a testnet rehearsal cannot be replayed on mainnet
|
|
30
|
-
* verifyingContract a signature for one vault is void at another
|
|
31
|
-
* nonce each approval is spendable once, in order
|
|
32
|
-
* token/to/amount changing any of them invalidates every signature
|
|
33
|
-
*
|
|
34
|
-
* Without all four, a signature gathered once is a signature reusable forever.
|
|
35
|
-
*/
|
|
36
|
-
contract ColdVault {
|
|
37
|
-
using SafeTransferLib for address;
|
|
38
|
-
|
|
39
|
-
/* --------------------------------------------------------------- state --- */
|
|
40
|
-
|
|
41
|
-
mapping(address => bool) public isCustodian;
|
|
42
|
-
address[] private custodianList;
|
|
43
|
-
uint256 public threshold;
|
|
44
|
-
uint256 public nonce;
|
|
45
|
-
|
|
46
|
-
bytes32 private immutable DOMAIN_SEPARATOR;
|
|
47
|
-
|
|
48
|
-
bytes32 private constant WITHDRAW_TYPEHASH =
|
|
49
|
-
keccak256('Withdraw(address token,address to,uint256 amount,uint256 nonce)');
|
|
50
|
-
|
|
51
|
-
/* -------------------------------------------------------------- events --- */
|
|
52
|
-
|
|
53
|
-
event Withdrawn(address indexed token, address indexed to, uint256 amount, uint256 nonce);
|
|
54
|
-
event CustodiansChanged(address[] custodians, uint256 threshold);
|
|
55
|
-
|
|
56
|
-
/* -------------------------------------------------------------- errors --- */
|
|
57
|
-
|
|
58
|
-
error BadThreshold();
|
|
59
|
-
error DuplicateCustodian();
|
|
60
|
-
error ZeroAddress();
|
|
61
|
-
error NotEnoughSignatures(uint256 got, uint256 need);
|
|
62
|
-
error SignaturesOutOfOrder();
|
|
63
|
-
error NotACustodian(address signer);
|
|
64
|
-
error NativeSendFailed();
|
|
65
|
-
error OnlySelf();
|
|
66
|
-
|
|
67
|
-
constructor(address[] memory initialCustodians, uint256 requiredSignatures) {
|
|
68
|
-
_setCustodians(initialCustodians, requiredSignatures);
|
|
69
|
-
DOMAIN_SEPARATOR = keccak256(
|
|
70
|
-
abi.encode(
|
|
71
|
-
keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'),
|
|
72
|
-
keccak256('ForgeZeroColdVault'),
|
|
73
|
-
keccak256('1'),
|
|
74
|
-
block.chainid,
|
|
75
|
-
address(this)
|
|
76
|
-
)
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/* ------------------------------------------------------------ withdraw --- */
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Move funds, given M valid custodian signatures over exactly this request.
|
|
84
|
-
*
|
|
85
|
-
* Signatures must be ordered by ascending signer address. That is not a style
|
|
86
|
-
* choice: it is how the same custodian signing twice is rejected in one pass
|
|
87
|
-
* without allocating a set, and without it an M-of-N vault is satisfied by
|
|
88
|
-
* one custodian submitting M copies of their own approval.
|
|
89
|
-
*/
|
|
90
|
-
function withdraw(
|
|
91
|
-
address token,
|
|
92
|
-
address to,
|
|
93
|
-
uint256 amount,
|
|
94
|
-
bytes[] calldata signatures
|
|
95
|
-
) external {
|
|
96
|
-
if (to == address(0)) revert ZeroAddress();
|
|
97
|
-
if (signatures.length < threshold) revert NotEnoughSignatures(signatures.length, threshold);
|
|
98
|
-
|
|
99
|
-
bytes32 digest = keccak256(
|
|
100
|
-
abi.encodePacked(
|
|
101
|
-
'\x19\x01',
|
|
102
|
-
DOMAIN_SEPARATOR,
|
|
103
|
-
keccak256(abi.encode(WITHDRAW_TYPEHASH, token, to, amount, nonce))
|
|
104
|
-
)
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
address previous = address(0);
|
|
108
|
-
for (uint256 i = 0; i < signatures.length; ) {
|
|
109
|
-
address signer = _recover(digest, signatures[i]);
|
|
110
|
-
if (!isCustodian[signer]) revert NotACustodian(signer);
|
|
111
|
-
// Strictly increasing. Equal means the same custodian counted twice.
|
|
112
|
-
if (signer <= previous) revert SignaturesOutOfOrder();
|
|
113
|
-
previous = signer;
|
|
114
|
-
unchecked { ++i; }
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// Bumped BEFORE the transfer, so every gathered signature is spent whether
|
|
118
|
-
// or not the token behaves, and a reverting token cannot be used to replay.
|
|
119
|
-
unchecked { ++nonce; }
|
|
120
|
-
|
|
121
|
-
if (token == address(0)) {
|
|
122
|
-
(bool ok, ) = to.call{ value: amount }('');
|
|
123
|
-
if (!ok) revert NativeSendFailed();
|
|
124
|
-
} else {
|
|
125
|
-
token.safeTransfer(to, amount);
|
|
126
|
-
}
|
|
127
|
-
emit Withdrawn(token, to, amount, nonce - 1);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Rotate the custodian set — itself requiring the current quorum.
|
|
132
|
-
*
|
|
133
|
-
* Callable only by this contract, through `withdraw`-style quorum: the
|
|
134
|
-
* platform submits a call to `address(this)` and the existing M must approve
|
|
135
|
-
* it. A custodian set changeable by anything less than the set itself is not
|
|
136
|
-
* a quorum.
|
|
137
|
-
*/
|
|
138
|
-
function setCustodians(address[] calldata nextCustodians, uint256 requiredSignatures) external {
|
|
139
|
-
if (msg.sender != address(this)) revert OnlySelf();
|
|
140
|
-
_setCustodians(nextCustodians, requiredSignatures);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function custodians() external view returns (address[] memory) {
|
|
144
|
-
return custodianList;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/** What a custodian's device should be asked to sign, for the next withdrawal. */
|
|
148
|
-
function digestFor(address token, address to, uint256 amount) external view returns (bytes32) {
|
|
149
|
-
return
|
|
150
|
-
keccak256(
|
|
151
|
-
abi.encodePacked(
|
|
152
|
-
'\x19\x01',
|
|
153
|
-
DOMAIN_SEPARATOR,
|
|
154
|
-
keccak256(abi.encode(WITHDRAW_TYPEHASH, token, to, amount, nonce))
|
|
155
|
-
)
|
|
156
|
-
);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/* ----------------------------------------------------------- internals --- */
|
|
160
|
-
|
|
161
|
-
function _setCustodians(address[] memory custodians_, uint256 requiredSignatures) private {
|
|
162
|
-
// A threshold of one is not a quorum, and a threshold above the set size
|
|
163
|
-
// is a vault nobody can ever open.
|
|
164
|
-
if (requiredSignatures < 2 || requiredSignatures > custodians_.length) revert BadThreshold();
|
|
165
|
-
|
|
166
|
-
for (uint256 i = 0; i < custodianList.length; ) {
|
|
167
|
-
isCustodian[custodianList[i]] = false;
|
|
168
|
-
unchecked { ++i; }
|
|
169
|
-
}
|
|
170
|
-
delete custodianList;
|
|
171
|
-
|
|
172
|
-
for (uint256 i = 0; i < custodians_.length; ) {
|
|
173
|
-
address who = custodians_[i];
|
|
174
|
-
if (who == address(0)) revert ZeroAddress();
|
|
175
|
-
if (isCustodian[who]) revert DuplicateCustodian();
|
|
176
|
-
isCustodian[who] = true;
|
|
177
|
-
custodianList.push(who);
|
|
178
|
-
unchecked { ++i; }
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
threshold = requiredSignatures;
|
|
182
|
-
emit CustodiansChanged(custodians_, requiredSignatures);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function _recover(bytes32 digest, bytes calldata signature) private pure returns (address) {
|
|
186
|
-
if (signature.length != 65) return address(0);
|
|
187
|
-
bytes32 r;
|
|
188
|
-
bytes32 s;
|
|
189
|
-
uint8 v;
|
|
190
|
-
assembly {
|
|
191
|
-
r := calldataload(signature.offset)
|
|
192
|
-
s := calldataload(add(signature.offset, 32))
|
|
193
|
-
v := byte(0, calldataload(add(signature.offset, 64)))
|
|
194
|
-
}
|
|
195
|
-
if (v < 27) v += 27;
|
|
196
|
-
// secp256k1 signatures are malleable: (r, s) and (r, n-s) both verify. A
|
|
197
|
-
// vault that accepted both would let a submitted approval be mutated into a
|
|
198
|
-
// second distinct signature by the same custodian.
|
|
199
|
-
if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
|
|
200
|
-
return address(0);
|
|
201
|
-
}
|
|
202
|
-
return ecrecover(digest, v, r, s);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
receive() external payable {}
|
|
206
|
-
}
|
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity ^0.8.24;
|
|
3
|
-
|
|
4
|
-
import { IERC20 } from './IERC20.sol';
|
|
5
|
-
import { DepositProxy } from './DepositProxy.sol';
|
|
6
|
-
import { SafeTransferLib } from './SafeTransferLib.sol';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Deterministic deposit addresses, and the sweep that collects them.
|
|
10
|
-
*
|
|
11
|
-
* ## The salt is opaque, deliberately
|
|
12
|
-
*
|
|
13
|
-
* This contract never learns what a salt MEANS. A platform that gives each user
|
|
14
|
-
* one lasting deposit address and a platform that mints a fresh address per
|
|
15
|
-
* invoice both work here unchanged, because the only thing this needs is a
|
|
16
|
-
* distinct 32-byte value. That is the difference between a runtime and one
|
|
17
|
-
* business's contract: encoding "user" or "invoice" here would force the choice
|
|
18
|
-
* on everybody who ever deployed it.
|
|
19
|
-
*
|
|
20
|
-
* The convention belongs to the caller — `keccak256("<app>:user:" || key)` or
|
|
21
|
-
* `keccak256("<app>:invoice:" || key)` — and both are just bytes32 to this
|
|
22
|
-
* contract.
|
|
23
|
-
*
|
|
24
|
-
* ## No chain write to create an address
|
|
25
|
-
*
|
|
26
|
-
* `computeAddress` is pure arithmetic over the deployer, the salt and the proxy
|
|
27
|
-
* init-code hash, so a platform derives a million deposit addresses with zero
|
|
28
|
-
* transactions and zero gas. The proxy is deployed lazily, at the first sweep,
|
|
29
|
-
* and only for addresses that actually received something.
|
|
30
|
-
*
|
|
31
|
-
* ## What this contract does NOT do
|
|
32
|
-
*
|
|
33
|
-
* It holds no policy. There is no per-user balance, no reserve, no fee, no
|
|
34
|
-
* notion of what is owed to whom — those are the platform's, and they belong in
|
|
35
|
-
* the platform's ledger where they can be changed without a redeploy. This moves
|
|
36
|
-
* tokens and nothing else.
|
|
37
|
-
*/
|
|
38
|
-
contract DepositFactory {
|
|
39
|
-
using SafeTransferLib for address;
|
|
40
|
-
|
|
41
|
-
address public owner;
|
|
42
|
-
address public pendingOwner;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* `keccak256(type(DepositProxy).creationCode)`, fixed at deploy.
|
|
46
|
-
*
|
|
47
|
-
* Exposed so an off-chain caller can mirror `computeAddress` byte for byte
|
|
48
|
-
* instead of hardcoding a hash that silently stops matching after a compiler
|
|
49
|
-
* upgrade.
|
|
50
|
-
*/
|
|
51
|
-
bytes32 public immutable PROXY_INIT_CODE_HASH;
|
|
52
|
-
|
|
53
|
-
event ProxyDeployed(bytes32 indexed salt, address indexed proxy);
|
|
54
|
-
event Swept(bytes32 indexed salt, address indexed token, uint256 amount);
|
|
55
|
-
event PaidOut(address indexed token, address indexed to, uint256 amount);
|
|
56
|
-
event OwnerProposed(address indexed newOwner);
|
|
57
|
-
event OwnerAccepted(address indexed previous, address indexed next);
|
|
58
|
-
|
|
59
|
-
error NotOwner();
|
|
60
|
-
error NotPendingOwner();
|
|
61
|
-
error ZeroAddress();
|
|
62
|
-
error Create2Failed();
|
|
63
|
-
error LengthMismatch();
|
|
64
|
-
error NativeSendFailed();
|
|
65
|
-
|
|
66
|
-
modifier onlyOwner() {
|
|
67
|
-
if (msg.sender != owner) revert NotOwner();
|
|
68
|
-
_;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
constructor(address initialOwner) {
|
|
72
|
-
if (initialOwner == address(0)) revert ZeroAddress();
|
|
73
|
-
owner = initialOwner;
|
|
74
|
-
PROXY_INIT_CODE_HASH = keccak256(type(DepositProxy).creationCode);
|
|
75
|
-
emit OwnerAccepted(address(0), initialOwner);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** The deposit address for a salt. Pure, so deriving costs nothing. */
|
|
79
|
-
function computeAddress(bytes32 salt) public view returns (address) {
|
|
80
|
-
return
|
|
81
|
-
address(
|
|
82
|
-
uint160(
|
|
83
|
-
uint256(
|
|
84
|
-
keccak256(
|
|
85
|
-
abi.encodePacked(bytes1(0xff), address(this), salt, PROXY_INIT_CODE_HASH)
|
|
86
|
-
)
|
|
87
|
-
)
|
|
88
|
-
)
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** Many at once, so a platform can reconcile a page of addresses in one call. */
|
|
93
|
-
function computeAddresses(bytes32[] calldata salts) external view returns (address[] memory out) {
|
|
94
|
-
out = new address[](salts.length);
|
|
95
|
-
for (uint256 i = 0; i < salts.length; ) {
|
|
96
|
-
out[i] = computeAddress(salts[i]);
|
|
97
|
-
unchecked { ++i; }
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Collect deposits into this contract, then pay out to the given destinations.
|
|
103
|
-
*
|
|
104
|
-
* One primitive rather than several, because every real operation is some
|
|
105
|
-
* combination of the two halves:
|
|
106
|
-
*
|
|
107
|
-
* withdraw for one user salts=[one] destinations=[external]
|
|
108
|
-
* consolidate to the float salts=[many] destinations=[hotAddress]
|
|
109
|
-
* move float to cold salts=[] destinations=[coldVault]
|
|
110
|
-
*
|
|
111
|
-
* Anything swept beyond what is paid out stays here as float, which is what
|
|
112
|
-
* makes the middle case a single transaction instead of one per address.
|
|
113
|
-
*/
|
|
114
|
-
function sweepAndPay(
|
|
115
|
-
bytes32[] calldata salts,
|
|
116
|
-
address token,
|
|
117
|
-
address[] calldata destinations,
|
|
118
|
-
uint256[] calldata amounts
|
|
119
|
-
) external onlyOwner {
|
|
120
|
-
if (destinations.length != amounts.length) revert LengthMismatch();
|
|
121
|
-
|
|
122
|
-
for (uint256 i = 0; i < salts.length; ) {
|
|
123
|
-
address proxy = _ensureProxy(salts[i]);
|
|
124
|
-
uint256 before = _balanceOf(token);
|
|
125
|
-
DepositProxy(payable(proxy)).sweep(token, address(this));
|
|
126
|
-
// The DIFFERENCE, not a number passed in. A fee-on-transfer token
|
|
127
|
-
// delivers less than it was asked to send, and an event reporting the
|
|
128
|
-
// requested figure is a ledger entry for money that never arrived.
|
|
129
|
-
emit Swept(salts[i], token, _balanceOf(token) - before);
|
|
130
|
-
unchecked { ++i; }
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
for (uint256 j = 0; j < destinations.length; ) {
|
|
134
|
-
if (destinations[j] == address(0)) revert ZeroAddress();
|
|
135
|
-
_payOut(token, destinations[j], amounts[j]);
|
|
136
|
-
unchecked { ++j; }
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/** Sweep only. The common case: consolidate into a vault held elsewhere. */
|
|
141
|
-
function sweepTo(bytes32[] calldata salts, address token, address destination)
|
|
142
|
-
external
|
|
143
|
-
onlyOwner
|
|
144
|
-
{
|
|
145
|
-
if (destination == address(0)) revert ZeroAddress();
|
|
146
|
-
for (uint256 i = 0; i < salts.length; ) {
|
|
147
|
-
DepositProxy(payable(_ensureProxy(salts[i]))).sweep(token, address(this));
|
|
148
|
-
unchecked { ++i; }
|
|
149
|
-
}
|
|
150
|
-
uint256 balance = _balanceOf(token);
|
|
151
|
-
if (balance > 0) _payOut(token, destination, balance);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function proposeOwner(address next) external onlyOwner {
|
|
155
|
-
if (next == address(0)) revert ZeroAddress();
|
|
156
|
-
pendingOwner = next;
|
|
157
|
-
emit OwnerProposed(next);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Accepted by the new owner's own key.
|
|
162
|
-
*
|
|
163
|
-
* A one-step transfer to a mistyped address bricks every deposit address this
|
|
164
|
-
* factory derives — the funds stay reachable only by a key nobody holds.
|
|
165
|
-
*/
|
|
166
|
-
function acceptOwnership() external {
|
|
167
|
-
if (msg.sender != pendingOwner) revert NotPendingOwner();
|
|
168
|
-
address previous = owner;
|
|
169
|
-
owner = msg.sender;
|
|
170
|
-
pendingOwner = address(0);
|
|
171
|
-
emit OwnerAccepted(previous, msg.sender);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function _ensureProxy(bytes32 salt) internal returns (address proxy) {
|
|
175
|
-
proxy = computeAddress(salt);
|
|
176
|
-
if (proxy.code.length == 0) {
|
|
177
|
-
bytes memory bytecode = type(DepositProxy).creationCode;
|
|
178
|
-
assembly {
|
|
179
|
-
proxy := create2(0, add(bytecode, 0x20), mload(bytecode), salt)
|
|
180
|
-
}
|
|
181
|
-
if (proxy == address(0)) revert Create2Failed();
|
|
182
|
-
emit ProxyDeployed(salt, proxy);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function _balanceOf(address token) internal view returns (uint256) {
|
|
187
|
-
return token == address(0) ? address(this).balance : IERC20(token).balanceOf(address(this));
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function _payOut(address token, address to, uint256 amount) internal {
|
|
191
|
-
if (amount == 0) return;
|
|
192
|
-
if (token == address(0)) {
|
|
193
|
-
(bool ok, ) = to.call{ value: amount }('');
|
|
194
|
-
if (!ok) revert NativeSendFailed();
|
|
195
|
-
} else {
|
|
196
|
-
token.safeTransfer(to, amount);
|
|
197
|
-
}
|
|
198
|
-
emit PaidOut(token, to, amount);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
receive() external payable {}
|
|
202
|
-
}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity ^0.8.24;
|
|
3
|
-
|
|
4
|
-
import { IERC20 } from './IERC20.sol';
|
|
5
|
-
import { SafeTransferLib } from './SafeTransferLib.sol';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* A deposit address that needs no key, no gas and no approval.
|
|
9
|
-
*
|
|
10
|
-
* This is the whole reason the CREATE2 pattern beats the obvious alternative.
|
|
11
|
-
* Sweeping from an ordinary account requires that account to sign an approval,
|
|
12
|
-
* which means every deposit address must first be funded with native coin and
|
|
13
|
-
* must have a private key derived, held and used. With a thousand deposit
|
|
14
|
-
* addresses that is a thousand gas top-ups and a thousand keys.
|
|
15
|
-
*
|
|
16
|
-
* A CREATE2 address holds balances before any code exists at it. The factory
|
|
17
|
-
* deploys this contract there only at the moment of the first sweep, and the
|
|
18
|
-
* constructor records the deployer, so nothing but that factory can ever move
|
|
19
|
-
* the funds.
|
|
20
|
-
*
|
|
21
|
-
* ## Never change this file
|
|
22
|
-
*
|
|
23
|
-
* The address depends on `keccak256(type(DepositProxy).creationCode)`. Adding a
|
|
24
|
-
* field, a function, or even changing the compiler settings changes the init
|
|
25
|
-
* code hash, which changes EVERY derived deposit address. Funds already sent to
|
|
26
|
-
* the old addresses stay recoverable only by the old factory. A change here is
|
|
27
|
-
* a new contract, deployed by a new factory, with addresses re-derived.
|
|
28
|
-
*/
|
|
29
|
-
contract DepositProxy {
|
|
30
|
-
using SafeTransferLib for address;
|
|
31
|
-
|
|
32
|
-
/** The factory that deployed this. Immutable, and the only caller allowed. */
|
|
33
|
-
address public immutable factory;
|
|
34
|
-
|
|
35
|
-
event Swept(address indexed token, address indexed to, uint256 amount);
|
|
36
|
-
|
|
37
|
-
error OnlyFactory();
|
|
38
|
-
error NativeSendFailed();
|
|
39
|
-
|
|
40
|
-
constructor() {
|
|
41
|
-
factory = msg.sender;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Move the whole balance of `token` to `to`. `address(0)` means native coin.
|
|
46
|
-
*
|
|
47
|
-
* Returns silently on a zero balance rather than reverting, so a batch across
|
|
48
|
-
* many addresses is not undone by the ones that were already swept.
|
|
49
|
-
*
|
|
50
|
-
* The amount is read from the chain rather than passed in: a figure computed
|
|
51
|
-
* off-chain is stale by the time it mines, and a fee-on-transfer token would
|
|
52
|
-
* make it wrong even if it were not.
|
|
53
|
-
*/
|
|
54
|
-
function sweep(address token, address to) external {
|
|
55
|
-
if (msg.sender != factory) revert OnlyFactory();
|
|
56
|
-
uint256 amount;
|
|
57
|
-
if (token == address(0)) {
|
|
58
|
-
amount = address(this).balance;
|
|
59
|
-
if (amount == 0) return;
|
|
60
|
-
(bool ok, ) = to.call{ value: amount }('');
|
|
61
|
-
if (!ok) revert NativeSendFailed();
|
|
62
|
-
} else {
|
|
63
|
-
amount = IERC20(token).balanceOf(address(this));
|
|
64
|
-
if (amount == 0) return;
|
|
65
|
-
token.safeTransfer(to, amount);
|
|
66
|
-
}
|
|
67
|
-
emit Swept(token, to, amount);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** Native coin sent to a deposit address lands here without hitting a fallback. */
|
|
71
|
-
receive() external payable {}
|
|
72
|
-
}
|
package/contracts/src/IERC20.sol
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity ^0.8.24;
|
|
3
|
-
|
|
4
|
-
contract MockToken {
|
|
5
|
-
event Transfer(address indexed from, address indexed to, uint256 value);
|
|
6
|
-
mapping(address => uint256) public balanceOf;
|
|
7
|
-
function mint(address to, uint256 a) external { balanceOf[to] += a; emit Transfer(address(0), to, a); }
|
|
8
|
-
function transfer(address to, uint256 a) external returns (bool) {
|
|
9
|
-
balanceOf[msg.sender] -= a; balanceOf[to] += a; emit Transfer(msg.sender, to, a); return true;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/** USDT: `transfer` returns NOTHING. */
|
|
14
|
-
contract MockNoReturnToken {
|
|
15
|
-
event Transfer(address indexed from, address indexed to, uint256 value);
|
|
16
|
-
mapping(address => uint256) public balanceOf;
|
|
17
|
-
function mint(address to, uint256 a) external { balanceOf[to] += a; emit Transfer(address(0), to, a); }
|
|
18
|
-
function transfer(address to, uint256 a) external {
|
|
19
|
-
balanceOf[msg.sender] -= a; balanceOf[to] += a; emit Transfer(msg.sender, to, a);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** Takes a 1% cut on the way through. Delivers less than it was asked to send. */
|
|
24
|
-
contract MockFeeToken {
|
|
25
|
-
event Transfer(address indexed from, address indexed to, uint256 value);
|
|
26
|
-
mapping(address => uint256) public balanceOf;
|
|
27
|
-
function mint(address to, uint256 a) external { balanceOf[to] += a; emit Transfer(address(0), to, a); }
|
|
28
|
-
function transfer(address to, uint256 a) external returns (bool) {
|
|
29
|
-
uint256 fee = a / 100;
|
|
30
|
-
balanceOf[msg.sender] -= a; balanceOf[to] += a - fee; emit Transfer(msg.sender, to, a - fee); return true;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity ^0.8.24;
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* ERC-20 transfer that survives the tokens people actually hold.
|
|
6
|
-
*
|
|
7
|
-
* returns true -> pass
|
|
8
|
-
* returns NOTHING -> pass (USDT on Ethereum, and it is not alone)
|
|
9
|
-
* returns false -> revert
|
|
10
|
-
* reverts -> revert
|
|
11
|
-
*
|
|
12
|
-
* The empty-returndata case is the one that matters. A call typed to return
|
|
13
|
-
* `bool` reverts on decode against USDT, and that failure appears only on a
|
|
14
|
-
* chain where real money is moving.
|
|
15
|
-
*
|
|
16
|
-
* Pair this with balanceOf-based sweeping — as the proxy and factory do — so a
|
|
17
|
-
* fee-on-transfer token moves what is ACTUALLY held rather than an overstated
|
|
18
|
-
* figure. Otherwise the platform pays out coins it does not have.
|
|
19
|
-
*/
|
|
20
|
-
library SafeTransferLib {
|
|
21
|
-
bytes4 private constant TRANSFER_SELECTOR = 0xa9059cbb;
|
|
22
|
-
|
|
23
|
-
error TransferFailed();
|
|
24
|
-
|
|
25
|
-
function safeTransfer(address token, address to, uint256 amount) internal {
|
|
26
|
-
(bool ok, bytes memory data) = token.call(
|
|
27
|
-
abi.encodeWithSelector(TRANSFER_SELECTOR, to, amount)
|
|
28
|
-
);
|
|
29
|
-
if (!ok || (data.length != 0 && !abi.decode(data, (bool)))) revert TransferFailed();
|
|
30
|
-
}
|
|
31
|
-
}
|