I looking for solution how to convert eth to dai with web3 api. something like https://kyber.network/swap/eth_knc (I can send eth to specyfic contract adress and I receive back DAI to the same address from I send eth.)

DAI contract address https://etherscan.io/address/0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359 Sorry for my english.

3 Answers 3


You will need some sort of oracle to determine the price of ETH (your exchange rate). Instead I would just use the official Maker library to do this. It will do exactly what you want - buying and selling MKR and Dai with built-in DEX integration.



The easiest way to do this is still through EtherDelta.

As far as I can tell Dai (and/or 0x) is building its own DEX that will become the official DEX for DAI, but the dai.js library and most of its documentation seem deliberately over-complex. There did used to be a library for trading DAI and ETH called OasisDex, but this was shut down due to some reason. Truthfully, it may have been due to incompetence -- every order on the site was fill-or-kill, which resulted in someone losing hundreds of thousands of dollars in DAI. I would not be surprised if the Maker DAO team is offloading its exchange integrations to the 0x team.

Off-topic: One thing that is pretty easy is accessing the DAI-USD price on-chain inside solidity. Visit the DAI chatroom for help using this.


First of all you should login your eth account like below

const keystore = fs.readFileSync("your keystore path", "utf8");
var activeAccount = web3.eth.accounts.decrypt(keystore, password);

Now you can sign and broadcast your transaction. You must encode your swap message and add your tx's data field. Don't miss eth amount how many want to swap with token.

var swap = UniswapV2Router02Contract.methods.swapExactETHForTokens(amountOutMin, [WETH[activeChain].address, token.address], activeAccount.address, timeStamp)
var encodedABI = swap.encodeABI()

var tx = {
    from: activeAccount.address,
    to: UniswapV2RouterAddress,
    gas: 200000,
    data: encodedABI,
    value: ethAmount

var signedTx = await activeAccount.signTransaction(tx)

.on('transactionHash', function(hash){

.on('confirmation', function(confirmationNumber, receipt){

.on('receipt', function(receipt){

.on('error', function(error, receipt) { // If the transaction was rejected by the network with a receipt, the second parameter will be the receipt.
    console.error("Error:", error, "Receipt:", receipt)

I also answered same question at here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.