Created
June 8, 2020 12:54
-
-
Save RachBLondon/6e83887646b45536b11dc162bee76bb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import LendingPoolAddressesProviderABI from "./ABI/LendingPoolAddressesProvider.json"; | |
import LendingPoolABI from "./ABI/LendingPool.json"; | |
import Web3 from "web3"; | |
const referralCode = "0"; | |
// const lpAddressProviderAddress = "0x24a42fD28C976A61Df5D00D0599C34c4f90748c8"; // mainnet address, for other addresses: https://docs.aave.com/developers/developing-on-aave/deployed-contract-instances | |
const lpAddressProviderAddress = "0x506B0B2CF20FAA8f38a4E2B524EE43e1f4458Cc5" // kovan | |
const web3 = new Web3(window.ethereum); | |
const lpAddressProviderContract = new web3.eth.Contract( | |
LendingPoolAddressesProviderABI, | |
lpAddressProviderAddress | |
); | |
export default class App extends Component { | |
state = { | |
needToAWeb3Browser: false, | |
}; | |
async getAddressFromMetaMask() { | |
if (typeof window.ethereum == "undefined") { | |
this.setState({ needToAWeb3Browser: true }); | |
} else { | |
window.ethereum.autoRefreshOnNetworkChange = false; //silences warning about no autofresh on network change | |
const accounts = await window.ethereum.enable(); | |
this.setState({ accounts }); | |
} | |
} | |
async componentDidMount() { | |
await this.getAddressFromMetaMask(); | |
if (this.state.accounts) { | |
// Get the latest LendingPoolCore address | |
const lpCoreAddress = await lpAddressProviderContract.methods | |
.getLendingPoolCore() | |
.call() | |
.catch((e) => { | |
throw Error(`Error getting lendingPool address: ${e.message}`); | |
}); | |
// Get the latest LendingPool contract address | |
const lpAddress = await lpAddressProviderContract.methods | |
.getLendingPool() | |
.call() | |
.catch((e) => { | |
throw Error(`Error getting lendingPool address: ${e.message}`); | |
}); | |
// get DAI from ETH | |
// const daiAddress = "0x6B175474E89094C44Da98b954EedeAC495271d0F"; // mainnet DAI | |
const daiAddress = '0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa' //kovan | |
const daiContract = new web3.eth.Contract(DAITokenABI, daiAddress); | |
const daiAmountinWei = web3.utils.toWei("100", "ether").toString(); | |
console.log("1", lpCoreAddress, daiAmountinWei, this.state.accounts) | |
await daiContract.methods | |
.approve(lpCoreAddress, daiAmountinWei) | |
.send({ from: this.state.accounts[0] }) | |
.catch((e) => { | |
throw Error(`Error approving DAI allowance: ${e.message}`); | |
}); | |
// get aDAI from DAI | |
const lpContract = new web3.eth.Contract(LendingPoolABI, lpAddress); | |
console.log("lpContratc", lpContract); | |
await lpContract.methods | |
.deposit(daiAddress, daiAmountinWei, referralCode) | |
.send({ from: this.state.accounts[0] }) | |
.catch((e) => { | |
throw Error( | |
`Error depositing to the LendingPool contract: ${e.message}` | |
); | |
}); | |
console.log("got aDAi") | |
} | |
} | |
render() { | |
if (this.state.needToAWeb3Browser) { | |
return <h1>Please install metamask</h1>; | |
} | |
return ( | |
<div> App </div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment