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

Non Fungible Token policies and guards (Feat) #1986

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3433721
feat: created nft policy object and added it to nft object
nil-amrutlal Apr 18, 2024
f240bba
fix: fixed naming of object types
nil-amrutlal Apr 18, 2024
cff5fd2
chore: renamed some files and functions to more accurate naming
nil-amrutlal Apr 19, 2024
b990dad
feat: implemented nft info data loader
nil-amrutlal Apr 19, 2024
cc0ce43
wip: implemented retry and object mapping as well as aux functions
nil-amrutlal Apr 22, 2024
5935564
style: lint fixes
nil-amrutlal Apr 22, 2024
826e935
fix: remove unecessary code
nil-amrutlal Apr 22, 2024
dae7ecb
feat: replaced custom code with get policy info and fixed policies
nil-amrutlal Apr 23, 2024
288dcad
chore: changeset file
nil-amrutlal Apr 24, 2024
08b2e50
feat: add error handling for socket hanging up
nil-amrutlal Apr 25, 2024
1ecc74f
feat: add json strings, change guards, add keysets, module references…
nil-amrutlal Apr 25, 2024
7b5c8a1
feat: ajust functions to return correct types
nil-amrutlal Apr 25, 2024
74c524b
Merge commit '37375fa7ed9cf06a99c68ab403320f00de3e66b8' into feat/gra…
nil-amrutlal Apr 26, 2024
b7659a3
feat: client changes to accomodate graph schema changes
nil-amrutlal Apr 29, 2024
60db032
style: lint fixes
nil-amrutlal Apr 29, 2024
36c54d0
fix: testing to see if build restarts
nil-amrutlal Apr 29, 2024
223096d
fix: style fix (attempting build pass)
nil-amrutlal Apr 29, 2024
1b39414
chore: formatting
Ghislain89 Apr 29, 2024
af5048d
fix: e2e testing
nil-amrutlal Apr 29, 2024
1c75595
Merge commit 'b96c16be311a3c66f8b479af2c487a7cda937a4b' into feat/gra…
nil-amrutlal May 2, 2024
37b01b8
feat: made some adjustments to the guard object for non-fungibles wit…
nil-amrutlal May 2, 2024
a059efe
fix: typing issue
nil-amrutlal May 2, 2024
31a258d
feat: restructure Ikeyset assignation in chain accounts
nil-amrutlal May 2, 2024
0d2f3a2
Merge commit '9ba191f1b66e6dd7fb11ad27b3b4bb19008b025e' into feat/gra…
nil-amrutlal May 8, 2024
f64e020
feat: update graph schema
nil-amrutlal May 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions packages/apps/graph/generated-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -278,25 +278,31 @@ type NonFungibleChainAccountTransactionsConnectionEdge {
node: Transaction!
}

"""Information related to a token."""
type NonFungibleToken {
precision: Int!
supply: Int!
uri: String!
}

"""The token identifier and its balance."""
type NonFungibleTokenBalance implements Node {
accountName: String!
balance: Int!
chainId: String!
guard: Guard!
id: ID!
info: NonFungibleToken
info: NonFungibleTokenInfo
tokenId: String!
version: String!
}

"""Information related to a token."""
type NonFungibleTokenInfo {
policies: [NonFungibleTokenPolicy!]!
precision: Int!
supply: Int!
uri: String!
}

"""A policy that defines the rules for a non-fungible token."""
type NonFungibleTokenPolicy {
moduleName: String!
}

input PactQuery {
chainId: String!
code: String!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface ITokenInfo {
precision: number;
uri: string;
id: string;
policies: { moduleName: string }[];
}

export const getTokenInfo = async (
Expand Down Expand Up @@ -97,5 +98,15 @@ export const getTokenInfo = async (
}
}

if ('policies' in tokenInfo) {
if (!Array.isArray(tokenInfo.policies)) {
tokenInfo.policies = tokenInfo.policies.map((policy: string) => ({
moduleName: policy,
}));
}
} else if ('policy' in tokenInfo) {
tokenInfo.policies = { moduleName: tokenInfo.policy.toString() };
}

return tokenInfo as ITokenInfo;
};
6 changes: 4 additions & 2 deletions packages/apps/graph/src/graph/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import type {
IGuard,
INonFungibleAccount,
INonFungibleChainAccount,
INonFungibleToken,
INonFungibleTokenBalance,
INonFungibleTokenInfo,
INonFungibleTokenPolicy,
IPactQueryResponse,
ITransactionCapability,
ITransactionCommand,
Expand Down Expand Up @@ -83,7 +84,8 @@ export const builder = new SchemaBuilder<
NonFungibleAccount: INonFungibleAccount;
NonFungibleChainAccount: INonFungibleChainAccount;
NonFungibleTokenBalance: INonFungibleTokenBalance;
NonFungibleToken: INonFungibleToken;
NonFungibleTokenPolicy: INonFungibleTokenPolicy;
NonFungibleTokenInfo: INonFungibleTokenInfo;
TransactionMeta: ITransactionMeta;
ExecutionPayload: IExecutionPayload;
ContinuationPayload: IContinuationPayload;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getNonFungibleTokenBalances } from '@services/token-service';
import DataLoader from 'dataloader';
import type { INonFungibleTokenBalance } from '../types/graphql-types';

interface INonFungibleTokenBalancesKey {
accountName: string;
chainId?: string;
}

export const nonFungibleTokenBalancesLoader = new DataLoader<
INonFungibleTokenBalancesKey,
INonFungibleTokenBalance[]
>(async (keys: readonly INonFungibleTokenBalancesKey[]) => {
const results = await Promise.all(
keys.map(({ accountName, chainId }) =>
getNonFungibleTokenBalances(accountName, chainId),
),
);

return results;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getNonFungibleTokenInfo } from '@services/token-service';
import DataLoader from 'dataloader';
import type { INonFungibleTokenInfo } from '../types/graphql-types';

interface INonFungibleTokenInfoKey {
tokenId: string;
chainId: string;
version: string;
}

/**
* Get the info for a non-fungible token
*/
export const nonFungibleTokenInfoLoader = new DataLoader<
INonFungibleTokenInfoKey,
INonFungibleTokenInfo | null
>(async (keys: readonly INonFungibleTokenInfoKey[]) => {
const results = await Promise.all(
keys.map(({ tokenId, chainId, version }) =>
getNonFungibleTokenInfo(tokenId, chainId, version),
),
);

return results;
});
21 changes: 0 additions & 21 deletions packages/apps/graph/src/graph/data-loaders/token-details.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/apps/graph/src/graph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import './objects/guard';
import './objects/miner-key';
import './objects/non-fungible-account';
import './objects/non-fungible-chain-account';
import './objects/non-fungible-token';
import './objects/non-fungible-token-balance';
import './objects/non-fungible-token-info';
import './objects/pact-query-response';
import './objects/signer';
import './objects/transaction';
Expand Down
8 changes: 4 additions & 4 deletions packages/apps/graph/src/graph/objects/non-fungible-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { dotenv } from '@utils/dotenv';
import { normalizeError } from '@utils/errors';
import { builder } from '../builder';
import { nonFungibleChainCheck } from '../data-loaders/non-fungible-chain-check';
import { tokenDetailsLoader } from '../data-loaders/token-details';
import { nonFungibleTokenBalancesLoader } from '../data-loaders/non-fungible-token-balances';
import type {
INonFungibleAccount,
INonFungibleChainAccount,
Expand All @@ -18,7 +18,7 @@ import {
NonFungibleAccountName,
NonFungibleChainAccountName,
} from '../types/graphql-types';
import Token from './non-fungible-token-balance';
import NonFungibleTokenBalance from './non-fungible-token-balance';

export default builder.node(
builder.objectRef<INonFungibleAccount>(NonFungibleAccountName),
Expand Down Expand Up @@ -77,11 +77,11 @@ export default builder.node(
},
}),
nonFungibleTokenBalances: t.field({
type: [Token],
type: [NonFungibleTokenBalance],
complexity: COMPLEXITY.FIELD.PRISMA_WITHOUT_RELATIONS,
async resolve(parent) {
try {
const tokenDetails = await tokenDetailsLoader.load({
const tokenDetails = await nonFungibleTokenBalancesLoader.load({
accountName: parent.accountName,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@services/complexity';
import { normalizeError } from '@utils/errors';
import { builder } from '../builder';
import { tokenDetailsLoader } from '../data-loaders/token-details';
import { nonFungibleTokenBalancesLoader } from '../data-loaders/non-fungible-token-balances';
import type { INonFungibleChainAccount } from '../types/graphql-types';
import { NonFungibleChainAccountName } from '../types/graphql-types';
import NonFungibleTokenBalance from './non-fungible-token-balance';
Expand Down Expand Up @@ -45,7 +45,7 @@ export default builder.node(
complexity: COMPLEXITY.FIELD.PRISMA_WITHOUT_RELATIONS,
async resolve(parent) {
try {
const tokenDetails = await tokenDetailsLoader.load({
const tokenDetails = await nonFungibleTokenBalancesLoader.load({
accountName: parent.accountName,
chainId: parent.chainId,
});
Expand Down
25 changes: 14 additions & 11 deletions packages/apps/graph/src/graph/objects/non-fungible-token-balance.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getTokenInfo } from '@devnet/simulation/marmalade/get-token-info';
import type { ChainId } from '@kadena/types';
import { COMPLEXITY } from '@services/complexity';
import { normalizeError } from '@utils/errors';
import { builder } from '../builder';
import { tokenDetailsLoader } from '../data-loaders/token-details';
import { nonFungibleTokenBalancesLoader } from '../data-loaders/non-fungible-token-balances';
import { nonFungibleTokenInfoLoader } from '../data-loaders/non-fungible-token-info';
import type { INonFungibleTokenBalance } from '../types/graphql-types';
import { NonFungibleTokenBalanceName } from '../types/graphql-types';
import NonFungibleToken from './non-fungible-token';
import NonFungibleTokenInfo from './non-fungible-token-info';

export default builder.node(
builder.objectRef<INonFungibleTokenBalance>(NonFungibleTokenBalanceName),
Expand All @@ -27,7 +27,7 @@ export default builder.node(
async loadOne({ tokenId, accountName, chainId }) {
try {
return (
await tokenDetailsLoader.load({
await nonFungibleTokenBalancesLoader.load({
accountName,
chainId,
})
Expand All @@ -49,15 +49,18 @@ export default builder.node(
},
}),
info: t.field({
type: NonFungibleToken,
type: NonFungibleTokenInfo,
complexity: COMPLEXITY.FIELD.CHAINWEB_NODE,
nullable: true,
async resolve(parent) {
try {
return await getTokenInfo(
parent.tokenId,
parent.chainId.toString() as ChainId,
parent.version,
);
const tokenInfo = await nonFungibleTokenInfoLoader.load({
tokenId: parent.tokenId,
chainId: parent.chainId,
version: parent.version,
});

return tokenInfo;
} catch (error) {
throw normalizeError(error);
}
Expand Down
15 changes: 15 additions & 0 deletions packages/apps/graph/src/graph/objects/non-fungible-token-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { builder } from '../builder';
import NonFungibleTokenPolicy from './non-fungible-token-policy';

export default builder.objectType('NonFungibleTokenInfo', {
description: 'Information related to a token.',
fields: (t) => ({
supply: t.exposeInt('supply'),
precision: t.exposeInt('precision'),
uri: t.exposeString('uri'),
policies: t.field({
type: [NonFungibleTokenPolicy],
resolve: (parent) => parent.policies,
}),
}),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { builder } from '../builder';

export default builder.objectType('NonFungibleTokenPolicy', {
description: 'A policy that defines the rules for a non-fungible token.',
fields: (t) => ({
moduleName: t.exposeString('moduleName'),
}),
});
10 changes: 0 additions & 10 deletions packages/apps/graph/src/graph/objects/non-fungible-token.ts

This file was deleted.

11 changes: 7 additions & 4 deletions packages/apps/graph/src/graph/types/graphql-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ export interface INonFungibleTokenBalance {
accountName: string;
chainId: string;
guard: IGuard;
info?: INonFungibleToken;
info?: INonFungibleTokenInfo;
version: string;
}

export interface INonFungibleToken {
export interface INonFungibleTokenPolicy {
moduleName: string;
}

export interface INonFungibleTokenInfo {
supply: number;
precision: number;
uri: string;
// TODO: figure out what to do with weird pact-arrays
// policies: string[];
policies: INonFungibleTokenPolicy[];
}

export const FungibleChainAccountName: 'FungibleChainAccount' =
Expand Down
7 changes: 5 additions & 2 deletions packages/apps/graph/src/services/account-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fungibleAccountDetailsLoader } from '../graph/data-loaders/fungible-account-details';
import { tokenDetailsLoader } from '../graph/data-loaders/token-details';
import { nonFungibleTokenBalancesLoader } from '../graph/data-loaders/non-fungible-token-balances';
import type {
IFungibleChainAccount,
INonFungibleChainAccount,
Expand Down Expand Up @@ -48,7 +48,10 @@ export async function getNonFungibleChainAccount({
chainId: string;
accountName: string;
}): Promise<INonFungibleChainAccount | null> {
const tokenDetails = await tokenDetailsLoader.load({ accountName, chainId });
const tokenDetails = await nonFungibleTokenBalancesLoader.load({
accountName,
chainId,
});

return tokenDetails !== null && tokenDetails.length !== 0
? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export async function getNonFungibleAccountDetails(
accountName: string,
chainId: string,
): Promise<INonFungibleChainAccountDetails | null> {
let result;

try {
let commandResult;

Expand Down Expand Up @@ -73,7 +71,7 @@ export async function getNonFungibleAccountDetails(
) {
return null;
} else {
throw new PactCommandError('Pact Command failed with error', result);
throw new PactCommandError('Pact Command failed with error', error);
}
}
}
Loading
Loading