Lending and borrowing using Ethers.js
This guide is created to help a potential investor to directly use the API of Minterest contract on Ethereum blockchain. The described actions will allow the user to lend and borrow assets without usage of Minterest UI.
Preconditions
In order to do a lend (as well as borrow) operation directly on the contract level using Ethers.js, one needs:
A private key of a wallet with enough ETH and target asset (USDC in the example)
Infura API key (can be easily substituted with Alchemy or any other provider)
Address and raw ABI of the target contract (Minterest USDC market or mUSDC in the example)
The example below is using node.js as the basis to execute the JavaScript code. One needs to set it up, as well as set up the Ethers.js library itself. The setup of node.js and Ethers.js is out of scope of the guide, as it may differ for different operating systems. Detailed instructions can be found on respective resources.
Considering the node.js and Ethers.js are installed and configured, two files should be created:
A JavaScript file, that will contain the code to execute transactions
A JSON file to store the ABI of the target contract
JSON file content
The ABI of the contract can be retrieved using Etherscan. Open the contract page and look for the ABI section. The result of 'RAW/Text Format" export should be copied to the JSON file. In the example below the JSON file is called mUSDC_abi.json and it is placed next to the JS file with the code - this is important to simplify the import of the data.
JavaScript file
The next step is to write the code in a JavaScript file. The first block of the code is the setup of RPC Provider. The provider is responsible for sending messages to the blockchain through Infura or similar services.
Next is a section with constants needed for the transactions - address of the contract, private key of a wallet that will be used to sign transactions, lend and borrow values.
The next line of code creates a wallet abstraction in the code. This abstraction will be used to sign transactions.
A contract abstraction (created in this line of code) will be used to call functions like 'lend' and 'borrow'. Note how the instance is already linked to the wallet that will be used to communicate with the chain.
The following blocks define two functions, which call respective contract API endpoints for lending and borrowing. The functions are very similar, they send a signed transaction to the chain and wait for a success or error message.
Finally, the code that invokes the lend and borrow functions:
The full code of the example
Last updated