@morpho-labs/morpho-ethers-contract

Use Morpho contracts with ethers js

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@morpho-labs/morpho-ethers-contract
301.20.110 months ago2 years agoMinified + gzip package size for @morpho-labs/morpho-ethers-contract in KB

Readme

Morpho Ethers Contract
!npm packagenpm-imgnpm-url !Build Statusbuild-imgbuild-url !Downloadsdownloads-imgdownloads-url !Issuesissues-imgissues-url !Commitizen Friendlycommitizen-imgcommitizen-url !Semantic Releasesemantic-release-imgsemantic-release-url
This package aims to facilitate the integration around morpho with ethers-v5. Instead of importing ABIs, finding implementations, and guessing which functions to call, this package gives you typed classes by using typechain, and mainnet addresses of deployed contracts.
The package contains all contracts to interract with Morpho and withe the Compound and Aave pools.
NB: for security reasons, we invite you to always check the addresses of the contracts used, and check whether they are indeed those of Morpho.
You will find more information on the integration of Morpho in the developer documentation.

Install

npm install @morpho-labs/morpho-ethers-contract

yarn add @morpho-labs/morpho-ethers-contract

Usage

import { providers, Wallet } from "ethers";
import { formatUnits, parseUnits } from "ethers/lib/utils";

import addresses from "@morpho-labs/morpho-ethers-contract/addresses"
import {
  MorphoAaveV2Lens__factory,
  MorphoAaveV2__factory,
  ERC20__factory,
} from "@morpho-labs/morpho-ethers-contract";

(async () => {
  const provider = new providers.StaticJsonRpcProvider(process.env.RPC, "mainnet");

  const morphoAaveLens = MorphoAaveV2Lens__factory.connect(addresses.morphoAave.lens, provider);

  // now you have autocompletion for morpho contract
  const morphoAaveMarkets = await morphoAaveLens.getAllMarkets();

  // For example, you can easily supply on Morpho

  const signer = new Wallet(process.env.PRIVATE_KEY!, provider);

  const morphoAaveV2 = MorphoAaveV2__factory.connect(addresses.morphoAave.morpho, provider);

  const toSupply = parseUnits("10"); // 10 DAI
  const daiAddress = "0x6b175474e89094c44da98b954eedeac495271d0f";
  const aDaiAddress = "0x028171bCA77440897B824Ca71D1c56caC55b68A3";
  // We first need to approve the amount to supply through the ERC20 token
  const DAI = ERC20__factory.connect(daiAddress, signer);
  const approvalTransaction = await DAI.approve(morphoAaveV2.address, toSupply);

  console.log(`Approval transaction: https://etherscan.io/tx/${approvalTransaction.hash}`);

  await approvalTransaction.wait(); // wait until transaction was mined

  console.log(`${formatUnits(toSupply)} DAI approved`);

  const supplyTransaction = await morphoAaveV2["supply(address,address,uint256)"](
    aDaiAddress, // poolToken aka aToken for aave
    signer.address, // onBehalf of the signer
    toSupply // amount to supply in WEI units
  );

  console.log(
    `Supply on Morpho-AaveV2 transaction: https://etherscan.io/tx/${supplyTransaction.hash}`
  );

  const receipt = await supplyTransaction.wait();

  console.log(
    `You have successfully supplied ${formatUnits(
      toSupply
    )} DAI on Morpho Aave, with a gas consuption of ${formatUnits(receipt.gasUsed, "gwei")} gWei`
  );
})();