-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Agent liquidation value #59
Merged
Merged
Changes from 31 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
ce8797e
Clean up types, delete unused code
Schwartz10 d85d00c
More type cleanup
Schwartz10 59fc674
Fetch MinerInfo directly from state, not api
Schwartz10 f21e554
Return Vesting and pledged bal in PreviewTerminateSectorsReturn
Schwartz10 50b7fcc
Cleanup agent liquidation summary method
Schwartz10 0557a03
Add helper methods to the PreviewAgentTerminationSummary struct
Schwartz10 8cf6c22
Move test helpers to utils package
Schwartz10 569b91a
Add tests
Schwartz10 b4971c7
Add Agent Liquid balance for ADO integration
Schwartz10 8d92c26
Delete max withdraw query
Schwartz10 cc4f497
Update mocks
Schwartz10 7a1f143
Fix max borrow helpers
Schwartz10 736b3aa
Polish
Schwartz10 a474b0d
Use new liquidation value in econ.ComputeAgentData
Schwartz10 6caef9e
Prepare for set-recovered handling of faulty agents
Schwartz10 b153010
Prepare for set-recovered with agents with bad LV's
Schwartz10 61d9266
Update types
Schwartz10 b1cff63
Remove incomplete test
Schwartz10 7b3c18e
Fix AgentMaxBorrow
Schwartz10 1ffd112
Add AgentCollateralStatsQuick method
Schwartz10 5045e48
Update types
Schwartz10 e601dad
Improve naming, add Summarize fn
Schwartz10 1b52715
Add LTV helper to PreviewATS struct
Schwartz10 d656775
Updated off-chain termination calculation for network v12
58e5682
Use StateCall instead of StateCompute for on-chain terminations
82c0da0
Check early terminations in state for on-chain
63569dd
Remove debug code
0f029ef
Update go sum
Schwartz10 ebb54b1
Change ComputeAgentData func sig
Schwartz10 243d60e
Add econ package tests
Schwartz10 be7eff8
Remove old unused code
a357290
Update naming
Schwartz10 3fe7015
Update use of WAD math
Schwartz10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,8 @@ import ( | |
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/filecoin-project/go-address" | ||
"github.com/filecoin-project/lotus/chain/types" | ||
"github.com/glifio/go-pools/constants" | ||
"github.com/glifio/go-pools/mstat" | ||
poolstypes "github.com/glifio/go-pools/types" | ||
"github.com/glifio/go-pools/vc" | ||
|
@@ -18,79 +18,60 @@ func ComputeAgentData( | |
sdk poolstypes.PoolsSDK, | ||
agentLiquidAssets *big.Int, | ||
principal *big.Int, | ||
minerIDs []address.Address, | ||
aggMinerStats *mstat.MinerStats, | ||
agentAddr common.Address, | ||
tsk *types.TipSet, | ||
) (*vc.AgentData, error) { | ||
lapi, closer, err := sdk.Extern().ConnectLotusClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer closer() | ||
|
||
// here we just work our way through the AgentData, computing each key | ||
// TODO: could probably parellize this elegently to speed things up | ||
data := &vc.AgentData{} | ||
|
||
aggMinerStats, err := mstat.ComputeMinersStats(ctx, minerIDs, tsk, lapi) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
data.QaPower = aggMinerStats.QualityAdjPower | ||
|
||
data.GreenScore = aggMinerStats.GreenScore | ||
|
||
data.AgentValue = big.NewInt(0).Add(agentLiquidAssets, aggMinerStats.Balance) | ||
|
||
data.LiveSectors = aggMinerStats.LiveSectors | ||
data.FaultySectors = aggMinerStats.FaultySectors | ||
|
||
/* ~~~~~ CollateralValue ~~~~~ */ | ||
|
||
// CV = AgentValue * terminationPenalty | ||
data.CollateralValue = CollateralValue(data.AgentValue) | ||
|
||
/* ~~~~~ ExpectedDailyFaultPenalties ~~~~~ */ | ||
|
||
// COULD REMOVE | ||
data.ExpectedDailyFaultPenalties = aggMinerStats.PenaltyFaultPerDay | ||
|
||
/* ~~~~~ ExpectedDailyRewards ~~~~~ */ | ||
ats, err := sdk.Query().AgentPreviewTerminationPrecise(ctx, agentAddr, tsk) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// here we replace the ats.AgentAvailableBal with the AgentLiquidAssets passed in this call to compute the post-action liquidation value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confusing...available balance is the same as liquid balance just in this case liquid assets has the post-action value |
||
ats.AgentAvailableBal = agentLiquidAssets | ||
|
||
data.ExpectedDailyRewards = aggMinerStats.ExpectedDailyReward | ||
data.CollateralValue = ats.LiquidationValue() | ||
|
||
/* ~~~~~ Principal ~~~~~ */ | ||
data.Principal = principal | ||
|
||
// TODO: What to do on a Pay action type? -> deduce principal _after_ the payment is made | ||
|
||
/* ~~~~~ GCRED ~~~~~ */ | ||
/* ~~~~~ SectorInfo ~~~~~ */ | ||
data.LiveSectors = aggMinerStats.LiveSectors | ||
|
||
// exposure at default is current principal, including newPrincipal | ||
var ead = data.Principal | ||
// loan to value computation uses agent's total value, less principal as its denominator | ||
// valueDenominator := lockedFunds.Balance.Add(lockedFunds.Balance, agentLiquidAssets) | ||
// subtract liabilities (upcoming payment or withdrawal) from the denominator | ||
// valueDenominator = valueDenominator.Sub(valueDenominator, deductibles) | ||
// if the LTV (loan to liquidationm value) is greater than the max LTV, we report faulty sectors in order to trigger a liquidation | ||
// this is a bit of a workaround until the liquidation value buffer is built-in to the contracts directly | ||
// using wad math | ||
numerator := new(big.Int).Mul(principal, constants.WAD) | ||
|
||
var ltv = LoanToValueRatio(ead, data.AgentValue) | ||
// loan to collateral value | ||
var ltcv = LoanToCollateralRatio(ead, data.CollateralValue) | ||
if (new(big.Int).Div(numerator, data.CollateralValue)).Cmp(constants.MAX_LTV) > 0 { | ||
// 100% faulty sectors if loan to liquidation value buffer is breached | ||
data.FaultySectors = aggMinerStats.LiveSectors | ||
} else { | ||
data.FaultySectors = aggMinerStats.FaultySectors | ||
} | ||
|
||
var equity = big.NewInt(0).Sub(data.AgentValue, data.Principal) | ||
/* ~~~~~ ExpectedDailyFaultPenalties ~~~~~ */ | ||
|
||
var dte = DebtToEquityRatio(ead, equity) | ||
// COULD REMOVE | ||
data.ExpectedDailyFaultPenalties = aggMinerStats.PenaltyFaultPerDay | ||
|
||
var faultRatio = big.NewFloat(0) | ||
var vestingToPledgeRatio = big.NewFloat(0) | ||
/* ~~~~~ ExpectedDailyRewards ~~~~~ */ | ||
|
||
if data.LiveSectors.Int64() > 0 { | ||
faultRatio = faultRatio.Quo(new(big.Float).SetInt(data.FaultySectors), new(big.Float).SetInt(data.LiveSectors)) | ||
vestingToPledgeRatio = vestingToPledgeRatio.Quo(new(big.Float).SetInt(aggMinerStats.VestingFunds), new(big.Float).SetInt(aggMinerStats.PledgedFunds)) | ||
} | ||
data.ExpectedDailyRewards = aggMinerStats.ExpectedDailyReward | ||
|
||
data.Gcred = CreditScoreSimple(ead, ltv, ltcv, data.ExpectedDailyRewards, dte, faultRatio, vestingToPledgeRatio) | ||
/* ~~~~~ GCRED (NOT IN USE) ~~~~~ */ | ||
data.Gcred = big.NewInt(100) | ||
|
||
return data, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think we should phase out the liquid assets term and use agentAvailableBalance