Skip to content

Commit

Permalink
factorize code
Browse files Browse the repository at this point in the history
Signed-off-by: belbazanas <[email protected]>
  • Loading branch information
anasbelbaz committed Sep 25, 2024
1 parent 7b13c86 commit 3a2b2bb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 94 deletions.
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"javascript": {
"formatter": {
"quoteStyle": "single",
"trailingCommas": "all",
"trailingComma": "all",
"semicolons": "asNeeded"
}
},
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "mgv",
"workspaces": ["src", "test"],
"workspaces": [
"src",
"test"
],
"module": "src/index.ts",
"private": true,
"type": "module",
Expand All @@ -15,7 +18,7 @@
"clean": "rimraf src/_esm src/_cjs src/_types",
"format": "biome format . --write",
"lint": "biome check .",
"lint:fix": "bun run lint --write",
"lint:fix": "biome check . -apply",
"prepublishOnly": "bun scripts/prepublishOnly.ts",
"test": "vitest -c ./test/vitest.config.ts dev",
"test:ci": "CI=true vitest -c ./test/vitest.config.ts --retry=3"
Expand Down
122 changes: 37 additions & 85 deletions src/builder/open-markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type ContractFunctionReturnType,
parseAbi,
} from 'viem'
import type { Client } from 'viem'
import type { Abi, Client } from 'viem'

import { multicall } from 'viem/actions'
import type { Token } from '~mgv/_types/index.js'

Check failure on line 9 in src/builder/open-markets.ts

View workflow job for this annotation

GitHub Actions / Verify / Build

Cannot find module '~mgv/_types/index.js' or its corresponding type declarations.
Expand All @@ -27,7 +27,7 @@ export const openMarketsABI = parseAbi([
*
* ```ts
* walletClient.writeContract({
* address: "0x...",
* address: '0x...',
* ...marketOrderParams({
* ...
* })
Expand Down Expand Up @@ -67,99 +67,51 @@ export async function parseOpenMarketResult({
const [rawMarkets, rawMarketsConfigs] = result

const markets = await Promise.all(
rawMarkets.map(async (item) => {
const { tkn0, tkn1, tickSpacing } = item
rawMarkets.map(async ({ tkn0, tkn1, tickSpacing }) => {
const tokenAbi = [
{
name: 'decimals',
inputs: [],
outputs: [{ type: 'uint8' }],
stateMutability: 'view',
type: 'function',
},
{
name: 'symbol',
inputs: [],
outputs: [{ type: 'string' }],
stateMutability: 'view',
type: 'function',
},
]

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

return {
tkn0: buildToken({
address: tkn0,
symbol: tokenInfos[1] as string, // Symbol for tkn0
displayDecimals: tokenInfos[0] as number,
priceDisplayDecimals: tokenInfos[0] as number, // Decimals for tkn0
mgvTestToken: false,
}) as Token,
tkn1: buildToken({
address: tkn1,
symbol: tokenInfos[3] as string, // Symbol for tkn1
displayDecimals: tokenInfos[2] as number,
priceDisplayDecimals: tokenInfos[2] as number, // Decimals for tkn1
const buildTokenFromInfo = (address: string, index: number) => {
const decimalsIndex = index * 2
const symbolIndex = decimalsIndex + 1
return buildToken({
address: address as `0x${string}`,
symbol: tokenInfos[symbolIndex] as string,
displayDecimals: tokenInfos[decimalsIndex] as number,
priceDisplayDecimals: tokenInfos[decimalsIndex] as number,
mgvTestToken: false,
}) as Token,
}) as Token
}

return {
tkn0: buildTokenFromInfo(tkn0, 0),
tkn1: buildTokenFromInfo(tkn1, 1),
tickSpacing,
}
}),
Expand Down
6 changes: 0 additions & 6 deletions src/types/actions/open-markets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { SimulateContractReturnType } from 'viem'
import type { LocalConfig, Token } from '~mgv/_types/index.js'

Check failure on line 1 in src/types/actions/open-markets.ts

View workflow job for this annotation

GitHub Actions / Verify / Build

Cannot find module '~mgv/_types/index.js' or its corresponding type declarations.
import type { openMarketsABI } from '~mgv/builder/open-markets.js'

export type OpenMarketsResult = {
markets: {
Expand All @@ -9,8 +7,4 @@ export type OpenMarketsResult = {
tickSpacing: bigint
}[]
marketsConfig: { config01: LocalConfig; config10: LocalConfig }[]
// request: SimulateContractReturnType<
// typeof openMarketsABI,
// "openMarkets"
// >["request"];
}

0 comments on commit 3a2b2bb

Please sign in to comment.