API classes overview
Numbers
As input, numbers can be as plain javascript
numbers,big.jsinstances, but also astring.
The precision used when dividing is 20 decimal places.
Overrides
All API functions that produce a signed transaction can be equipped with the usual ethers.js overrides as optional parameters.
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.
mgv.contractgives access to the standard ethers.js contract and allows one to interact with the deployed Mangrove using low-level ethers.js calls. Hence, await mgv.contract.f(...) will produce the ethers.js call to Mangrove (signed when needed by the signer provided to the connect function).
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).
MgvToken.contract gives access to the ethers.js contract allowing one to interact with the deployed contract using low level calls (for instance if the token has functions that are do not belong to the ERC20 standard).
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:
Upon connection to a market, the API subscribes to events emanating from Mangrove in order to maintain a local cache of the order book. One may increase the size of the cache by using mgv.connect({..., maxOffers:<size of the book>}).
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:
The EOA providing the liquidity for ask and bid offers emanating from a direct liquidity provider is the address of the mgv's signer provided at the creation of the Mangrove instance.
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:

