Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getMarkets action #137

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/actions/market.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, expect, inject, it } from 'vitest'
import { getClient } from '~test/src/client.js'
import { getMarkets } from '../actions/market.js'
import { blastUSDB } from '../addresses/index.js'

const mgv = inject('mangrove')
const client = getClient()

describe('market', () => {
it('should fetch all the markets', async () => {
const markets = await getMarkets(client, mgv, {
USDC: { ...blastUSDB },
WETH: { cashness: 50 },
DAI: { cashness: 10000 },
})
expect(markets).toStrictEqual([
{
base: {
decimals: 18,
symbol: 'WETH',
address: '0x5FbDB2315678afecb367f032d93F642f64180aa3',
displayDecimals: 3,
mgvTestToken: false,
priceDisplayDecimals: 4,
},
quote: {
decimals: 6,
symbol: 'USDC',
address: '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512',
displayDecimals: 2,
mgvTestToken: false,
priceDisplayDecimals: 4,
},
tickSpacing: 1n,
},
{
base: {
decimals: 18,
symbol: 'WETH',
address: '0x5FbDB2315678afecb367f032d93F642f64180aa3',
displayDecimals: 3,
mgvTestToken: false,
priceDisplayDecimals: 4,
},
quote: {
decimals: 18,
symbol: 'DAI',
address: '0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0',
displayDecimals: 3,
mgvTestToken: false,
priceDisplayDecimals: 4,
},
tickSpacing: 1n,
},
])
})
})
67 changes: 67 additions & 0 deletions src/actions/market.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { Client } from 'viem'
import { readContract } from 'viem/actions'
import { buildToken } from '../addresses/index.js'
import {
getMarketsABI,
getMarketsBytecode,
} from '../addresses/logics/fetcher.js'
import type {
GetMarketsParams,
MangroveActionsDefaultParams,
MarketParams,
} from '../types/actions/index.js'
import { getAction } from '../utils/getAction.js'

/**
*
* @param client the viem client (public or wallet)
* @param actionParams the parameters for the Mangrove actions
* @param params the parameters for a given configs
* @returns gets the Markets
*/

export async function getMarkets(
client: Client,
actionParams: MangroveActionsDefaultParams,
eborrallo marked this conversation as resolved.
Show resolved Hide resolved
params: GetMarketsParams,
): Promise<MarketParams[]> {
const { mgvReader } = actionParams

const markets = await getAction(
client,
readContract,
'readContract',
)({
code: getMarketsBytecode,
abi: getMarketsABI,
functionName: 'getMarkets',
args: [mgvReader],
})

return markets.map((market: any) => {
const buildTokenWithParams = (tkn: any) =>
buildToken({
address: tkn.token,
symbol: tkn.symbol,
decimals: tkn.decimals,
displayDecimals: params[tkn.symbol]?.displayDecimals,
priceDisplayDecimals: params[tkn.symbol]?.priceDisplayDecimals,
})

const tkn0 = buildTokenWithParams(market.tkn0)
const tkn1 = buildTokenWithParams(market.tkn1)
const [base, quote] =
params[market.tkn0.symbol] &&
params[market.tkn1.symbol] &&
params[market.tkn0.symbol]!.cashness >
params[market.tkn1.symbol]!.cashness
? [tkn1, tkn0]
: [tkn0, tkn1]

return {
base,
quote,
tickSpacing: BigInt(market.tickSpacing),
}
})
}
41 changes: 41 additions & 0 deletions src/addresses/logics/fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { parseAbi } from 'viem'

/**
* @dev The bytecode for the getMarkets function, which depends on the mgv core contracts and the mgv reader contract.
* function getMarkets(MgvReader reader) public view returns (MarketWithToken[] memory) {
* (Market[] memory markets,) = reader.openMarkets();
* uint numMarkets = markets.length;
* MarketWithToken[] memory marketsWithToken = new MarketWithToken[](numMarkets);
*
* for (uint i = 0; i < numMarkets;) {
* IERC20 token0 = IERC20(markets[i].tkn0);
* IERC20 token1 = IERC20(markets[i].tkn1);
*
* marketsWithToken[i] = MarketWithToken({
* tkn0: Token({
* token: address(token0),
* decimals: token0.decimals(),
* symbol: token0.symbol()
* }),
* tkn1: Token({
* token: address(token1),
* decimals: token1.decimals(),
* symbol: token1.symbol()
* }),
* adr:
* tickSpacing: markets[i].tickSpacing
* });
*
* unchecked {
* i++;
* }
* }
*
* return marketsWithToken;
* }
*/
export const getMarketsBytecode =
'0x608060405234801561001057600080fd5b506108cb806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063d60d9bd714610030575b600080fd5b61004361003e366004610403565b610059565b60405161005091906104a0565b60405180910390f35b60606000826001600160a01b0316638267f6496040518163ffffffff1660e01b8152600401600060405180830381865afa15801561009b573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526100c391908101906106d6565b50805190915060008167ffffffffffffffff8111156100e4576100e4610531565b60405190808252806020026020018201604052801561015957816020015b6040805160c081018252600060608083018281526080840183905260a08401829052835283518082018552828152602080820184905281860192909252818401529282015282526000199092019101816101025790505b50905060005b828110156103e257600084828151811061017b5761017b6107d1565b6020026020010151600001519050600085838151811061019d5761019d6107d1565b602002602001015160200151905060405180606001604052806040518060600160405280856001600160a01b03168152602001856001600160a01b031663313ce5676040518163ffffffff1660e01b8152600401602060405180830381865afa15801561020e573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061023291906107e7565b60ff168152602001856001600160a01b03166395d89b416040518163ffffffff1660e01b8152600401600060405180830381865afa158015610278573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526102a0919081019061080a565b81525081526020016040518060600160405280846001600160a01b03168152602001846001600160a01b031663313ce5676040518163ffffffff1660e01b8152600401602060405180830381865afa158015610300573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061032491906107e7565b60ff168152602001846001600160a01b03166395d89b416040518163ffffffff1660e01b8152600401600060405180830381865afa15801561036a573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610392919081019061080a565b81525081526020018785815181106103ac576103ac6107d1565b6020026020010151604001518152508484815181106103cd576103cd6107d1565b6020908102919091010152505060010161015f565b50949350505050565b6001600160a01b038116811461040057600080fd5b50565b60006020828403121561041557600080fd5b8135610420816103eb565b9392505050565b60005b8381101561044257818101518382015260200161042a565b50506000910152565b60018060a01b03815116825260ff60208201511660208301526000604082015160606040850152805180606086015261048b816080870160208501610427565b601f01601f1916939093016080019392505050565b60006020808301818452808551808352604092508286019150828160051b87010184880160005b8381101561052357603f198984030185528151606081518186526104ed8287018261044b565b915050888201518582038a870152610505828261044b565b928901519589019590955250948701949250908601906001016104c7565b509098975050505050505050565b634e487b7160e01b600052604160045260246000fd5b604051610160810167ffffffffffffffff8111828210171561056b5761056b610531565b60405290565b6040516060810167ffffffffffffffff8111828210171561056b5761056b610531565b604051601f8201601f1916810167ffffffffffffffff811182821017156105bd576105bd610531565b604052919050565b600067ffffffffffffffff8211156105df576105df610531565b5060051b60200190565b600082601f8301126105fa57600080fd5b8151602061060f61060a836105c5565b610594565b828152610160928302850182019282820191908785111561062f57600080fd5b8387015b858110156106c95781818a03121561064b5760008081fd5b610653610547565b81518152858201518682015260408083015190820152606080830151908201526080808301519082015260a0808301519082015260c0808301519082015260e080830151908201526101008083015190820152610120808301519082015261014080830151908201528452928401928101610633565b5090979650505050505050565b60008060408084860312156106ea57600080fd5b835167ffffffffffffffff8082111561070257600080fd5b818601915086601f83011261071657600080fd5b8151602061072661060a836105c5565b8281526060928302850182019282820191908b85111561074557600080fd5b958301955b8487101561079f5780878d0312156107625760008081fd5b61076a610571565b8751610775816103eb565b815287850151610784816103eb565b8186015287890151898201528352958601959183019161074a565b50918901519197509094505050808311156107b957600080fd5b50506107c7858286016105e9565b9150509250929050565b634e487b7160e01b600052603260045260246000fd5b6000602082840312156107f957600080fd5b815160ff8116811461042057600080fd5b60006020828403121561081c57600080fd5b815167ffffffffffffffff8082111561083457600080fd5b818401915084601f83011261084857600080fd5b81518181111561085a5761085a610531565b61086d601f8201601f1916602001610594565b915080825285602082850101111561088457600080fd5b6103e281602084016020860161042756fea26469706673582212209ca3fb95d424a89adb93fea0a33bbc95b9acfb2caa994376dec3cab5a0940fb264736f6c63430008150033'
export const getMarketsABI = parseAbi([
'function getMarkets(address reader) view returns (((address token, uint8 decimals, string symbol) tkn0, (address token, uint8 decimals, string symbol) tkn1, uint256 tickSpacing)[])',
])
8 changes: 8 additions & 0 deletions src/types/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export type MarketParams = {
tickSpacing: bigint
}

export type TokenConfig = {
cashness: number
displayDecimals?: number
priceDisplayDecimals?: number
}

export type GetMarketsParams = Record<string, TokenConfig>

/**
* A serializable version of a market
* @param base The base token
Expand Down