API classes overview
Mangrove
The root class of the API. Use Mangrove.connect to get an instance of it. Here are a few possibilities:
mgv = await Mangrove.connect(window.ethereum); // web browser
mgv = await Mangrove.connect('http://127.0.0.1:8545'); // HTTP provider
mgv = await Mangrove.connect(); // Uses Ethers.js fallback mainnet (for testing only)
mgv = await Mangrove.connect('rinkeby'); // Uses Ethers.js fallback (for testing only)
// Init with private key (server side)
mgv = await Mangrove.connect(
'https://mainnet.infura.io/v3/_your_project_id_', // provider
{
privateKey: '0x_your_private_key_', // preferably with environment variable
});
// Init with HD mnemonic (server side)
mgv = await Mangrove.connect( {
signer: myEthersWallet
});You can test you are indeed connected to the deployed Mangrove by asking for the current global configuration of Mangrove:
config = await mgv.config()
The above mgv object gives you access to the MgvToken, Market and OfferLogic (allowing one to connect to an onchain offer logic) and LiquidityProvider(an abstraction layer to pass bids and asks on Mangrove) objects.
MgvToken
This class provides easy means to interact with a deployed contract on the standard EIP-20. To obtain an instance use:
with the above MgvT object one has access to standard calls using human readable input/outputs. For instance:
Note that Mangrove's API deals with token decimals automatically (see definitions in constants.ts).
Market
The Market class is an abstraction layer to interact with Mangrove as a liquidity taker, using standard market buy and sell orders. To obtain one instance use:
For debugging purpose, the class provides a console of the current state of bids and asks posted on Mangrove. For instance to display the bid offers on Mangrove on this market:
Market instances allow one to subscribe to markets events using:
To unsubscribe f from market events simply use mgvMarket.unsubscribe(f).
Market events are records of the following kinds:
{type: 'OfferRetract', ba:'asks'|'bids', offer:Market.Offer}when an ask or a bidofferis removed from the book{type: 'OfferWrite', ba:'asks'|'bids', offer:Market.Offer}when a bid or askofferis added to the book (or updated){type:'OfferFail', ba:'asks'|'bids', taker:string, 'takerWants':Big, takerGives:Big, mgvData:string, offer:Market.Offer}whenofferfailed to deliver. Note thatmgvDatais a bytes32 string encoding of the fail reason (according to Mangrove).{type: 'OfferSuccess', ba: 'asks'|'bids', taker: string, takerWants:Big, takerGives:Big, offer:Market.Offer}whenofferwas successfully executed (possibly on a partial fill wheneveroffer.gives>takerWants).
and where Market.Offer has the following main fields:
OfferLogic
A reactive offer is managed by a smart contract which implements its logic. One may use the API to post liquidity on Mangrove via a deployed logic that complies to the IOfferLogic interface. To do so, one first need an OfferLogic instance:
The mgvLogic instance offers various function to query and set the underlying contract state, for instance:
When using an offer logic that inherits from the MultiUser.sol solidity class, one should always use the above depositToken (and tokenBalance) instead of sending tokens (or querying balance) directly to the contract which might result in the tokens being burnt (as only depositToken will increase user balance on the contract).
LiquidityProvider
A LiquidityProvider instance is the object one needs to post Bids and Asks on a Mangrove market. There are two means to obtain an LiquidityProvider: either to post a direct Offer or to post an Offer relying on some onchain logic.
To act as a direct liquidity provider on a some mgvMarket you must obtain a LiquidityProvider instance from an mgv object using:
For more complete experience of the Mangrove capabilities, on may rather post bids and asks via an offer logic mgvLogic. To do so, one does:
Besides posting offers on Mangrove, a LiquidityProvider instance mgvLP gives access to various useful functions such as:
which return the missing provision (in native tokens) this liquidity provider needs to deposit on Mangrove if it wishes to post a new bid or ask. When provision is missing, one may fund Mangrove using:

