Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to 0.4.2 #271

Closed
wants to merge 2 commits into from
Closed
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: 2 additions & 2 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ version = 1

[[package]]
name = "dojo"
version = "0.3.15"
source = "git+https://github.com/dojoengine/dojo.git?tag=v0.3.15#cdbaca4121b1642e1a8ca40d6831bad73d28ed26"
version = "0.4.2"
source = "git+https://github.com/dojoengine/dojo.git?tag=v0.4.2#3b8252dc64c2ec2c5df17f2922c5a5abab668cf3"
dependencies = [
"dojo_plugin",
]
Expand Down
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
cairo-version = "2.3.0"
cairo-version = "2.4.0"
name = "rollyourown"
version = "0.1.0"

Expand All @@ -8,7 +8,7 @@ sierra-replace-ids = true

[dependencies]
# cubit = {git = "https://github.com/influenceth/cubit.git"}
dojo = {git = "https://github.com/dojoengine/dojo.git", tag = "v0.3.15"}
dojo = {git = "https://github.com/dojoengine/dojo.git", tag = "v0.4.2"}
# dojo = {git = "https://github.com/dojoengine/dojo.git", rev = "d7f46502cba3d6462b68a4dfb07336377bca2678"}

[[target.dojo]]
Expand Down
12 changes: 6 additions & 6 deletions scripts/default_auth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ fi

export WORLD_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.world.address')

export LOBBY_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "lobby" ).address')
export TRAVEL_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "travel" ).address')
export DECIDE_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "decide" ).address')
export TRADE_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "trade" ).address')
export SHOP_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "shop" ).address')
export RYO_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "ryo" ).address')
export LOBBY_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "rollyourown::systems::lobby::lobby" ).address')
export TRAVEL_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "rollyourown::systems::travel::travel" ).address')
export DECIDE_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "rollyourown::systems::decide::decide" ).address')
export TRADE_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "rollyourown::systems::trade::trade" ).address')
export SHOP_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "rollyourown::systems::shop::shop" ).address')
export RYO_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "rollyourown::systems::ryo::ryo" ).address')

