diff --git a/packages/client/src/mud/createSystemCalls.ts b/packages/client/src/mud/createSystemCalls.ts index d4fb0c8..7212992 100644 --- a/packages/client/src/mud/createSystemCalls.ts +++ b/packages/client/src/mud/createSystemCalls.ts @@ -6,8 +6,15 @@ import { Hex } from "viem"; import { SetupNetworkResult } from "./setupNetwork"; +import eventEmitter from '../utils/eventEmitter'; +import { message } from 'antd'; + +import { delay } from '../utils/delay'; + export type SystemCalls = ReturnType; +let wait = false; + export function createSystemCalls( /* * The parameter list informs TypeScript that: @@ -28,29 +35,415 @@ export function createSystemCalls( * syncToRecs * (https://github.com/latticexyz/mud/blob/main/templates/react/packages/client/src/mud/setupNetwork.ts#L77-L83). */ - { tables, useStore, worldContract, waitForTransaction }: SetupNetworkResult, + { tables, useStore, worldContract, waitForTransaction, publicClient }: SetupNetworkResult ) { - const addTask = async (label: string) => { - const tx = await worldContract.write.app__addTask([label]); - await waitForTransaction(tx); - }; - const toggleTask = async (id: Hex) => { - const isComplete = (useStore.getState().getValue(tables.Tasks, { id })?.completedAt ?? 0n) > 0n; - const tx = isComplete - ? await worldContract.write.app__resetTask([id]) - : await worldContract.write.app__completeTask([id]); - await waitForTransaction(tx); - }; + const getBlockNumber = async (tx: any) => { + const receipt = await publicClient.getTransactionReceipt({ hash: tx }); + return receipt.blockNumber.toString() + } - const deleteTask = async (id: Hex) => { - const tx = await worldContract.write.app__deleteTask([id]); - await waitForTransaction(tx); + const transfer = async (addr: any, transferData: any) => { + try { + const tx = await worldContract.write.transfer([addr, ...transferData]); + await waitForTransaction(tx); + } catch (error) { + message.error(error.cause.reason || error.cause.details); + } + } + + const move = async (steps: any) => { + let time = new Date().getTime() + let log = { + time, + msg: `move to ${steps[steps.length - 1][0]}, ${steps[steps.length - 1][1]}` + } + eventEmitter.emit('log', log) + try { + const tx = await worldContract.write.move([steps]); + await waitForTransaction(tx); + const receipt = await publicClient.getTransactionReceipt({ hash: tx }); + // let receipt = r.receipt + log.block = await getBlockNumber(tx) + eventEmitter.emit('log', log) + } catch (error) { + console.log('move', error); + log.type = 'error' + log.msg = 'move:' + error.cause.reason || error.cause.details + eventEmitter.emit('log', log) + console.log('move', error); + message.error(error.cause.reason || error.cause.details); + } }; + const selectBothNFT = async (userTokenId: any, lootTokenId: any, address: any) => { + try { + const tx = await worldContract.write.selectBothNFT([userTokenId, lootTokenId]); + await waitForTransaction(tx); + await delay(300) + return { + playerData: useStore.getState().getValue(tables.PlayerParams, { addr: address }), + lootData: useStore.getState().getValue(tables.LootList1, { addr: address }) + } + } catch (error) { + console.log('selectUserNft', error); + message.error(error.cause.reason || error.cause.details); + return { + type: 'error' + } + } + } + + const setInfo = async (name: any, url: any) => { + console.log('setInfo', new Date().getTime()); + try { + const tx = await worldContract.write.setInfo([name]); + await waitForTransaction(tx); + console.log('setInfo success', new Date().getTime(), tx); + return tx + } catch (error) { + console.log('setInfo', error); + message.error(error.cause.reason || error.cause.details); + } + } + + const joinBattlefield = async () => { + console.log('joinBattlefield', new Date().getTime()); + let time = new Date().getTime() + let log = { + time, + msg: `join battlefield` + } + eventEmitter.emit('log', log) + try { + const tx = await worldContract.write.joinBattlefield(); + let r = await waitForTransaction(tx); + console.log('joinBattlefield success', new Date().getTime(), tx); + // let receipt = r.receipt + log.block = await getBlockNumber(tx) + eventEmitter.emit('log', log) + return tx + } catch (error) { + log.type = 'error' + log.msg = 'joinBattlefield:' + error.cause.reason || error.cause.details + eventEmitter.emit('log', log) + console.log('joinBattlefield', error); + } + } + + const CreateBox = async (x: any, y: any) => { + try { + const tx = await worldContract.write.CreateBox([x, y]); + await waitForTransaction(tx); + } catch (error) { + console.log('CreateBox', error); + message.error(error.cause.reason || error.cause.details); + } + } + + const openBox = async (boxId: any) => { + if (wait) return + wait = true + let time = new Date().getTime() + let log = { + time, + msg: `open box` + } + eventEmitter.emit('log', log) + try { + const tx = await worldContract.write.openBox([boxId]); + await waitForTransaction(tx); + log.block = await getBlockNumber(tx) + eventEmitter.emit('log', log) + wait = false + } catch (error) { + log.type = 'error' + log.msg = 'openBox:' + error.cause.reason || error.cause.details + eventEmitter.emit('log', log) + console.log('openBox', error); + wait = false + } + } + + const revealBox = async (boxId: any) => { + if (wait) return + wait = true + console.log('revealBox', new Date().getTime()); + let time = new Date().getTime() + let log = { + time, + msg: `reveal box` + } + eventEmitter.emit('log', log) + try { + const tx = await worldContract.write.revealBox([boxId]); + await waitForTransaction(tx); + log.block = await getBlockNumber(tx) + eventEmitter.emit('log', log) + console.log('revealBox success', new Date().getTime(), tx); + wait = false + await delay(300) + return useStore.getState().getValue(tables.BoxList, { boxId }) + } catch (error) { + console.log(error) + log.msg = 'revealBox:' + error.cause.reason || error.cause.details + eventEmitter.emit('log', log) + console.log('revealBox', error); + wait = false + } + } + + const getCollections = async (boxId: any, oreAmount: any, treasureAmount: any) => { + if (wait) return + wait = true + let time = new Date().getTime() + let log = { + time, + msg: `get collections ${oreAmount} Gems` + } + eventEmitter.emit('log', log) + try { + const tx = await worldContract.write.getCollections([boxId, oreAmount, treasureAmount]); + await waitForTransaction(tx); + log.block = await getBlockNumber(tx) + eventEmitter.emit('log', log) + wait = false + await delay(300) + return { + type: 'success' + } + } catch (error) { + log.type = 'error' + log.msg = 'getCollections:' + error.cause.reason || error.cause.details + eventEmitter.emit('log', log) + console.log('getCollections', error); + wait = false + return { + type: 'error' + } + } + } + + const battleInvitation = async (addr: any, steps: any) => { + if (wait) return + wait = true + let time = new Date().getTime() + let log = { + time, + msg: `battle invitation with ${addr}` + } + eventEmitter.emit('log', log) + try { + const tx = await worldContract.write.battleInvitation([addr, steps]); + await waitForTransaction(tx); + log.block = await getBlockNumber(tx) + eventEmitter.emit('log', log) + wait = false + return tx + } catch (error) { + log.type = 'error' + log.msg = 'battleInvitation:' + error.cause.reason || error.cause.details + eventEmitter.emit('log', log) + console.log('battleInvitation', error); + wait = false + } + } + + const confirmBattle = async (buffHash: any, battleId: any) => { + if (wait) return + wait = true + let time = new Date().getTime() + let log = { + time, + msg: `confirm battle` + } + eventEmitter.emit('log', log) + try { + const tx = await worldContract.write.confirmBattle([buffHash, battleId]); + await waitForTransaction(tx); + log.block = await getBlockNumber(tx) + eventEmitter.emit('log', log) + wait = false + await delay(300) + return { + type: 'success', + data: useStore.getState().getValue(tables.BattleList, { battleId }) + } + } catch (error) { + log.type = 'error' + log.msg = 'confirmBattle:' + error.cause.reason || error.cause.details + eventEmitter.emit('log', log) + console.log('confirmBattle', error); + wait = false + return { + type: 'error', + msg: error.cause.reason || error.cause.details || error.cause + } + } + } + + const revealBattle = async (battleId: any, action: any, arg: any, nonce: any) => { + if (wait) return + wait = true + let time = new Date().getTime() + let log = { + time, + msg: `reveal battle` + } + eventEmitter.emit('log', log) + try { + const tx = await worldContract.write.revealBattle([battleId, action, arg, nonce]) + await waitForTransaction(tx) + log.block = await getBlockNumber(tx) + eventEmitter.emit('log', log) + wait = false + await delay(300) + return { + type: 'success', + data: useStore.getState().getValue(tables.BattleList, { battleId }) + } + } catch (error) { + log.type = 'error' + log.msg = 'revealBattle:' + error.cause.reason || error.cause.details + eventEmitter.emit('log', log) + console.log('revealBattle', error); + wait = false + return { + type: 'error', + msg: error.cause.reason || error.cause.details || error.cause + } + } + } + + const forceEnd = async (battleId: any) => { + if (wait) return + wait = true + let time = new Date().getTime() + let log = { + time, + msg: `force end` + } + eventEmitter.emit('log', log) + try { + const tx = await worldContract.write.forceEnd([battleId]); + await waitForTransaction(tx); + log.block = await getBlockNumber(tx) + eventEmitter.emit('log', log) + wait = false + await delay(300) + return useStore.getState().getValue(tables.BattleList, { battleId }) + } catch (error) { + log.type = 'error' + log.msg = 'forceEnd:' + error.cause.reason || error.cause.details + eventEmitter.emit('log', log) + console.log('forceEnd', error); + wait = false + return { + type: 'error' + } + } + } + + const unlockUserLocation = async () => { + let time = new Date().getTime() + let log = { + time, + msg: `unlock user location` + } + eventEmitter.emit('log', log) + try { + const tx = await worldContract.write.unlockUserLocation(); + await waitForTransaction(tx); + log.block = await getBlockNumber(tx) + eventEmitter.emit('log', log) + console.log('unlockUserLocation success', new Date().getTime(), tx); + return tx + } catch (error) { + log.type = 'error' + log.msg = 'unlockUserLocation:' + error.cause.reason || error.cause.details + eventEmitter.emit('log', log) + console.log('unlockUserLocation', error); + } + } + + const goHome = async () => { + let time = new Date().getTime() + let log = { + time, + msg: `go home` + } + eventEmitter.emit('log', log) + try { + const tx = await worldContract.write.goHome(); + await waitForTransaction(tx); + log.block = await getBlockNumber(tx) + eventEmitter.emit('log', log) + return tx + } catch (error) { + log.type = 'error' + log.msg = 'goHome:' + error.cause.reason || error.cause.details + eventEmitter.emit('log', log) + console.log('goHome', error); + } + } + + const submitGem = async () => { + let time = new Date().getTime() + let log = { + time, + msg: `submit gem` + } + eventEmitter.emit('log', log) + try { + const tx = await worldContract.write.submitGem(); + await waitForTransaction(tx); + log.block = await getBlockNumber(tx) + eventEmitter.emit('log', log) + console.log('submitGem success', new Date().getTime(), tx); + return tx + } catch (error) { + log.type = 'error' + log.msg = 'submitGem:' + error.cause.reason || error.cause.details + eventEmitter.emit('log', log) + console.log('submitGem', error); + } + } + + const initUserInfo = async (addr) => { + if (wait) return + wait = true + console.log('initUserInfo', new Date().getTime()); + try { + const tx = await worldContract.write.initUserInfo([addr]); + await waitForTransaction(tx); + console.log('initUserInfo success', new Date().getTime(), tx); + wait = false + return tx + } catch (error) { + console.log('initUserInfo', error); + message.error(error.cause.reason || error.cause.details); + wait = false + } + } + + return { - addTask, - toggleTask, - deleteTask, + move, + selectBothNFT, + setInfo, + joinBattlefield, + CreateBox, + openBox, + revealBox, + getCollections, + battleInvitation, + confirmBattle, + revealBattle, + forceEnd, + unlockUserLocation, + goHome, + submitGem, + transfer, + initUserInfo }; } diff --git a/packages/client/src/pages/home/header/index.tsx b/packages/client/src/pages/home/header/index.tsx index c53e626..609ee7b 100644 --- a/packages/client/src/pages/home/header/index.tsx +++ b/packages/client/src/pages/home/header/index.tsx @@ -38,13 +38,11 @@ const HomeHeader = (props: IProps) => { let rpc = network.walletClient?.chain?.rpcUrls?.default?.http[0] || 'http://127.0.0.1:8545' let provider = new ethers.providers.JsonRpcProvider(rpc) let wallet = new ethers.Wallet(PRIVATE_KEY, provider) - console.log(wallet, 'wallet') let transferGas = TRANSFER_GAS[network.walletClient?.chain?.id || 31337] wallet.sendTransaction({ to, value: ethers.utils.parseEther(transferGas) }).then(res => { - console.log(res, 'res') transfering = false getBalance() }).catch(err => {