Skip to main content

Custom Market Metadata

You specify a custom market metadata type that extends the zeitgeist standard or that is a completely different type.

осторожно

If you do not adhere to the zeitgeist official metadata specification your created markets might not show up in the indexer and the application.

1. Creating the type.

First we construct a type of custom market metadata we want to attach to our markets.

type CustomMarketMetadata = {
__meta: "markets";
customValue: string;
};
к сведению

Note that the literal tag __meta: "markets" is required on the custom market metadata type.

2. Initializing the SDK

Next we initialize the sdk with the custom metadata as a generic.

const sdk = await create(
batterystationRpc<MetadataStorage<CustomMarketMetadata>>()
);

Or if we want to use a local test node

const sdk = await create<MetadataStorage<CustomMarketMetadata>>({
provider: "wss://localhost:9944",
storage: createStorage(
IPFS.storage({
node: { url: "localhost:5001" },
})
),
});

3. Creating a custom Market

Then we can create a market with the custom metadata.

const params: CreateStandaloneMarketParams<typeof sdk> = {
...,
metadata: {
__meta: 'markets',
customValue: 'custom text value',
},
}

const response = await sdk.model.markets.create(params)
const { market } = await response.saturate().unwrap()

const marketMetadata: CustomMarketMetadata = await market.fetchMetadata().unwrap()

console.log(`Market created with custom metadata: ${marketMetadata.customValue}`)
к сведению

Using typescript will give you strong type checking when creating and fetching metadata for markets with custom metadata types.

4. Extending Standard Metadata (optional)

If you want to make sure the market shows up in the indexer and the application you should extend the standard metadata.

import { MarketMetadata } from "@zeitgeistpm/sdk";

type CustomMarketMetadata = MarketMetadata & {
__meta: "markets";
customValue: string;
};
примечание

Note that in this example you would also have to pass question, description etc to the metadata when creating a market.

Ref official metadata spec

Indexer and Custom Metadata

осторожно

Note that the custom metadata is not indexed and if you want to use it in your application you have to fetch it manually.

We can still fetch it after the market is fetched if we have access to rpc/storage.

const sdk = await create(
batterystation<MetadataStorage<CustomMarketMetadata>>()
);

const market = (await sdk.model.markets.get(585)).unwrap()!;

// Question, slug etc are indexed so can be accessed directly
console.log(market.question);

// the custom value have to be fetched from IPFS
const metadata = await market.fetchMetadata().unwrap();
console.log(metadata.customValue);