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

[controller] add erc20 resources to controller #2485

Merged
merged 2 commits into from
Dec 15, 2024
Merged
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
33 changes: 33 additions & 0 deletions client/public/resource_addresses/local/resource_addresses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"STONE": [1, "0x10b3d48c8f0cb6a3d009d3548ab3db455d7fa86407c8869c8525d7f977cfedd"],
"COAL": [2, "0x5dfd0869838aa5a19c54a28717322df1c29c8e0d41a5c6ef126c003f9c29c5d"],
"WOOD": [3, "0x76b8390d8ba5d26423ea5af6ad31b62639ca4a5238e74db1e41dc67b8f57bcd"],
"COPPER": [4, "0x11f4b77a1e0aa2642730a38b4cea40ad4bea984379234bdb60cc41d9ebcd17f"],
"IRONWOOD": [5, "0x453e4a8802c423877beaa3f1ca0fcc3fcfcb543ff92e8b1355381bda4775d48"],
"OBSIDIAN": [6, "0x2c155f2164b68657d0ea8206fcb8c852e91dd5168126437708138d679e31877"],
"GOLD": [7, "0x54a6240d12444353c78a7f64d91e4f8eabc947b760b9369a05b3b154b68b503"],
"SILVER": [8, "0x35e191ed0abcf8270e1b4ae5c7121669ebdc675424dc669f74f8f32a62a61b1"],
"MITHRAL": [9, "0x2925a949eee52af5820a6ba2bdd9aae082ef46082f9a4e1edeb376ce61278a8"],
"ALCHEMICALSILVER": [10, "0x22ae713a2c06f3687f8a50823a16a8e86f49ed1505c43902849fd251e0f5adf"],
"COLDIRON": [11, "0x14e6946dbe9b530bd04d1c347ed695e6f49a163985d8a3410a8a1e1628178a4"],
"DEEPCRYSTAL": [12, "0x30617a0b90f2b04fb1c3e4d5f9e8baca60bde357cdceaa1ea136b1ee7312807"],
"RUBY": [13, "0x47286cdb7554cef18df9eba041164b450fbd1b860825cfa3db2621e4dc7d303"],
"DIAMONDS": [14, "0x18159aba74ae56043f653e5971be4339b0ddc1acdded44d4d1d5373cf7f5534"],
"HARTWOOD": [15, "0x4c7d2035de503300081c41c10dbec1cb2059e6291c7c9fda86610b4b8ecd579"],
"IGNIUM": [16, "0x62d79c2f5f0a7e1485f1948145197dee7f1e35443999daf55016ae39cad7810"],
"TWILIGHTQUARTZ": [17, "0x6cb29e0530292656a0e8ecf73fc04e00c8b68c58ff44a69cca2919cd804c528"],
"TRUEICE": [18, "0x10bc643e2a16bc214c6d3e27972263828e8a137fc634099255b7302eaf38a23"],
"ADAMANTINE": [19, "0x6c4c97ecee863c57c978952ca271065704c3b5ac7010962bdc9c97944c0bd37"],
"SAPPHIRE": [20, "0x70b0dbcbfafd50a1bd7602fd02fc6fcdfc48ece068603a016ffdafb968e2587"],
"ETHEREALSILICA": [21, "0x72fb88593bd78eb19f1514f6a2537b943d23db15ad067e85678f6e9a52345c5"],
"DRAGONHIDE": [22, "0xbeaedcc84e52b9f6197ad5cb5b441fac304afdd036438c208ae3928dd30de0"],
"DEMONHIDE": [28, "0x3f7d97969da364fd2ac0da5b6a877e6c4d4bfade4b3db50561abe02aac3ea48"],
"ANCIENTFRAGMENT": [29, "0x10cb48622578710d600c1d8bf4942035f573d8f1ee95c8a4d3d242139441dbe"],
"DONKEY": [249, "0x1d1542407040c2dce32b3ea35b06c627d4c7c0c6b133972fb903a71fc96156d"],
"KNIGHT": [250, "0x25cebc9ae778dc52ba67b722406586cbabcfd752dda5c3aa541313189617806"],
"CROSSBOWMAN": [251, "0x32ffb922c07155cad08b60a98ce2f660d25b818945d111dc184c7da31cd118f"],
"PALADIN": [252, "0x6910caf9920622e1e0c1ae7b210204268b48146a717e83353a1cf307e42c635"],
"WHEAT": [254, "0x360a3d398a2164b8404a69b2c6f0527c3d6cef480d776b0cf38bb6f531473ab"],
"FISH": [255, "0x613688e2001af02f4181d76587f3e8bb90b516985087b64ec0eaa866ec3ae29"],
"LORDS": [253, "0x4b2162f4b591fbf2052f0600652655675471a850656468dc3ddf57ad4fdbf56"]
}
32 changes: 32 additions & 0 deletions client/public/resource_addresses/mainnet/resource_addresses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"STONE": [1, "0x439a1c010e3e1bb2d43d43411000893c0042bd88f6c701611a0ea914d426da4"],
"COAL": [2, "0xce635e3f241b0ae78c46a929d84a9101910188f9c4024eaa7559556503c31a"],
"WOOD": [3, "0x40d8907cec0f7ae9c364dfb12485a1314d84c129bf1898d2f3d4b7fcc7d44f4"],
"COPPER": [4, "0x66ed5c928ee027a9419ace1cbea8389885161db5572a7c5c4fef2310e9bf494"],
"IRONWOOD": [5, "0x1720cf6318bff45e62acc588680ae3cd4d5f8465b1d52cb710533c9299b031a"],
"OBSIDIAN": [6, "0x3b6448d09dcd023507376402686261f5d6739455fa02f804907b066e488da66"],
"GOLD": [7, "0xdff9dca192609c4e86ab3be22c7ec1e968876c992d21986f3c542be97fa2f"],
"SILVER": [8, "0x6fe21d2d4a8a05bdb70f09c9250af9870020d5dcc35f410b4a39d6605c3e353"],
"MITHRAL": [9, "0x67ba235c569c23877064b2ac6ebd4d79f32d3c00f5fab8e28a3b5700b957f6"],
"ALCHEMICALSILVER": [10, "0x3956a5301e99522038a2e7dcb9c2a89bf087ffa79310ee0a508b5538efd8ddd"],
"COLDIRON": [11, "0x555d713e59d4ff96b7960447e9bc9e79bfdeab5b0eea74e3df81bce61cfbc77"],
"DEEPCRYSTAL": [12, "0x1d655ac834d38df7921074fc1588411e202b1af83307cbd996983aff52db3a8"],
"RUBY": [13, "0x3d9b66720959d0e7687b898292c10e62e78626f2dba5e1909961a2ce3f86612"],
"DIAMONDS": [14, "0xe03ea8ae385f64754820af5c01c36abf1b8130dd6797d3fd9d430e4114e876"],
"HARTWOOD": [15, "0x5620aa7170cd66dbcbc37d03087bfe4633ffef91d3e4d97b501de906004f79b"],
"IGNIUM": [16, "0x625c1f789b03ebebc7a9322366f38ebad1f693b84b2abd8cb8f5b2748b0cdd5"],
"TWILIGHTQUARTZ": [17, "0x35e24c02409c3cfe8d5646399a62c4d102bb782938d5f5180e92c9c62d3faf7"],
"TRUEICE": [18, "0x4485f5a6e16562e1c761cd348e63256d00389e3ddf4f5d98afe7ab44c57c481"],
"ADAMANTINE": [19, "0x367f838f85a2f5e1580d6f011e4476f581083314cff8721ba3dda9706076eed"],
"SAPPHIRE": [20, "0x2f8dd022568af8f9f718aa37707a9b858529db56910633a160456838b6cbcbc"],
"ETHEREALSILICA": [21, "0x68b6e23cbbd58a644700f55e96c83580921e9f521b6e5175396b53ba7910e7d"],
"DRAGONHIDE": [22, "0x3bf856515bece3c93f5061b7941b8645f817a0acab93c758b8c7b4bc0afa3c6"],
"ANCIENTFRAGMENT": [29, "0x0695b08ecdfdd828c2e6267da62f59e6d7543e690ef56a484df25c8566b332a5"],
"DONKEY": [249, "0x264be95a4a2ace20add68cb321acdccd2f9f8440ee1c7abd85da44ddab01085"],
"KNIGHT": [250, "0xac965f9e67164723c16735a9da8dbc9eb8e43b1bd0323591e87c056badf606"],
"CROSSBOWMAN": [251, "0x67e4ac00a241be06ba6afc11fa2715ec7da0c42c05a67ef6ecfcfeda725aaa8"],
"PALADIN": [252, "0x3bc86299bee061c7c8d7546ccb62b9daf9bffc653b1508facb722c6593874bc"],
"WHEAT": [254, "0x57a3f1ee475e072ce3be41785c0e889b7295d7a0dcc22b992c5b9408dbeb280"],
"FISH": [255, "0x27719173cfe10f1aa38d2aaed0a075b6077290f1e817aa3485d2b828394f4d9"],
"LORDS": [253, "0x0124aeb495b947201f5fac96fd1138e326ad86195b98df6dec9009158a533b49"]
}
33 changes: 33 additions & 0 deletions client/public/resource_addresses/sepolia/resource_addresses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"STONE": [1, "0x6158648e6c53488e316a9f681623b9e0359240329a00e5d5f5e82da21453b4c"],
"COAL": [2, "0xeff1980a4f28877118e06aff65d8c972ccc69c7d699975194ae8e83978b116"],
"WOOD": [3, "0x1605f515516e0ea60df3ae92a8204037866c3b27199865133f9f9f35208d298"],
"COPPER": [4, "0x76c0ee15cd5e588f0fa6ffb7c9ce301f96d33cd9f6bf0760ed2e55407130e4"],
"IRONWOOD": [5, "0x9f9cb4a6cb392f2c8526ae6c1f48ddb1eb67598030afdcabe06a48c752b3c6"],
"OBSIDIAN": [6, "0x53d73cddd205f590bf93290fcce62cb1079bd440e7253bef50ce2d890583c09"],
"GOLD": [7, "0x45ee17873d009cb7f8ad88bdd01218b07ceb055a8b22d6a0795028b2aafd8d8"],
"SILVER": [8, "0x56defcf99c674db261f508b584537c760b9001df9a8cb13275afa564f600dbe"],
"MITHRAL": [9, "0x7613ec3c16e4cefc9c33bea2b7e16614a0c4b5237e26cf75f335e0f028da988"],
"ALCHEMICALSILVER": [10, "0x705f42f2a89bf32c504c19c618ca6738747871016bba0b3fc32f3cd73782160"],
"COLDIRON": [11, "0x22bc712f59ceefc7c7521c607eb5f48759ffd906ee6342ee80dad2973f58d8"],
"DEEPCRYSTAL": [12, "0x242f3bcca7f72b5ca034dfe9903bb73024353cec795dca1fd8e2f500d17a733"],
"RUBY": [13, "0x9c473b26f7df7897b517f3c9e1ce8f1f7236b67666983f5ddaf039ca4c46a9"],
"DIAMONDS": [14, "0x78d34a4af3742f6f3377e97dd69f95680bb0f08521ad935428e3b2c3c5ed01e"],
"HARTWOOD": [15, "0x3d3f23f4829b8fe343b00c44c63828f5a81e468fb30eb9504c7c44455cead7b"],
"IGNIUM": [16, "0x204f9596fa6f05c2de88f3a7fe42c0d11fb04cc96128f18fbe112e44f360696"],
"TWILIGHTQUARTZ": [17, "0x1a0f1b62ef495956532db0306d13c92c02604995178d3fc98b84c8b8fb8a18e"],
"TRUEICE": [18, "0x3e089cd9be6c251dae20c64649a770507750f4b4c9e65ebf30bdb63b541605d"],
"ADAMANTINE": [19, "0x55399ca187391f4cc03abb00a04e3b6974bc0d7757ad959ac2804e6c1c468d5"],
"SAPPHIRE": [20, "0x62feb496f301b27bbda2389463d1ca06ef9f22d4208aecef2e85da1e4c90c4f"],
"ETHEREALSILICA": [21, "0x25eb12bb5298a2b14ceb9109086d330810d55c5e9a8c17e60fc956453b30d14"],
"DRAGONHIDE": [22, "0x463ebcc1b9ac911930a471fb9b608e9e7ca07ed81aab66f8889dd45b41c28b2"],
"DEMONHIDE": [28, "0x9613c7e4c04929c616a81f150006bc1a96bb77426851d300ef3dfaf2d0c318"],
"ANCIENTFRAGMENT": [29, "0x403c764fd76f926eec43218f46f79cfb5fe86dfeb1ae76010027697f65733f1"],
"DONKEY": [249, "0x6b6c4437543e46a8a80835fa50643fbd32275d3836396d8cd9c1d7c987f4ea9"],
"KNIGHT": [250, "0x298cba6b152d04fd54195dbae0e74512ff740d14946d2d65f26d0c039feffe2"],
"CROSSBOWMAN": [251, "0x116ce3b8c4a70f61e0131cc8681bc50a5eb35276e7985c07e3a508c2eb25f8f"],
"PALADIN": [252, "0x63f2da6f023bb0f281daa4011beb62ef501207569654e01d9315673471b8839"],
"WHEAT": [254, "0x2cbcad77a7b2bdf87c128f035beef05e3a007c15b2e332b1f1fb04d19ffd1b4"],
"FISH": [255, "0x3da1d29652bf12f64366ca4e9b36ec046cc92f6dc3b4675d956db1c3d742f0f"],
"LORDS": [253, "0x342ad5cc14002c005a5cedcfce2bd3af98d5e7fb79e9bf949b3a91cf145d72e"]
}
Comment on lines +1 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add schema validation for resource addresses

