Skip to content

Commit

Permalink
Merge pull request #45 from Cygnusfear/sing/feat-mudresourcesystem-02
Browse files Browse the repository at this point in the history
Sing/feat mudresourcesystem 02
  • Loading branch information
singyiu authored Oct 22, 2023
2 parents 1399b48 + 46be15f commit 31fb99f
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 62 deletions.
9 changes: 9 additions & 0 deletions packages/client/src/components/ui/tutorialModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { getState } from "@/game/store";
import { useState, useEffect } from "react";
import { useSpring, animated, config } from "@react-spring/web";

import { useMUD } from "@/useMUD";

function TutorialModal({
step,
screenIndex,
Expand Down Expand Up @@ -75,6 +77,11 @@ function TutorialModal({
}

const Tutorial = () => {
const {
systemCalls: { mockLapuVaultFundPlayer },
} = useMUD();
const playerAddress = getState().player?.playerData?.address;

const [currentTutorial, setCurrentTutorial] = useState<
TutorialStep | undefined
>(undefined);
Expand Down Expand Up @@ -119,6 +126,8 @@ const Tutorial = () => {
} else {
setScreenIndex(screenIndex + 1);
}

mockLapuVaultFundPlayer(playerAddress);
}}
/>
)}
Expand Down
72 changes: 68 additions & 4 deletions packages/client/src/mud/createSystemCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ export function createSystemCalls(
EntityType,
OwnedBy,
GameSetting,
EntityTypeDetail,
}: ClientComponents
) {
const defaultVector3 = new Vector3(1, 0, 3);

const delay = async (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};

const increment = async () => {
/*
* Because IncrementSystem
Expand Down Expand Up @@ -92,6 +97,14 @@ export function createSystemCalls(
return res;
};

const mudGetLapuBuildingCost = async (entityTypeId: number) => {
const entityTypeDetail = await getComponentValue(
EntityTypeDetail,
entityTypeId
);
return entityTypeDetail?.buildingCostLapu || 400;
};

const mudBuildFacility = async (
entityTypeId: number = 10,
x: number = defaultVector3.x,
Expand All @@ -101,6 +114,18 @@ export function createSystemCalls(
color: string = "#ff00ff",
variant: number = 0
) => {
//approve mud world to spend LAPU for building
const buildingCostLapu = await mudGetLapuBuildingCost(entityTypeId);
console.log(
"mudBuildFacility approveLapuToMudWorldForTheConnectedPlayer",
buildingCostLapu
);
await approveLapuToMudWorldForTheConnectedPlayer(buildingCostLapu);
console.log(
"mudBuildFacility approveLapuToMudWorldForTheConnectedPlayer done"
);

delay(1000);
const tx = await worldContract.write.buildFacility([
entityTypeId,
x,
Expand Down Expand Up @@ -222,7 +247,10 @@ export function createSystemCalls(
account: walletClient?.account,
});
const res = await walletClient.writeContract(request);
return res;
const transaction = await publicClient.waitForTransactionReceipt({
hash: res,
});
return transaction;
};

const approveLapuToMudWorldForTheConnectedPlayer = async (amount) => {
Expand All @@ -235,11 +263,19 @@ export function createSystemCalls(
account: walletClient?.account,
});
const res = await walletClient.writeContract(request);
return res;
const transaction = await publicClient.waitForTransactionReceipt({
hash: res,
});
return transaction;
};

const depositDaiToLapuVaultForTheConnectedPlayer = async (amount) => {
const gameSetting = await getComponentValue(GameSetting, singletonEntity);
console.log("gameSetting?.lapuVaultAddress", gameSetting?.lapuVaultAddress);
console.log(
"depositDaiToLapuVaultForTheConnectedPlayer walletClient?.account.address",
walletClient?.account.address
);
const { request } = await publicClient.simulateContract({
address: gameSetting?.lapuVaultAddress,
abi: ILapuVaultAbi,
Expand All @@ -248,7 +284,10 @@ export function createSystemCalls(
account: walletClient?.account,
});
const res = await walletClient.writeContract(request);
return res;
const transaction = await publicClient.waitForTransactionReceipt({
hash: res,
});
return transaction;
};

const withdrawDaiFromLapuVaultForTheConnectedPlayer = async (amount) => {
Expand All @@ -265,7 +304,10 @@ export function createSystemCalls(
account: walletClient?.account,
});
const res = await walletClient.writeContract(request);
return res;
const transaction = await publicClient.waitForTransactionReceipt({
hash: res,
});
return transaction;
};

const mudDefiConsumesLapuFromPlayer = async (amount, playerAddress) => {
Expand All @@ -278,6 +320,7 @@ export function createSystemCalls(
};

const mudMockYieldGenerationFromDeFiPool = async (amount) => {
console.log("mudMockYieldGenerationFromDeFiPool", amount);
const tx = await worldContract.write.mockYieldGenerationFromDeFiPool([
amount,
]);
Expand All @@ -304,6 +347,25 @@ export function createSystemCalls(
return data;
};

const mockLapuVaultFundPlayer = async (playerAddress, amount = 1000) => {
console.log("mockLapuVaultFundPlayer start", playerAddress, amount);
await mudMockDaiFaucet(playerAddress, amount);
console.log("mudMockDaiFaucet done");
delay(1000);
await approveDaiToLapuVaultForTheConnectedPlayer(amount);
console.log("approveDaiToLapuVaultForTheConnectedPlayer done");
delay(1000);
await depositDaiToLapuVaultForTheConnectedPlayer(amount);
console.log("mockLapuVaultFundPlayer done", playerAddress, amount);
};

const mudDefiDistributeRewardsToPlayers = async () => {
console.log("mudDefiDistributeRewardsToPlayers");
const tx = await worldContract.write.defiDistributeRewardsToPlayers();
await waitForTransaction(tx);
return tx;
};

return {
increment,
mudGetEntityType,
Expand All @@ -327,5 +389,7 @@ export function createSystemCalls(
mudMockYieldGenerationFromDeFiPool,
mudMockReleaseRewardToPlayer,
lapuVaultGetTotalSupply,
mockLapuVaultFundPlayer,
mudDefiDistributeRewardsToPlayers,
};
}
76 changes: 41 additions & 35 deletions packages/client/src/mudExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@ export const MudExample = () => {
components: { Counter, GameSetting },
systemCalls: {
mudDefiDaiBalanceOf,
mudMockDaiFaucet,
approveDaiToLapuVaultForTheConnectedPlayer,
approveLapuToMudWorldForTheConnectedPlayer,
depositDaiToLapuVaultForTheConnectedPlayer,
withdrawDaiFromLapuVaultForTheConnectedPlayer,
mudDefiLapuBalanceOf,
mudDefiConsumesLapuFromPlayer,
mudDefiGetTotalRewardBalance,
mudMockYieldGenerationFromDeFiPool,
mudMockReleaseRewardToPlayer,
lapuVaultGetTotalSupply,
mockLapuVaultFundPlayer,
mudDefiDistributeRewardsToPlayers,
},
} = useMUD();

const defaultTestAmount = 1000;
const defaultConsumeAmount = 100;
const defaultConsumeAmount = 400;
const defaultYieldAmount = 50;
const defaultRewardAmount = 30;
const backgroundTxIntervalTime = 20000;

const counter = useComponentValue(Counter, singletonEntity);
const gameSetting = useComponentValue(GameSetting, singletonEntity);
Expand All @@ -41,6 +40,8 @@ export const MudExample = () => {
null
);
const [lapuVaultTvl, setLapuVaultTvl] = useState<bigint | null>(0);
const [backgroundTxEnabled, setBackgroundTxEnabled] =
useState<boolean>(false);

useEffect(() => {
const refreshData = async () => {
Expand All @@ -62,7 +63,7 @@ export const MudExample = () => {
setLapuVaultTvl(lapuVaultTvl_);
};

const refreshDataIntervalId = setInterval(refreshData, 1000);
const refreshDataIntervalId = setInterval(refreshData, 3000);
return () => {
clearInterval(refreshDataIntervalId);
};
Expand All @@ -75,22 +76,29 @@ export const MudExample = () => {
]);

useEffect(() => {
const generateYieldIntervalId = setInterval(() => {
mudMockYieldGenerationFromDeFiPool(defaultYieldAmount);
}, 10000);
return () => {
clearInterval(generateYieldIntervalId);
const executeBackgroundTx = async () => {
const rewardBalance = (await mudDefiGetTotalRewardBalance()) as number;
if (rewardBalance > 0) {
await mudDefiDistributeRewardsToPlayers();
} else {
await mudMockYieldGenerationFromDeFiPool(defaultYieldAmount);
}
};
}, [mudMockYieldGenerationFromDeFiPool]);

useEffect(() => {
const rewardPlayerIntervalId = setInterval(() => {
mudMockReleaseRewardToPlayer(playerAddress, defaultRewardAmount);
}, 15000);
const backgroundTxIntervalId = setInterval(() => {
if (backgroundTxEnabled) {
executeBackgroundTx();
}
}, backgroundTxIntervalTime);
return () => {
clearInterval(rewardPlayerIntervalId);
clearInterval(backgroundTxIntervalId);
};
}, [playerAddress, mudMockReleaseRewardToPlayer]);
}, [
backgroundTxEnabled,
mudDefiGetTotalRewardBalance,
mudMockYieldGenerationFromDeFiPool,
mudDefiDistributeRewardsToPlayers,
]);

const delay = async (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
Expand Down Expand Up @@ -123,17 +131,12 @@ export const MudExample = () => {
onClick={async (event) => {
event.preventDefault();
console.log(
"mudMockDaiFaucet:",
await mudMockDaiFaucet(playerAddress, defaultTestAmount)
"mockLapuVaultFundPlayer:",
await mockLapuVaultFundPlayer(playerAddress)
);
const playerDaiBalance_ = (await mudDefiDaiBalanceOf(
playerAddress
)) as number;
console.log("playerDaiBalance_:", playerDaiBalance_);
setPlayerDaiBalance(playerDaiBalance_);
}}
>
getDaiFromFaucet
FundPlayer
</button>
<button
type="button"
Expand Down Expand Up @@ -164,17 +167,9 @@ export const MudExample = () => {
defaultConsumeAmount
)
);
await delay(1000);
console.log(
"mudDefiConsumesLapuFromPlayer:",
await mudDefiConsumesLapuFromPlayer(
defaultConsumeAmount,
playerAddress
)
);
}}
>
consumeLapu
approveLapu
</button>
<button
type="button"
Expand All @@ -191,6 +186,17 @@ export const MudExample = () => {
>
swapLapuToDai
</button>
<button
type="button"
className="px-100 py-100 rounded-md border border-gray-300 bg-white text-base font-medium text-gray-700 shadow-sm hover:bg-gray-50"
onClick={async (event) => {
event.preventDefault();
console.log("toggle backgroundTxEnabled from:", backgroundTxEnabled);
setBackgroundTxEnabled(!backgroundTxEnabled);
}}
>
{backgroundTxEnabled ? "Disable Background Tx" : "Enable Background Tx"}
</button>
</div>
);
};
21 changes: 21 additions & 0 deletions packages/contracts/mud.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default mudConfig({
defiPoolAddress: "address",
daiAddress: "address",
lapuVaultAddress: "address",
totalResidence: "uint256",
totalRewarded: "uint256",
},
},
Expand All @@ -39,6 +40,21 @@ export default mudConfig({
typeId: "uint32",
},
},
EntityTypeDetail: {
keySchema: {
entityTypeId: "uint32",
},
valueSchema: {
buildingCostLapu: "uint256",
residence: "uint256",
},
},
PlayerDataDetail: {
valueSchema: {
residence: "uint256",
rewarded: "uint256",
},
},
OwnedBy: {
valueSchema: {
owner: "address",
Expand All @@ -56,5 +72,10 @@ export default mudConfig({
root: true,
args: [resolveTableId("Position")],
},
{
name: "KeysInTableModule",
root: true,
args: [resolveTableId("PlayerDataDetail")],
},
],
});
2 changes: 2 additions & 0 deletions packages/contracts/script/PostDeploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ contract PostDeploy is Script {
address(lapuVault)
);

IWorld(worldAddress).facilitySystemSetupEntityTypeDetails();

vm.stopBroadcast();
}
}
Loading

0 comments on commit 31fb99f

Please sign in to comment.