Skip to content
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 33 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ce8797e
Clean up types, delete unused code
Schwartz10 Jan 16, 2024
d85d00c
More type cleanup
Schwartz10 Jan 16, 2024
59fc674
Fetch MinerInfo directly from state, not api
Schwartz10 Jan 16, 2024
f21e554
Return Vesting and pledged bal in PreviewTerminateSectorsReturn
Schwartz10 Jan 16, 2024
50b7fcc
Cleanup agent liquidation summary method
Schwartz10 Jan 16, 2024
0557a03
Add helper methods to the PreviewAgentTerminationSummary struct
Schwartz10 Jan 16, 2024
8cf6c22
Move test helpers to utils package
Schwartz10 Jan 16, 2024
569b91a
Add tests
Schwartz10 Jan 17, 2024
b4971c7
Add Agent Liquid balance for ADO integration
Schwartz10 Jan 17, 2024
8d92c26
Delete max withdraw query
Schwartz10 Jan 17, 2024
cc4f497
Update mocks
Schwartz10 Jan 17, 2024
7a1f143
Fix max borrow helpers
Schwartz10 Jan 18, 2024
736b3aa
Polish
Schwartz10 Jan 19, 2024
a474b0d
Use new liquidation value in econ.ComputeAgentData
Schwartz10 Jan 19, 2024
6caef9e
Prepare for set-recovered handling of faulty agents
Schwartz10 Jan 19, 2024
b153010
Prepare for set-recovered with agents with bad LV's
Schwartz10 Jan 19, 2024
61d9266
Update types
Schwartz10 Jan 19, 2024
b1cff63
Remove incomplete test
Schwartz10 Jan 19, 2024
7b3c18e
Fix AgentMaxBorrow
Schwartz10 Jan 19, 2024
1ffd112
Add AgentCollateralStatsQuick method
Schwartz10 Jan 22, 2024
5045e48
Update types
Schwartz10 Jan 22, 2024
e601dad
Improve naming, add Summarize fn
Schwartz10 Jan 22, 2024
1b52715
Add LTV helper to PreviewATS struct
Schwartz10 Jan 22, 2024
d656775
Updated off-chain termination calculation for network v12
Jan 23, 2024
58e5682
Use StateCall instead of StateCompute for on-chain terminations
Jan 23, 2024
82c0da0
Check early terminations in state for on-chain
Jan 23, 2024
63569dd
Remove debug code
Jan 23, 2024
0f029ef
Update go sum
Schwartz10 Jan 23, 2024
ebb54b1
Change ComputeAgentData func sig
Schwartz10 Jan 24, 2024
243d60e
Add econ package tests
Schwartz10 Jan 24, 2024
be7eff8
Remove old unused code
Jan 24, 2024
a357290
Update naming
Schwartz10 Jan 24, 2024
3fe7015
Update use of WAD math
Schwartz10 Jan 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ var (
var dialAddr = "https://api.node.glif.io/rpc/v1"
var t_dialAddr = "https://api.calibration.node.glif.io/rpc/v1"

var EventsURL = "https://events.glif.link"

var MAX_UINT256 = new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(1))
var WAD = big.NewInt(1e18)

Expand All @@ -64,3 +66,8 @@ var INFINITY_POOL_ID = big.NewInt(0)
var CHUNKSIZE = big.NewInt(50000)

var FAULTY_SECTOR_TOLERANCE = big.NewFloat(0.001)

// NOTE these should be generated in the future from the live values in the contracts
var MAX_DTE = big.NewInt(2e18)
var MAX_LTV = big.NewInt(8e17)
var MAX_DTI = big.NewInt(25e16)
79 changes: 30 additions & 49 deletions econ/econ.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -16,81 +16,62 @@ import (
func ComputeAgentData(
ctx context.Context,
sdk poolstypes.PoolsSDK,
agentLiquidAssets *big.Int,
agentAvailableBalance *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
data.AgentValue = big.NewInt(0).Add(agentAvailableBalance, aggMinerStats.Balance)

/* ~~~~~ 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 agentAvailableBalance passed in this call to compute the post-action liquidation value
ats.AgentAvailableBal = agentAvailableBalance

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
}
Loading