The following code example is provided to help you understand the business process. For commercial use, you must handle parameter validation and exception handling on your own.
This example is tested on the Polygon-Amoy chain. Information used in the example can be retrieved from the Source directory. All assets have been pre-approved (authorized) to the contract amoy_contract_approveTrade. Any amounts involved need to be converted using toWei(amount, "decimal") for proper precision.
Code Example
javascriptCopy code// Import web3js libraryimport Web3 from'web3';import ApproveTradeABI from'../abi/ApproveTrade.json';// This example is tested on the Polygon-Amoy chainconstamoy_chainId=80002;constamoy_chainName="AMOY";constamoy_chainRpcUrl="https://polygon-amoy.infura.io/v3/4ba314367838400fb88f2a1d0e14d42d";constamoy_contract_approveTrade="0xF1831ebb3f92A8607E644A1E54Fde4b09F6FE5dE";constamoy_account="0xA3932E6Dbf96983Ffdf43974c0BF7edE9fed76DF";// Initialize web3 instance// WalletProvider or HttpProviderconstweb3=newWeb3("** Wallet **"); // window.ethereumconstContract=newweb3.eth.Contract(ApproveTradeABI, amoy_contract_approveTrade);// 1. Loan Example Parameters// Parameters are retrieved from the market creator's history API, located in the `f_order_info` field. No additional processing is required for these fields.
constmaker="0xA3932E6Dbf96983Ffdf43974c0BF7edE9fed76DF";consttaker="0x0000000000000000000000000000000000000000"; // Open to all lenders// Assets (collateral from the borrower)constassets= [ {collection: "0x9A3fad316eB9cC7db65aB6f89672796574CD1B76", assetClass: "0x0000000000000000000000000000000000000721", amountOrID: "28308257", name: "Bored Ape Yacht Club", symbol: "BAYC", decimal: 0},
{collection: "0x9A3fad316eB9cC7db65aB6f89672796574CD1B76", assetClass: "0x0000000000000000000000000000000000000721", amountOrID: "28308487", name: "Bored Ape Yacht Club", symbol: "BAYC", decimal: 0},
{collection: "0x98700d8fF27Af5F16FdA3bE3bD30aa4585234DCa", assetClass: "0x0000000000000000000000000000000000000020", amountOrID: "100000000", name: "WBTC", symbol: "Wrapped BTC", decimal: 9},
{collection: "0xc94BC02ecFf5f14b73fe1A3137bb587f5Fa62F5d", assetClass: "0x0000000000000000000000000000000000000020", amountOrID: "100000", name: "USDT", symbol: "Tether USD", decimal: 6}
];// Currency (lending asset provided by the lender)constcurrency= [ {collection: "0x0000000000000000000000000000000000000001", assetClass: "0x0000000000000000000000000000000000000000", amountOrID: "100000000000000000", name: "MATIC", symbol: "MATIC", decimal: 18}
];constdeadline="1728955149"; // End of fundraising time (in seconds)constduration="1036800"; // Loan duration (in seconds)constinterestPerSecond="3488077118214104000000000000"; // Interest per secondconstendTime="0"; // No end time specifiedconst sig = "0x65ca1ca8dc706025eb125e40e059e1b768abf909956698237d3ed9eafb276ec03ddb9ef68364652c362f245fce464a286a0fc692470d09de741404621236a4db1b"; // Signature from order creation
// 2. Loan Operation// The `assets` are collateralized by the borrower, and the `currency` is the asset being lent by the lender.// Ensure the lender has sufficient wallet and approval balances for the currency asset.consthandleLoan=Contract.methods.loan([ maker, taker,assets.map(item => { return [item.collection,item.assetClass,item.amountOrID]; }), currency[0].collection, currency[0].amountOrID, deadline, duration, interestPerSecond, endTime, sig]).send({ from: amoy_account, value: currency[0].amountOrID // Note: if the lending asset is the network native token});handleLoan.then(receipt => {console.log(receipt);}).catch(error => {console.log(error);});
Key Notes:
Collateral Assets: The assets array contains the borrower's collateralized assets, which could be ERC20 or ERC721 tokens.
Lending Assets: The currency array holds the lending asset provided by the lender. Ensure that the lender has pre-approved the contract for the specified amount.
Precision: If using ERC20 assets, precision is important, and values should be converted using toWei(amount, "decimal") where applicable.
Lending with Network Native Tokens: When lending native-native coins (like MATIC), ensure to set the value in the .send() method to the corresponding amountOrID.
This example provides the basic structure for a lending operation in the Polygon-Amoy chain. Be sure to adjust parameters and logic based on your specific needs.