This plugin can be injected into dai.js to execute atomic trades on Eth2Dai. It uses the contract FKA Oasis Direct Proxy to interact with the underlying Maker OTC contract.
To configure the SDK with this plugin:
$ yarn add @makerdao/dai
$ yarn add @makerdao/dai-plugin-eth2dai-instant
import Maker from '@makerdao/dai';
import Eth2DaiInstant from '@makerdao/dai-plugin-eth2dai-instant';
async function createMakerAndSellEth(amount) {
const maker = await Maker.create('browser', {
plugins: [Eth2DaiInstant]
});
await maker.authenticate();
await maker.service('exchange').sell('ETH', 'DAI', amount);
}
Note that the 'browser'
preset above is only an example, not a specific requirement of the Eth2Dai Instant plugin. For more information about available presets, configuration options, and additional plugins, check the dai.js docs.
The Eth2DaiInstantService
normalizes the syntax across different types of trades, so the main functionality is represented simply by sell
and buy
. The difference between these two functions is the value defined explicitly as a parameter; for example, a user might want to sell
one hundred Dai for however much ETH that Dai can buy.
The valid token symbols for either side of any trade are 'ETH'
, 'WETH'
, 'PETH'
, and 'DAI'
.
sell
takes three parameters: thesellToken
(string), thebuyToken
(string), and theamount
(string or number)
maker.service('exchange').sell('ETH', 'DAI', '1');
buy
takes three parameters: thebuyToken
(string), thesellToken
(string), and theamount
(string or number)
maker.service('exchange').buy('DAI', 'ETH', 150);
The service can also query the OTC contract for the buy amount for a supplied pay amount and the pay amount for a supplied buy amount. If the price of the exchange deviates from this estimate by more than a configurable slippage limit
, the trade will be reverted.
getBuyAmount
takes three parameters:buyToken
(string),sellToken
(string), andsellAmount
(string or number)
const amount = await maker.service('exchange').getBuyAmount('DAI', 'ETH', 150);
getPayAmount
takes three paramaters:sellToken
(string),buyToken
(string), andbuyAmount
(string or number)
const amount = await maker.service('exchange').getPayAmount('ETH', 'DAI', '1');
setSlippageLimit
takes one parameter:limit
(float). The default slippage limit is 0.02, or 2%.
maker.service('exchange').setSlippageLimit(0.05);
$ git clone https://github.com/makerdao/dai-plugin-eth2dai-instant.git
$ cd dai-plugin-eth2dai-instant/
$ yarn
$ git submodule update --init --recursive
- Run tests with
yarn test
- Run testnet with
yarn test:net
. This will run by default withyarn test
, but can also run independently - Build for publication with
yarn build