Skip to content

Commit

Permalink
feat(typescript): new typescript template
Browse files Browse the repository at this point in the history
  • Loading branch information
tuler committed Sep 1, 2023
1 parent 816bc70 commit a25598f
Show file tree
Hide file tree
Showing 7 changed files with 410 additions and 0 deletions.
3 changes: 3 additions & 0 deletions typescript/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.sunodo
/dist
/node_modules
3 changes: 3 additions & 0 deletions typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.sunodo
/dist
/node_modules
42 changes: 42 additions & 0 deletions typescript/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# syntax=docker.io/docker/dockerfile:1.4

# build stage: includes resources necessary for installing dependencies

# Here the image's platform does not necessarily have to be riscv64.
# If any needed dependencies rely on native binaries, you must use
# a riscv64 image such as cartesi/node:19-jammy for the build stage,
# to ensure that the appropriate binaries will be generated.
FROM node:20.5.1-bullseye as build-stage

WORKDIR /opt/cartesi/dapp
COPY . .
RUN yarn install && yarn build

# runtime stage: produces final image that will be executed

# Here the image's platform MUST be linux/riscv64.
# Give preference to small base images, which lead to better start-up
# performance when loading the Cartesi Machine.
FROM --platform=linux/riscv64 cartesi/node:20.5.1-jammy-slim

LABEL io.sunodo.sdk_version=0.2.0
LABEL io.cartesi.rollups.ram_size=128Mi

ARG MACHINE_EMULATOR_TOOLS_VERSION=0.12.0
RUN <<EOF
apt-get update
apt-get install -y --no-install-recommends busybox-static=1:1.30.1-7ubuntu3 ca-certificates=20230311ubuntu0.22.04.1 curl=7.81.0-1ubuntu1.13
curl -fsSL https://github.com/cartesi/machine-emulator-tools/releases/download/v${MACHINE_EMULATOR_TOOLS_VERSION}/machine-emulator-tools-v${MACHINE_EMULATOR_TOOLS_VERSION}.tar.gz \
| tar -C / --overwrite -xvzf -
rm -rf /var/lib/apt/lists/*
EOF

ENV PATH="/opt/cartesi/bin:${PATH}"

WORKDIR /opt/cartesi/dapp
COPY --from=build-stage /opt/cartesi/dapp/dist .

ENV ROLLUP_HTTP_SERVER_URL="http://127.0.0.1:5004"

ENTRYPOINT ["rollup-init"]
CMD ["node", "index.js"]
4 changes: 4 additions & 0 deletions typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# TypeScript DApp Template

This is a template for TypeScript Cartesi DApps. It uses node to execute the backend application.
The application entrypoint is the `src/index.ts` file. It is bundled with [esbuild](https://esbuild.github.io), but any bundler can be used.
20 changes: 20 additions & 0 deletions typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "my-dapp",
"version": "0.1.0",
"description": "TypeScript DApp",
"dependencies": {
"ethers": "^6.7.1"
},
"devDependencies": {
"esbuild": "^0.19.2",
"ts-node": "^10.9.1"
},
"scripts": {
"build": "esbuild ./src/index.ts --bundle --outfile=dist/index.js --platform=node --target=node20",
"start": "ROLLUP_HTTP_SERVER_URL=\"http://127.0.0.1:5004\" ts-node src/index.ts"
},
"keywords": [
"cartesi"
],
"license": "MIT"
}
45 changes: 45 additions & 0 deletions typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// XXX even though ethers is not used in the code below, it's very likely
// it will be used by any DApp, so we are already including it here
import ethers from "ethers";

const rollup_server = process.env.ROLLUP_HTTP_SERVER_URL;
console.log("HTTP rollup_server url is " + rollup_server);

async function handle_advance(data) {
console.log("Received advance request data " + JSON.stringify(data));
return "accept";
}

async function handle_inspect(data) {
console.log("Received inspect request data " + JSON.stringify(data));
return "accept";
}

const handlers = {
advance_state: handle_advance,
inspect_state: handle_inspect,
};

const finish = { status: "accept" };

(async () => {
while (true) {
const finish_req = await fetch(rollup_server + "/finish", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ status: "accept" }),
});

console.log("Received finish status " + finish_req.status);

if (finish_req.status == 202) {
console.log("No pending rollup request, trying again");
} else {
const rollup_req = await finish_req.json();
const handler = handlers[rollup_req["request_type"]];
finish["status"] = await handler(rollup_req["data"]);
}
}
})();
Loading

0 comments on commit a25598f

Please sign in to comment.