Für Entwickler / SENDIT

SenditBank

Ein Contract hält das Geld jedes SENDIT-Kontos. Eine Wallet zahlt einmal ein und registriert einen Schlüssel; danach signiert der Schlüssel die Trades, und jeder Venue führt ihn auf das Konto der Wallet zurück.

Das Modell

  • Konten. Ein Konto ist eine Wallet-Adresse (oder ein beliebiger Contract, der als sein eigenes Konto handelt). Guthaben sind balanceOf[account][asset], nie nach einem Schlüssel indiziert.
  • Assets. pUSD, ETH als address(0) und Tokens eines Launchers, den der Betreiber hinzugefügt hat (isAsset). Jeder andere Token wird abgelehnt. totalHeld[asset] ist die exakte Summe aller Guthaben und nie mehr, als die Bank hält.
  • Schlüssel. Adressen, die ein Konto mit einem vom Schlüssel signierten Nachweis registriert hat. Ein Schlüssel gibt das Guthaben des Kontos bei Venues aus, sendet, hebt an die eigene Wallet des Kontos ab, tankt sich selbst auf und widerruft. Er kann keine Schlüssel registrieren, keine Limits ändern und nicht per withdrawTo an eine andere Adresse abheben.
  • Venues. Die Contracts, die Konten belasten dürfen: die Engine, das Orderbuch, der Vault, die Copy-Factory, der Spot-Router, die pUSD-Exchange. Jeder ruft bank.pull(msg.sender, asset, amount) mit seinem eigenen Aufrufer auf, sodass ein Venue nicht bestimmen kann, wessen Geld er nimmt. Jede Auszahlung kommt mit bank.push(account, ...) zurück, nie an den Signierenden.

Es gibt kein receive(), keinen Sweep, keine Guthabenbearbeitung, kein Upgrade und keine Gebühr. ETH kommt nur über deposit und push hinein.

Konten und accountOf

accountOf(sender) ist das Konto, für das ein Absender handelt: der Inhaber, solange sender ein Schlüssel ist, der unter der aktuellen keyEpoch des Kontos und der aktuellen globalEpoch registriert ist, und sonst sender selbst. Eine Wallet löst sich also auf sich selbst auf, und jeder Venue funktioniert auch per Wallet signiert. Positionen, Vault-Anteile, Copy-Konten und jedes Event nennen das Konto, nie den Schlüssel.

Ein Schlüssel ist veraltet, wenn accountOf(keyOf(account)) != account. Ein playerOf gibt es hier nicht: So heißt dieselbe Idee in der Arcade-Bank.

Der Schlüsselnachweis

Der Schlüssel willigt ein, dem Konto zu dienen, indem er einen Digest signiert:

Solidity
keyDigest(account, key) = keccak256(abi.encode(
  "SENDIT/key-proof/v1", block.chainid, address(bank), account, key,
  keyEpoch[account], globalEpoch))

Der Nachweis ist die EIP-191-Signatur des Schlüssels über diese 32 Bytes ("\x19Ethereum Signed Message:\n32" || digest): 65 Bytes, niedriges s, v 27 oder 28 (0 und 1 werden akzeptiert). Die Wallet sendet ihn dann mit setKey(key, proof) oder innerhalb von deposit(pusdAmount, gasToKey, key, proof), das in derselben Transaktion auch das Gas des Schlüssels finanzieren kann.

viem
import { encodeAbiParameters, parseAbiParameters, keccak256 } from "viem";
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";

const key = privateKeyToAccount(generatePrivateKey()); // keep the private key safe

// ADD a key under the epochs in force: the bank computes the digest for you.
const digest = await client.readContract({
  address: bank, abi: bankAbi, functionName: "keyDigest", args: [account, key.address],
});
const proof = await key.signMessage({ message: { raw: digest } }); // EIP-191, 32 bytes

