Skip to content

Commit

Permalink
- migrate constant addresses to .env
Browse files Browse the repository at this point in the history
- create more get functions based on NEXT setting
  • Loading branch information
starknetdev committed Oct 4, 2023
1 parent 07d77f1 commit aca9d2f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 41 deletions.
16 changes: 12 additions & 4 deletions ui/.env
Original file line number Diff line number Diff line change
@@ -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
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/
20 changes: 6 additions & 14 deletions ui/src/app/hooks/useContracts.tsx
Original file line number Diff line number Diff line change
@@ -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 = [
{
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions ui/src/app/lib/burner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -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,
Expand Down
54 changes: 36 additions & 18 deletions ui/src/app/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
8 changes: 5 additions & 3 deletions ui/src/app/template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit aca9d2f

Please sign in to comment.