Skip to content

Commit

Permalink
Upgrade for zeitgeist-v0.3.4 (#205)
Browse files Browse the repository at this point in the history
* Get latest metadata

* Introduce swapFee for deploySwapPoolAndAdditionalLiquidity

* Accept swapFee for deploySwapPool

* Update deployKusamaDerby.ts

* Add swapFee to createCpmmMarketAndDeployAssets

* Sync changes on cli handlers

* Accept swapFee via cli

* Update type-defs

* Generate type changes
  • Loading branch information
saboonikhil committed Aug 1, 2022
1 parent a3c53c7 commit cd3417b
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 66 deletions.
13 changes: 4 additions & 9 deletions packages/cli/src/actions/buyAssetsAndDeployPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ type Options = {
endpoint: string;
marketId: number;
seed: string;
swapFee: string;
amount: string;
weights: string;
};

const buyAssetsAndDeployPool = async (opts: Options): Promise<void> => {
const { endpoint, marketId, seed, weights, amount } = opts;
const { endpoint, marketId, seed, weights, amount, swapFee } = opts;

const sdk = await SDK.initialize(endpoint);

Expand All @@ -25,22 +26,16 @@ const buyAssetsAndDeployPool = async (opts: Options): Promise<void> => {
let wts = [];
if (weights) {
wts = weights.split(",");
if (wts.length !== outcomeAssets.length + 1) {
throw new Error(
`Provided weights length must match assets length!\nWeights: ${
wts.length
}\nAssets: ${outcomeAssets.length + 1}`
);
}
} else {
//default
// do not exceed: pub const MaxWeight: Balance = 50 * BASE;
// (See: /zeitgeist/runtime/src/lib.rs )
wts = Array(outcomeAssets.length + 1).fill("1".concat("0".repeat(10)));
wts = Array(outcomeAssets.length).fill("1".concat("0".repeat(10)));
}

const poolId = await market.deploySwapPoolAndAdditionalLiquidity(
signer,
swapFee,
amount,
wts,
false
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/actions/createMarketAndDeployPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Options = {
timestamp: boolean;
authorized: string;
court: boolean;
swapFee: string;
amount: string;
weights: string;
};
Expand All @@ -33,6 +34,7 @@ const createMarketAndDeployPool = async (opts: Options): Promise<void> => {
timestamp,
authorized,
court,
swapFee,
amount,
weights,
} = opts;
Expand Down Expand Up @@ -101,6 +103,7 @@ const createMarketAndDeployPool = async (opts: Options): Promise<void> => {
period: marketPeriod,
marketType,
mdm,
swapFee,
amount,
weights: weights.split(`,`),
metadata,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/actions/deployKusamaDerby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const deployKusamaDerby = async (opts: Options): Promise<void> => {
await market.deploySwapPool(
signer,
`10000000000`,
`10`,
[
`10000000000`,
`10000000000`,
Expand All @@ -63,7 +64,6 @@ const deployKusamaDerby = async (opts: Options): Promise<void> => {
`10000000000`,
`10000000000`,
`10000000000`,
`80000000000`,
],
false
);
Expand Down
20 changes: 10 additions & 10 deletions packages/cli/src/actions/deployPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ type Options = {
endpoint: string;
marketId: number;
seed: string;
swapFee: string;
amount: string;
weights: string;
};

const deployPool = async (opts: Options): Promise<void> => {
const { endpoint, marketId, seed, weights, amount } = opts;
const { endpoint, marketId, seed, weights, amount, swapFee } = opts;

const sdk = await SDK.initialize(endpoint);

Expand All @@ -25,21 +26,20 @@ const deployPool = async (opts: Options): Promise<void> => {
let wts = [];
if (weights) {
wts = weights.split(",");
if (wts.length !== outcomeAssets.length + 1) {
throw new Error(
`Provided weights length must match assets length!\nWeights: ${
wts.length
}\nAssets: ${outcomeAssets.length + 1}`
);
}
} else {
//default
// do not exceed: pub const MaxWeight: Balance = 50 * BASE;
// (See: /zeitgeist/runtime/src/lib.rs )
wts = Array(outcomeAssets.length + 1).fill("1".concat("0".repeat(10)));
wts = Array(outcomeAssets.length).fill("1".concat("0".repeat(10)));
}

const poolId = await market.deploySwapPool(signer, amount, wts, false);
const poolId = await market.deploySwapPool(
signer,
swapFee,
amount,
wts,
false
);
if (poolId && poolId.length > 0) {
console.log(`\x1b[36m%s\x1b[0m`, `\nDeployPool successful!`);
} else {
Expand Down
29 changes: 21 additions & 8 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ program

program
.command(
"createMarketAndDeployPool <slug> <description> <oracle> <period> <question>"
"createMarketAndDeployPool <slug> <description> <oracle> <period> <question> <swapFee> <amount>"
)
.option(
"-c --categories [categories...]",
Expand All @@ -89,7 +89,6 @@ program
"Use Court instead of Simple Disputes as Market Dispute Mechanism",
false
)
.option("--amount <string>", "The amount of each token to add to the pool")
.option(
"--weights <string>",
"A comma-separated list of relative denormalized weights of each asset price",
Expand All @@ -102,20 +101,29 @@ program
oracle: string,
period: string,
question: string,
swapFee: string,
amount: string,
opts: {
endpoint: string;
seed: string;
categories: string[];
timestamp: boolean;
authorized: string;
court: boolean;
amount: string;
weights: string;
}
) =>
catchErrorsAndExit(
createMarketAndDeployPool,
Object.assign(opts, { slug, description, oracle, period, question })
Object.assign(opts, {
slug,
description,
oracle,
period,
question,
swapFee,
amount,
})
)
);

Expand Down Expand Up @@ -371,7 +379,7 @@ program
);

program
.command("buyAssetsAndDeployPool <marketId> <amount>")
.command("buyAssetsAndDeployPool <marketId> <swapFee> <amount>")
.option("--seed <string>", "The signer's seed", "//Alice")
.option(
"--endpoint <string>",
Expand All @@ -386,17 +394,18 @@ program
.action(
(
marketId: number,
swapFee: string,
amount: string,
opts: { endpoint: string; seed: string; weights: string }
) =>
catchErrorsAndExit(
buyAssetsAndDeployPool,
Object.assign(opts, { marketId, amount })
Object.assign(opts, { marketId, swapFee, amount })
)
);

program
.command("deployPool <marketId> <amount>")
.command("deployPool <marketId> <swapFee> <amount>")
.option("--seed <string>", "The signer's seed", "//Alice")
.option(
"--endpoint <string>",
Expand All @@ -411,10 +420,14 @@ program
.action(
(
marketId: number,
swapFee: string,
amount: string,
opts: { endpoint: string; seed: string; weights: string }
) =>
catchErrorsAndExit(deployPool, Object.assign(opts, { marketId, amount }))
catchErrorsAndExit(
deployPool,
Object.assign(opts, { marketId, swapFee, amount })
)
);

program
Expand Down
7 changes: 6 additions & 1 deletion packages/sdk/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export default class Models {
* @param {MarketTypeOf} params.marketType `Categorical` or `Scalar`
* @param {MarketDisputeMechanism} params.mdm Dispute settlement can only be `Authorized` currently
* @param {DecodedMarketMetadata} params.metadata A hash pointer to the metadata of the market.
* @param {string} params.swapFee The fee applied to each swap after pool creation.
* @param {string} params.amount The amount of each token to add to the pool.
* @param {string[]} params.weights List of relative denormalized weights of each asset.
* @param {boolean} params.callbackOrPaymentInfo `true` to get txn fee estimation otherwise `false`
Expand All @@ -129,6 +130,7 @@ export default class Models {
oracle,
period,
metadata,
swapFee,
amount,
marketType,
mdm,
Expand All @@ -150,6 +152,7 @@ export default class Models {
multihash,
marketType,
mdm,
swapFee,
amount,
weights
);
Expand Down Expand Up @@ -177,7 +180,9 @@ export default class Models {
);

events.forEach(({ event: { data, method, section } }, index) => {
console.log(`Event ${index} -> ${section}.${method} :: ${data}`);
console.log(
`Event ${index + 1} -> ${section}.${method} :: ${data}`
);

if (method == `MarketCreated`) {
console.log(
Expand Down
36 changes: 32 additions & 4 deletions packages/sdk/src/models/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,14 @@ class Market {
/**
* Buy complete sets and deploy a pool with specified liquidity for a market.
* @param {KeyringPairOrExtSigner} signer The actual signer provider to sign the transaction.
* @param {string} swapFee The fee applied to each swap after pool creation.
* @param {string} amount The amount of each token to add to the pool.
* @param {string[]} weights The relative denormalized weight of each asset.
* @param {boolean} callbackOrPaymentInfo `true` to get txn fee estimation otherwise callback to capture transaction result.
*/
deploySwapPoolAndAdditionalLiquidity = async (
signer: KeyringPairOrExtSigner,
swapFee: string,
amount: string,
weights: string[],
callbackOrPaymentInfo:
Expand All @@ -239,12 +241,22 @@ class Market {
): Promise<string> => {
const poolId = await this.getPoolId();
if (poolId) {
throw new Error(`Pool already exists for this market.`);
throw new Error(`Pool already exists for this market`);
}

if (weights.length !== this.outcomeAssets.length) {
console.log(
`Weights: ${weights.length}\nOutcome Assets: ${this.outcomeAssets.length}`
);
throw new Error(
`Provided weights length must match outcome assets length`
);
}

const tx =
this.api.tx.predictionMarkets.deploySwapPoolAndAdditionalLiquidity(
this.marketId,
swapFee,
amount,
weights
);
Expand Down Expand Up @@ -272,7 +284,9 @@ class Market {
);

events.forEach(({ event: { data, method, section } }, index) => {
console.log(`Event ${index} -> ${section}.${method} :: ${data}`);
console.log(
`Event ${index + 1} -> ${section}.${method} :: ${data}`
);

if (method == `PoolCreate`) {
console.log(
Expand Down Expand Up @@ -324,12 +338,14 @@ class Market {
/**
* Creates swap pool for this market with specified liquidity.
* @param {KeyringPairOrExtSigner} signer The actual signer provider to sign the transaction.
* @param {string} swapFee The fee applied to each swap after pool creation.
* @param {string} amount The amount of each token to add to the pool.
* @param {string[]} weights The relative denormalized weight of each asset.
* @param {boolean} callbackOrPaymentInfo `true` to get txn fee estimation otherwise callback to capture transaction result.
*/
deploySwapPool = async (
signer: KeyringPairOrExtSigner,
swapFee: string,
amount: string,
weights: string[],
callbackOrPaymentInfo:
Expand All @@ -338,11 +354,21 @@ class Market {
): Promise<string> => {
const poolId = await this.getPoolId();
if (poolId) {
throw new Error(`Pool already exists for this market.`);
throw new Error(`Pool already exists for this market`);
}

if (weights.length !== this.outcomeAssets.length) {
console.log(
`Weights: ${weights.length}\nOutcome Assets: ${this.outcomeAssets.length}`
);
throw new Error(
`Provided weights length must match outcome assets length`
);
}

const tx = this.api.tx.predictionMarkets.deploySwapPoolForMarket(
this.marketId,
swapFee,
amount,
weights
);
Expand Down Expand Up @@ -370,7 +396,9 @@ class Market {
);

events.forEach(({ event: { data, method, section } }, index) => {
console.log(`Event ${index} -> ${section}.${method} :: ${data}`);
console.log(
`Event ${index + 1} -> ${section}.${method} :: ${data}`
);

if (method == `PoolCreate`) {
console.log(
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/src/types/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ export type CreateCpmmMarketAndDeployAssetsParams = {
signer: KeyringPairOrExtSigner;
oracle: string;
period: MarketPeriod;
metadata: DecodedMarketMetadata;
marketType: MarketTypeOf;
mdm: MarketDisputeMechanism;
swapFee: string;
amount: string;
weights: string[];
metadata: DecodedMarketMetadata;
callbackOrPaymentInfo:
| ((result: ISubmittableResult, _unsub: () => void) => void)
| boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/type-defs/src/predictionMarkets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default {
status: "MarketStatus",
report: "Option<Report>",
resolvedOutcome: "Option<OutcomeReport>",
mdm: "MarketDisputeMechanism",
disputeMechanism: "MarketDisputeMechanism",
},
ScoringRule: {
_enum: ["CPMM", "RikiddoSigmoidFeeMarketEma"],
Expand Down
2 changes: 1 addition & 1 deletion packages/type-defs/src/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default {
},
PoolId: "u128",
PoolStatus: {
_enum: ["Active", "CollectingSubsidy", "Stale"],
_enum: ["Active", "CollectingSubsidy", "Closed", "Clean", "Initialized"],
},
SubsidyUntil: {
marketId: "MarketId",
Expand Down
Loading

0 comments on commit cd3417b

Please sign in to comment.