Skip to content

Commit

Permalink
Merge pull request MonacoProtocol#60 from BetDexLabs/main
Browse files Browse the repository at this point in the history
merge main into develop
  • Loading branch information
eoin-betdex authored Feb 8, 2023
2 parents 823d57a + c464322 commit cfe17b2
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

127 changes: 127 additions & 0 deletions npm-admin-client/docs/endpoints/market_outcome.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

* [initialiseOutcome][1]
* [Parameters][2]
* [Examples][3]
* [initialiseOutcomes][4]
* [Parameters][5]
* [Examples][6]
* [findMarketOutcomePda][7]
* [Parameters][8]
* [Examples][9]
* [findNextOutcomePda][10]
* [Parameters][11]
* [Examples][12]

## initialiseOutcome

For the given market account, initialise an outcome account for the provided outcome.

**Note** To add prices to an outcome use `batchAddPricesToOutcomePool`.

### Parameters

* `program` **Program** {program} anchor program initialized by the consuming client
* `marketPk` **PublicKey** {PublicKey} publicKey of the market to initialise the outcome for
* `outcome` **[string][13]** {string} string representation of the outcome

### Examples

```javascript
const marketPk = new PublicKey('7o1PXyYZtBBDFZf9cEhHopn2C9R4G6GaPwFAxaNWM33D')
const outcome = "Draw"
const initialiseOutcomeRequest = await initialiseOutcome(program, marketPk, outcome)
```

Returns **OutcomeInitialisationResponse** the outcome provided, the pda for the outcome account and the transaction ID of the request

## initialiseOutcomes

For the given market account, initialise outcome accounts for the provided outcomes

### Parameters

* `program` **Program** {program} anchor program initialized by the consuming client
* `marketPk` **PublicKey** {PublicKey} publicKey of the market to initialise the outcome for
* `outcomes` **[Array][14]<[string][13]>** {string\[]} list of strings representing the market outcomes

### Examples

```javascript
const marketPk = new PublicKey('7o1PXyYZtBBDFZf9cEhHopn2C9R4G6GaPwFAxaNWM33D')
const outcomes = ["Monaco Protocol", "Draw"]
const initialiseOutcomeRequest = await initialiseOutcomes(program, marketPk, outcomes)
```

Returns **OutcomeInitialisationsResponse** list of the outcomes provided, their pdas and the transaction IDs performed in the request

## findMarketOutcomePda

For the given market and outcome index, returns the pda for that outcome account

### Parameters

* `program` **Program** {program} anchor program initialized by the consuming client
* `marketPk` **PublicKey** {PublicKey} publicKey of the market to initialise the outcome for
* `marketOutcomeIndex` **[number][15]** {number} number representing the index of the outcome

### Examples

```javascript
const marketPk = new PublicKey('7o1PXyYZtBBDFZf9cEhHopn2C9R4G6GaPwFAxaNWM33D')
const outcomeIndex = 2
const outcomePda = await findMarketOutcomePda(program, marketPk, outcomeIndex)
```

Returns **FindPdaResponse** pda of the market outcome account

## findNextOutcomePda

For the given market, return the pda for the next possible outcome account based off how many outcomes already exist on that market account

### Parameters

* `program` **Program** {program} anchor program initialized by the consuming client
* `marketPk` **PublicKey** {PublicKey} publicKey of the market to initialise the outcome for

### Examples

```javascript
const marketPk = new PublicKey('7o1PXyYZtBBDFZf9cEhHopn2C9R4G6GaPwFAxaNWM33D')
const outcomeIndex = 2
const outcomePda = await findNextOutcomePda(program, marketPk)
```

Returns **FindPdaResponse** pda of the next possible market outcome account

[1]: #initialiseoutcome

[2]: #parameters

[3]: #examples

[4]: #initialiseoutcomes

[5]: #parameters-1

[6]: #examples-1

[7]: #findmarketoutcomepda

[8]: #parameters-2

[9]: #examples-2

[10]: #findnextoutcomepda

[11]: #parameters-3

[12]: #examples-3

[13]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String

[14]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array

[15]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
3 changes: 0 additions & 3 deletions npm-admin-client/docs/endpoints/market_outcomes.md

This file was deleted.

2 changes: 1 addition & 1 deletion npm-admin-client/generate_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ declare -a endpoints=(
"market_create"
"market_management"
"market_outcome_prices"
"market_outcomes"
"market_outcome"
"operators"
"utils"
)
Expand Down
4 changes: 2 additions & 2 deletions npm-admin-client/package-lock.json

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

