Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@
*.exe
*.out
*.app
.vscode/settings.json
exchange/.bash_history
.gitignore
exchange/exchange-emu.ext2
15 changes: 15 additions & 0 deletions exchange/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.dSYM/**
.DS_store
.vscode
lambda-state
*.o
exchange
exchange-emu
exchange-linux
exchange-cli
exchange-cli-emu
exchange-cli-linux
state.bin



90 changes: 90 additions & 0 deletions exchange/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
TOOLCHAIN:=cartesi/toolchain:0.15.0
LINUX:=gcc:12.2.0

CXX = g++
CXXFLAGS = -std=c++17
SRCS = exchange-cli.cpp state-mgr.cpp

RUN_TOOLCHAIN = \
docker run \
-e USER=$$(id -u -n) \
-e GROUP=$$(id -g -n) \
-e UID=$$(id -u) \
-e GID=$$(id -g) \
-v `pwd`:/home/$$(id -u -n) \
-w /home/$$(id -u -n) \
--rm \
-$(INTERACTIVE)t $(TOOLCHAIN)

RUN_LINUX = \
docker run \
-e USER=$$(id -u -n) \
-e GROUP=$$(id -g -n) \
-e UID=$$(id -u) \
-e GID=$$(id -g) \
-v `pwd`:/home/$$(id -u -n) \
-w /home/$$(id -u -n) \
--rm \
-$(INTERACTIVE)t $(LINUX)

RUN_CARTESI_MACHINE := \
cartesi-machine.lua \
--append-dtb-bootargs="single=yes" \
--flash-drive="label:fs,filename:exchange-cli-emu.ext2" \
--flash-drive=label:"state.bin,filename:state.bin,shared" \
-i

exchange-cli: $(SRCS)
$(CXX) $(CXXFLAGS) -o $@ $^

STATE_SIZE := 8192
STATE_FILE = state.bin
$(STATE_FILE): exchange-cli-linux
dd if=/dev/zero of=$@ bs=1 count=$(STATE_SIZE)
$(RUN_LINUX) ./exchange-cli-linux --init-state --setup-test-fixture --lambda-state $(STATE_FILE)
chmod 777 $(STATE_FILE)

init-state:
rm -f $(STATE_FILE)
$(MAKE) $(STATE_FILE)

exchange-cli-emu.ext2: exchange-cli-emu $(STATE_FILE)
mkdir -p fs
cp -f exchange-cli-emu $(STATE_FILE) fs/
$(RUN_TOOLCHAIN) genext2fs -f -i 512 -b 8192 -d fs $@
truncate -s %4096 $@

exchange-cli-emu: CXX = $(RUN_TOOLCHAIN) riscv64-cartesi-linux-gnu-g++
exchange-cli-emu: %.o = $(RUN_TOOLCHAIN) g++
exchange-cli-emu: $(SRCS)
$(CXX) $(CXXFLAGS) -o $@ $^

exchange-cli-linux: CXX = $(RUN_LINUX) g++
exchange-cli-linux: %.o = $(RUN_LINUX) g++
exchange-cli-linux: $(SRCS)
$(CXX) $(CXXFLAGS) -o $@ $^

cartesi-machine: exchange-cli-emu.ext2
$(RUN_CARTESI_MACHINE) /bin/sh

run-emu: exchange-cli-emu.ext2
$(RUN_CARTESI_MACHINE) /mnt/fs/exchange-cli-emu --interactive --lambda-state /mnt/fs/state.bin

run-linux: INTERACTIVE =i
run-linux: exchange-cli-emu.ext2
$(RUN_LINUX) ./exchange-cli-linux --lambda-state state.bin --interactive


%.o: %.cpp $(wildcard *.h)
$(CXX) $(CXXFLAGS) -c $< -o $@

toolchain-env: INTERACTIVE =i
toolchain-env:
$(RUN_TOOLCHAIN) /bin/bash

linux-env: INTERACTIVE =i
linux-env:
$(RUN_LINUX) /bin/sh

clean:
rm -f $(OBJS) exchange-cli exchange-cli-emu exchange-cli-linux
73 changes: 73 additions & 0 deletions exchange/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Toy Exchange

## Build
```
make
```

## Run initializing state
```
dd if=/dev/zero of=lambda-state bs=1 count=8192
./exchange --interactive --init-state --setup-test-fixture --lambda-state ./lambda-state
```

## Run using existing state
```
./exchange --interactive --lambda-state ./lambda-state
```

## Run interactive tests
```
./exchange --interactive --lambda-state ./lambda-state
gold:exchange mpernambuco$ ./exchange --interactive --init-state --setup-test-fixture --lambda-state /Users/mpernambuco/ctsi2/hackaton/lambadex/exchange/lambda-state
init state....

> book ctsi/usdc
id tradr qty bid | ask qty tradr id
0 perna 40 111 | 112 50 diego 0
0 perna 50 110 | 120 100 diego 0
0 perna 100 100 |

> wallet perna
ctsi: 1000000
usdc: 980060

> wallet diego
ctsi: 999850
usdc: 1000000

> deposit perna brl 10
> wallet perna
brl: 10
ctsi: 1000000
usdc: 980060


> buy ctsi/brl perna 10 1
Execution - Trader: perna Order ID: 0 Execution Type: N Side: B Quantity: 1 Price: 10


> book ctsi/brl
id tradr qty bid | ask qty tradr id
0 perna 1 10 |

> sell ctsi/brl diego 10 1
Execution - Trader: diego Order ID: 0 Execution Type: N Side: S Quantity: 1 Price: 10
Execution - Trader: perna Order ID: 0 Execution Type: E Side: B Quantity: 1 Price: 10
Execution - Trader: diego Order ID: 0 Execution Type: E Side: S Quantity: 1 Price: 10
Enter command. Format is: (buy|sell|book) symbol trader price qty

> book ctsi/brl
id tradr qty bid | ask qty tradr id

> wallet perna
brl: 0
ctsi: 1000001
usdc: 980060

> wallet diego
brl: 10
ctsi: 999848
usdc: 1000000

```
Loading