Skip to content

Commit

Permalink
feat: show march
Browse files Browse the repository at this point in the history
  • Loading branch information
noyyyy committed Apr 14, 2024
1 parent 4359457 commit d7b2c59
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 36 deletions.
1 change: 1 addition & 0 deletions packages/client/src/Web3Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const config = createConfig(
[arbitrumSepolia.id]: http("https://arbitrum-sepolia-rpc.publicnode.com"),
[bscTestnet.id]: http("https://bsc-testnet-rpc.publicnode.com"),
[baseSepolia.id]: http("https://base-sepolia-rpc.publicnode.com"),
[sepolia.id]: http("https://ethereum-sepolia-rpc.publicnode.com"),
},

// Required API Keys
Expand Down
48 changes: 33 additions & 15 deletions packages/client/src/abis/Troy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ export const TroyAbi = [
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: "bytes32",
name: "messageId",
type: "bytes32",
},
{
indexed: true,
internalType: "uint256",
Expand Down Expand Up @@ -340,6 +346,25 @@ export const TroyAbi = [
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [
{
internalType: "bytes32",
name: "messageId",
type: "bytes32",
},
],
name: "isMessageIdFulFilled",
outputs: [
{
internalType: "bool",
name: "",
type: "bool",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "owner",
Expand Down Expand Up @@ -384,21 +409,14 @@ export const TroyAbi = [
name: "readStorageValue",
outputs: [
{
components: [
{
internalType: "uint256",
name: "championChainId",
type: "uint256",
},
{
internalType: "uint256",
name: "soldierAmount",
type: "uint256",
},
],
internalType: "struct Troy.TroyStorage",
name: "",
type: "tuple",
internalType: "uint256",
name: "championChainId",
type: "uint256",
},
{
internalType: "uint256",
name: "soldierAmount",
type: "uint256",
},
],
stateMutability: "view",
Expand Down
63 changes: 59 additions & 4 deletions packages/client/src/component/March.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { useWatchContractEvent } from "wagmi";
import { TrojanHorseAddress } from "../constant";
import { usePublicClient, useWatchContractEvent } from "wagmi";
import { TrojanHorseAddress, TroyAddress } from "../constant";
import { TrojanHorseAbi } from "../abis/TrojanHorse";
import { useState } from "react";
import { useEffect, useState } from "react";
import { parseAbiItem } from "viem";
import { TroyAbi } from "../abis/Troy";
import { sepolia } from "viem/chains";

export function March({
chainId,
Expand All @@ -10,12 +13,59 @@ export function March({
chainId: number;
className: string;
}) {
const client = usePublicClient({ chainId: chainId });
const sepoliaClient = usePublicClient({ chainId: sepolia.id });

const [inComing, setInComing] = useState<
{
messageId: string;
soldiers: number;
}[]
>([]);

useEffect(() => {
async function fetchOldEvent() {
const blockNumber = await client!.getBlockNumber();

const logs = await client!.getLogs({
address: TrojanHorseAddress[chainId],
event: parseAbiItem(
"event HorseSent(bytes32 indexed messageId, uint256 soldierAmount, uint256 fees)"
),
fromBlock: blockNumber - 1800n,
toBlock: blockNumber,
});

setInComing(
(
await Promise.all(
logs.map(async (log) => {
const fulfilled = await sepoliaClient?.readContract({
address: TroyAddress,
abi: TroyAbi,
functionName: "isMessageIdFulFilled",
args: [log.args.messageId!],
});

if (fulfilled) {
return undefined;
}
return {
messageId: log.args.messageId!,
soldiers: Number(log.args.soldierAmount!),
};
})
)
).filter(Boolean) as {
messageId: string;
soldiers: number;
}[]
);
}

fetchOldEvent();
}, []);

useWatchContractEvent({
address: TrojanHorseAddress[chainId],
abi: TrojanHorseAbi,
Expand All @@ -34,14 +84,19 @@ export function March({
chainId: chainId,
poll: true,
pollingInterval: 10000,
strict: true,
});

return (
<div
className={`absolute bg-black h-1 transform flex flex-row ${className}`}
>
{inComing.map((i) => (
<a id={i.messageId} href={`https://ccip.chain.link/msg/${i.messageId}`}>
<a
className="m-2"
id={i.messageId}
href={`https://ccip.chain.link/msg/${i.messageId}`}
>
{i.soldiers}
</a>
))}
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/hooks/useChampion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function useChampion() {
chainId: sepolia.id,
});

const champion = troyStorage.data?.championChainId;
const championSoldiersAmount = troyStorage.data?.soldierAmount;
const champion = troyStorage.data?.[0];
const championSoldiersAmount = troyStorage.data?.[1];
return { champion, championSoldiersAmount };
}
23 changes: 21 additions & 2 deletions packages/contracts/deployments/sepolia/Troy.json
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,25 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "messageId",
"type": "bytes32"
}
],
"name": "isMessageIdFulFilled",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
Expand Down Expand Up @@ -539,8 +558,8 @@
"0x2B4899c7316cc7d15301dbD0A5D7204456D3B441",
"0x485cc955000000000000000000000000ba45b3d7a42c3554fa98bdc3f790da2676cb05600000000000000000000000000bf3de8c5d3e8a2b34d2beeb17abfcebaf363a59"
],
"numDeployments": 2,
"numDeployments": 3,
"bytecode": "0x60806040526040516107353803806107358339810160408190526100229161031e565b61002e82826000610035565b505061043b565b61003e8361006b565b60008251118061004b5750805b156100665761006483836100ab60201b6100291760201c565b505b505050565b610074816100d7565b6040516001600160a01b038216907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b90600090a250565b60606100d0838360405180606001604052806027815260200161070e602791396101a9565b9392505050565b6100ea8161022260201b6100551760201c565b6101515760405162461bcd60e51b815260206004820152602d60248201527f455243313936373a206e657720696d706c656d656e746174696f6e206973206e60448201526c1bdd08184818dbdb9d1c9858dd609a1b60648201526084015b60405180910390fd5b806101887f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc60001b61023160201b6100641760201c565b80546001600160a01b0319166001600160a01b039290921691909117905550565b6060600080856001600160a01b0316856040516101c691906103ec565b600060405180830381855af49150503d8060008114610201576040519150601f19603f3d011682016040523d82523d6000602084013e610206565b606091505b50909250905061021886838387610234565b9695505050505050565b6001600160a01b03163b151590565b90565b606083156102a0578251610299576001600160a01b0385163b6102995760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152606401610148565b50816102aa565b6102aa83836102b2565b949350505050565b8151156102c25781518083602001fd5b8060405162461bcd60e51b81526004016101489190610408565b634e487b7160e01b600052604160045260246000fd5b60005b8381101561030d5781810151838201526020016102f5565b838111156100645750506000910152565b6000806040838503121561033157600080fd5b82516001600160a01b038116811461034857600080fd5b60208401519092506001600160401b038082111561036557600080fd5b818501915085601f83011261037957600080fd5b81518181111561038b5761038b6102dc565b604051601f8201601f19908116603f011681019083821181831017156103b3576103b36102dc565b816040528281528860208487010111156103cc57600080fd5b6103dd8360208301602088016102f2565b80955050505050509250929050565b600082516103fe8184602087016102f2565b9190910192915050565b60208152600082518060208401526104278160408501602087016102f2565b601f01601f19169190910160400192915050565b6102c48061044a6000396000f3fe60806040523661001357610011610017565b005b6100115b610027610022610067565b61009f565b565b606061004e8383604051806060016040528060278152602001610268602791396100c3565b9392505050565b6001600160a01b03163b151590565b90565b600061009a7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc546001600160a01b031690565b905090565b3660008037600080366000845af43d6000803e8080156100be573d6000f35b3d6000fd5b6060600080856001600160a01b0316856040516100e09190610218565b600060405180830381855af49150503d806000811461011b576040519150601f19603f3d011682016040523d82523d6000602084013e610120565b606091505b50915091506101318683838761013b565b9695505050505050565b606083156101ac5782516101a5576001600160a01b0385163b6101a55760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000060448201526064015b60405180910390fd5b50816101b6565b6101b683836101be565b949350505050565b8151156101ce5781518083602001fd5b8060405162461bcd60e51b815260040161019c9190610234565b60005b838110156102035781810151838201526020016101eb565b83811115610212576000848401525b50505050565b6000825161022a8184602087016101e8565b9190910192915050565b60208152600082518060208401526102538160408501602087016101e8565b601f01601f1916919091016040019291505056fe416464726573733a206c6f772d6c6576656c2064656c65676174652063616c6c206661696c6564a2646970667358221220ff8e6f2d761d58b3bd984933269e01a7ff1f70a460b808056daa4cff1ee8ab6964736f6c63430008090033416464726573733a206c6f772d6c6576656c2064656c65676174652063616c6c206661696c6564",
"deployedBytecode": "0x60806040523661001357610011610017565b005b6100115b610027610022610067565b61009f565b565b606061004e8383604051806060016040528060278152602001610268602791396100c3565b9392505050565b6001600160a01b03163b151590565b90565b600061009a7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc546001600160a01b031690565b905090565b3660008037600080366000845af43d6000803e8080156100be573d6000f35b3d6000fd5b6060600080856001600160a01b0316856040516100e09190610218565b600060405180830381855af49150503d806000811461011b576040519150601f19603f3d011682016040523d82523d6000602084013e610120565b606091505b50915091506101318683838761013b565b9695505050505050565b606083156101ac5782516101a5576001600160a01b0385163b6101a55760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000060448201526064015b60405180910390fd5b50816101b6565b6101b683836101be565b949350505050565b8151156101ce5781518083602001fd5b8060405162461bcd60e51b815260040161019c9190610234565b60005b838110156102035781810151838201526020016101eb565b83811115610212576000848401525b50505050565b6000825161022a8184602087016101e8565b9190910192915050565b60208152600082518060208401526102538160408501602087016101e8565b601f01601f1916919091016040019291505056fe416464726573733a206c6f772d6c6576656c2064656c65676174652063616c6c206661696c6564a2646970667358221220ff8e6f2d761d58b3bd984933269e01a7ff1f70a460b808056daa4cff1ee8ab6964736f6c63430008090033",
"implementation": "0x324E0F328747fDe1c85ae4ab3e6be41d2199b531"
"implementation": "0x4880bF58668f53EbD9694026244a40De1E323483"
}
45 changes: 32 additions & 13 deletions packages/contracts/deployments/sepolia/Troy_Implementation.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions packages/contracts/src/Troy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ contract Troy is UUPSUpgradeable, Ownable2StepUpgradeable, CCIPReceiverUpgradeab
TroyStorage storage $ = _getTroyStorage();
return ($.championChainId, $.soldierAmount);
}

function isMessageIdFulFilled(bytes32 messageId) public view returns (bool) {
TroyStorage storage $ = _getTroyStorage();
return $.messageIdArrived[messageId];
}
}

0 comments on commit d7b2c59

Please sign in to comment.