Skip to content

Commit

Permalink
chore: [GMS-2129] Update code examples for blockchain data package (#…
Browse files Browse the repository at this point in the history
…2181)

Co-authored-by: Hiep Doan <[email protected]>
  • Loading branch information
allan-almeida-imtbl and hiep-immutable authored Sep 19, 2024
1 parent 6fe5428 commit c94d48d
Show file tree
Hide file tree
Showing 24 changed files with 425 additions and 2 deletions.
3 changes: 3 additions & 0 deletions examples/blockchain-data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# yarn v2
.yarn
.pnp.*
24 changes: 23 additions & 1 deletion examples/blockchain-data/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
# Coming Soon™
# Blockchain Data API Examples

These examples demonstrate ways of implementing the methods exposed by the blockchainData package of `@imtbl/sdk`, in order to interact with Immutable's [Blockchain Data APIs](https://docs.immutable.com/products/zkEVM/blockchain-data).

Immutable's Blockchain Data APIs index on-chain state changes and off-chain metadata to allow developers to efficiently retrieve information from Immutable's rollups.

## Getting Started

Pre-requisites:

- Node.js 20.0.0 or higher

Install dependencies:

```bash
yarn install
```

Test the examples:

```bash
yarn test
```
19 changes: 19 additions & 0 deletions examples/blockchain-data/api-examples-with-node/exported-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { blockchainData, config } from '@imtbl/sdk';

const configuration: blockchainData.BlockchainDataModuleConfiguration = {
baseConfig: new config.ImmutableConfiguration({
environment: config.Environment.PRODUCTION,
}),
};

const client = new blockchainData.BlockchainData(configuration);

export async function getChains(
request: blockchainData.Types.ListChainsRequestParams,
): Promise<blockchainData.Types.Chain> {
const chains: blockchainData.Types.ListChainsResult = await client.listChains(
request,
);

return chains.result[0]; // type inference, autocomplete works here for `Chain` object
}
12 changes: 12 additions & 0 deletions examples/blockchain-data/api-examples-with-node/get-collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { blockchainData } from '@imtbl/sdk';

import { client } from '../lib';

export async function getCollection(
contractAddress: string,
): Promise<blockchainData.Types.GetCollectionResult> {
return await client.getCollection({
chainName: 'imtbl-zkevm-testnet',
contractAddress,
});
};
14 changes: 14 additions & 0 deletions examples/blockchain-data/api-examples-with-node/get-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { blockchainData } from "@imtbl/sdk";
import { client } from "../lib";

export async function getMetadata(
chainName: string,
contractAddress: string,
metadataId: string
): Promise<blockchainData.Types.GetMetadataResult> {
return await client.getMetadata({
chainName,
contractAddress,
metadataId,
});
}
10 changes: 10 additions & 0 deletions examples/blockchain-data/api-examples-with-node/get-nft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { blockchainData } from "@imtbl/sdk";
import { client } from "../lib";

export async function getNFT(chainName: string, contractAddress: string, tokenId: string): Promise<blockchainData.Types.GetNFTResult> {
return await client.getNFT({
chainName: chainName,
contractAddress: contractAddress,
tokenId: tokenId,
});
}
27 changes: 27 additions & 0 deletions examples/blockchain-data/api-examples-with-node/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { verifySuccessfulMints } from "./verify-successful-mints";
import { getChains } from "./exported-types";
import { getCollection } from "./get-collection";
import { getMetadata } from "./get-metadata";
import { getNFT } from "./get-nft";
import { listMetadata } from "./list-metadata";
import { listCollections } from "./list-collections";
import { listCollectionsByNFTOwner } from "./list-collections-by-owner";
import { listActivities } from "./list-activities";
import { listNFTsByAccountAddress } from "./list-nfts-by-account-address";
import { refreshNFTMetadata } from "./refresh-nft-metadata";
import { refreshStackedMetadata } from "./refresh-stacked-metadata";

export {
verifySuccessfulMints,
getChains,
getCollection,
getMetadata,
getNFT,
listMetadata,
listCollections,
listCollectionsByNFTOwner,
listActivities,
listNFTsByAccountAddress,
refreshNFTMetadata,
refreshStackedMetadata,
};
14 changes: 14 additions & 0 deletions examples/blockchain-data/api-examples-with-node/list-activities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { blockchainData } from "@imtbl/sdk";
import { client } from "../lib";

