-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmanual_liquidation.cdc
More file actions
63 lines (53 loc) · 3.28 KB
/
Copy pathmanual_liquidation.cdc
File metadata and controls
63 lines (53 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import "FungibleToken"
import "FungibleTokenMetadataViews"
import "MetadataViews"
import "FlowALPv0"
/// Attempt to liquidation a position by repaying `repayAmount`.
///
/// debtVaultIdentifier: e.g., Type<@MOET.Vault>().identifier
/// seizeVaultIdentifier: e.g., Type<@FlowToken.Vault>().identifier
transaction(pid: UInt64, debtVaultIdentifier: String, seizeVaultIdentifier: String, seizeAmount: UFix64, repayAmount: UFix64) {
let pool: auth(FlowALPv0.EParticipant) &FlowALPv0.Pool
let receiver: &{FungibleToken.Receiver}
let debtType: Type
let seizeType: Type
let repay: @{FungibleToken.Vault}
prepare(signer: auth(BorrowValue, SaveValue, IssueStorageCapabilityController, PublishCapability, UnpublishCapability) &Account) {
let cap = signer.storage.borrow<&Capability<auth(FlowALPv0.EParticipant, FlowALPv0.EPosition) &FlowALPv0.Pool>>(
from: FlowALPv0.PoolCapStoragePath
) ?? panic("Could not borrow Pool capability from storage - ensure the signer has been granted Pool access with EParticipant entitlement")
self.pool = cap.borrow() ?? panic("Could not borrow Pool from capability")
// Resolve types
self.debtType = CompositeType(debtVaultIdentifier) ?? panic("Invalid debtVaultIdentifier: \(debtVaultIdentifier)")
self.seizeType = CompositeType(seizeVaultIdentifier) ?? panic("Invalid seizeVaultIdentifier: \(seizeVaultIdentifier)")
// Get the path and type data for the provided token type identifier
let debtVaultData = MetadataViews.resolveContractViewFromTypeIdentifier(
resourceTypeIdentifier: debtVaultIdentifier,
viewType: Type<FungibleTokenMetadataViews.FTVaultData>()
) as? FungibleTokenMetadataViews.FTVaultData
?? panic("Could not construct valid FT type and view from identifier \(debtVaultIdentifier)")
let seizeVaultData = MetadataViews.resolveContractViewFromTypeIdentifier(
resourceTypeIdentifier: seizeVaultIdentifier,
viewType: Type<FungibleTokenMetadataViews.FTVaultData>()
) as? FungibleTokenMetadataViews.FTVaultData
?? panic("Could not construct valid FT type and view from identifier \(seizeVaultIdentifier)")
// Check if the service account has a vault for this token type at the correct storage path
let debtVaultRef = signer.storage.borrow<auth(FungibleToken.Withdraw) &{FungibleToken.Vault}>(from: debtVaultData.storagePath)
?? panic("no debt vault in storage at path \(debtVaultData.storagePath)")
assert(debtVaultRef.balance >= repayAmount, message: "Insufficient debt token \(debtVaultRef.getType().identifier) balance \(debtVaultRef.balance)<\(repayAmount)")
self.repay <- debtVaultRef.withdraw(amount: repayAmount)
let seizeVaultRef = signer.capabilities.borrow<&{FungibleToken.Receiver}>(seizeVaultData.receiverPath)
?? panic("no seize receiver in storage at path \(seizeVaultData.receiverPath)")
self.receiver = seizeVaultRef
}
execute {
let seizedVault <- self.pool.manualLiquidation(
pid: pid,
debtType: self.debtType,
seizeType: self.seizeType,
seizeAmount: seizeAmount,
repayment: <-self.repay
)
self.receiver.deposit(from: <-seizedVault)
}
}