The JSON configuration contains critical contract addresses. Consider adding schema validation to ensure data integrity.

Example schema (create as resource-addresses.schema.json):

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "additionalProperties": false,
  "patternProperties": {
    "^[A-Z]+$": {
      "type": "array",
      "items": [
        { "type": "integer", "minimum": 1 },
        { "type": "string", "pattern": "^0x[a-f0-9]+$" }
      ],
      "minItems": 2,
      "maxItems": 2
    }
  },
  "required": ["LORDS"]
}

Also, add validation to the getSeasonAddresses function:

import Ajv from 'ajv';
import schema from './resource-addresses.schema.json';

export async function getSeasonAddresses() {
  const ajv = new Ajv();
  const validate = ajv.compile(schema);
  const data = await fetchAddresses();
  
  if (!validate(data)) {
    throw new Error('Invalid resource addresses format');
  }
  
  return data;
}

15 changes: 11 additions & 4 deletions client/src/hooks/context/starknet-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import React, { useCallback } from "react";

import { getSeasonAddresses } from "@/ui/utils/utils";
import ControllerConnector from "@cartridge/connector/controller";
import { ColorMode } from "@cartridge/controller";
import { mainnet, sepolia } from "@starknet-react/chains";
import { Connector, StarknetConfig, jsonRpcProvider, voyager } from "@starknet-react/core";
import React, { useCallback } from "react";
import { env } from "../../../env";
import { policies } from "./policies";
import { signingPolicy } from "./signing-policy";

const resourceAddresses = await getSeasonAddresses();

const LORDS = resourceAddresses["LORDS"][1];
const otherResources = Object.entries(resourceAddresses)
.filter(([key]) => key !== "LORDS")
.map(([_, [__, address]]) => address);

Comment on lines +11 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consolidate provider implementations to avoid duplication

This provider implementation duplicates logic from the landing provider. Consider extracting common functionality into a shared utility.

// src/shared/providers/resource-loader.ts
export function useResourceAddresses() {
  const [addresses, setAddresses] = useState<ResourceAddresses | null>(null);
  const [error, setError] = useState<Error | null>(null);

  useEffect(() => {
    async function load() {
      try {
        const data = await getSeasonAddresses();
        setAddresses(data);
      } catch (err) {
        setError(err as Error);
      }
    }
    load();
  }, []);

  return { addresses, error };
}

const preset: string = "eternum";
const theme: string = "eternum";
const slot: string = env.VITE_PUBLIC_SLOT;
Expand All @@ -28,7 +35,7 @@ const controller =
slot,
preset,
tokens: {
erc20: ["0x0124aeb495b947201f5fac96fd1138e326ad86195b98df6dec9009158a533b49"],
erc20: [LORDS, ...otherResources],
},
colorMode,
})
Expand All @@ -40,7 +47,7 @@ const controller =
policies: [...signingPolicy, ...policies, vrfPolicy],
theme,
tokens: {
erc20: ["0x0124aeb495b947201f5fac96fd1138e326ad86195b98df6dec9009158a533b49"],
erc20: [LORDS, ...otherResources],
},
colorMode,
});
Expand Down
23 changes: 23 additions & 0 deletions client/src/ui/utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { type ComponentValue } from "@dojoengine/recs";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import * as THREE from "three";
import { env } from "../../../env";
import { SortInterface } from "../elements/SortButton";

export { getEntityIdFromKeys };
Expand Down Expand Up @@ -501,6 +502,28 @@ export const adjustWonderLordsCost = (cost: ResourceCost[]): ResourceCost[] => {
return cost.map((item) => (item.resource === ResourcesIds.Lords ? { ...item, amount: item.amount * 0.1 } : item));
};

export const getSeasonAddressesPath = () => {
return `/resource_addresses/${env.VITE_PUBLIC_CHAIN}/resource_addresses.json`;
};
export const getJSONFile = async (filePath: string) => {
const response = await fetch(filePath);
const data = await response.json();
return data;
};
interface ResourceAddresses {
[key: string]: [number, string];
}

export const getSeasonAddresses = async (): Promise<ResourceAddresses> => {
try {
const path = getSeasonAddressesPath();
const data = await getJSONFile(path);
return data;
} catch (error) {
console.error("Error loading season addresses:", error);
return {};
}
}
Comment on lines +505 to +526
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Consolidate duplicated code into a shared utility.

This code is duplicated from landing/src/components/ui/utils/utils.ts. Consider moving these functions to a shared utility module to avoid maintenance issues and ensure consistent behavior.

  1. Create a new shared module (e.g., shared/utils/resource-addresses.ts)
  2. Move the following to the shared module:
    • ResourceAddresses interface
    • getSeasonAddressesPath
    • getJSONFile
    • getSeasonAddresses
  3. Import from the shared module in both locations

