Hard Audit: refresh borrow/deposit after syncing (#824)

* refresh borrow/deposit after syncing

* revisions
This commit is contained in:
Denali Marsh 2021-02-12 21:56:03 +01:00 committed by GitHub
parent a5b72363d0
commit 15da55b451
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View File

@ -20,6 +20,9 @@ func (k Keeper) Repay(ctx sdk.Context, sender, owner sdk.AccAddress, coins sdk.C
// Sync borrow interest so loan is up-to-date // Sync borrow interest so loan is up-to-date
k.SyncBorrowInterest(ctx, owner) k.SyncBorrowInterest(ctx, owner)
// Refresh borrow after syncing interest
borrow, _ = k.GetBorrow(ctx, owner)
// Validate that sender holds coins for repayment // Validate that sender holds coins for repayment
err := k.ValidateRepay(ctx, sender, owner, coins) err := k.ValidateRepay(ctx, sender, owner, coins)
if err != nil { if err != nil {

View File

@ -9,20 +9,25 @@ import (
// Withdraw returns some or all of a deposit back to original depositor // Withdraw returns some or all of a deposit back to original depositor
func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, coins sdk.Coins) error { func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, coins sdk.Coins) error {
deposit, found := k.GetDeposit(ctx, depositor) // Call incentive hooks
existingDeposit, found := k.GetDeposit(ctx, depositor)
if !found { if !found {
return sdkerrors.Wrapf(types.ErrDepositNotFound, "no deposit found for %s", depositor) return sdkerrors.Wrapf(types.ErrDepositNotFound, "no deposit found for %s", depositor)
} }
// Call incentive hooks k.BeforeDepositModified(ctx, existingDeposit)
k.BeforeDepositModified(ctx, deposit)
existingBorrow, hasExistingBorrow := k.GetBorrow(ctx, depositor) existingBorrow, hasExistingBorrow := k.GetBorrow(ctx, depositor)
if hasExistingBorrow { if hasExistingBorrow {
k.BeforeBorrowModified(ctx, existingBorrow) k.BeforeBorrowModified(ctx, existingBorrow)
} }
// Sync interest
k.SyncBorrowInterest(ctx, depositor) k.SyncBorrowInterest(ctx, depositor)
k.SyncSupplyInterest(ctx, depositor) k.SyncSupplyInterest(ctx, depositor)
// Refresh Deposit after syncing interest
deposit, _ := k.GetDeposit(ctx, depositor)
amount, err := k.CalculateWithdrawAmount(deposit.Amount, coins) amount, err := k.CalculateWithdrawAmount(deposit.Amount, coins)
if err != nil { if err != nil {
return err return err