Skip to content

Commit

Permalink
feat: executeBackgroundTx
Browse files Browse the repository at this point in the history
  • Loading branch information
singyiu committed Oct 22, 2023
1 parent 061e1ea commit 2785b2f
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 43 deletions.
56 changes: 52 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,7 +263,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 depositDaiToLapuVaultForTheConnectedPlayer = async (amount) => {
Expand All @@ -253,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 @@ -270,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 @@ -283,6 +320,7 @@ export function createSystemCalls(
};

const mudMockYieldGenerationFromDeFiPool = async (amount) => {
console.log("mudMockYieldGenerationFromDeFiPool", amount);
const tx = await worldContract.write.mockYieldGenerationFromDeFiPool([
amount,
]);
Expand Down Expand Up @@ -313,12 +351,21 @@ export function createSystemCalls(
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 @@ -343,5 +390,6 @@ export function createSystemCalls(
mudMockReleaseRewardToPlayer,
lapuVaultGetTotalSupply,
mockLapuVaultFundPlayer,
mudDefiDistributeRewardsToPlayers,
};
}
81 changes: 42 additions & 39 deletions packages/client/src/mudExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,22 @@ export const MudExample = () => {
components: { Counter, GameSetting },
systemCalls: {
mudDefiDaiBalanceOf,
mudMockDaiFaucet,
approveDaiToLapuVaultForTheConnectedPlayer,
approveLapuToMudWorldForTheConnectedPlayer,
depositDaiToLapuVaultForTheConnectedPlayer,
withdrawDaiFromLapuVaultForTheConnectedPlayer,
mudDefiLapuBalanceOf,
mudDefiConsumesLapuFromPlayer,
mudDefiGetTotalRewardBalance,
//mudMockYieldGenerationFromDeFiPool,
//mudMockReleaseRewardToPlayer,
mudMockYieldGenerationFromDeFiPool,
lapuVaultGetTotalSupply,
mockLapuVaultFundPlayer,
mudDefiDistributeRewardsToPlayers,
},
} = useMUD();

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

const counter = useComponentValue(Counter, singletonEntity);
const gameSetting = useComponentValue(GameSetting, singletonEntity);
Expand All @@ -41,6 +39,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 +62,7 @@ export const MudExample = () => {
setLapuVaultTvl(lapuVaultTvl_);
};

const refreshDataIntervalId = setInterval(refreshData, 1000);
const refreshDataIntervalId = setInterval(refreshData, 3000);
return () => {
clearInterval(refreshDataIntervalId);
};
Expand All @@ -74,25 +74,30 @@ export const MudExample = () => {
lapuVaultGetTotalSupply,
]);

/*
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();
}
}, 30000);
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 @@ -125,17 +130,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 @@ -166,17 +166,9 @@ export const MudExample = () => {
defaultConsumeAmount
)
);
await delay(1000);
console.log(
"mudDefiConsumesLapuFromPlayer:",
await mudDefiConsumesLapuFromPlayer(
defaultConsumeAmount,
playerAddress
)
);
}}
>
consumeLapu
approveLapu
</button>
<button
type="button"
Expand All @@ -193,6 +185,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>
);
};

0 comments on commit 2785b2f

Please sign in to comment.