So I am trying to create a smart contract with the following logic:
- A
lendercan send USDC to a pool. Let’s call this poolUSDC Lender Pool 1. - Then, the smart contract sends the
lenderan ASA symbolizing the receipt of the deposit. Let’s call this ASAToken A. The smart contract sends thelenderan amount ofToken Athat is directly proportional to the amount of USDC sent toUSDC Lender Pool 1by thelender. Meaning, if the lender sends 100 USDC toUSDC Lender Pool 1, the smart contract sends 100Token Ato the lender. - The
borrowermakes a repayment of the loan to another USDC pool. Let’s call this poolUSDC Borrower Pool 2. - The lender can then stake his
Token Ain the contract to redeem his portion of the loan.
Caveat: There can be many lenders and many borrowers depositing/borrowing USDC from USDC Lender Pool 1.
Problem:
I don’t know how to make the lenders’ redeeming process equitative. What do I mean by this? Well, I know the lender can redeem his contribution to the pool by a simple percentage:
percentage = amount of tokens staked / amount in USDC Borrower Pool 2
Example: If the size of the USDC Borrower Pool 2 is 100 and lender 1 stakes 50 Token A in the contract, lender 1 is entitled to 50% of the USDC in USDC Borrower Pool 2, or 50 USDC.
The issue is that staking transactions made by the lender to the smart contract are serialized, meaning one comes after another.
Example continued: If there is a second lender lender 2 and he/she stakes 50 Token A in the contract, lender 2 should be entitled to 50% of the USDC in USDC Borrower Pool 2, or 50 USDC. However, the size of the pool has been reduced to 50 now, so lender 2 has 50% of a pool that is now size 50, so lender 2 only redeems 25 USDC from the contract.
See diagram below for clarification:
After any lender stakes his/her Token A and receives USDC from USDC Borrower Pool 2, the corresponding amount of Token A is “burned”. So if the lender stakes 50 Token A and receives 50 USDC, those 50 Token A are burned.
I don’t want to ramble on for too much longer, but creating local state in the USDC Borrower Pool can be tricky because it is unclear when to lower or increase that number. Ideally, the smart contract could just send equal amounts to both lenders in one single grouped transaction, but this is not scalable as there could be thousands of lenders and borrowers.
