Skip to content

Commit

Permalink
fix: remove delegation during settlement/default claiming of loans
Browse files Browse the repository at this point in the history
  • Loading branch information
DioPires authored and cfcfs committed Oct 24, 2024
1 parent bb804ee commit 11f6416
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
16 changes: 13 additions & 3 deletions contracts/P2PLendingNfts.vy
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ struct Loan:
collateral_token_id: uint256
fees: DynArray[Fee, MAX_FEES]
pro_rata: bool
delegate: address


struct CollectionStatus:
Expand Down Expand Up @@ -476,7 +477,8 @@ def create_loan(
collateral_contract: collection_status.contract,
collateral_token_id: collateral_token_id,
fees: fees,
pro_rata: offer.offer.pro_rata
pro_rata: offer.offer.pro_rata,
delegate: delegate
})
loan.id = self._compute_loan_id(loan)

Expand Down Expand Up @@ -543,6 +545,9 @@ def settle_loan(loan: Loan):

self._transfer_collateral(loan.borrower, loan.collateral_contract, loan.collateral_token_id)

if loan.delegate != empty(address):
self._set_delegation(loan.delegate, loan.collateral_contract, loan.collateral_token_id, False)

log LoanPaid(
loan.id,
loan.borrower,
Expand Down Expand Up @@ -570,6 +575,9 @@ def claim_defaulted_loan_collateral(loan: Loan):

self._transfer_collateral(loan.lender, loan.collateral_contract, loan.collateral_token_id)

if loan.delegate != empty(address):
self._set_delegation(loan.delegate, loan.collateral_contract, loan.collateral_token_id, False)

log LoanCollateralClaimed(
loan.id,
loan.borrower,
Expand Down Expand Up @@ -671,7 +679,8 @@ def replace_loan(
collateral_contract: collection_status.contract,
collateral_token_id: loan.collateral_token_id,
fees: new_loan_fees,
pro_rata: offer.offer.pro_rata
pro_rata: offer.offer.pro_rata,
delegate: loan.delegate
})
new_loan.id = self._compute_loan_id(new_loan)

Expand Down Expand Up @@ -793,7 +802,8 @@ def replace_loan_lender(loan: Loan, offer: SignedOffer, collateral_proof: DynArr
collateral_contract: collection_status.contract,
collateral_token_id: loan.collateral_token_id,
fees: new_loan_fees,
pro_rata: offer.offer.pro_rata
pro_rata: offer.offer.pro_rata,
delegate: loan.delegate
})
new_loan.id = self._compute_loan_id(new_loan)

Expand Down
4 changes: 2 additions & 2 deletions tests/conftest_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
from boa.contracts.vyper.event import Event
from boa.contracts.vyper.vyper_contract import VyperContract
from eth.exceptions import Revert
from eth_abi import keccak
from eth_abi import encode
from eth_account import Account
from eth_account.messages import encode_structured_data
from eth_utils import encode
from eth_utils import keccak
from web3 import Web3

ZERO_ADDRESS = boa.eval("empty(address)")
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/p2p_nfts/test_p2p_nfts_claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def ongoing_loan_bayc(p2p_nfts_usdc, offer_bayc, usdc, borrower, lender, bayc, n
@pytest.fixture
def ongoing_loan_bayc_no_delegate(p2p_nfts_usdc, offer_bayc, usdc, borrower, lender, bayc, now, borrower_broker_fee):
offer = offer_bayc.offer
token_id = offer.token_ids[0]
token_id = offer.token_id
principal = offer.principal
origination_fee = offer.origination_fee_amount

Expand All @@ -119,6 +119,7 @@ def ongoing_loan_bayc_no_delegate(p2p_nfts_usdc, offer_bayc, usdc, borrower, len
loan_id = p2p_nfts_usdc.create_loan(
offer_bayc,
token_id,
[],
ZERO_ADDRESS,
borrower_broker_fee.upfront_amount,
borrower_broker_fee.settlement_bps,
Expand All @@ -128,6 +129,8 @@ def ongoing_loan_bayc_no_delegate(p2p_nfts_usdc, offer_bayc, usdc, borrower, len

loan = Loan(
id=loan_id,
offer_id=compute_signed_offer_id(offer_bayc),
offer_tracing_id=offer.tracing_id,
amount=offer.principal,
interest=offer.interest,
payment_token=offer.payment_token,
Expand Down
11 changes: 7 additions & 4 deletions tests/unit/p2p_nfts/test_p2p_nfts_settle.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
FeeType,
Loan,
Offer,
WhitelistRecord,
compute_loan_hash,
compute_signed_offer_id,
get_last_event,
Expand Down Expand Up @@ -150,7 +149,7 @@ def ongoing_loan_bayc_no_delegate(
p2p_nfts_usdc, offer_bayc, usdc, borrower, lender, bayc, now, protocol_fees, borrower_broker_fee
):
offer = offer_bayc.offer
token_id = offer.token_ids[0]
token_id = offer.token_id
principal = offer.principal
origination_fee = offer.origination_fee_amount

Expand All @@ -163,6 +162,7 @@ def ongoing_loan_bayc_no_delegate(
loan_id = p2p_nfts_usdc.create_loan(
offer_bayc,
token_id,
[],
ZERO_ADDRESS,
borrower_broker_fee.upfront_amount,
borrower_broker_fee.settlement_bps,
Expand All @@ -172,6 +172,8 @@ def ongoing_loan_bayc_no_delegate(

loan = Loan(
id=loan_id,
offer_id=compute_signed_offer_id(offer_bayc),
offer_tracing_id=offer.tracing_id,
amount=offer.principal,
interest=offer.interest,
payment_token=offer.payment_token,
Expand Down Expand Up @@ -368,7 +370,7 @@ def test_settle_loan(p2p_nfts_usdc, delegation_registry, ongoing_loan_bayc, usdc
)


def test_settle_loan_no_delegate(p2p_nfts_usdc, delegation_registry, ongoing_loan_bayc_no_delegate, usdc):
def test_settle_loan_no_delegate(p2p_nfts_usdc, delegation_registry, ongoing_loan_bayc_no_delegate, usdc, now):
assert not delegation_registry.checkDelegateForERC721(
ongoing_loan_bayc_no_delegate.delegate,
p2p_nfts_usdc.address,
Expand All @@ -379,7 +381,8 @@ def test_settle_loan_no_delegate(p2p_nfts_usdc, delegation_registry, ongoing_loa

loan = ongoing_loan_bayc_no_delegate
interest = loan.interest
amount_to_settle = loan.amount + interest
borrower_broker_fee = loan.calc_borrower_broker_settlement_fee(now)
amount_to_settle = loan.amount + interest + borrower_broker_fee

usdc.approve(p2p_nfts_usdc.address, amount_to_settle, sender=loan.borrower)
p2p_nfts_usdc.settle_loan(loan, sender=loan.borrower)
Expand Down

0 comments on commit 11f6416

Please # to comment.