2 changes: 1 addition & 1 deletion npm-admin-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@monaco-protocol/admin-client",
"version": "2.0.0-rc1",
"version": "2.0.0-rc2",
"description": "Admin interface package for the Monaco Protocol on Solana",
"author": "Monaco Protocol",
"license": "MIT",
Expand Down
13 changes: 5 additions & 8 deletions npm-admin-client/src/market_outcome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,25 @@ import { confirmTransaction } from "./utils";
import { findAuthorisedOperatorsAccountPda } from "./operators";

/**
* For the given market account, initialise an outcome account for the provided outcome; optional priceLadder added to the outcome if supplied.
* For the given market account, initialise an outcome account for the provided outcome.
*
* **Note** It is advised to create an outcome with an empty priceLadder then use `batchAddPricesToOutcomePool` if adding a large priceLadder.
* **Note** To add prices to an outcome use `batchAddPricesToOutcomePool`.
*
* @param program {program} anchor program initialized by the consuming client
* @param marketPk {PublicKey} publicKey of the market to initialise the outcome for
* @param outcome {string} string representation of the outcome
* @param priceLadder {number[]} optional priceLadder to add to the outcome at time of creation
* @returns {OutcomeInitialisationResponse} the outcome provided, the pda for the outcome account and the transaction ID of the request
*
* @example
*
* const marketPk = new PublicKey('7o1PXyYZtBBDFZf9cEhHopn2C9R4G6GaPwFAxaNWM33D')
* const outcome = "Draw"
* const priceLadder = [1, 2, 3, 4]
* const initialiseOutcomeRequest = await initialiseOutcome(program, marketPk, outcome, priceLadder)
* const initialiseOutcomeRequest = await initialiseOutcome(program, marketPk, outcome)
*/
export async function initialiseOutcome(
program: Program,
marketPk: PublicKey,
outcome: string,
priceLadder: number[] = [],
): Promise<ClientResponse<OutcomeInitialisationResponse>> {
const response = new ResponseFactory({} as OutcomeInitialisationResponse);
const provider = program.provider as AnchorProvider;
Expand All @@ -54,7 +51,7 @@ export async function initialiseOutcome(
}
try {
const tnxId = await program.methods
.initializeMarketOutcome(outcome, priceLadder)
.initializeMarketOutcome(outcome)
.accounts({
systemProgram: SystemProgram.programId,
outcome: nextOutcomePda.data.pda,
Expand Down Expand Up @@ -120,7 +117,7 @@ export async function initialiseOutcomes(
outcomeIndex,
);
const tnxId = await program.methods
.initializeMarketOutcome(outcomes[i], [])
.initializeMarketOutcome(outcomes[i])
.accounts({
systemProgram: SystemProgram.programId,
outcome: nextOutcomePda.data.pda,
Expand Down
4 changes: 2 additions & 2 deletions npm-client/package-lock.json

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

2 changes: 1 addition & 1 deletion npm-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@monaco-protocol/client",
"version": "3.0.0-rc1",
"version": "3.0.0-rc2",
"description": "Interface package for the Monaco Protocol on Solana",
"author": "Monaco Protocol",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion programs/monaco_protocol/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "monaco_protocol"
version = "0.6.0-rc1"
version = "0.6.0-rc2"
description = "Created with Anchor"
edition = "2018"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,18 @@ pub fn create(
Ok(())
}

pub fn initialize_outcome(
ctx: Context<InitializeMarketOutcome>,
title: String,
price_ladder: Vec<f64>,
) -> Result<()> {
pub fn initialize_outcome(ctx: Context<InitializeMarketOutcome>, title: String) -> Result<()> {
require!(
ctx.accounts.market.market_status == MarketStatus::Initializing,
CoreError::MarketOutcomeMarketInvalidStatus
);
verify_prices_precision(&price_ladder)?;

ctx.accounts.outcome.market = ctx.accounts.market.key();
ctx.accounts.outcome.index = ctx.accounts.market.market_outcomes_count;
ctx.accounts.outcome.title = title;
ctx.accounts.outcome.latest_matched_price = 0_f64;
ctx.accounts.outcome.matched_total = 0_u64;
ctx.accounts.outcome.price_ladder = price_ladder;
ctx.accounts.outcome.price_ladder = vec![];

ctx.accounts
.market
Expand All @@ -79,7 +74,7 @@ fn verify_prices_precision(prices: &[f64]) -> Result<()> {
require!(
prices
.iter()
.all(|&value| format!("{value}") <= format!("{value:.3}")),
.all(|&value| (format!("{value}")).len() <= (format!("{value:.3}")).len()),
CoreError::MarketPricePrecisionTooLarge
);
Ok(())
Expand Down Expand Up @@ -161,5 +156,8 @@ mod tests {

let not_ok_3 = verify_prices_precision(&vec![1.111, 1.11, 1.1, 1_f64, 1.1111]);
assert!(not_ok_3.is_err());

let attempting_to_round_not_ok = verify_prices_precision(&vec![1.1118]);
assert!(attempting_to_round_not_ok.is_err());
}
}
3 changes: 1 addition & 2 deletions programs/monaco_protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ pub mod monaco_protocol {
pub fn initialize_market_outcome(
ctx: Context<InitializeMarketOutcome>,
title: String,
price_ladder: Vec<f64>,
) -> Result<()> {
msg!("Initializing market outcome");
verify_operator_authority(
Expand All @@ -187,7 +186,7 @@ pub mod monaco_protocol {
&ctx.accounts.market.authority,
)?;

instructions::market::initialize_outcome(ctx, title, price_ladder)?;
instructions::market::initialize_outcome(ctx, title)?;
msg!("Initialized market outcome");
Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions tests/market/initialize_market_outcome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("Market: creation", () => {
);

await monaco.program.methods
.initializeMarketOutcome("EXTRA", [1.001, 1.01, 1.1, 1.2])
.initializeMarketOutcome("EXTRA")
.accounts({
systemProgram: SystemProgram.programId,
outcome: marketOutcomePk,
Expand All @@ -43,7 +43,7 @@ describe("Market: creation", () => {
const marketOutcome = await monaco.fetchMarketOutcome(marketOutcomePk);
assert.deepEqual(marketOutcome.index, 3);
assert.deepEqual(marketOutcome.title, "EXTRA");
assert.deepEqual(marketOutcome.priceLadder, [1.001, 1.01, 1.1, 1.2]);
assert.deepEqual(marketOutcome.priceLadder, []);
});

it("Failure: using incorrect market account", async () => {
Expand All @@ -66,7 +66,7 @@ describe("Market: creation", () => {

try {
await monaco.program.methods
.initializeMarketOutcome("EXTRA", [1.001, 1.01, 1.1, 1.2])
.initializeMarketOutcome("EXTRA")
.accounts({
systemProgram: SystemProgram.programId,
outcome: marketOutcomePk,
Expand Down Expand Up @@ -116,7 +116,7 @@ describe("Market: creation", () => {

try {
await monaco.program.methods
.initializeMarketOutcome("EXTRA", [1.001, 1.01, 1.1, 1.2])
.initializeMarketOutcome("EXTRA")
.accounts({
systemProgram: SystemProgram.programId,
outcome: marketOutcomePk,
Expand Down Expand Up @@ -165,7 +165,7 @@ describe("Market: creation", () => {

try {
await monaco.program.methods
.initializeMarketOutcome("EXTRA", [1.001, 1.01, 1.1, 1.2])
.initializeMarketOutcome("EXTRA")
.accounts({
systemProgram: SystemProgram.programId,
outcome: marketOutcomePk,
Expand Down
25 changes: 24 additions & 1 deletion tests/util/test_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export async function createMarket(
for (let index = 0; index < outcomes.length; index++) {
await getAnchorProvider().connection.confirmTransaction(
await protocolProgram.methods
.initializeMarketOutcome(outcomes[index], priceLadder)
.initializeMarketOutcome(outcomes[index])
.accounts({
systemProgram: SystemProgram.programId,
outcome: outcomePdas[index],
Expand All @@ -260,6 +260,29 @@ export async function createMarket(
}),
"confirmed",
);

const priceLadderBatchSize = 20;
for (let i = 0; i < priceLadder.length; i += priceLadderBatchSize) {
const batch = priceLadder.slice(i, i + priceLadderBatchSize);
await getAnchorProvider().connection.confirmTransaction(
await protocolProgram.methods
.addPricesToMarketOutcome(index, batch)
.accounts({
systemProgram: SystemProgram.programId,
outcome: outcomePdas[index],
market: marketPda,
authorisedOperators: authorisedMarketOperators,
marketOperator: marketOperator.publicKey,
})
.signers([marketOperator])
.rpc()
.catch((e) => {
console.error(e);
throw e;
}),
"confirmed",
);
}
}

let matchingPools: { against: PublicKey; forOutcome: PublicKey }[][] = [];
Expand Down
Loading

0 comments on commit cfe17b2

Please sign in to comment.