Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add local network and create avatar in example #114

Merged
merged 13 commits into from
Jul 18, 2024
2 changes: 1 addition & 1 deletion background/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@noble/hashes": "^1.4.0",
"@planetarium/account": "~4.4.2",
"@planetarium/bencodex": "0.2.2",
"@planetarium/lib9c": "npm:@jsr/planetarium__lib9c@0.2.0-dev.202406280302125015+422656569f31ecb5f6bb9bf2ab71ab84bc6ad4c3",
"@planetarium/lib9c": "npm:@jsr/planetarium__lib9c@^0.3.0-dev.202407170506408289+2af04aea533872ccba4f01c583b2e53694d2e18e",
"@planetarium/tx": "^4.4.2",
"axios": "^0.24.0",
"bencodex": "^0.1.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/daily-reward-dapp/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ dist-ssr
*.sln
*.sw?

src/generated
/graphql/generated
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
query getTip {
nodeStatus {
tip {
index
}
}
}

query getAvatars($agentAddress: Address!) {
stateQuery {
agent(address: $agentAddress) {
avatarStates {
address,
name,
level,
actionPoint,
dailyRewardReceivedIndex,
}
}
}
}

query GetAvatarsWithTip($agentAddress: Address!) {
nodeStatus {
tip {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
overwrite: true
schema: ./schema.graphql
documents: ./api.graphql
schema: https://odin-rpc-1.nine-chronicles.com/graphql
# schema: ./graphql/schema.graphql
moreal marked this conversation as resolved.
Show resolved Hide resolved
documents: ./graphql/api.graphql
generates:
src/generated/graphql.tsx:
./graphql/generated/graphql.tsx:
plugins:
- typescript
- typescript-operations
Expand All @@ -15,8 +16,6 @@ generates:
scalars:
Long: number
TxId: string
src/generated/graphql-request.ts:
./graphql/generated/schema.json:
plugins:
- typescript
- typescript-operations
- typescript-graphql-request
- introspection
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@ type StandaloneQuery {
stateQuery(
# Offset block hash for query.
hash: ByteString

# Offset block index for query.
index: Long
): StateQuery!

# This field allows you to query the diffs between two blocks. `baseIndex` is the reference block index, and changedIndex is the block index from which to check what changes have occurred relative to `baseIndex`. Both indices must not be higher than the current block on the chain nor lower than the genesis block index (0). The difference between the two blocks must be greater than zero for a valid comparison and less than ten for performance reasons.
diffs(
# The index of the reference block from which the state is retrieved.
baseIndex: Long!

# The index of the target block for comparison.
changedIndex: Long!
): [Diff!]!
state(
# The hash of the block used to fetch state from chain.
hash: ByteString

# The index of the block used to fetch state from chain.
index: Long

# The address of account to fetch from the chain.
accountAddress: Address!

Expand Down Expand Up @@ -428,6 +443,7 @@ enum ItemSubType {
)
TITLE
AURA
GRIMOIRE
}

enum ElementalType {
Expand Down Expand Up @@ -1132,6 +1148,27 @@ type ArenaParticipantType {
nameWithHash: String!
}

union Diff = RootStateDiff | StateDiff

type RootStateDiff {
# The path to the root state difference.
path: String!

# List of state differences under this root.
diffs: [StateDiff!]!
}

type StateDiff {
# The path of the state difference.
path: String!

# The base state before changes.
baseState: String!

# The state after changes.
changedState: String
}

type TransferNCGHistoryType {
blockHash: ByteString!
txId: ByteString!
Expand Down Expand Up @@ -1248,14 +1285,24 @@ type ExplorerQuery {
stateQuery: LibplanetStateQuery
nodeState: NodeState!
helperQuery: HelperQuery
rawStateQuery: RawStateQuery
}

type BlockQuery {
blocks(
desc: Boolean = false
offset: Int = 0
# Whether to query blocks in descending order or not.
desc: Boolean! = false

# The offset of the first queried block.
offset: Int! = 0

# The maximum number of blocks to return. This limits the offset index range to query, not the result, i.e. excluded blocks due to a block being empty or not matching the miner (if specified in other arguments) are still counted.
limit: Int
excludeEmptyTxs: Boolean = false

# Whether to include empty blocks with no transactions or not. Default is set to false, i.e. to return empty blocks.
excludeEmptyTxs: Boolean! = false

# If not null, returns blocks only by mined by the address given. Default is set to null.
miner: Address
): [Block!]!
block(hash: ID, index: ID): Block
Expand Down Expand Up @@ -1304,6 +1351,9 @@ type Block {

# The hash of PreEvaluationBlock.
preEvaluationHash: ByteString!

# The protocol version number of the block.
protocolVersion: Int!
}

scalar PublicKey
Expand Down Expand Up @@ -1507,6 +1557,9 @@ type WorldState {
# The legacy flag of the world state.
legacy: Boolean!

# The version of the bakcing data model.
version: Int!

# Gets the account associated with given address.
account(
# The address of an account to retrieve.
Expand All @@ -1518,6 +1571,24 @@ type WorldState {
# The list of addresses of accounts to retrieve.
addresses: [Address!]!
): [AccountState!]!

# Balance at given address and currency pair.
balance(
# The currency to look up.
currency: CurrencyInput!

# The address to look up.
address: Address!
): FungibleAssetValue!

# Total supply in circulation for given currency.
totalSupply(
# The currency to look up.
currency: CurrencyInput!
): FungibleAssetValue!

# The validator set.
validatorSet: IValue!
}

scalar HashDigestSHA256
Expand Down Expand Up @@ -1546,7 +1617,7 @@ type AccountState {

# The currency hash to look up.
currencyHash: HashDigestSHA1!
): IValue
): IValue @deprecated(reason: "Does not work post block protocol version 7.")

# Balances at given addresses and currency hash pair.
balances(
Expand All @@ -1556,15 +1627,17 @@ type AccountState {
# The currency hash to look up.
currencyHash: HashDigestSHA1!
): [IValue]!
@deprecated(reason: "Does not work post block protocol version 7.")

# Total supply in circulation, if recorded, for given currency hash.
totalSupply(
# The currency hash to look up.
currencyHash: HashDigestSHA1!
): IValue
): IValue @deprecated(reason: "Does not work post block protocol version 7.")

# The validator set.
validatorSet: IValue
@deprecated(reason: "Does not work post block protocol version 6.")
}

type IValue {
Expand Down Expand Up @@ -1598,8 +1671,8 @@ type Validator {

type NodeState {
preloaded: Boolean!
peers: [BoundPeer!]
validators: [BoundPeer!]
peers: [BoundPeer!]!
validators: [BoundPeer!]!
}

type BoundPeer {
Expand All @@ -1626,6 +1699,33 @@ type HelperQuery {
# The byte array in hex representation to decode.
hex: String!
): IValue!

# Converts string to key hex representation
keyHex(
# The string value to convert to key hex.
value: String!
): KeyBytes!
}

scalar KeyBytes

type RawStateQuery {
# Retrieves trie from given state root hash.
trie(stateRootHash: HashDigest_SHA256): Trie!
}

type Trie {
# Gets the value stored at given key.
value(
# The key to search.
key: KeyBytes!
): IValue

# Gets the values stored at given multiple keys.
values(
# The list of keys to search.
keys: [KeyBytes!]!
): IValue
}

type ValidationQuery {
Expand Down
13 changes: 7 additions & 6 deletions examples/daily-reward-dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
"type": "module",
"scripts": {
"dev": "vite",
"build": "graphql-codegen && tsc && vite build",
"build": "graphql-codegen --config ./graphql/codegen.yaml && tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"fmt": "pnpm dlx @biomejs/biome format --write ./src",
"preview": "vite preview",
"codegen": "graphql-codegen"
"codegen": "graphql-codegen --config ./graphql/codegen.yaml"
},
"dependencies": {
"@apollo/client": "^3.9.11",
"@planetarium/account": "~4.4.2",
"@planetarium/bencodex": "^0.2.2",
"@planetarium/chrono-sdk": "workspace:^",
"@planetarium/lib9c": "npm:@jsr/planetarium__lib9c@^0.2.0-dev.202405270607435878+5300e991debf3ce9444201f037490c4f0e02406e",
"@planetarium/lib9c": "npm:@jsr/planetarium__lib9c@^0.3.0-dev.202407170506408289+2af04aea533872ccba4f01c583b2e53694d2e18e",
"@tanstack/react-query": "^5.37.1",
"buffer": "^6.0.3",
"graphql": "^16.8.1",
Expand All @@ -29,11 +29,12 @@
"devDependencies": {
"@biomejs/biome": "^1.7.3",
"@graphql-codegen/cli": "^5.0.2",
"@graphql-codegen/typescript": "^4.0.6",
"@graphql-codegen/introspection": "^4.0.3",
"@graphql-codegen/typescript": "^4.0.9",
"@graphql-codegen/typescript-graphql-request": "^6.2.0",
"@graphql-codegen/typescript-operations": "^4.2.0",
"@graphql-codegen/typescript-operations": "^4.2.3",
"@graphql-codegen/typescript-react-apollo": "^4.3.0",
"@graphql-codegen/typescript-resolvers": "^4.0.6",
"@graphql-codegen/typescript-resolvers": "^4.2.1",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@types/uuid": "^9.0.8",
Expand Down
56 changes: 33 additions & 23 deletions examples/daily-reward-dapp/src/Agent.tsx
Original file line number Diff line number Diff line change
@@ -1,52 +1,62 @@
import { Address } from "@planetarium/account";
import { RefillButton } from "./RefillButton";
import { useAgent } from "./hooks/useAgent";
import { useTip } from "./hooks/useTip";
import { useGetAvatarsQuery, useGetTipQuery } from "../graphql/generated/graphql";
import { CreateAvatarButton } from "./CreateAvatarButton";

interface AgentProps {
network: "odin" | "heimdall";
agentAddress: Address;

setTxId: (value: string | null) => void;
}

function Agent({ network, agentAddress, setTxId }: AgentProps) {
const {
data: agent,
isLoading: agentLoading,
isSuccess: agentSuccess,
} = useAgent(network, agentAddress.toString());
function Agent({ agentAddress, setTxId }: AgentProps) {
const {
data: tip,
isLoading: tipLoading,
isSuccess: tipSuccess,
} = useTip(network);
data: tipData,
loading: tipLoading,
error: tipError,
} = useGetTipQuery({
pollInterval: 1000,
});

const isLoading = agentLoading || tipLoading;
const isSuccess = agentSuccess && tipSuccess;
const {
data: avatarsData,
loading: avatarsLoading,
error: avatarsError
} = useGetAvatarsQuery({
variables: { agentAddress: agentAddress.toString() },
pollInterval: 1000,
});

if (isLoading) {
if (tipLoading || avatarsLoading) {
return <p className="mt-8 text-white">Loading...</p>;
}

if (!isSuccess) {
if (tipError || avatarsError) {
return <p className="mt-8 text-white">Failed to fetch data.</p>;
}

if (agent.avatars.length < 1) {
const tip = tipData?.nodeStatus?.tip?.index || -1;
const avatars = avatarsData?.stateQuery?.agent?.avatarStates || [];
if (avatars.length < 1) {
return (
<p className="mt-8 text-white">The agent may not have any avatars.</p>
<div>
<p className="mt-8 text-white">The agent may not have any avatars.</p>
<CreateAvatarButton
signer={agentAddress}
avatarIndex={0}
setTxId={setTxId}
/>
</div>
);
}
const avatars = agent.avatars;

const REFILL_INTERVAL = 2550 as const;

return (
<div className="flex flex-col mt-8 min-w-full min-h-full justify-center items-center">
{avatars.map(
({
avatarAddress,
avatarName,
address: avatarAddress,
name: avatarName,
actionPoint,
dailyRewardReceivedIndex,
}) => (
Expand Down
Loading