Skip to content

Commit

Permalink
parse open markets result (buildTokens)
Browse files Browse the repository at this point in the history
Signed-off-by: belbazanas <[email protected]>
  • Loading branch information
anasbelbaz committed Sep 23, 2024
1 parent 4559bf7 commit 56b7580
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 98 deletions.
48 changes: 24 additions & 24 deletions src/actions/open-markets.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import { describe, expect, inject, it } from "vitest";
import { getClient } from "~test/src/client.js";
import { getOpenMarkets } from "./open-markets.js";
import type { Token } from "~mgv/_types/index.js";
import { describe, expect, inject, it } from 'vitest'
import type { Token } from '~mgv/_types/index.js'
import { getClient } from '~test/src/client.js'
import { getOpenMarkets } from './open-markets.js'

const params = inject("mangrove");
const params = inject('mangrove')

describe("Getting the open markets", () => {
it("should get the open markets", async () => {
const client = getClient();
const openMarkets = await getOpenMarkets(client, params);
describe('Getting the open markets', () => {
it('should get the open markets', async () => {
const client = getClient()
const openMarkets = await getOpenMarkets(client, params)

console.log(openMarkets.markets, "markets");
console.log(openMarkets.markets, 'markets')

expect(openMarkets.markets[0]?.tkn0).toBeTypeOf("object");
expect(openMarkets.markets[0]?.tkn1).toBeTypeOf("object");
expect(openMarkets.markets[0]?.tickSpacing).toBeTypeOf("bigint");
expect(openMarkets.markets[0]?.tkn0).toBeTypeOf('object')
expect(openMarkets.markets[0]?.tkn1).toBeTypeOf('object')
expect(openMarkets.markets[0]?.tickSpacing).toBeTypeOf('bigint')

expect(openMarkets.markets[1]?.tkn0).toBeTypeOf("object");
expect(openMarkets.markets[1]?.tkn1).toBeTypeOf("object");
expect(openMarkets.markets[1]?.tickSpacing).toBeTypeOf("bigint");
});
expect(openMarkets.markets[1]?.tkn0).toBeTypeOf('object')
expect(openMarkets.markets[1]?.tkn1).toBeTypeOf('object')
expect(openMarkets.markets[1]?.tickSpacing).toBeTypeOf('bigint')
})

it("should get the open markets config", async () => {
const client = getClient();
const openMarkets = await getOpenMarkets(client, params);
console.log(openMarkets.marketsConfig, "config");
it('should get the open markets config', async () => {
const client = getClient()
const openMarkets = await getOpenMarkets(client, params)
console.log(openMarkets.marketsConfig, 'config')

expect(openMarkets.marketsConfig).toBeTypeOf("object");
});
});
expect(openMarkets.marketsConfig).toBeTypeOf('object')
})
})
24 changes: 12 additions & 12 deletions src/actions/open-markets.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
// import type { SimulationParams } from '~mgv/types/actions/simulation.js'
// import type { openMarketsABI } from '../builder/open-markets.js'
import type { Client, ContractFunctionReturnType } from "viem";
import { multicall } from "viem/actions";
import type { Client, ContractFunctionReturnType } from 'viem'
import { multicall } from 'viem/actions'

import type { OpenMarketsResult } from "~mgv/types/actions/open-markets.js";
import type { MangroveActionsDefaultParams } from "~mgv/types/index.js";
import type { OpenMarketsResult } from '~mgv/types/actions/open-markets.js'
import type { MangroveActionsDefaultParams } from '~mgv/types/index.js'
import {
openMarketsABI,
openMarketsParams,
parseOpenMarketResult,
} from "../builder/open-markets.js";
import { getAction } from "../utils/getAction.js";
} from '../builder/open-markets.js'
import { getAction } from '../utils/getAction.js'

export async function getOpenMarkets(
client: Client,
actionParams: MangroveActionsDefaultParams
actionParams: MangroveActionsDefaultParams,
): Promise<OpenMarketsResult> {
const { mgvReader } = actionParams;
const { mgvReader } = actionParams

const result = await getAction(
client,
multicall,
"multicall"
'multicall',
)({
contracts: [
{
Expand All @@ -30,12 +30,12 @@ export async function getOpenMarkets(
},
],
allowFailure: false,
});
})

const parsedOpenMarkets = await parseOpenMarketResult({
client,
result: result[0],
});
})

return parsedOpenMarkets;
return parsedOpenMarkets
}
106 changes: 53 additions & 53 deletions src/builder/open-markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import {
type ContractFunctionParameters,
type ContractFunctionReturnType,
parseAbi,
} from "viem";
import type { Client } from "viem";
} from 'viem'
import type { Client } from 'viem'

import { multicall } from "viem/actions";
import type { Token } from "~mgv/_types/index.js";
import { buildToken } from "~mgv/addresses/index.js";
import type { OpenMarketsResult } from "~mgv/types/actions/open-markets.js";
import type { LocalConfig } from "~mgv/types/lib.js";
import { getAction } from "~mgv/utils/getAction.js";
import { multicall } from 'viem/actions'
import type { Token } from '~mgv/_types/index.js'
import { buildToken } from '~mgv/addresses/index.js'
import type { OpenMarketsResult } from '~mgv/types/actions/open-markets.js'
import type { LocalConfig } from '~mgv/types/lib.js'
import { getAction } from '~mgv/utils/getAction.js'

