Skip to main content

Market Creation

1. Set up

Import the needed modules and initialize the sdk in either full or rpc mode to be able to submit transactions.

import {
Sdk,
FullContext,
CreateStandaloneMarketParams,
create,
mainnet,
ZTG,
} from "@zeitgeistpm/sdk";

const sdk: Sdk<FullContext> = await create(mainnet());

2. Init Market Creation Params

First we need to set up or get the signer from the wallet extension(browser) or other keyring mechanism.

const signer: KeyringPair = getSigner();

Or using the extension and the @polkadot/extension-dapp library.

const injector = await web3FromAddress(address);
const signer = {
address,
signer: injector.signer,
};
信息

Read more about signing transactions here. And how to sign transactions in the browser here.

Then we set up the parameters needed to create a market. In this case we are creating a market with a pool with a liquidity of 300 ZTG and even weighting among the two categorical outcomes.

const params: CreateMarketWithPoolParams<typeof sdk> = {
baseAsset: { Ztg: null },
signer,
disputeMechanism: "Authorized",
marketType: { Categorical: 2 },
oracle: signer.address,
period: { Timestamp: [Date.now(), Date.now() + 60 * 60 * 24 * 1000 * 2] },
deadlines: {
disputeDuration: 5000,
gracePeriod: 200,
oracleDuration: 500,
},
metadata: {
__meta: "markets",
question: "Will the example work?",
description: "Testing the sdk.",
slug: "standalone-market-example",
categories: [
{ name: "yes", ticker: "Y" },
{ name: "no", ticker: "N" },
],
tags: ["dev"],
},
pool: {
amount: ZTG.mul(300).toString(),
swapFee: "1",
weights: ["50000000000", "50000000000"],
},
};

Read more about market parameters

3. Submitting transaction

The create method on sdk.model.markets will first add the metadata to the IPFS(or other custom) storage and then submit the transaction needed to create the market on chain.

If then transaction fails it will also try to remove the metadata from IPFS and unpin it in the cluster if a cluster config is configured. The standard zeitgeist ipfs storage config uses a cluster.

const response = await sdk.model.markets.create(params);
提示

At this point the market has been created and the next step is optional and only necessary if you need the created marketId in the next step of your application.

4. Extracting Created Market and Pool (optional)

The saturate method will try to extract the create market and pool from the submitted extrinsic events; and unwrap will unwrap the raw value.

const { market, pool } = response.saturate().unwrap();

console.log(`Market created with id: ${market.marketId}`);
console.log(`Pool created with id: ${pool.poolId}`);
危险

The unwrap method will throw and error if failing and should be exception handled.

5. Exception Safe Error Handling

In case you want more fine grained error handling, the sdk has some more functional approaches to dealing with it.

const response = await sdk.model.markets.create(params);
const data = response.saturate();

if (data.isRight()) {
const { market, pool } = data.unwrap();
console.log(`Market created with id: ${market.marketId}`);
console.log(`Pool created with id: ${pool.poolId}`);
} else {
console.log(`Market creation had error: ${data.unwrapLeft().message}`);
}

Full Code

Here is the full code snippet for this example with some smaller differences for development and testing ease.

Full code snippet