Post a simple offer
Posting a simple offer is also referred to as an %%on-the-fly offer|on-the-fly-offer%%.
Prerequisitesβ
The tutorial assumes knowledge of JavaScript. Follow preparation to create a new tutorial
folder.
Make sure to use a chain where Mangrove is live. You can find all live addresses for Mangrove here.
When running the tutorial be aware that some of the script calls the chain and it can therefore take a few seconds before the transaction is completed.
Start local nodeβ
Before proceeding, import the environment variables made as part of the preparation
source .env
Start Foundry's local node anvil
to test things locally, with $RPC_URL
coming from .env
and pointing, for instance, to the Polygon Mumbai testnet.
anvil --fork-url $RPC_URL
Import and connectβ
Start up node
in a new terminal and issue the following code which performs the following steps:
- The first thing needed is to import
dotenv
, this handles the.env
file you added in the preparation. - Then import both
Mangrove
andethers
from the Mangrove package.ethers
will allow you to connect to a node and your wallet.Mangrove
will allow you to connect to the Mangrove protocol. - We connect to a local
anvil
node throughLOCAL_URL
. In order to connect to a real chain can replaceLOCAL_URL
withRPC_URL
. - The
PRIVATE_KEY
is needed in order to connect to your wallet. - Once you have connected your wallet, you can connect to the Mangrove protocol using your wallet.
loading...
Check existing marketβ
Next you need to connect to a market, in order to see the existing offers. This way you can figure out at what price you want to post your offer.
- Connect to the market using
mgv.market
, with a base and a quote. - Console log asks. This outputs table of the 50 best asks.
- Console log bids. This outputs table of the 50 best bids.
loading...
βββββββββββ¬βββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββ¬βββββββββββββββββββββββββ
β (index) β id β maker β volume β price β
βββββββββββΌβββββββΌβββββββββββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββΌβββββββββββββββββββββββββ€
β 0 β 2774 β '0x2CB51201CD176CcEa67a9c0B64391aE34e50C058' β 1317.1775894557795 β 1.00337113885004310077 β
β 1 β 3299 β '0x2CB51201CD176CcEa67a9c0B64391aE34e50C058' β 1308.2741138999688 β 1.00337482875577922516 β
β 2 β 1829 β '0x4326Ab97823d7509C1f0CB3bF68151081B26c970' β 50.674948479792484 β 1.00337923422410191358 β
β 3 β 598 β '0x4326Ab97823d7509C1f0CB3bF68151081B26c970' β 561.6921678391515 β 1.00337932460078748916 β
β 4 β 5026 β '0x2CB51201CD176CcEa67a9c0B64391aE34e50C058' β 189.47603337984367 β 1.00338137023837699789 β
...
Post new offerβ
After having looked at the market you now know what the prices are and you can now post an offer at a better price, so that the offer will be on top of the book.
- First create a
LiquidityProvider
. This allows for posting new offers. - Then you need to approve your account/wallet. To make sure that the transaction has been made, we do
await tx.wait()
. - Then you need to calculate how much %%provision|provision%% is needed.
- You can then post an offer using, in this case
wants: 100.5
andgives:100.4
, which gives a price of . And since you saw that the best price was you know our offer will be at the top of the list.
loading...
Check market after new offerβ
We can then check if our offer has best been posted and is on the top of the list, as excepted.
- First log the
offerId
in order to makes sure, you know what offer is yours. - Then log the asks for the market. You will then see that your offer is on top of the list.
loading...
> console.log(offerId);
5571
undefined
> market.consoleAsks();
βββββββββββ¬βββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββ¬βββββββββββββββββββββββββ
β (index) β id β maker β volume β price β
βββββββββββΌβββββββΌβββββββββββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββΌβββββββββββββββββββββββββ€
β 0 β 5571 β '0xA4C7c59EB3D4Ab5CA4E6fB012CeD9c8F9A5Ecdd8' β 100.4 β 1.00099601593625498008 β
β 1 β 2774 β '0x2CB51201CD176CcEa67a9c0B64391aE34e50C058' β 1317.1775894557795 β 1.00337113885004310077 β
β 2 β 3299 β '0x2CB51201CD176CcEa67a9c0B64391aE34e50C058' β 1308.2741138999688 β 1.00337482875577922516 β
β 3 β 1829 β '0x4326Ab97823d7509C1f0CB3bF68151081B26c970' β 50.674948479792484 β 1.00337923422410191358 β
Another way to check your offer is to go to testnet and look at the asks for the pair. Here you will be able to see your offer. This can only be done if you didn't use a local chain, but actually ran on a real chain.
The full script can be found on github.