Would you like me to help create this shared module?

export const calculateDonkeysNeeded = (orderWeight: number): number => {
const configManager = ClientConfigManager.instance();

Expand Down
10 changes: 9 additions & 1 deletion landing/src/components/providers/starknet-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ import { mainnet, sepolia } from "@starknet-react/chains";
import { StarknetConfig, argent, braavos, jsonRpcProvider, useInjectedConnectors, voyager } from "@starknet-react/core";
import React, { useCallback } from "react";
import { env } from "../../../env";
import { getSeasonAddresses } from "../ui/utils/utils";
//import { cartridgeController } from "./cartridge-controller";

const resourceAddresses = await getSeasonAddresses();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid top-level await - move initialization into component

Using top-level await can cause issues in environments that don't support it. Consider moving the resource address initialization into the component.

-const resourceAddresses = await getSeasonAddresses();
+export function StarknetProvider({ children, onlyCartridge }: { children: React.ReactNode; onlyCartridge?: boolean }) {
+  const [resourceAddresses, setResourceAddresses] = useState<ResourceAddresses | null>(null);
+  
+  useEffect(() => {
+    async function initializeAddresses() {
+      try {
+        const addresses = await getSeasonAddresses();
+        setResourceAddresses(addresses);
+      } catch (error) {
+        console.error('Failed to load resource addresses:', error);
+      }
+    }
+    initializeAddresses();
+  }, []);
+
+  if (!resourceAddresses) {
+    return <div>Loading resources...</div>;
+  }

Committable suggestion skipped: line range outside the PR's diff.


const LORDS = resourceAddresses["LORDS"][1];
const otherResources = Object.entries(resourceAddresses)
.filter(([key]) => key !== "LORDS")
.map(([_, [__, address]]) => address);
Comment on lines +13 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add type safety and error handling for resource mapping

The current implementation assumes the presence of "LORDS" key and correct array structure. Consider adding type checking and error handling.

-const otherResources = Object.entries(resourceAddresses)
-  .filter(([key]) => key !== "LORDS")
-  .map(([_, [__, address]]) => address);
+const otherResources = Object.entries(resourceAddresses)
+  .filter(([key]) => key !== "LORDS")
+  .map(([_, value]) => {
+    if (!Array.isArray(value) || value.length < 2) {
+      console.warn(`Invalid resource format for ${key}`);
+      return null;
+    }
+    return value[1];
+  })
+  .filter((address): address is string => address !== null);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const otherResources = Object.entries(resourceAddresses)
.filter(([key]) => key !== "LORDS")
.map(([_, [__, address]]) => address);
const otherResources = Object.entries(resourceAddresses)
.filter(([key]) => key !== "LORDS")
.map(([_, value]) => {
if (!Array.isArray(value) || value.length < 2) {
console.warn(`Invalid resource format for ${key}`);
return null;
}
return value[1];
})
.filter((address): address is string => address !== null);


const theme: string = "eternum";
const slot: string = "realms-world-04";
const namespace: string = "eternum";
Expand All @@ -17,7 +25,7 @@ const cartridgeController = new ControllerConnector({
theme,
colorMode,
tokens: {
erc20: ["0x0124aeb495b947201f5fac96fd1138e326ad86195b98df6dec9009158a533b49"],
erc20: [LORDS, ...otherResources],
},
// namespace,
slot,
Expand Down
6 changes: 5 additions & 1 deletion landing/src/components/ui/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,11 @@ export const getJSONFile = async (filePath: string) => {
return data;
};

export const getSeasonAddresses = async () => {
interface ResourceAddresses {
[key: string]: [number, string];
}

export const getSeasonAddresses = async (): Promise<ResourceAddresses> => {
try {
const path = getSeasonAddressesPath();
const data = await getJSONFile(path);
Expand Down
Loading