export async function listActivities(
chainName: string,
contractAddress: string,
pageSize: number
): Promise<blockchainData.Types.ListActivitiesResult> {
return await client.listActivities({
chainName,
contractAddress,
pageSize,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { blockchainData } from "@imtbl/sdk";
import { client } from "../lib";

export async function listCollectionsByNFTOwner(
chainName: string,
accountAddress: string
): Promise<blockchainData.Types.ListCollectionsResult> {
return await client.listCollectionsByNFTOwner({
chainName,
accountAddress,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { blockchainData } from "@imtbl/sdk";
import { client } from "../lib";

export async function listCollections(
chainName: string
): Promise<blockchainData.Types.ListCollectionsResult> {
return await client.listCollections({
chainName,
});
}
12 changes: 12 additions & 0 deletions examples/blockchain-data/api-examples-with-node/list-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { blockchainData } from "@imtbl/sdk";
import { client } from "../lib";

export async function listMetadata(
chainName: string,
contractAddress: string
): Promise<blockchainData.Types.ListMetadataResult> {
return await client.listNFTMetadataByContractAddress({
chainName,
contractAddress,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { blockchainData } from '@imtbl/sdk';
import { client } from '../lib';

export async function listNFTsByAccountAddress(
chainName: string,
contractAddress: string,
accountAddress: string,
): Promise<blockchainData.Types.ListNFTsResult> {
return await client.listNFTsByAccountAddress({
chainName,
contractAddress,
accountAddress,
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { blockchainData } from "@imtbl/sdk";
import { client } from "../lib";

export async function refreshNFTMetadata(
chainName: string,
contractAddress: string,
newName: string
): Promise<blockchainData.Types.MetadataRefreshRateLimitResult> {
const nftMetadata: blockchainData.Types.RefreshMetadataByTokenID[] = [
{
name: newName,
animation_url: null,
image: null,
external_url: null,
youtube_url: null,
description: null,
attributes: [
{
trait_type: 'Power',
value: 'Happy',
},
],
token_id: '1',
},
];

return await client.refreshNFTMetadata({
chainName,
contractAddress,
refreshNFTMetadataByTokenIDRequest: {
nft_metadata: nftMetadata,
},
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { blockchainData } from '@imtbl/sdk';
import { client } from '../lib';

export async function refreshStackedMetadata(
chainName: string,
contractAddress: string,
newName: string
): Promise<blockchainData.Types.MetadataRefreshRateLimitResult> {
return await client.refreshStackedMetadata({
chainName,
contractAddress,
refreshMetadataByIDRequest: {
metadata: [
{
name: newName,
animation_url: null,
image: null,
external_url: null,
youtube_url: null,
description: null,
attributes: [],
metadata_id: '1',
},
],
},
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { config, blockchainData } from '@imtbl/sdk';

const API_KEY = 'YOUR_API_KEY';
const PUBLISHABLE_KEY = 'YOUR_PUBLISHABLE_KEY';

const client = new blockchainData.BlockchainData({
baseConfig: {
environment: config.Environment.PRODUCTION,
apiKey: API_KEY,
publishableKey: PUBLISHABLE_KEY,
},
});
10 changes: 10 additions & 0 deletions examples/blockchain-data/api-examples-with-node/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { config, blockchainData } from '@imtbl/sdk';

const PUBLISHABLE_KEY = 'YOUR_PUBLISHABLE_KEY'; // Replace with your Publishable Key from the Immutable Hub

const client = new blockchainData.BlockchainData({
baseConfig: {
environment: config.Environment.PRODUCTION,
publishableKey: PUBLISHABLE_KEY,
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { blockchainData } from '@imtbl/sdk';
import { client } from '../lib';

export async function verifySuccessfulMints(
contractAddress: string,
): Promise<blockchainData.Types.ListActivitiesResult> {
return await client.listActivities({
chainName: 'imtbl-zkevm-testnet',
contractAddress,
activityType: blockchainData.Types.ActivityType.Mint,
});
}
8 changes: 8 additions & 0 deletions examples/blockchain-data/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
testEnvironment: 'node',
transform: {
'^.+.tsx?$': ['ts-jest', {}],
},
setupFiles: ['dotenv/config'],
};
11 changes: 11 additions & 0 deletions examples/blockchain-data/lib/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { config as immutableConfig, blockchainData } from '@imtbl/sdk';

export const config: blockchainData.BlockchainDataModuleConfiguration = {
baseConfig: {
environment: immutableConfig.Environment.SANDBOX,
apiKey: process.env.API_KEY,
publishableKey: process.env.PUBLISHABLE_KEY,
},
};

export const client = new blockchainData.BlockchainData(config);
3 changes: 3 additions & 0 deletions examples/blockchain-data/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { client } from './client';

export { client };
21 changes: 21 additions & 0 deletions examples/blockchain-data/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "blockchain-data-api-examples",
"version": "1.0.0",
"description": "Code examples for interacting with the Blockchain Data API package of @imtbl/sdk",
"main": "index.js",
"scripts": {
"test": "jest"
},
"author": "",
"license": "ISC",
"dependencies": {
"@imtbl/sdk": "latest"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typescript": "^5"
}
}
Loading

0 comments on commit c94d48d

Please sign in to comment.