# Question about inner transactions in smart contracts

So I am trying to create a smart contract with the following logic:

1. A `lender` can send USDC to a pool. Let’s call this pool `USDC Lender Pool 1`.
2. Then, the smart contract sends the `lender` an ASA symbolizing the receipt of the deposit. Let’s call this ASA `Token A`. The smart contract sends the `lender` an amount of `Token A` that is directly proportional to the amount of USDC sent to `USDC Lender Pool 1` by the `lender`. Meaning, if the lender sends 100 USDC to `USDC Lender Pool 1`, the smart contract sends 100 `Token A` to the lender.
3. The `borrower` makes a repayment of the loan to another USDC pool. Let’s call this pool `USDC Borrower Pool 2`.
4. The lender can then stake his `Token A` in 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.