AAVE Deposit/Borrowing Tutorial Part II

Using Uniswap to get DAI

This is part 2 of a 4 part series. In this tutorial we’ll be getting some DAI from the Uniswap contract so we can play with Aave’s smart contracts.

It would probably be simpler to go to some account that already has some DAI (and Ether for gas). Or to even mint some DAI from the DAI contract, but this was an interesting exercise…

You can download the code below from https://github.com/cryptocamtech/aave-borrowtutorial.

Install these node libraries.

npm install web3
npm install ethereumjs-tx
npm install dotenv

Let’s get some DAI from Uniswap!

  1. Copy/modify .env to add your account number and private key (you can export that from Metamask).
  2. Connect to ganache-cli and import the contracts we’ll be using. For your own interest it will be worth pasting these contract addresses into Etherscan and taking a look.
const web3 = new Web3('http://localhost:8545');
const uniswapV1ExchangeAddress = '0x2a1530C4C41db0B0b2bB646CB5Eb1A67b7158667';
const uniswapV1ExchangeABI = require('./ABIs/uniswapv1.json');
const daiAddress = '0x6b175474e89094c44da98b954eedeac495271d0f';
const daiABI = JSON.parse(fs.readFileSync('./ABIs/DAI.json'));
  1. Initialize the contracts.
const uniswapExchangeContract = new web3.eth.Contract(uniswapV1ExchangeABI, uniswapV1ExchangeAddress);
const daiContract = new web3.eth.Contract(daiABI, daiAddress);
  1. Set up some constants including how much we are spending. ‘min_tokens’ is set to a minimum (200 DAI for 1 ETH) and ‘deadline’ is 10 seconds from now.
const ETH_SOLD = web3.utils.toHex(web3.utils.toWei('1', 'ether'));
const MIN_TOKENS = web3.utils.toHex(200 * 10 ** 18);
const DEADLINE = Math.floor(new Date() / 1000) + 10; 
const myAccount = process.env.ACCOUNT;
  1. Set up the transaction (https://defihandbook.cc/exchange/uniswap for the method used):
const exchangeEncodedABI = uniswapExchangeContract.methods
    .ethToTokenSwapInput(MIN_TOKENS, DEADLINE)
    .encodeABI();
const txCount = await web3.eth.getTransactionCount(myAccount);
const txObject = {
    nonce: web3.utils.toHex(txCount),
    gasLimit: web3.utils.toHex(60000),
    gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
    to: uniswapV1ExchangeAddress,
    from: myAccount,
    data: exchangeEncodedABI,
    value: ETH_SOLD
};
  1. Send the transaction.
const tx = new Tx(txObject);
const privateKey = new Buffer.from(process.env.PRIVATE_KEY, "hex");
tx.sign(privateKey);
const serializedEthTx = tx.serialize().toString("hex");
const txHash = await web3.eth.sendSignedTransaction(`0x${serializedEthTx}`)
            .catch(e => { throw Error('Error sending transaction: ' + e.message); });

If all goes well, you should see the new tokens in Metamask. You will have to manually add “DAI” to your list of tokens (use the custom DAI address if you need to). The amount of Ether will go down by 1 plus some gas that was used.

Next, we’ll deposit some DAI into Aave in Part III.

Published by Crypto Cam Tech

Professional developer for 30 years + with the passion for helping others in their journey.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: