Skip to content

Commit

Permalink
get asset ID automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahschwartz committed Apr 12, 2024
1 parent f78ada6 commit d5912ab
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 14 deletions.
3 changes: 3 additions & 0 deletions contract/src/abi_structs.sw
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ abi GameContract {
#[storage(read, write)]
fn new_player();

// get asset ID
fn get_asset_id() -> AssetId;

// level up farming skill
#[storage(read, write)]
fn level_up();
Expand Down
4 changes: 4 additions & 0 deletions contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ impl GameContract for Contract {
mint_to(sender, DEFAULT_SUB_ID, 1_000_000_000);
}

fn get_asset_id() -> AssetId {
AssetId::default()
}

#[storage(read, write)]
fn level_up() {
// get the player with the message sender
Expand Down
21 changes: 18 additions & 3 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
FARM_COIN_ASSET,
FARM_COIN_ASSET_ID,
FUEL_PROVIDER_URL,
VERCEL_ENV,
} from './constants';
import './App.css';
import { ContractAbi__factory } from './sway-api';
Expand All @@ -26,6 +27,7 @@ function App() {
const [burnerWallet, setBurnerWallet] = useState<Wallet>();
const [mounted, setMounted] = useState<boolean>(false);
const [isMobile, setIsMobile] = useState(false);
const [farmCoinAssetID, setFarmCoinAssetId] = useState<string | null>(FARM_COIN_ASSET_ID);
const { isConnected } = useIsConnected();
const { wallet } = useWallet();
const { assets } = useAssets();
Expand All @@ -40,18 +42,20 @@ function App() {
useEffect(() => {
async function getAccounts() {
if (mounted) {
if(VERCEL_ENV === 'development'){
let hasAsset = false;
for (let i = 0; i < assets.length; i++) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const thisAsset = assets[i] as any;
if (thisAsset.assetId && thisAsset.assetId === FARM_COIN_ASSET_ID) {
if (thisAsset.assetId && thisAsset.assetId === farmCoinAssetID) {
hasAsset = true;
break;
}
}
if (!hasAsset) {
addAssets([FARM_COIN_ASSET]);
}
}
} else {
setMounted(true);
}
Expand Down Expand Up @@ -87,10 +91,21 @@ function App() {
return null;
}, [wallet, burnerWallet]);

useEffect(() => {
async function getAssetId() {
if(contract){
const { value } = await contract.functions.get_asset_id().get();
console.log("VALUE:", value)
setFarmCoinAssetId(value.value);
}
}
getAssetId();
}, [contract]);

return (
<Box css={styles.root}>
{isConnected || (contract && burnerWallet) ? (
<Game contract={contract} isMobile={isMobile} />
{(isConnected || (contract && burnerWallet)) && farmCoinAssetID ? (
<Game contract={contract} isMobile={isMobile} farmCoinAssetID={farmCoinAssetID} />
) : (
<BoxCentered css={styles.box}>
<BoxCentered css={styles.innerBox}>
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/components/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { KeyboardControlsEntry } from '@react-three/drei';
import { KeyboardControls } from '@react-three/drei';
import { Canvas } from '@react-three/fiber';
import { BN } from 'fuels';
import type { BytesLike } from 'fuels';
import { useState, useEffect, useMemo, Suspense } from 'react';

import type { Modals } from '../constants';
Expand Down Expand Up @@ -31,6 +32,7 @@ import Info from './show/Info';
interface GameProps {
contract: ContractAbi | null;
isMobile: boolean;
farmCoinAssetID: BytesLike;
}

export type Position =
Expand All @@ -42,7 +44,7 @@ export type Position =
| 'right-bottom';
export type MobileControls = 'none' | 'up' | 'down' | 'left' | 'right';

export default function Game({ contract, isMobile }: GameProps) {
export default function Game({ contract, isMobile, farmCoinAssetID }: GameProps) {
const [modal, setModal] = useState<Modals>('none');
const [tileStates, setTileStates] = useState<
GardenVectorOutput | undefined
Expand Down Expand Up @@ -188,6 +190,7 @@ export default function Game({ contract, isMobile }: GameProps) {
updateNum={updateNum}
seeds={seeds}
items={items}
farmCoinAssetID={farmCoinAssetID}
/>

{player !== null && (
Expand Down Expand Up @@ -217,6 +220,7 @@ export default function Game({ contract, isMobile }: GameProps) {
updatePageNum={updatePageNum}
items={items}
setCanMove={setCanMove}
farmCoinAssetID={farmCoinAssetID}
/>
)}
</>
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/components/modals/BuySeeds.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Button, Spinner, BoxCentered } from '@fuel-ui/react';
import { bn } from 'fuels';
import type { BytesLike } from 'fuels';
import type { Dispatch, SetStateAction } from 'react';
import { useState } from 'react';

import {
FARM_COIN_ASSET_ID,
buttonStyle,
FoodTypeInput,
} from '../../constants';
Expand All @@ -14,12 +14,14 @@ interface BuySeedsProps {
contract: ContractAbi | null;
updatePageNum: () => void;
setCanMove: Dispatch<SetStateAction<boolean>>;
farmCoinAssetID: BytesLike;
}

export default function BuySeeds({
contract,
updatePageNum,
setCanMove,
farmCoinAssetID,
}: BuySeedsProps) {
const [status, setStatus] = useState<'error' | 'none' | `loading`>('none');

Expand All @@ -36,7 +38,7 @@ export default function BuySeeds({
await contract.functions
.buy_seeds(seedType, inputAmount)
.callParams({
forward: [price, FARM_COIN_ASSET_ID],
forward: [price, farmCoinAssetID],
})
.txParams({ gasPrice: 1, gasLimit: 800_000 })
.call();
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/components/modals/MarketModal.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { BytesLike } from 'fuels';
import type { Dispatch, SetStateAction } from 'react';

import type { ContractAbi } from '../../sway-api';
Expand All @@ -10,20 +11,23 @@ interface MarketModalProps {
updatePageNum: () => void;
items: number;
setCanMove: Dispatch<SetStateAction<boolean>>;
farmCoinAssetID: BytesLike;
}

export default function MarketModal({
contract,
updatePageNum,
items,
setCanMove,
farmCoinAssetID
}: MarketModalProps) {
return (
<div className="market-modal">
<BuySeeds
contract={contract}
updatePageNum={updatePageNum}
setCanMove={setCanMove}
farmCoinAssetID={farmCoinAssetID}
/>
{items > 0 && (
<SellItem
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/components/show/Info.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { cssObj } from '@fuel-ui/css';
import { Box } from '@fuel-ui/react';
import type { BytesLike } from 'fuels';

import type {
ContractAbi,
Expand All @@ -16,6 +17,7 @@ interface InfoProps {
updateNum: number;
seeds: number;
items: number;
farmCoinAssetID: BytesLike;
}

export default function Info({
Expand All @@ -24,6 +26,7 @@ export default function Info({
updateNum,
seeds,
items,
farmCoinAssetID
}: InfoProps) {
return (
<Box css={styles.container}>
Expand All @@ -33,6 +36,7 @@ export default function Info({
player={player}
contract={contract}
updateNum={updateNum}
farmCoinAssetID={farmCoinAssetID}
/>
</Box>
<Inventory seeds={seeds} items={items} />
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/components/show/ShowCoins.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { cssObj } from '@fuel-ui/css';
import { Box } from '@fuel-ui/react';
import { useWallet } from '@fuels/react';
import type { BN } from 'fuels';
import type { BN, BytesLike } from 'fuels';
import { useState, useEffect } from 'react';

import { FARM_COIN_ASSET_ID } from '../../constants';
import type { ContractAbi } from '../../sway-api';

interface ShowCoinsProps {
updateNum: number;
contract: ContractAbi | null;
farmCoinAssetID: BytesLike;
}

export default function ShowCoins({ updateNum, contract }: ShowCoinsProps) {
export default function ShowCoins({ updateNum, contract, farmCoinAssetID }: ShowCoinsProps) {
const { wallet } = useWallet();
const [balance, setBalance] = useState<BN>();

useEffect(() => {
async function getBalance() {
const thisWallet = wallet ?? contract?.account;
const balanceBN = await thisWallet!.getBalance(FARM_COIN_ASSET_ID);
const balanceBN = await thisWallet!.getBalance(farmCoinAssetID);
setBalance(balanceBN);
}
getBalance();
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/components/show/ShowPlayerInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { cssObj } from '@fuel-ui/css';
import { Flex, Box } from '@fuel-ui/react';
import type { BytesLike } from 'fuels';

import type {
ContractAbi,
Expand All @@ -12,12 +13,14 @@ interface PlayerProps {
player: PlayerOutput | null;
contract: ContractAbi | null;
updateNum: number;
farmCoinAssetID: BytesLike;
}

export default function ShowPlayerInfo({
player,
contract,
updateNum,
farmCoinAssetID
}: PlayerProps) {
let valSold;
if (player !== null) {
Expand All @@ -28,7 +31,7 @@ export default function ShowPlayerInfo({
<Box css={styles.playerInfo}>
<Flex direction={'column'} justify="space-around">
<Box css={styles.box}>Value Sold: {valSold ?? '0'}</Box>
<ShowCoins contract={contract} updateNum={updateNum} />
<ShowCoins contract={contract} updateNum={updateNum} farmCoinAssetID={farmCoinAssetID} />
</Flex>
</Box>
);
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const CONTRACT_ID =
export const BASE_ASSET_ID =
'0x0000000000000000000000000000000000000000000000000000000000000000';

export const FARM_COIN_ASSET_ID =
export const FARM_COIN_ASSET_ID = VERCEL_ENV === 'development'
? null :
'0x0cfabde7bbe58d253cf3103d8f55d26987b3dc4691205b9299ac6826c613a2e2';

export const FARM_COIN_NETWORK_ASSET = {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/sway-api/contract-ids.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"contract": "0xec1c12168029e397aac29333369e656ff284f25c6d68524b480a363a1d8d0c5e"
"contract": "0x2fae96500bc2252846e8508f8a56fac6dd5969df0cb137fb1ce86a83188fe14a"
}
4 changes: 4 additions & 0 deletions frontend/src/sway-api/contracts/ContractAbi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ interface ContractAbiInterface extends Interface {
buy_seeds_free: FunctionFragment;
can_harvest: FunctionFragment;
can_level_up: FunctionFragment;
get_asset_id: FunctionFragment;
get_garden_vec: FunctionFragment;
get_item_amount: FunctionFragment;
get_player: FunctionFragment;
Expand All @@ -64,6 +65,7 @@ interface ContractAbiInterface extends Interface {
encodeFunctionData(functionFragment: 'buy_seeds_free', values: [FoodTypeInput, BigNumberish]): Uint8Array;
encodeFunctionData(functionFragment: 'can_harvest', values: [BigNumberish]): Uint8Array;
encodeFunctionData(functionFragment: 'can_level_up', values: [IdentityInput]): Uint8Array;
encodeFunctionData(functionFragment: 'get_asset_id', values: []): Uint8Array;
encodeFunctionData(functionFragment: 'get_garden_vec', values: [IdentityInput]): Uint8Array;
encodeFunctionData(functionFragment: 'get_item_amount', values: [IdentityInput, FoodTypeInput]): Uint8Array;
encodeFunctionData(functionFragment: 'get_player', values: [IdentityInput]): Uint8Array;
Expand All @@ -79,6 +81,7 @@ interface ContractAbiInterface extends Interface {
decodeFunctionData(functionFragment: 'buy_seeds_free', data: BytesLike): DecodedValue;
decodeFunctionData(functionFragment: 'can_harvest', data: BytesLike): DecodedValue;
decodeFunctionData(functionFragment: 'can_level_up', data: BytesLike): DecodedValue;
decodeFunctionData(functionFragment: 'get_asset_id', data: BytesLike): DecodedValue;
decodeFunctionData(functionFragment: 'get_garden_vec', data: BytesLike): DecodedValue;
decodeFunctionData(functionFragment: 'get_item_amount', data: BytesLike): DecodedValue;
decodeFunctionData(functionFragment: 'get_player', data: BytesLike): DecodedValue;
Expand All @@ -98,6 +101,7 @@ export class ContractAbi extends Contract {
buy_seeds_free: InvokeFunction<[food_type: FoodTypeInput, amount: BigNumberish], void>;
can_harvest: InvokeFunction<[index: BigNumberish], boolean>;
can_level_up: InvokeFunction<[id: IdentityInput], boolean>;
get_asset_id: InvokeFunction<[], AssetIdOutput>;
get_garden_vec: InvokeFunction<[id: IdentityInput], GardenVectorOutput>;
get_item_amount: InvokeFunction<[id: IdentityInput, item: FoodTypeInput], BN>;
get_player: InvokeFunction<[id: IdentityInput], Option<PlayerOutput>>;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/sway-api/contracts/ContractAbi.hex.ts

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions frontend/src/sway-api/contracts/factories/ContractAbi__factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,16 @@ const _abi = {
}
]
},
{
"inputs": [],
"name": "get_asset_id",
"output": {
"name": "",
"type": 12,
"typeArguments": null
},
"attributes": null
},
{
"inputs": [
{
Expand Down

0 comments on commit d5912ab

Please sign in to comment.