From aca9d2fb81169ec0f064a99586671b9c87106f54 Mon Sep 17 00:00:00 2001 From: starknetdev Date: Wed, 4 Oct 2023 17:30:57 +0100 Subject: [PATCH] - migrate constant addresses to .env - create more get functions based on NEXT setting --- ui/.env | 16 ++++++--- ui/src/app/hooks/useContracts.tsx | 20 ++++-------- ui/src/app/lib/burner.tsx | 4 +-- ui/src/app/lib/constants.ts | 54 ++++++++++++++++++++----------- ui/src/app/template.tsx | 8 +++-- 5 files changed, 61 insertions(+), 41 deletions(-) diff --git a/ui/.env b/ui/.env index 481c9e338..ecc5a1a6f 100644 --- a/ui/.env +++ b/ui/.env @@ -1,7 +1,15 @@ -NEXT_PUBLIC_NETWORK=dev -NEXT_PUBLIC_ACCOUNT_CLASS_HASH=0x0715b5e10bf63c36e69c402a81e1eb96b9107ef56eb5e821b00893e39bdcf545 -NEXT_PUBLIC_ETH_CONTRACT_ADDRESS=0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7 +NEXT_PUBLIC_NETWORK=goerli +NEXT_PUBLIC_GOERLI_ARCADE_ACCOUNT_CLASS_HASH=0x0715b5e10bf63c36e69c402a81e1eb96b9107ef56eb5e821b00893e39bdcf545 +NEXT_PUBLIC_MAINNET_ARCADE_ACCOUNT_CLASS_HASH=0x0 +NEXT_PUBLIC_GOERLI_ETH_CONTRACT_ADDRESS=0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7 +NEXT_PUBLIC_MAINNET_ETH_CONTRACT_ADDRESS=0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7 +NEXT_PUBLIC_GOERLI_GAME_CONTRACT_ADDRESS=0x01263ecbc05e28d1e99f531894838db10b90cfcdd39d020642da1747a733a37a +NEXT_PUBLIC_MAINNET_GAME_CONTRACT_ADDRESS=0x0 +NEXT_PUBLIC_GOERLI_LORDS_CONTRACT_ADDRESS=0x059dac5df32cbce17b081399e97d90be5fba726f97f00638f838613d088e5a47 +NEXT_PUBLIC_MAINNET_LORDS_CONTRACT_ADDRESS=0x0124aeb495b947201f5fac96fd1138e326ad86195b98df6dec9009158a533b49 NEXT_PUBLIC_ADMIN_ADDRESS=0x020b96923a9e60f63a1829d440a03cf680768cadbc8fe737f71380258817d85b NEXT_PUBLIC_ADMIN_PRIVATE_KEY=0x01253fc8c5f3e6cd420089afe09fcfc55d4f459ae676129615c1cb0249ea3b69 NEXT_PUBLIC_RPC_MAINNET_ENDPOINT=https://starknet-mainnet.infura.io/v3/6c536e8272f84d3ba63bf9f248c5e128 -NEXT_PUBLIC_RPC_GOERLI_ENDPOINT=https://starknet-goerli.infura.io/v3/6c536e8272f84d3ba63bf9f248c5e128 \ No newline at end of file +NEXT_PUBLIC_RPC_GOERLI_ENDPOINT=https://starknet-goerli.infura.io/v3/6c536e8272f84d3ba63bf9f248c5e128 +NEXT_PUBLIC_GOERLI_APP_URL=https://goerli-survivor.realms.world/ +NEXT_PUBLIC_MAINNET_APP_URL=https://goerli-survivor.realms.world/ \ No newline at end of file diff --git a/ui/src/app/hooks/useContracts.tsx b/ui/src/app/hooks/useContracts.tsx index 35f2b1983..19dd36c10 100644 --- a/ui/src/app/hooks/useContracts.tsx +++ b/ui/src/app/hooks/useContracts.tsx @@ -1,7 +1,7 @@ -import { useContract, useAccount } from "@starknet-react/core"; +import { useContract } from "@starknet-react/core"; import Adventurer from "../abi/Adventurer.json"; import Lords_ERC20_Mintable from "../abi/Lords_ERC20_Mintable.json"; -import { contracts, rpc_addr } from "../lib/constants"; +import { getContracts } from "../lib/constants"; const ethBalanceABIFragment = [ { @@ -65,28 +65,20 @@ const ethBalanceABIFragment = [ ]; export const useContracts = () => { - const { account } = useAccount(); + const contracts = getContracts(); const { contract: gameContract } = useContract({ - address: - (account as any)?.provider?.baseUrl == rpc_addr || - (account as any)?.baseUrl == rpc_addr - ? contracts.mainnet.game - : contracts.goerli.game, + address: contracts?.game, abi: Adventurer, }); const { contract: lordsContract } = useContract({ - address: - (account as any)?.provider?.baseUrl == rpc_addr || - (account as any)?.baseUrl == rpc_addr - ? contracts.mainnet.lords_erc20_mintable - : contracts.goerli.lords_erc20_mintable, + address: contracts?.lords, abi: Lords_ERC20_Mintable, }); const { contract: ethContract } = useContract({ + address: contracts?.eth, abi: ethBalanceABIFragment, - address: process.env.NEXT_PUBLIC_ETH_CONTRACT_ADDRESS!, }); return { diff --git a/ui/src/app/lib/burner.tsx b/ui/src/app/lib/burner.tsx index 9b791b8f6..25a5a0a6e 100644 --- a/ui/src/app/lib/burner.tsx +++ b/ui/src/app/lib/burner.tsx @@ -140,7 +140,7 @@ export const useBurner = () => { const address = hash.calculateContractAddressFromHash( publicKey, - process.env.NEXT_PUBLIC_ACCOUNT_CLASS_HASH!, + process.env.NEXT_PUBLIC_GOERLI_ARCADE_ACCOUNT_CLASS_HASH!, constructorAACalldata, 0 ); @@ -158,7 +158,7 @@ export const useBurner = () => { transaction_hash: deployTx, contract_address: accountAAFinalAdress, } = await burner.deployAccount({ - classHash: process.env.NEXT_PUBLIC_ACCOUNT_CLASS_HASH!, + classHash: process.env.NEXT_PUBLIC_GOERLI_ARCADE_ACCOUNT_CLASS_HASH!, constructorCalldata: constructorAACalldata, contractAddress: address, addressSalt: publicKey, diff --git a/ui/src/app/lib/constants.ts b/ui/src/app/lib/constants.ts index 5fe1d8cbd..bd79fcbcf 100644 --- a/ui/src/app/lib/constants.ts +++ b/ui/src/app/lib/constants.ts @@ -10,35 +10,53 @@ export const chapter4 = "They find golden coins in their pocket, glimmering in the dim light - an enigma wrapped in the shroud of the unexpected."; export const battle = "A beast lurks in the shadow, prepare for battle!"; -export const rpc_addr = - process.env.NEXT_PUBLIC_RPC_GOERLI_ENDPOINT || - "https://alpha-mainnet.starknet.io"; +export function getRPCUrl() { + switch (process.env.NEXT_PUBLIC_NETWORK) { + case "goerli": + return process.env.NEXT_PUBLIC_RPC_GOERLI_ENDPOINT; + case "mainnet": + return process.env.NEXT_PUBLIC_RPC_MAINNET_ENDPOINT; + default: + return "http://localhost:8000/graphql"; + } +} export function getGraphQLUrl() { switch (process.env.NEXT_PUBLIC_NETWORK) { - case "dev": + case "goerli": return "https://survivor-indexer.realms.world/goerli-graphql"; - case "production": + case "mainnet": return "https://survivor-indexer.realms.world/graphql"; default: return "http://localhost:8000/graphql"; } } -export const contracts = { - mainnet: { - game: "0x0", - lords_erc20_mintable: - "0x067e87cea28bfd9314a1d3c41fb26a58ca1346ff0ea2452e59b9eeb2828692dc", - }, - goerli: { - game: "0x01263ecbc05e28d1e99f531894838db10b90cfcdd39d020642da1747a733a37a", - lords_erc20_mintable: - "0x059dac5df32cbce17b081399e97d90be5fba726f97f00638f838613d088e5a47", - }, -}; +export function getContracts() { + switch (process.env.NEXT_PUBLIC_NETWORK) { + case "goerli": + return { + eth: process.env.NEXT_PUBLIC_GOERLI_ETH_CONTRACT_ADDRESS, + game: process.env.NEXT_PUBLIC_GOERLI_GAME_CONTRACT_ADDRESS, + lords: process.env.NEXT_PUBLIC_GOERLI_GAME_CONTRACT_ADDRESS, + }; + case "mainnet": + return { + eth: process.env.NEXT_PUBLIC_MAINNET_ETH_CONTRACT_ADDRESS, + game: process.env.NEXT_PUBLIC_MAINNET_GAME_CONTRACT_ADDRESS, + lords: process.env.NEXT_PUBLIC_MAINNET_LORDS_CONTRACT_ADDRESS, + }; + } +} -export const appUrl = "https://survivor.realms.world/"; +export function getAppUrl() { + switch (process.env.NEXT_PUBLIC_NETWORK) { + case "goerli": + return process.env.NEXT_PUBLIC_GOERLI_APP_URL; + case "mainnet": + return process.env.NEXT_PUBLIC_MAINNET_APP_URL; + } +} export const notificationAnimations = [ { name: "idle", startFrame: 0, frameCount: 4 }, diff --git a/ui/src/app/template.tsx b/ui/src/app/template.tsx index 20fa3b3c1..bafe78fa9 100644 --- a/ui/src/app/template.tsx +++ b/ui/src/app/template.tsx @@ -4,14 +4,16 @@ import { Provider } from "starknet"; import { useBurner } from "./lib/burner"; import { connectors } from "./lib/connectors"; import { StarknetConfig } from "@starknet-react/core"; -import { rpc_addr } from "./lib/constants"; +import { getRPCUrl } from "./lib/constants"; export default function Template({ children }: { children: React.ReactNode }) { const { listConnectors } = useBurner(); + const rpc_addr = getRPCUrl(); + const provider = new Provider({ - rpc: { nodeUrl: rpc_addr }, - sequencer: { baseUrl: rpc_addr }, + rpc: { nodeUrl: rpc_addr! }, + sequencer: { baseUrl: rpc_addr! }, }); return (