// REPLACE every key of the account: the same digest under keyEpoch + 1.
const [epoch, global] = await Promise.all([
  client.readContract({ address: bank, abi: bankAbi, functionName: "keyEpoch", args: [account] }),
  client.readContract({ address: bank, abi: bankAbi, functionName: "globalEpoch" }),
]);
const nextDigest = keccak256(encodeAbiParameters(
  parseAbiParameters("string, uint256, address, address, address, uint64, uint64"),
  ["SENDIT/key-proof/v1", 78270n, bank, account, key.address, epoch + 1n, global],
));
const replaceProof = await key.signMessage({ message: { raw: nextDigest } });
Wie die App ihren Schlüssel erzeugt

Das Frontend von SENDIT leitet den Schlüssel aus einer Wallet-Signatur einer festen EIP-4361-Nachricht ab ("Unlock my SENDIT trading key...", gebunden an die Domain, die Chain, die Bank und beide Epochen), sodass jedes Gerät denselben Schlüssel neu ableitet. Ein Bot braucht das nicht: Jeder Schlüssel, den Sie halten, funktioniert, solange sein Nachweis stimmt.

Epochen: hinzufügen oder ersetzen

Ein Nachweis wird gegen eines von zwei Epochenpaaren geprüft, und welches es ist, entscheidet, was passiert:

  • Aktuelle Epochen: ADD. Der Schlüssel kommt zu den aktiven Schlüsseln des Kontos hinzu; jeder andere Schlüssel bleibt aktiv (ein Schlüssel pro Gerät).
  • keyEpoch + 1: REPLACE. Die Bank widerruft zuerst (die Epoche rückt vor, jeder Schlüssel endet, KeyRevoked) und registriert dann den Schlüssel unter der neuen Epoche.

revokeKey() beendet jeden Schlüssel des Kontos; rotateKey(newKey, proof) widerruft und registriert in einer Transaktion, mit einem Nachweis unter der neuen Epoche. invalidateAllKeys() verschiebt globalEpoch und beendet jeden Schlüssel der Bank. keyOf(account) ist nur der zuletzt registrierte Schlüssel, an den das Gas der Einzahlung geht.

Limits

Die pUSD- und ETH-Abflüsse eines Schlüssels an Dritte (send, refuel und Pulls durch einen begrenzten Venue, derzeit der Spot-Router) zählen gegen tägliche Limits pro Konto, über ein festes 24-Stunden-Fenster ab dem ersten angerechneten Abfluss. Perps, der Vault und Copy sind unbegrenzt, gestartete Tokens ebenso, und per Wallet signierte Aufrufe werden nie angerechnet. Die Standardwerte des Contracts sind 10.000 pUSD und 5 ETH pro Tag; das Testnet-Deployment hat sie ausgeschaltet (UNCAPPED, siehe defaultCaps()). Ein Konto setzt seine eigenen mit setKeyCaps von seiner Wallet aus: 0 bedeutet den Standardwert.

Pause

setPaused(true) stoppt pull und move der Venues sowie per Schlüssel signiertes send und refuel. Es stoppt nie withdraw, withdrawTo, push (Positionen schließen also weiterhin und Auszahlungen kommen an), Einzahlungen, Registrierung und Widerruf von Schlüsseln oder das eigene send einer Wallet oder eines Contracts (ein Copy-Konto zahlt seinen Kopierenden über send aus). Die pUSD-Exchange respektiert die Pause der Bank an beiden ihrer Türen.

Venues und die 24-Stunden-Frist

Ein Venue kann jedes Konto über den Absender belasten, den er übergibt; Venues sind daher die Vertrauensbasis der Bank. Einen hinzuzufügen heißt proposeVenue, dann activateVenue nach VENUE_DELAY (24 Stunden): Ein VenueProposed-Event gibt jedem Konto einen Tag, um vorher abzuheben. listVenueNow überspringt die Wartezeit nur vor der allerersten Gutschrift der Bank. removeVenue wirkt sofort, und ein entfernter Venue kann weiterhin push aufrufen.

Funktionen und Events

Where every SENDIT account's money lives: pUSD, ETH (address(0)) and launched tokens, keyed by the account (the wallet), never by a key. Venues debit it with pull and credit it with push.

Wallet

