2.0.5-51 • Published 12 days ago

@mangrovedao/mangrove.js v2.0.5-51

Weekly downloads
-
License
MIT
Repository
github
Last release
12 days ago

mangrove.js

A JavaScript library for Mangrove. Wraps around Ethers.js. Works in the web browser and Node.js.

This SDK is in open beta, and is constantly under development. USE AT YOUR OWN RISK.

Install / Import

Web Browser

<script
  type="text/javascript"
  src="https://cdn.jsdelivr.net/npm/@mangrovedao/mangrove.js"
></script>

<script type="text/javascript">
  Mangrove.(...)
</script>

Node.js

npm install @mangrovedao/mangrove.js
const { Mangrove } = require("..."); // cjs
import { Mangrove } from "..."; // or using ES6

Using the API as a liquidity taker

Connecting the API to a deployed Mangrove contract

// use alchemy or infura to connect to the network
const mgv = await Mangrove.connect({
  provider: process.env.NODE_URL,
  privateKey: process.env.SK,
});

Obtaining a market object

  // Connect to ETH-USDC market
  const market = mgv.market({ base: "ETH", quote: "USDC" });

  // Check allowance
  const allowance = await market.base.allowance(); // by default returns Mangrove allowance for current signer address
  // Set max allowance for Mangrove spending quote tokens on behalf of signer
  await market.quote.approveMangrove();
  // Set specific allowance
  await market.base.approveMangrove(0.42);

    // Read order book
  market.book();
  /*
    Returns
    {
      asks: [
        {id: 3, price: 3700, volume: 4, ...},
        {id: 56, price: 3701, volume: 7.12, ...}
      ],
      bids: [
        {id: 811, price: 3600, volume: 1.23, ...},
        {id: 80, price: 3550, volume: 1.11, ...}
      ]
    }
  */
 // pretty printing to console
 await market.consoleAsks();
 /*
 prints by default
┌─────────┬─────┬──────────────────────────────────────────────┬────────────────────┬────────────────────────┐
│ (index) │ id  │                    maker                     │       volume       │         price          │
├─────────┼─────┼──────────────────────────────────────────────┼────────────────────┼────────────────────────┤
│    0    │ 63  │ '0xcBb37575320FF499E9F69d0090b6944bc0aD7585' │        1000        │          1.01          │
│    1    │ 82  │ '0x3073A02460D7BE1A1C9afC60A059Ad8d788A4502' │ 6.661453068436484  │ 1.01007136594287569481 │
│    2    │ 152 │ '0x3073A02460D7BE1A1C9afC60A059Ad8d788A4502' │ 2.5010075605785964 │ 1.01008930953234142236 │
│    3    │ 123 │ '0x3073A02460D7BE1A1C9afC60A059Ad8d788A4502' │ 8.931944047363888  │ 1.01028129510766635908 │
│    4    │ 140 │ '0x3073A02460D7BE1A1C9afC60A059Ad8d788A4502' │ 3.6346162334836647 │ 1.01073119251408598466 │
└─────────┴─────┴──────────────────────────────────────────────┴────────────────────┴────────────────────────┘
 */
// Apply filter using
const filter = ["id", "wants", "gives", "gasprice", "maker"];
await market.consoleAsks(filter);

