Getting started
Full source code for this example can be found here: https://github.com/mangrovedao/mgv-example
To interact with mangrove in javascript, it is preferred to use viem with the @mangrovedao/mgv sdk that was built on top of it.
To get started with a project (using bun) type the following commands:
bun init
Then add viem and mgv as dependencies to your project:
bun add viem @mangrovedao/mgv
Before starting we will define our client and config.
For the config, you have to define the mangrove addresses needed for your chains. Here is an example of the config for mangrove base addresses (All addresses can be found in the docs or requested to the mangrove team):
import type { MangroveActionsDefaultParams } from "@mangrovedao/mgv";
export const mangroveConfig = {
mgv: "0x22613524f5905Cb17cbD785b956e9238Bf725FAa",
mgvOrder: "0xA3c363Ca0EA3603faEe9FAcffD65E777122adF36",
mgvReader: "0xe5B118Ea1ffBC502EA7A666376d448209BFB50d3",
smartRouter: "0x1424D7428dc11623100df1A3D06088C2d87fBE32",
routerProxyFactory: "0x2926Cc3977F93a51465f9742c548e67220Af54e9",
} satisfies MangroveActionsDefaultParams;
Then we need to configure the client. We assume an optional `RPC_URL` environment variable and a mandatory `PRIVATE_KEY` environment variable.
import { mangroveActions } from "@mangrovedao/mgv";
import { createWalletClient, http, publicActions, type Hex } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { base } from "viem/chains";
import { mangroveConfig } from "./config";
export const client = createWalletClient({
chain: base,
transport: http(process.env.RPC_URL),
account: privateKeyToAccount(process.env.PRIVATE_KEY as Hex),
})
.extend(publicActions)
.extend(mangroveActions(mangroveConfig));
With this ready, we are ready to query mangrove.
Last updated