Skip to content

Commit

Permalink
Kv market (#495)
Browse files Browse the repository at this point in the history
* start market kvsync app

* use table, inspector, logged in header

* fixes

* buy procedure

* fix

* disable buy button if bought

* disable txn gc

* add some padding
  • Loading branch information
vilterp authored Oct 6, 2024
1 parent 99c563b commit 1daa7db
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 18 deletions.
5 changes: 4 additions & 1 deletion apps/actors/systems/kvSync/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,12 @@ export function updateClient(
const resp = updateClientInner(state, init);
switch (resp.type) {
case "continue": {
// TODO: notify ppl that their txn was rejected
const incremented = incrementTime(resp.state);
const collected = garbageCollectTransactions(incremented);
return {
...resp,
state: garbageCollectTransactions(incrementTime(resp.state)),
state: incremented,
};
}
default:
Expand Down
30 changes: 29 additions & 1 deletion apps/actors/systems/kvSync/examples/bank.dd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ tickState{}?
----
application/datalog
tickState{actorID: "client0", state: ClientState{data: {"bob": [{"transactionID": "10698", "value": 10},{"transactionID": "4540893", "value": 20}]}, id: "0", liveQueries: {}, loginState: LoggedIn{loggingOut: false, token: "0.009657739204967151", username: "bob"}, mutationDefns: {"CreateAccount": Lambda{args: ["name"], body: Write{key: Var{name: "name"}, val: IntLit{val: 0}}}, "Deposit": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "+"}}}}, "Transfer": Lambda{args: ["toAccount","amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "fromBalance"},{"val": Read{default: 0, key: Var{name: "toAccount"}}, "varName": "toBalance"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "fromBalance"}], name: ">"}, ifFalse: Do{ops: [Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "fromBalance"},Var{name: "amount"}], name: "-"}},Write{key: Var{name: "toAccount"}, val: Apply{args: [Var{name: "toBalance"},Var{name: "amount"}], name: "+"}}]}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}, "Withdraw": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "balanceBefore"}], name: ">"}, ifFalse: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "-"}}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}}, randSeed: 5046918, time: 6, transactions: {"10698": {"clientTrace": [Read{key: "bob", transactionID: "-1"},Write{desc: Insert{after: {"transactionID": "10698", "value": 10}}, key: "bob"}], "fromMe": true, "invocation": Invocation{args: [10], name: "Deposit"}, "state": Committed{serverTimestamp: 0}, "writes": []}, "4540893": {"clientTrace": [Read{key: "bob", transactionID: "10698"},Write{desc: Update{after: {"transactionID": "4540893", "value": 20}, before: {"transactionID": "10698", "value": 10}}, key: "bob"}], "fromMe": true, "invocation": Invocation{args: [10], name: "Deposit"}, "state": Pending{sentTime: 5}, "writes": []}}}, tickID: 19}.
tickState{actorID: "client0", state: ClientState{data: {"bob": [{"transactionID": "10698", "value": 10},{"transactionID": "4540893", "value": 20}]}, id: "0", liveQueries: {}, loginState: LoggedIn{loggingOut: false, token: "0.009657739204967151", username: "bob"}, mutationDefns: {"CreateAccount": Lambda{args: ["name"], body: Write{key: Var{name: "name"}, val: IntLit{val: 0}}}, "Deposit": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "+"}}}}, "Transfer": Lambda{args: ["toAccount","amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "fromBalance"},{"val": Read{default: 0, key: Var{name: "toAccount"}}, "varName": "toBalance"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "fromBalance"}], name: ">"}, ifFalse: Do{ops: [Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "fromBalance"},Var{name: "amount"}], name: "-"}},Write{key: Var{name: "toAccount"}, val: Apply{args: [Var{name: "toBalance"},Var{name: "amount"}], name: "+"}}]}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}, "Withdraw": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "balanceBefore"}], name: ">"}, ifFalse: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "-"}}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}}, randSeed: 5046918, time: 7, transactions: {"10698": {"clientTrace": [Read{key: "bob", transactionID: "-1"},Write{desc: Insert{after: {"transactionID": "10698", "value": 10}}, key: "bob"}], "fromMe": true, "invocation": Invocation{args: [10], name: "Deposit"}, "state": Committed{serverTimestamp: 0}, "writes": []}, "4540893": {"clientTrace": [Read{key: "bob", transactionID: "10698"},Write{desc: Update{after: {"transactionID": "4540893", "value": 20}, before: {"transactionID": "10698", "value": 10}}, key: "bob"}], "fromMe": true, "invocation": Invocation{args: [10], name: "Deposit"}, "state": Committed{serverTimestamp: 1}, "writes": []}}}, tickID: 23}.
tickState{actorID: "client0", state: ClientState{data: {"bob": [{"transactionID": "10698", "value": 10}]}, id: "0", liveQueries: {}, loginState: LoggedIn{loggingOut: false, token: "0.009657739204967151", username: "bob"}, mutationDefns: {"CreateAccount": Lambda{args: ["name"], body: Write{key: Var{name: "name"}, val: IntLit{val: 0}}}, "Deposit": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "+"}}}}, "Transfer": Lambda{args: ["toAccount","amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "fromBalance"},{"val": Read{default: 0, key: Var{name: "toAccount"}}, "varName": "toBalance"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "fromBalance"}], name: ">"}, ifFalse: Do{ops: [Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "fromBalance"},Var{name: "amount"}], name: "-"}},Write{key: Var{name: "toAccount"}, val: Apply{args: [Var{name: "toBalance"},Var{name: "amount"}], name: "+"}}]}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}, "Withdraw": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "balanceBefore"}], name: ">"}, ifFalse: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "-"}}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}}, randSeed: 2502798, time: 4, transactions: {"10698": {"clientTrace": [Read{key: "bob", transactionID: "-1"},Write{desc: Insert{after: {"transactionID": "10698", "value": 10}}, key: "bob"}], "fromMe": true, "invocation": Invocation{args: [10], name: "Deposit"}, "state": Pending{sentTime: 3}, "writes": []}}}, tickID: 12}.
tickState{actorID: "client0", state: ClientState{data: {"bob": [{"transactionID": "10698", "value": 10}]}, id: "0", liveQueries: {}, loginState: LoggedIn{loggingOut: false, token: "0.009657739204967151", username: "bob"}, mutationDefns: {"CreateAccount": Lambda{args: ["name"], body: Write{key: Var{name: "name"}, val: IntLit{val: 0}}}, "Deposit": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "+"}}}}, "Transfer": Lambda{args: ["toAccount","amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "fromBalance"},{"val": Read{default: 0, key: Var{name: "toAccount"}}, "varName": "toBalance"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "fromBalance"}], name: ">"}, ifFalse: Do{ops: [Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "fromBalance"},Var{name: "amount"}], name: "-"}},Write{key: Var{name: "toAccount"}, val: Apply{args: [Var{name: "toBalance"},Var{name: "amount"}], name: "+"}}]}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}, "Withdraw": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "balanceBefore"}], name: ">"}, ifFalse: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "-"}}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}}, randSeed: 2502798, time: 5, transactions: {"10698": {"clientTrace": [Read{key: "bob", transactionID: "-1"},Write{desc: Insert{after: {"transactionID": "10698", "value": 10}}, key: "bob"}], "fromMe": true, "invocation": Invocation{args: [10], name: "Deposit"}, "state": Committed{serverTimestamp: 0}, "writes": []}}}, tickID: 16}.
tickState{actorID: "client0", state: ClientState{data: {"bob": [{"transactionID": "4540893", "value": 20}]}, id: "0", liveQueries: {}, loginState: LoggedIn{loggingOut: false, token: "0.009657739204967151", username: "bob"}, mutationDefns: {"CreateAccount": Lambda{args: ["name"], body: Write{key: Var{name: "name"}, val: IntLit{val: 0}}}, "Deposit": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "+"}}}}, "Transfer": Lambda{args: ["toAccount","amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "fromBalance"},{"val": Read{default: 0, key: Var{name: "toAccount"}}, "varName": "toBalance"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "fromBalance"}], name: ">"}, ifFalse: Do{ops: [Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "fromBalance"},Var{name: "amount"}], name: "-"}},Write{key: Var{name: "toAccount"}, val: Apply{args: [Var{name: "toBalance"},Var{name: "amount"}], name: "+"}}]}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}, "Withdraw": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "balanceBefore"}], name: ">"}, ifFalse: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "-"}}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}}, randSeed: 5046918, time: 7, transactions: {"4540893": {"clientTrace": [Read{key: "bob", transactionID: "10698"},Write{desc: Update{after: {"transactionID": "4540893", "value": 20}, before: {"transactionID": "10698", "value": 10}}, key: "bob"}], "fromMe": true, "invocation": Invocation{args: [10], name: "Deposit"}, "state": Committed{serverTimestamp: 1}, "writes": []}}}, tickID: 23}.
tickState{actorID: "client0", state: ClientState{data: {}, id: "0", liveQueries: {}, loginState: LoggedIn{loggingOut: false, token: "0.009657739204967151", username: "bob"}, mutationDefns: {"CreateAccount": Lambda{args: ["name"], body: Write{key: Var{name: "name"}, val: IntLit{val: 0}}}, "Deposit": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "+"}}}}, "Transfer": Lambda{args: ["toAccount","amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "fromBalance"},{"val": Read{default: 0, key: Var{name: "toAccount"}}, "varName": "toBalance"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "fromBalance"}], name: ">"}, ifFalse: Do{ops: [Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "fromBalance"},Var{name: "amount"}], name: "-"}},Write{key: Var{name: "toAccount"}, val: Apply{args: [Var{name: "toBalance"},Var{name: "amount"}], name: "+"}}]}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}, "Withdraw": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "balanceBefore"}], name: ">"}, ifFalse: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "-"}}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}}, randSeed: 48, time: 3, transactions: {}}, tickID: 9}.
tickState{actorID: "client0", state: ClientState{data: {}, id: "0", liveQueries: {}, loginState: LoggedOut{loggingInAs: "bob"}, mutationDefns: {"CreateAccount": Lambda{args: ["name"], body: Write{key: Var{name: "name"}, val: IntLit{val: 0}}}, "Deposit": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "+"}}}}, "Transfer": Lambda{args: ["toAccount","amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "fromBalance"},{"val": Read{default: 0, key: Var{name: "toAccount"}}, "varName": "toBalance"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "fromBalance"}], name: ">"}, ifFalse: Do{ops: [Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "fromBalance"},Var{name: "amount"}], name: "-"}},Write{key: Var{name: "toAccount"}, val: Apply{args: [Var{name: "toBalance"},Var{name: "amount"}], name: "+"}}]}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}, "Withdraw": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "balanceBefore"}], name: ">"}, ifFalse: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "-"}}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}}, randSeed: 48, time: 2, transactions: {}}, tickID: 5}.
tickState{actorID: "client0", state: ClientState{data: {}, id: "0", liveQueries: {}, loginState: LoggedOut{loggingInAs: null{}}, mutationDefns: {"CreateAccount": Lambda{args: ["name"], body: Write{key: Var{name: "name"}, val: IntLit{val: 0}}}, "Deposit": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "+"}}}}, "Transfer": Lambda{args: ["toAccount","amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "fromBalance"},{"val": Read{default: 0, key: Var{name: "toAccount"}}, "varName": "toBalance"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "fromBalance"}], name: ">"}, ifFalse: Do{ops: [Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "fromBalance"},Var{name: "amount"}], name: "-"}},Write{key: Var{name: "toAccount"}, val: Apply{args: [Var{name: "toBalance"},Var{name: "amount"}], name: "+"}}]}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}, "Withdraw": Lambda{args: ["amount"], body: Let{bindings: [{"val": Read{default: 0, key: Var{name: "curUser"}}, "varName": "balanceBefore"}], body: If{cond: Apply{args: [Var{name: "amount"},Var{name: "balanceBefore"}], name: ">"}, ifFalse: Write{key: Var{name: "curUser"}, val: Apply{args: [Var{name: "balanceBefore"},Var{name: "amount"}], name: "-"}}, ifTrue: Abort{reason: StringLit{val: "balance not high enough"}}}}}}, randSeed: 48, time: 1, transactions: {}}, tickID: 2}.
Expand Down Expand Up @@ -86,6 +86,34 @@ application/json
"user0": {},
"client0": {
"bob": [
{
"transactionID": "10698",
"value": 10
},
{
"transactionID": "4540893",
"value": 20
},
{
"transactionID": "3045903",
"value": 0
},
{
"transactionID": "1418433",
"value": 88
},
{
"transactionID": "2583933",
"value": 78
},
{
"transactionID": "2943483",
"value": 73
},
{
"transactionID": "2573133",
"value": 103
},
{
"transactionID": "3162498",
"value": 90
Expand Down
2 changes: 2 additions & 0 deletions apps/actors/systems/kvSync/examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { bank } from "./bank";
import { chat } from "./chat";
import { counter } from "./counter";
import { todoMVC } from "./todoMVC";
import { market } from "./market";
import { KVApp } from "./types";

export const EXAMPLES: { [name: string]: KVApp } = {
bank,
chat,
counter,
todoMVC,
market,
};
Loading

0 comments on commit 1daa7db

Please sign in to comment.