|
| 1 | +import { schema, SenderError, table, t } from 'spacetimedb/server'; |
| 2 | + |
| 3 | +const STARTING_BALANCE_CENTS = 10_000n; |
| 4 | +const MAX_NAME_LENGTH = 20; |
| 5 | +const MAX_U64 = (1n << 64n) - 1n; |
| 6 | + |
| 7 | +const directory = table( |
| 8 | + { name: 'directory', public: true }, |
| 9 | + { |
| 10 | + identity: t.identity().primaryKey(), |
| 11 | + name: t.string(), |
| 12 | + nameKey: t.string().unique(), |
| 13 | + } |
| 14 | +); |
| 15 | + |
| 16 | +const account = table( |
| 17 | + { name: 'account' }, |
| 18 | + { |
| 19 | + identity: t.identity().primaryKey(), |
| 20 | + balanceCents: t.u64(), |
| 21 | + } |
| 22 | +); |
| 23 | + |
| 24 | +const changeDirection = t.enum('ChangeDirection', { |
| 25 | + Credit: t.unit(), |
| 26 | + Debit: t.unit(), |
| 27 | +}); |
| 28 | + |
| 29 | +const accountChange = table( |
| 30 | + { name: 'account_change' }, |
| 31 | + { |
| 32 | + id: t.u64().primaryKey().autoInc(), |
| 33 | + accountIdentity: t.identity().index('btree'), |
| 34 | + counterpartyIdentity: t.identity(), |
| 35 | + direction: changeDirection, |
| 36 | + amountCents: t.u64(), |
| 37 | + createdAt: t.timestamp(), |
| 38 | + } |
| 39 | +); |
| 40 | + |
| 41 | +const spacetimedb = schema({ directory, account, accountChange }); |
| 42 | +export default spacetimedb; |
| 43 | + |
| 44 | +export const onConnect = spacetimedb.clientConnected(ctx => { |
| 45 | + if (!ctx.db.account.identity.find(ctx.sender)) { |
| 46 | + ctx.db.account.insert({ |
| 47 | + identity: ctx.sender, |
| 48 | + balanceCents: STARTING_BALANCE_CENTS, |
| 49 | + }); |
| 50 | + } |
| 51 | +}); |
| 52 | + |
| 53 | +export const my_account = spacetimedb.view( |
| 54 | + { name: 'my_account', public: true }, |
| 55 | + account.rowType.optional(), |
| 56 | + ctx => ctx.db.account.identity.find(ctx.sender) ?? undefined |
| 57 | +); |
| 58 | + |
| 59 | +export const my_account_changes = spacetimedb.view( |
| 60 | + { name: 'my_account_changes', public: true }, |
| 61 | + t.array(accountChange.rowType), |
| 62 | + ctx => [...ctx.db.accountChange.accountIdentity.filter(ctx.sender)] |
| 63 | +); |
| 64 | + |
| 65 | +export const set_name = spacetimedb.reducer( |
| 66 | + { name: t.string() }, |
| 67 | + (ctx, { name }) => { |
| 68 | + if (!ctx.db.account.identity.find(ctx.sender)) { |
| 69 | + throw new SenderError('Account is not ready yet'); |
| 70 | + } |
| 71 | + |
| 72 | + const displayName = name.trim(); |
| 73 | + if (displayName.length === 0 || displayName.length > MAX_NAME_LENGTH) { |
| 74 | + throw new SenderError('Names must be between 1 and 20 characters'); |
| 75 | + } |
| 76 | + |
| 77 | + const nameKey = displayName.toLowerCase(); |
| 78 | + const owner = ctx.db.directory.nameKey.find(nameKey); |
| 79 | + if (owner && !owner.identity.isEqual(ctx.sender)) { |
| 80 | + throw new SenderError('That name is already in use'); |
| 81 | + } |
| 82 | + |
| 83 | + const existing = ctx.db.directory.identity.find(ctx.sender); |
| 84 | + if (existing) { |
| 85 | + ctx.db.directory.identity.update({ |
| 86 | + identity: ctx.sender, |
| 87 | + name: displayName, |
| 88 | + nameKey, |
| 89 | + }); |
| 90 | + } else { |
| 91 | + ctx.db.directory.insert({ |
| 92 | + identity: ctx.sender, |
| 93 | + name: displayName, |
| 94 | + nameKey, |
| 95 | + }); |
| 96 | + } |
| 97 | + } |
| 98 | +); |
| 99 | + |
| 100 | +export const transfer = spacetimedb.reducer( |
| 101 | + { recipient: t.identity(), amountCents: t.u64() }, |
| 102 | + (ctx, { recipient: recipientIdentity, amountCents }) => { |
| 103 | + const sender = ctx.db.directory.identity.find(ctx.sender); |
| 104 | + if (!sender) { |
| 105 | + throw new SenderError('Choose a name before sending money'); |
| 106 | + } |
| 107 | + if (recipientIdentity.isEqual(ctx.sender)) { |
| 108 | + throw new SenderError('You cannot send money to yourself'); |
| 109 | + } |
| 110 | + if (amountCents === 0n) { |
| 111 | + throw new SenderError('Amount must be greater than zero'); |
| 112 | + } |
| 113 | + if (!ctx.db.directory.identity.find(recipientIdentity)) { |
| 114 | + throw new SenderError('Recipient does not exist'); |
| 115 | + } |
| 116 | + |
| 117 | + const fromAccount = ctx.db.account.identity.find(ctx.sender); |
| 118 | + const toAccount = ctx.db.account.identity.find(recipientIdentity); |
| 119 | + if (!fromAccount || !toAccount) { |
| 120 | + throw new SenderError('Account does not exist'); |
| 121 | + } |
| 122 | + if (fromAccount.balanceCents < amountCents) { |
| 123 | + throw new SenderError('Insufficient funds'); |
| 124 | + } |
| 125 | + if (toAccount.balanceCents > MAX_U64 - amountCents) { |
| 126 | + throw new SenderError('Recipient balance is too large'); |
| 127 | + } |
| 128 | + |
| 129 | + ctx.db.account.identity.update({ |
| 130 | + ...fromAccount, |
| 131 | + balanceCents: fromAccount.balanceCents - amountCents, |
| 132 | + }); |
| 133 | + ctx.db.account.identity.update({ |
| 134 | + ...toAccount, |
| 135 | + balanceCents: toAccount.balanceCents + amountCents, |
| 136 | + }); |
| 137 | + ctx.db.accountChange.insert({ |
| 138 | + id: 0n, |
| 139 | + accountIdentity: ctx.sender, |
| 140 | + counterpartyIdentity: recipientIdentity, |
| 141 | + direction: { tag: 'Debit' }, |
| 142 | + amountCents, |
| 143 | + createdAt: ctx.timestamp, |
| 144 | + }); |
| 145 | + ctx.db.accountChange.insert({ |
| 146 | + id: 0n, |
| 147 | + accountIdentity: recipientIdentity, |
| 148 | + counterpartyIdentity: ctx.sender, |
| 149 | + direction: { tag: 'Credit' }, |
| 150 | + amountCents, |
| 151 | + createdAt: ctx.timestamp, |
| 152 | + }); |
| 153 | + } |
| 154 | +); |
0 commit comments