// Subscribe to order book
market.subscribe((event) => {
  /* `event` is an offer write, failure, success, or cancel */
  console.log(market.book());
}

Buying and selling

// Buy ETH with USDC (taker needs to approve USDC for mangrove transfer)
await market.quote.approveMangrove();
const { takerGot, takerGave, bounty } = await market.buy({
  volume: 2.1,
  price: 3700,
});
// Sell ETH for USDC (taker needs to approve WETH for mangrove transfer)
await market.base.approveMangrove();
const { takerGot2, takerGave2, bounty2 } = await market.sell({
  volume: 1.1,
  price: 3750,
});

Using the API as a liquidity provider

Connect to a deployed offer logic (that should match the IOfferLogic.sol interface)

const mgv = await Mangrove.connect("maticmum"); // Mumbai testnet
// get an `OfferLogic` object connected to a deployed offer logic (not an async function)
const logic = mgv.offerLogic(logicAddress);

// approve mangrove for transfers from the logic
logic.approveMangrove("WETH", 10000);
logic.approveMangrove("USDC", 1000000000000000);

// read current allowance
const allowance = await logic.mangroveAllowance("USDC");
// which is equivalent to the more generic call
const allowance2 = await mgv
  .token("USDC")
  .allowance({ owner: logic.address, spender: mgv.contract.address });

// fund Mangrove with 0.1 ethers so that offer logic contract can post offers (bot must have ethers)
await logic.fundMangrove(0.1);
// equivalent to
await logic.mgv.fundMangrove(0.1, logic.address);

// check current balance of offer logic at Mangrove
let balance = await logic.balanceOnMangrove();
// which is equivalent to the more generic call
balance = await mgv.balanceOf(logic.address);

// withdraw offer logic's balance from Mangrove to the signer's address
await logic.withdrawFromMangrove(0.01);

Become a liquidity provider on a market

Connect an EOA to a market

To post direct offers on a market (via signer's EOA), on gets a LiquidityProvider object from the API by connecting it to a market:

const market = await mgv.market({ base: "ETH", quote: "USDC" });
// get a liquidity provider using signer's account for posting offers
let liquidity_provider = await mgv.liquidityProvider(market);
//or one is not connected to a market already:
liquidity_provider = await mgv.liquidityProvider({
  base: "ETH",
  quote: "USDC",
});

Connect an onchain offer logic to a market

To post an offer via an onchain logic, one uses the OfferLogic object to connect to a market:

const logic = mgv.offerLogic(logicAddress);
// get a liquidity provider using an onchain offer logic for posting offers
const liquidity_provider = logic.liquidityProvider({
  base: "ETH",
  quote: "USDC",
});

Send bids and asks to Mangrove

// post a new offer giving weth against usdc
// you can give gives/wants or price/volume
// id:number is the new offer id
// event:ethers.Event is the ethers.js event that created the offer
const { id: askId, event } = await liquidity_provider.newAsk({
  wants: 3500,
  gives: 1,
});

// post a new offer giving usdc against weth
const { id: bidId, event_ } = await liquidity_provider.newBid({
  price: 3400,
  volume: 2,
});

const asks = await liquidity_provider.asks();
const bids = await liquidity_provider.bids();

// Update an existing ask.
// You can update gives/wants or price/volume. Other parameters will not change.

const promise = liquidity_provider.updateAsk(askId, { wants: 3600, gives: 1 });
promise.then((event) => console.log("offer updated", event));

// Cancel an existing bid.
await liquidity_provider.retractBid(bidId);

More Code Examples

See the docblock comments above each function definition or the official SDK Documentation.

Instance Creation (Ethereum provider and signer)

See examples directory for instance creation examples.

Mangrove SDK initialization requires an Ethereum provider and an Ethereum signer. They can be provided and configured in various ways, some of which are described below.

The following are valid Ethereum providers for initialization of the SDK.

mgv = await Mangrove.connect(window.ethereum); // web browser

mgv = await Mangrove.connect('http://127.0.0.1:8545'); // HTTP provider

mgv = await Mangrove.connect('rinkeby'); // Uses Ethers.js fallback (for testing only)

// Init with private key (server side)
/* Note that if you provide BOTH provider and signer info,
   any connection info on the signer will be ignored, and any credentials on the provider will be ignored. */
mgv = await Mangrove.connect({
  provider: 'https://mainnet.infura.io/v3/_your_project_id_',
  privateKey: '0x_your_private_key_', // preferably with environment variable
});

// Init with a signer and a provider,
mgv = await Mangrove.connect({
  provider: 'https://mainnet.infura.io/v3/_your_project_id_',
  signer: await new ethers.Wallet(...)
});

// Init with HD mnemonic (server side)
mgv = await Mangrove.connect({
  provider: 'mainnet',
  mnemonic: 'clutch captain shoe...', // preferably with environment variable
});

// Init with a keystore file (json wallet)
mgv = await Mangrove.connect({
  provider: 'https://mainnet.infura.io/v3/_your_project_id_',
  jsonWallet: {
    path: 'path/to/UTC--created_date_time--address',
    password: 'wallet_password' // preferably with environment variable
  }
});

// Init with a custom ethers.js provider, for example a WebSocketProvider (server side)
provider = new ethers.providers.WebSocketProvider('wss://polygon-mumbai.g.alchemy.com/v2/_your_project_id_');
signer = new ethers.Wallet('0x_your_private_key_', provider);
mgv = await Mangrove.connect({signer: signer});

// Init with a EIP-1193 provider object (server side)
provider = new Web3.providers.WebsocketProvider('wss://polygon-mumbai.g.alchemy.com/v2/_your_project_id_');
mgv = await Mangrove.connect({
  provider: provider,
  privateKey: '0x_your_private_key_'
});

Here is the type of the argument to connect: string | CreateSignerOption, where a string indicates a URL, and:

/* privateKey, mnemonic, signer, jsonWallet *will override*
   any credentials stored in provider object */
export interface CreateSignerOptions {
  // object or URL
  provider?: Provider | string;
  // optional in addition to provider object: gets signer number `signerIndex` of the provider
  signerIndex?: number;
  // raw privkey without 0x prefix
  privateKey?: string;
  // BIP39 mnemonic
  mnemonic?: string;
  // optional in addition to mnemonic: BIP44 path
  path?: string;
  // signer object
  signer?: any;
  // json wallet access information
  jsonWallet?: JsonWalletOptions;
  // if constructor finds no signer, it will throw unless this option is set to true.
  forceReadOnly?: boolean;
}

interface JsonWalletOptions {
  // local path to json wallet file
  path: string;
  // json wallet password
  password: string;
}

Constants and Contract Addresses

Names of contracts, their addresses and token decimals can be found in /src/constants.ts. ABIs and typechain-generated types are in types/typechain/. Addresses, for all networks, can be easily fetched using the getAddress function, combined with contract name constants.

cUsdtAddress = Mangrove.getAddress("USDC");
// Mainnet USDC address. Second parameter can be a network like 'rinkeby'.

Numbers

Numbers returned by functions are either plain js numbers or big.js instances. Some functions with names ending in Raw may return ether.js's BigNumbers.

As input, numbers can be as plain js numbers, big.js instances, but also strings.

The precision used when dividing is 20 decimal places.

Transaction Options

TODO include transaction options (see here)https://github.com/compound-finance/compound-js#transaction-options

Package configuration

mangrove.js uses the node-config package for configuration.

It allows apps who requires mangrove.js to override default package configuration, by setting configuration in MangroveJs namespace.

Example of app configuration (config/default.js):

var config = {
  ...

  MangroveJs: {
    logLevel: "info",
    ...
  }
};
module.exports = config;

Logging

Console logging is enabled by default.

Logging can be configured with the following directives (see Package configuration).

  • logLevel: set logging level;
  • logFile: enable file logging.

Tests

Tests are available in ./test/integration/*.integration.test.js. Methods are tested using a spawned anvil process. For free archive node access, get a provider URL from Alchemy.

## Run all tests
yarn test

## Run a single test (Mocha JS grep option)
yarn test -- -g 'subscribes'

Test configuration and root hooks

Tests are based on Mocha. Mocha configuration can be found in ./test/mocha/config.

The integration tests use the Root Hooks provided by src/util/test/mochaHooks.ts, which spawn an anvil process with Mangrove deployed and add information to the server and accounts properties of the Mocha Context.

Build for Node.js & Web Browser

$ git clone ...
$ cd <Mangrove clone>
$ yarn install              # <- Only required after initial clone, afterwards 'yarn build' is sufficient
$ cd packages/mangrove.js
$ yarn build

The build artifacts will be placed in ./dist/nodejs and ./dist/browser.

CLI: mgv

mangrove.js includes an experimental command line interface (CLI) for interacting with Mangrove. You can run it using npx, yarn, or directly (if you install mangrove.js globally):

$ npx mgv
$ yarn mgv
$ mgv         # requires mangrove.js to be installed globally: npm -g install mangrove.js
mgv.js <command>

Commands:
  mgv.js parrot                  reports the current environment and warns of
                                 any discrepancies       [aliases: env-overview]
  mgv.js print <base> <quote>    print the offers on a market
  mgv.js retract <base> <quote>  retracts all offers from the given market

Options:
  --version  Show version number                                       [boolean]
  --help     Show help                                                 [boolean]

Arguments may be provided in env vars beginning with 'MGV_'. For example,
MGV_NODE_URL=https://node.url can be used instead of --nodeUrl https://node.url

You need at least one command before moving on
2.0.5-50

12 days ago

2.0.5-51

12 days ago

2.0.5-49

25 days ago

2.0.5-48

1 month ago

2.0.5-46

1 month ago

2.0.5-47

1 month ago

2.0.5-44

1 month ago

2.0.5-45

1 month ago

2.0.5-39

2 months ago

2.0.5-42

2 months ago

2.0.5-43

2 months ago

2.0.5-40

2 months ago

2.0.5-41

2 months ago

2.0.5-37

2 months ago

2.0.5-38

2 months ago

2.0.5-36

2 months ago

2.0.5-35

2 months ago

2.0.5-34

2 months ago

2.0.5-32

2 months ago

2.0.5-33

2 months ago

2.0.5-29

2 months ago

2.0.5-31

2 months ago

2.0.5-30

2 months ago

2.0.5-28

2 months ago

2.0.5-24

2 months ago

2.0.5-25

2 months ago

2.0.5-26

2 months ago

2.0.5-27

2 months ago

2.0.5-22

2 months ago

2.0.5-23

2 months ago

2.0.5-20

2 months ago

2.0.5-21

2 months ago

2.0.5-19

2 months ago

2.0.5-17

2 months ago

2.0.5-18

2 months ago

2.0.5-14

2 months ago

2.0.5-15

2 months ago

2.0.5-16

2 months ago

2.0.5-13

2 months ago

2.0.5-9

2 months ago

2.0.5-10

2 months ago

2.0.5-11

2 months ago

2.0.5-12

2 months ago

2.0.5-8

2 months ago

2.0.5-7

2 months ago

2.0.5-6

3 months ago

2.0.5-5

3 months ago

2.0.5-4

3 months ago

2.0.5-1

3 months ago

2.0.5-3

3 months ago

2.0.5-2

3 months ago

2.0.5-0

3 months ago

2.0.3

3 months ago

2.0.2

3 months ago

2.0.4

3 months ago

2.0.1

4 months ago

2.0.0

4 months ago

2.0.0-19

4 months ago

2.0.0-20

4 months ago

2.0.0-18

4 months ago

2.0.0-17

4 months ago

2.0.0-16

4 months ago

2.0.0-15

4 months ago

2.0.0-14

5 months ago

2.0.0-13

5 months ago

2.0.0-12

5 months ago

2.0.0-11

5 months ago

2.0.0-10

5 months ago

1.4.20

8 months ago

1.4.22

7 months ago

1.4.21

8 months ago

1.4.24

7 months ago

1.4.23

7 months ago

1.4.26

7 months ago

1.4.25

7 months ago

1.4.28

7 months ago

1.4.27

7 months ago

1.4.29

6 months ago

1.4.30

6 months ago

1.4.11

9 months ago

1.4.10

10 months ago

1.4.13

9 months ago

1.4.12

9 months ago

1.4.15

9 months ago

1.4.14

9 months ago

1.4.17

9 months ago

1.4.16

9 months ago

1.4.19

8 months ago

1.4.18

9 months ago

1.4.6

10 months ago

1.4.5

10 months ago

1.4.4

10 months ago

1.4.3

10 months ago

2.0.0-6

5 months ago

2.0.0-5

5 months ago

2.0.0-4

5 months ago

2.0.0-3

5 months ago

2.0.0-2

5 months ago

2.0.0-1

6 months ago

1.4.22-0

8 months ago

2.0.0-9

5 months ago

2.0.0-8

5 months ago

2.0.0-7

5 months ago

1.4.15-0

9 months ago

1.4.11-0

9 months ago

1.4.30-0

6 months ago

1.4.18-0

9 months ago

1.4.18-1

8 months ago

1.4.9

10 months ago

1.4.8

10 months ago

1.4.7

10 months ago

1.4.2

10 months ago

1.2.8

11 months ago

1.2.7

11 months ago

1.2.6

11 months ago

1.4.1

10 months ago

1.4.0

10 months ago

1.2.6-1

11 months ago

1.2.6-0

11 months ago

1.2.10

11 months ago

1.2.6-3

11 months ago

1.2.6-2

11 months ago

1.2.6-5

11 months ago

1.2.4-7

12 months ago

1.2.6-4

11 months ago

1.2.4-9

12 months ago

1.2.6-6

11 months ago

1.2.4-8

12 months ago

1.2.5-0

11 months ago

1.3.2

11 months ago

1.3.1

11 months ago

1.3.0

11 months ago

1.2.4-13

12 months ago

1.2.4-14

12 months ago

1.2.4-15

11 months ago

1.2.4-10

12 months ago

1.2.4-11

12 months ago

1.2.4-12

12 months ago

1.2.9

11 months ago

1.2.4-1

1 year ago

1.2.4-0

1 year ago

1.2.5

1 year ago

1.2.3

1 year ago

1.2.4-3

1 year ago

1.2.4-2

1 year ago

1.2.4-5

1 year ago

1.2.4-4

1 year ago

1.2.4-6

1 year ago

1.2.0

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.3-0

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.13.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

0.12.0

1 year ago

0.14.0

1 year ago

0.11.1

2 years ago

0.11.2

2 years ago

0.11.3

2 years ago

0.11.4

2 years ago

0.11.0

2 years ago

0.10.1

2 years ago

0.10.2

2 years ago

0.10.0

2 years ago

0.9.2

2 years ago

0.9.0

2 years ago

0.8.1

2 years ago

0.8.0

2 years ago

0.7.1

2 years ago

0.9.1

2 years ago

0.7.0

2 years ago

0.5.7

2 years ago

0.6.3

2 years ago

0.5.4

2 years ago

0.6.2

2 years ago

0.5.6

2 years ago

0.6.4

2 years ago

0.5.5

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.3

2 years ago

0.3.8

2 years ago

0.3.7

2 years ago

0.5.0

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.3.0

2 years ago

0.3.5

2 years ago

0.3.1

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.1.0

2 years ago

0.2.0

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.3

2 years ago