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

Add collection info - RPC call #792

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions e2e/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ export const rpcs = {
],
type: "Json",
},
collectionDetails: {
description: "Returns the collection info for a NFT collection",
params: [
{
name: "collectionId",
type: "u32",
},
],
type: "CollectionDetail",
},
},
};
export const typedefs = {
Expand Down Expand Up @@ -196,6 +206,20 @@ export const typedefs = {
crossChainCompatibility: "CrossChainCompatibility",
ownedTokens: "Vec<TokenOwnership>",
},
CollectionDetail: {
owner: "AccountId",
name: "Vec<u8>",
metadataScheme: "Vec<u8>",
royaltiesSchedule: "Option<Vec<(T::AccountId, Permill)>>",
maxIssuance: "Option<u32>",
originChain: "Text",
nextSerialNumber: "u32",
collectionIssuance: "u32",
crossChainCompatibility: "CrossChainCompatibility",
},
CrossChainCompatibility: {
xrpl: "bool",
},
};

/** CONSTANTS */
Expand Down
149 changes: 149 additions & 0 deletions e2e/test/NftRPC.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { ApiPromise, Keyring, WsProvider } from "@polkadot/api";
import { KeyringPair } from "@polkadot/keyring/types";
import { hexToString, hexToU8a, u8aToString } from "@polkadot/util";
import axios from "axios";
import { expect } from "chai";

import { ALITH_PRIVATE_KEY, NodeProcess, startNode, typedefs } from "../common";

export const rpc = {
nft: {
collectionDetails: {
description: "Returns the collection info for a NFT collection",
params: [
{
name: "collectionId",
type: "u32",
},
],
type: "CollectionDetail",
},
},
};

describe("NftRPC", () => {
let node: NodeProcess;
let api: ApiPromise;
let collectionId: any;
let alith: KeyringPair;

before(async () => {
node = await startNode();

await node.wait(); // wait for the node to be ready
const wsProvider = new WsProvider(`ws://localhost:${node.wsPort}`);
api = await ApiPromise.create({
provider: wsProvider,
types: typedefs,
rpc,
});

const keyring = new Keyring({ type: "ethereum" });
alith = keyring.addFromSeed(hexToU8a(ALITH_PRIVATE_KEY));

collectionId = await api.query.nft.nextCollectionId();
const royaltiesSchedule = {
entitlements: [[alith.address, 10000 /* one percent */]],
};
await new Promise<void>((resolve, reject) => {
api.tx.nft
.createCollection("test-collection", 0, null, null, "https://test/api/", royaltiesSchedule, { xrpl: false })
.signAndSend(alith, ({ status, events }) => {
if (status.isInBlock) {
events.forEach(({ event: { data, method } }) => {
if (method == "CollectionCreate") {
collectionId = (data.toJSON() as any)[0];
console.log(`Collection UUID: ${collectionId}`);
resolve();
}
});
}
})
.catch((err) => reject(err));
});
});

after(async () => node.stop());

it("collection info rpc works [http - axios]", async () => {
const httpResult = await axios.post(`http://localhost:${node.httpPort}`, {
id: 1,
jsonrpc: "2.0",
method: "nft_collectionDetails",
params: [collectionId],
});

expect(httpResult.status).to.eql(200);
expect(httpResult.data).to.haveOwnProperty("result");
expect(httpResult.statusText).to.eql("OK");
const { result } = httpResult.data;
const {
owner,
name,
metadata_scheme,
royalties_schedule,
max_issuance,
origin_chain,
next_serial_number,
collection_issuance,
cross_chain_compatibility,
} = result;
expect(owner).to.eql("0xf24ff3a9cf04c71dbc94d0b566f7a27b94566cac");
expect(u8aToString(new Uint8Array(name))).to.eql("test-collection");
expect(u8aToString(new Uint8Array(metadata_scheme))).to.eql("https://test/api/0");
expect(royalties_schedule).to.eql([["0xf24ff3a9cf04c71dbc94d0b566f7a27b94566cac", 10000]]);
expect(max_issuance).to.eql(null);
expect(next_serial_number).to.eql(0);
expect(collection_issuance).to.eql(0);
expect(cross_chain_compatibility).to.eql({ xrpl: false });
expect(origin_chain).to.eql("Root");
});

it("collection info rpc works [library]", async () => {
const result = await (api.rpc as any).nft.collectionDetails(collectionId);
const {
owner,
name,
metadataScheme,
royaltiesSchedule,
maxIssuance,
originChain,
nextSerialNumber,
collectionIssuance,
crossChainCompatibility,
} = result.toJSON();
expect(owner.toLowerCase()).to.eql("0xf24ff3a9cf04c71dbc94d0b566f7a27b94566cac");
expect(hexToString(name)).to.eql("test-collection");
expect(hexToString(metadataScheme)).to.eql("https://test/api/0");
expect(royaltiesSchedule).to.eql([[alith.address, 10000]]);
expect(maxIssuance).to.eql(null);
expect(nextSerialNumber).to.eql(0);
expect(collectionIssuance).to.eql(0);
expect(crossChainCompatibility).to.eql({ xrpl: false });
expect(originChain).to.eql("Root");
});

it("collection info rpc works [library] that does not exist", async () => {
const result = await (api.rpc as any).nft.collectionDetails(100);
const {
owner,
name,
metadataScheme,
royaltiesSchedule,
maxIssuance,
originChain,
nextSerialNumber,
collectionIssuance,
crossChainCompatibility,
} = result.toJSON();
expect(owner).to.eql("0x0000000000000000000000000000000000000000");
expect(name).to.eql("0x");
expect(metadataScheme).to.eql("0x");
expect(royaltiesSchedule).to.eql(null);
expect(maxIssuance).to.eql(null);
expect(nextSerialNumber).to.eql(0);
expect(collectionIssuance).to.eql(0);
expect(crossChainCompatibility).to.eql({ xrpl: false });
expect(originChain).to.eql("Root");
});
});
Loading
Loading