FunctionWhat it does
deposit(uint256 pusdAmount, uint256 gasToKey, address key, bytes keyProof) payableThe one wallet transaction: pulls pUSD with no approve, credits msg.value - gasToKey as ETH, registers key when non-zero, and sends gasToKey to the account's key. Every argument may be zero.
depositToken(address token, uint256 amount)Deposits a launched token (or pUSD) already in the wallet. A launched token needs an approve to the bank first; pUSD does not.
setKey(address key, bytes keyProof)Registers a key. A proof under the current epochs ADDS it; a proof under keyEpoch + 1 REPLACES every key of the account.
setKeyCaps(uint128 pusdPerDay, uint128 ethPerDay)The account's own daily caps on its keys' sends, spot buys and launches. 0 means the default, UNCAPPED means none. Wallet only.
withdrawTo(address asset, uint256 amount, address to)Wallet only: pays another address, for a smart account with no receive(). Never paused, never capped.

Wallet or key

FunctionWhat it does
withdraw(address asset, uint256 amount)Pays accountOf(msg.sender), the wallet, whoever signs. Never paused, never capped.
send(address to, address asset, uint256 amount, bool toWallet)From the signer's account to `to`: credited in the bank (toWallet false; a live key named as `to` credits its account) or paid out as the asset. Key-signed pUSD and ETH count against the caps and stop while paused.
revokeKey()Ends every key of the account: keyEpoch moves on, so every device derives a new key.
rotateKey(address newKey, bytes keyProof)revokeKey, then registers newKey with a proof under the NEW account epoch, in one transaction.
refuel() returns (uint256 amount)A live key below REFUEL_BELOW tops itself up to REFUEL_TO from its account's ETH, once per REFUEL_EVERY. Stops while paused.

Venues

FunctionWhat it does
pull(address sender, address asset, uint256 amount) returns (address account)A venue debits accountOf(sender) and receives the asset. The venue passes its own msg.sender, so it never names whose money it takes. Stops while paused.
push(address account, address asset, uint256 amount) payableA venue credits `account` with what it hands over in this call. Never paused, and a removed venue can still push, so payouts in flight land.
move(address sender, address to, address asset, uint256 amount) returns (address account)A venue debits accountOf(sender) and credits `to` with no token moving (how a copy account is funded). Stops while paused.

Reading an account

FunctionWhat it does
accountOf(address sender) view returns (address)The account behind a sender: the owner while sender is one of its live keys, sender itself otherwise.
balanceOf(address account, address asset) view returns (uint256)One balance. asset address(0) is ETH.
keyOf(address account) view returns (address)The key registered last (address(0) after a revoke). Not proof that it is live: compare accountOf(keyOf(account)) with account.
ownerOfKey(address key) view returns (address)The account a key was registered for, live or not.
keyEpoch(address account) view returns (uint64)The account's key epoch. A revoke adds one.
keyAccountEpochOf(address key) view returns (uint64)The account epoch a key was registered under.
keyGlobalEpochOf(address key) view returns (uint64)The global epoch a key was registered under.
globalEpoch() view returns (uint64)The bank-wide epoch. invalidateAllKeys adds one.
keyDigest(address account, address key) view returns (bytes32)What the key signs (EIP-191) to consent to serving the account under the epochs in force now.
keyCapsOf(address account) view returns (uint256 pusdPerDay, uint256 ethPerDay, uint256 pusdSpent, uint256 ethSpent, uint64 windowEnds)The caps in force and what the keys spent in the current 24-hour window (zeros once it ended).
lastRefuelAt(address key) view returns (uint64)When a key last refuelled.
totalHeld(address asset) view returns (uint256)The sum of every balance of an asset. Never more than the bank holds.
isAsset(address asset) view returns (bool)ETH, pUSD, or a token launched by a launcher the operator added.

Reading the bank

