# StonkBroker Investment Accounts Integration

Open investment accounts for your own users on Robinhood Chain (chain id 4663). Each account is an NFT with its own ERC 6551 wallet that holds stock tokens, funds and ETH. Whoever holds the NFT controls the wallet.

## Contracts (Robinhood Chain 4663)

| Contract | Address |
| --- | --- |
| StonkBroker Accounts (NFT) | `0x0ef148BC47B18E5C93314Da76931860175d4bC2E` |
| ERC 6551 registry | `0xE272B47AF5d8de38E7D09c048a81480290478193` |
| Account implementation | `0xEC570541DeE69540F4D438093Ed4f8f925aeFE35` |
| USDG (6 decimals) | `0x5fc5360D0400a0Fd4f2af552ADD042D716F1d168` |

ABIs in this folder: `StonkBrokerAccounts.abi.json`, `InvestAccountWallet.abi.json`.

## Gas

The accounts contract has no owner and no fees. Your app or your user calls it directly and pays the network gas. StonkBrokers does not sponsor gas for third party accounts. To run your users signature free with sponsored gas from your own prepaid balance, apply at https://stonkbrokers.io/developers/gasless.

## Daily sponsorship limit

Approved gasless teams choose how many transactions they sponsor each day, from 1 up to the platform maximum of 500. Read usage with `GET /v1/usage` and change the limit with `POST /v1/settings` and body `{ "dailyOpCap": 200 }`, both with `Authorization: Bearer <your API key>`. Once the limit is reached, further operations answer 429 until the next UTC day. The same setting is on https://stonkbrokers.io/developers/gasless.

## Account trading limit

On stonkbrokers.io the account wallet approves the gasless router for a limit the user picks (Set limit, Use full balance, or Revoke). Gasless orders can never spend more than that limit, and never more than what the user deposited into the account.

## Flow

1. Open an account: `open()` mints to the caller, `openFor(to)` mints to another address. Read the wallet from the `AccountOpened(id, owner, wallet)` event or `walletOf(id)`.
2. Deposit: a plain ERC 20 `transfer` (or ETH send) to the account wallet.
3. Act from the account: the NFT holder calls `executeCall(to, value, data)` on the account wallet.
4. Withdraw: `executeTokenTransfer(token, to, amount)` on the account wallet.

## Bounded allowances

Never approve an unlimited allowance. Approve the swap router for the exact trade amount, or for a limit the user chooses (for example "let gasless orders spend up to 500 USDG"), and skip the approval when the current allowance already covers the trade. Show users what is approved and give them a revoke button (`approve(router, 0)`). A bounded allowance caps what anyone could ever pull, even if a relayer key leaked.

```ts
async function ensureAllowance(token, amount, userLimit = 0n) {
  const current = await pub.readContract({ address: token, abi: erc20Abi, functionName: "allowance", args: [wallet, ROUTER] });
  if (current >= amount) return;
  const target = userLimit > amount ? userLimit : amount;
  const approve = encodeFunctionData({ abi: erc20Abi, functionName: "approve", args: [ROUTER, target] });
  await user.writeContract({ chain, account: me, address: wallet, abi: walletAbi, functionName: "executeCall", args: [token, 0n, approve] });
}
```

## Buy

`ensureAllowance(USDG, amount)`, then `executeCall(ROUTER, 0, swapCalldata)` swapping USDG into the stock token. The stock lands in the account wallet.

## Sell

`ensureAllowance(STOCK, amount)`, then `executeCall(ROUTER, 0, swapCalldata)` swapping the stock token into USDG. Proceeds stay in the account wallet until withdrawn. Stock tokens use 18 decimals.

## Notes

- Pin chain 4663 on every write and check the connected wallet is on 4663 before signing.
- Stock tokens are issued by Robinhood Assets (Jersey) Limited, which can pause or burn them.