echo "---------------------------------------------------------------------------"
echo profile : $PROFILE
Expand Down
129 changes: 106 additions & 23 deletions web/bin/generateEvents.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,18 @@ if (process.argv.length !== 4) {
const jsonFilePath = path.resolve(process.argv[2]);
const jsFilePath = path.resolve(process.argv[3]);

// Extract recs package version
const { dependencies } = require(path.resolve("./package.json"));
const recsVersion = dependencies?.["@latticexyz/recs"] ?? "";
const isRecsVersion2 = /^[\^\~]?2./g.exec(recsVersion) != null;
console.log(`...generating for @latticexyz/recs version ${isRecsVersion2 ? '2 (bigint support, Entity as string)' : '1 (no bigint, EntityIndex as number)'}`)

const cairoToRecsType = {
"bool": "RecsType.Boolean",
"u8": "RecsType.Number",
"u16": "RecsType.Number",
"u32": "RecsType.Number",
"u64": "RecsType.Number",
"usize": "RecsType.Number",
"u128": isRecsVersion2 ? "RecsType.BigInt" : "RecsType.Number",
"u256": isRecsVersion2 ? "RecsType.BigInt" : "RecsType.NumberArray",
"felt252": isRecsVersion2 ? "RecsType.BigInt" : "RecsType.Number",
"ContractAddress": isRecsVersion2 ? "RecsType.String" : "RecsType.String",

const cairoToTypescriptType = {
"bool": "boolean",
"u8": "number",
"u16": "number",
"u32": "number",
"u64": "number",
"usize": "number",
"u128": "bigint",
"u256": "bigint",
"felt252": "string",
"ContractAddress": "string",
}


Expand All @@ -56,8 +51,9 @@ fs.readFile(jsonFilePath, "utf8", (err, jsonString) => {
const cairoType = member.type.substring(member.type.lastIndexOf('::') + 2)
return {
name: member.name,
kind: member.kind,
cairoType,
type: cairoToRecsType[cairoType] || "RecsType.String"
type: cairoToTypescriptType[cairoType] || "String"
}
})

Expand All @@ -83,19 +79,103 @@ fs.readFile(jsonFilePath, "utf8", (err, jsonString) => {
const eventsEnum = `export enum WorldEvents {\n ${eventsMeta.map(e => `${e.name}= "${e.selector}",\n`).join("")
}};\n\n`

const eventsData = eventsMeta.map(e => `export interface ${e.name}Data{
${e.members.map(m => `${m.name}: ${m.type}`).join('\n')}
}\n\n`).join("")
const baseEventData = `
export interface BaseEventData {
game_id: number;
event_type: WorldEvents;
event_name: string;
}\n\n\n`

const eventsData = eventsMeta.map(e => {
if (e.name === 'Upgraded') return
return `export interface ${e.name}Data extends BaseEventData {
${e.members.map(m => `${m.name}: ${m.type};`).join('\n')}
}\n\n`
}).join("")


const parseEvents = `
export const parseAllEvents = (receipt: GetTransactionReceiptResponse) => {
if (receipt.status === "REJECTED") {
throw new Error(\`transaction REJECTED\`);
}
if (receipt.status === "REVERTED") {
throw new Error(\`transaction REVERTED\`);
}

const flatEvents = parseEvents(receipt as SuccessfulTransactionReceiptResponse)
return flatEvents
}

export const parseEvents = (receipt: SuccessfulTransactionReceiptResponse) => {
const parsed = receipt.events.map(e => parseEvent(e))
return parsed
}

export type ParseEventResult = ReturnType<typeof parseEvent>;\n\n
`

let parseEventInner = eventsMeta.map(event => {
if (event.name === 'Upgraded') return

let keyCount = 1 // first key is selector
let dataCount = 0
let code = `case WorldEvents.${event.name}:\n`;
code += `return {\n`
code += `event_type: WorldEvents.${event.name},\n`
code += `event_name: "${event.name}",\n`
for (let member of event.members) {
code += `${member.name}: `
const prop = member.kind === "key" ? `raw.keys[${keyCount++}]` : `raw.data[${dataCount++}]`
switch (member.type) {
case "boolean":
code += `${prop} === "0x0" ? false : true,\n`
break;
case "number":
code += `Number(${prop}),\n`
break;
case "bigint":
code += `BigInt(${prop}),\n`
break;
default:
code += `num.toHexString(${prop}),\n`
break;
}
}
code += `} as ${event.name}Data;\n`
return code
}).join("\n")

parseEventInner += `\n
default:
return {
gameId: undefined,
event_type: raw.keys[0],
event_name: raw.keys[0],
}
break;\n\n`


const parseEvent = `
export const parseEvent = (raw: any) => {
switch (raw.keys[0]) {
${parseEventInner}
}
}\n\n
`

// console.log(eventsEnum)
// console.log(eventsData)

let fileContent = `/* Autogenerated file. Do not edit manually. */\n\n`;
fileContent += `import { Type as RecsType } from "@latticexyz/recs";\n`;
fileContent += `import { GetTransactionReceiptResponse, Contract } from "starknet";\n\n`;
fileContent += `import {num, GetTransactionReceiptResponse, InvokeTransactionReceiptResponse, SuccessfulTransactionReceiptResponse, Contract } from "starknet";\n\n`;

fileContent += eventsEnum;
fileContent += baseEventData;
fileContent += eventsData;
fileContent += parseEvents;
fileContent += parseEvent;


fs.writeFile(jsFilePath, fileContent, (err) => {
if (err) {
Expand All @@ -108,3 +188,6 @@ fs.readFile(jsonFilePath, "utf8", (err, jsonString) => {
console.log("Error parsing JSON string:", err);
}
});



Loading
Loading