FunctionWhat it does
pusd() view returns (address)The pUSD token.
operator() view returns (address)The operator: pause, venues, default caps, launchers, the key brake.
paused() view returns (bool)Whether venue pulls and moves and key-signed sends and refuels are stopped.
everFunded() view returns (bool)Set by the first credit; closes listVenueNow for ever.
defaultCaps() view returns (uint128 pusdPerDay, uint128 ethPerDay)The caps an account gets when it set none.
isVenue(address) view returns (bool)Whether an address may pull and move.
isCappedVenue(address) view returns (bool)Whether a key's pUSD and ETH spent through this venue count against the caps (the spot router).
wasVenue(address) view returns (bool)Ever listed: may still push after a removal.
venueActiveAt(address) view returns (uint64)When a proposed venue may be activated; 0 when none is pending.
proposedCapped(address) view returns (bool)The capped flag a pending venue will be listed with.
isLauncher(address) view returns (bool)Whether a launcher's tokens are accepted.
launchers(uint256) view returns (address)The added launchers, by index.
launcherCount() view returns (uint256)How many launchers were added.

Operator

FunctionWhat it does
proposeVenue(address venue, bool capped)Starts the VENUE_DELAY wait before a venue can be listed.
activateVenue(address venue)Lists a proposed venue once its delay has passed.
listVenueNow(address venue, bool capped)Lists a venue at once, only while the bank has never been credited (how the deploy lists its own venues).
removeVenue(address venue)Immediate. The venue can no longer pull or move, but can still push.
setPaused(bool paused_)The pause (see the pause rules).
setDefaultCaps(uint128 pusdPerDay, uint128 ethPerDay)The default caps. Both must be non-zero.
addLauncher(address launcher)Add-only: a launcher's tokens stay accepted for ever.
invalidateAllKeys()Moves globalEpoch on: every key on the bank stops resolving at once. Moves no balance; costs every account one setKey.
setOperator(address next)Hands the operator role on.

Constants

FunctionWhat it does
VERSION() view returns (uint256)1.
ETH() view returns (address)address(0), the ETH asset.
VENUE_DELAY() view returns (uint64)24 hours between proposeVenue and activateVenue.
CAP_WINDOW() view returns (uint64)24 hours, from the first charged outflow.
UNCAPPED() view returns (uint128)type(uint128).max: a cap of this value is no cap.
REFUEL_BELOW() view returns (uint256)0.002 ETH: a key may refuel below this.
REFUEL_TO() view returns (uint256)0.01 ETH: what a refuel tops the key up to.
REFUEL_EVERY() view returns (uint256)1 hour between two refuels of one key.

Events

EventWhat it does
Deposited(address indexed account, address indexed asset, uint256 amount)A deposit credited, pUSD, ETH or a token.
GasSent(address indexed account, address indexed key, uint256 amount, bool fromBalance)ETH sent to a key: the deposit's gas seed (fromBalance false) or a refuel (true).
Withdrawn(address indexed account, address indexed asset, uint256 amount, address to, address by)A withdrawal; `by` is the signer, a key or the wallet.
Sent(address indexed from, address indexed to, address indexed asset, uint256 amount, bool toWallet, address by)A send, in the bank or paid out.
Pulled(address indexed venue, address indexed account, address indexed asset, uint256 amount, address by)A venue took money from an account; `by` is who signed.
Pushed(address indexed venue, address indexed account, address indexed asset, uint256 amount)A venue paid an account.
Moved(address indexed venue, address indexed from, address indexed to, address asset, uint256 amount, address by)A venue moved money between two accounts.
KeySet(address indexed account, address indexed key, uint64 accountEpoch, uint64 globalEpoch)A key registered.
KeyRevoked(address indexed account, address indexed key, uint64 nextEpoch, address by)Every key of an account ended.
KeysInvalidated(uint64 globalEpoch)The operator ended every key on the bank.
CapsSet(address indexed account, uint256 pusdPerDay, uint256 ethPerDay)Caps changed; account address(0) is the default.
VenueProposed(address indexed venue, bool capped, uint64 activeAt)A venue proposed; activeAt is when it can be listed.
VenueSet(address indexed venue, bool allowed, bool capped)A venue listed or removed.
Paused(bool paused)The pause switched.
OperatorSet(address indexed previous, address indexed next)The operator changed.
LauncherAdded(address indexed launcher)A launcher's tokens accepted.