export const openMarketsABI = parseAbi([
"struct Market { address tkn0; address tkn1; uint tickSpacing; }",
"struct LocalUnpacked { bool active; uint fee; uint rawDensity; uint binPosInLeaf; uint level3; uint level2; uint level1; uint root; uint kilo_offer_gasbase; bool lock; uint last;}",
"struct MarketConfig { LocalUnpacked config01; LocalUnpacked config10; }",
"function openMarkets() external view returns (Market[] memory, MarketConfig[] memory)",
]);
'struct Market { address tkn0; address tkn1; uint tickSpacing; }',
'struct LocalUnpacked { bool active; uint fee; uint rawDensity; uint binPosInLeaf; uint level3; uint level2; uint level1; uint root; uint kilo_offer_gasbase; bool lock; uint last;}',
'struct MarketConfig { LocalUnpacked config01; LocalUnpacked config10; }',
'function openMarkets() external view returns (Market[] memory, MarketConfig[] memory)',
])

/**
*
Expand All @@ -38,22 +38,22 @@ export const openMarketsABI = parseAbi([
export function openMarketsParams() {
return {
abi: openMarketsABI,
functionName: "openMarkets",
functionName: 'openMarkets',
args: [],
} satisfies Omit<
ContractFunctionParameters<typeof openMarketsABI, "view", "openMarkets">,
"address"
>;
ContractFunctionParameters<typeof openMarketsABI, 'view', 'openMarkets'>,
'address'
>
}

export type ParseOpenMarketsParams = {
client: Client;
client: Client
result: ContractFunctionReturnType<
typeof openMarketsABI,
"view",
"openMarkets"
>;
};
'view',
'openMarkets'
>
}

/**
*
Expand All @@ -64,86 +64,86 @@ export async function parseOpenMarketResult({
client,
result,
}: ParseOpenMarketsParams): Promise<OpenMarketsResult> {
const [rawMarkets, rawMarketsConfigs] = result;
const [rawMarkets, rawMarketsConfigs] = result

const markets = await Promise.all(
rawMarkets.map(async (item) => {
const { tkn0, tkn1, tickSpacing } = item;
const { tkn0, tkn1, tickSpacing } = item

// Fetch token information for both tokens (tkn0 and tkn1)
const tokenInfos = await getAction(
client,
multicall,
"multicall"
'multicall',
)({
contracts: [
{
functionName: "decimals",
functionName: 'decimals',
abi: [
{
name: "decimals",
name: 'decimals',
outputs: [
{
type: "uint8",
type: 'uint8',
},
],
stateMutability: "view",
type: "function",
stateMutability: 'view',
type: 'function',
},
],
address: tkn0,
},
{
functionName: "symbol",
functionName: 'symbol',
abi: [
{
name: "symbol",
name: 'symbol',
outputs: [
{
type: "string",
type: 'string',
},
],
stateMutability: "view",
type: "function",
stateMutability: 'view',
type: 'function',
},
],
address: tkn0,
},
{
functionName: "decimals",
functionName: 'decimals',
abi: [
{
name: "decimals",
name: 'decimals',
outputs: [
{
type: "uint8",
type: 'uint8',
},
],
stateMutability: "view",
type: "function",
stateMutability: 'view',
type: 'function',
},
],
address: tkn1,
},
{
functionName: "symbol",
functionName: 'symbol',
abi: [
{
name: "symbol",
name: 'symbol',
outputs: [
{
type: "string",
type: 'string',
},
],
stateMutability: "view",
type: "function",
stateMutability: 'view',
type: 'function',
},
],
address: tkn1,
},
],
allowFailure: false,
});
})

return {
tkn0: buildToken({
Expand All @@ -161,12 +161,12 @@ export async function parseOpenMarketResult({
mgvTestToken: false,
}) as Token,
tickSpacing,
};
})
);
}
}),
)

const marketsConfig = rawMarketsConfigs?.map((item) => {
const { config01, config10 } = item;
const { config01, config10 } = item

return {
config01: {
Expand All @@ -179,8 +179,8 @@ export async function parseOpenMarketResult({
density: Number(config10.rawDensity),
offer_gasbase: config10.kilo_offer_gasbase,
} as LocalConfig,
};
});
}
})

return { markets, marketsConfig };
return { markets, marketsConfig }
}
18 changes: 9 additions & 9 deletions src/types/actions/open-markets.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { SimulateContractReturnType } from "viem";
import type { LocalConfig, Token } from "~mgv/_types/index.js";
import type { openMarketsABI } from "~mgv/builder/open-markets.js";
import type { SimulateContractReturnType } from 'viem'
import type { LocalConfig, Token } from '~mgv/_types/index.js'
import type { openMarketsABI } from '~mgv/builder/open-markets.js'

export type OpenMarketsResult = {
markets: {
tkn0: Token;
tkn1: Token;
tickSpacing: bigint;
}[];
marketsConfig: { config01: LocalConfig; config10: LocalConfig }[];
tkn0: Token
tkn1: Token
tickSpacing: bigint
}[]
marketsConfig: { config01: LocalConfig; config10: LocalConfig }[]
// request: SimulateContractReturnType<
// typeof openMarketsABI,
// "openMarkets"
// >["request"];
};
}

0 comments on commit 56b7580

Please sign in to comment.