Skip to content

Commit

Permalink
Protocol v2 (#95)
Browse files Browse the repository at this point in the history
* Update abigenerated files

* Schwartz protocol v2 (#98)

* Refactor basic methods for V2

* Add AgentDebt call

* Add initial framework for margin helpers

* Add _fi helper methods

* Refactor liquidation queries and consolidate packages

* Add back tests

* Small fixes

* Small tweaks

* Add fee debt test

Fix tests

* Add econ tests, bug fixes and improvements

* Get rid of pool registry

* Small tweaks

* Add agent data helpers and tests

Fix collateral value not changing on computeagentdata

* Add push funds helper with protect against push to fee debt miner

* Improve tests

* Add test helper

* More cleanup

* Nix the mock ado

* Delete unused constants

* Add claims decoder

* More cleanup

* Change method name

* Fix issue with agentfi using only principal when computing debt (#100)

* Add back edr calc

* Update abigen

* Update deployments

* Add poolsapi calls to econ package (#101)

Still need to do interest in agentfi call

* add margin and leverage ratio

* handle edge cases in margin and dtl

* Schwartz improvements (#102)

* Update deployments

* Add more api helpers and tests

* More helper methods

* Revert bad merge

* Update abigen

* Update afi with spendable balance (#106)

* Fix/borrow (#107)

* fix max borrow on agent fi

* Create minerfi type

---------

Co-authored-by: Jonathan <jpschwartz2@uwalumni.com>

* fix index out of range when miner sectors are less than desired sample size (#108)

* add miner that gave error on staging to test cases (#109)

* add miner that gave error on staging to test cases

* fix step size rounding error

* change BaseFi test to MinerFi test

* Add Events API endpoint to extern struct (#105)

* Add Events API endpoint to extern struct

Also change functions that use it to take a SDK argument so
they can look up the endpoint.

* Polish

---------

Co-authored-by: Jonathan <jpschwartz2@uwalumni.com>

* Add test github action

* Fix/glifapi (#110)

* Add the AgentMarginJSON struct type

* Update type name

* Add LiveSectors and FaultySectors to AgentMarginJSON

* Update type

* Potential fix for eventsapi once pools-events is redeployed

Consolidate function calls to the api

* Update types

* Fix glifapi queries

* Refactor principal and interest sdk funcs to work for v0 historically

Fix tests

---------

Co-authored-by: ganzai-san <ganzai@protonmail.com>
Co-authored-by: ganzai <79986704+ganzai-san@users.noreply.github.com>
Co-authored-by: Jim Pick <jim@jimpick.com>
  • Loading branch information
4 people authored Sep 4, 2024
1 parent 8b1bbc0 commit 028a585
Show file tree
Hide file tree
Showing 66 changed files with 9,002 additions and 13,642 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Go Test

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
name: Run Go Tests
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.22.0

- name: Install dependencies
run: go mod download

- name: Run tests
run: go test -v ./...
env:
LOTUS_DIAL_ADDR: ${{ secrets.LOTUS_DIAL_ADDR }}
LOTUS_TOKEN: ${{ secrets.LOTUS_TOKEN }}
1,779 changes: 0 additions & 1,779 deletions abigen/AgentPolice.go

This file was deleted.

2,344 changes: 2,344 additions & 0 deletions abigen/AgentPoliceV2.go

Large diffs are not rendered by default.

2,436 changes: 0 additions & 2,436 deletions abigen/InfinityPool.go

This file was deleted.

3,281 changes: 3,281 additions & 0 deletions abigen/InfinityPoolV2.go

Large diffs are not rendered by default.

564 changes: 0 additions & 564 deletions abigen/MockMiner.go

This file was deleted.

906 changes: 0 additions & 906 deletions abigen/Ping.go

This file was deleted.

545 changes: 0 additions & 545 deletions abigen/PoolRegistry.go

This file was deleted.

1,038 changes: 0 additions & 1,038 deletions abigen/PreStake.go

This file was deleted.

975 changes: 0 additions & 975 deletions abigen/PublicGoodsDonator.go

This file was deleted.

959 changes: 0 additions & 959 deletions abigen/RateModule.go

This file was deleted.

803 changes: 0 additions & 803 deletions abigen/SimpleRamp.go

This file was deleted.

421 changes: 421 additions & 0 deletions abigen/UpgradeToV2.go

Large diffs are not rendered by default.

13 changes: 4 additions & 9 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
MethodPushFunds Method = "pushFunds"
MethodPullFunds Method = "pullFunds"
MethodSetRecovered Method = "setRecovered"
MethodDefaultDTL Method = "setAgentDefaultDTL"
)

type Route string
Expand All @@ -52,13 +53,9 @@ var (
RouteVCIssuer Route = "ROUTER_VC_ISSUER"
RouteCredParser Route = "ROUTER_CRED_PARSER"
RouteAgentDeployer Route = "ROUTER_AGENT_DEPLOYER"
RouteInfinityPool Route = "ROUTER_INFINITY_POOL"
)

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 @@ -70,7 +67,5 @@ var CHUNKSIZE = big.NewInt(EpochsInDay)

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)
var MAX_BORROW_DTL = big.NewInt(75e16)
var LIQUIDATION_DTL = big.NewInt(85e16)
42 changes: 34 additions & 8 deletions deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,53 @@ import (
var dialAddr = "https://api.node.glif.io/rpc/v1"
var t_dialAddr = "https://api.calibration.node.glif.io/rpc/v1"

var MainnetEventsURL = "https://events.glif.link"
var CalibnetEventsURL = "https://events-calibration.glif.link"
var StagingEventsURL = "https://events-staging.glif.link"

var Extern = types.Extern{
AdoAddr: "https://ado.glif.link/rpc/v1",
LotusDialAddr: dialAddr,
LotusToken: "",
EventsURL: MainnetEventsURL,
}

var TestExtern = types.Extern{
AdoAddr: "https://ado-testnet.glif.link/rpc/v1",
LotusDialAddr: t_dialAddr,
LotusToken: "",
EventsURL: CalibnetEventsURL,
}

var InfinityPoolV0 = common.HexToAddress("0x43dAe5624445e7679D16a63211c5ff368681500c")
var InfinityPoolV2 = common.HexToAddress("0xe764Acf02D8B7c21d2B6A8f0a96C78541e0DC3fd")

var AgentPoliceV0 = common.HexToAddress("0x1403D2163B4beA09F1b26cA4479FFc60Ca2E46BF")
var AgentPoliceV2 = common.HexToAddress("0xE87f7d4C9f9BD235CB1b15b88Ed5E9a844726947")

var ProtoMeta = types.ProtocolMeta{
AgentFactory: common.HexToAddress("0x526Ab27Af261d28c2aC1fD24f63CcB3bd44D50e0"),
AgentPolice: common.HexToAddress("0x1403D2163B4beA09F1b26cA4479FFc60Ca2E46BF"),
AgentPolice: AgentPoliceV2,
IFIL: common.HexToAddress("0x690908f7fa93afC040CFbD9fE1dDd2C2668Aa0e0"),
InfinityPool: common.HexToAddress("0x43dAe5624445e7679D16a63211c5ff368681500c"),
SimpleRamp: common.HexToAddress("0x8147AccE69d711bcED176a0b7a029Ff54800d930"),
InfinityPool: InfinityPoolV2,
MinerRegistry: common.HexToAddress("0xF246acF2b24316fE108ff906f6e90833a14e9263"),
PoolRegistry: common.HexToAddress("0x7aFe4f4Ca8f301FA4177CCCDd10a4a22756588e2"),
Router: common.HexToAddress("0xe6e6e71E747EeD9fe45F0ff63E1B3E4a7c1199bF"),
WFIL: common.HexToAddress("0x60E1773636CF5E4A227d9AC24F20fEca034ee25A"),
ChainID: big.NewInt(314),
}

var TestInfinityPoolV0 = common.HexToAddress("0x37621D9a49FFE8f13BF2Adcb87f1b115D82ac378")
var TestInfinityPoolV2 = common.HexToAddress("0x0E5Ef6a4848d6F5eF18db8928672a29Cdc6c9a92")

var TestAgentPoliceV0 = common.HexToAddress("0xDA730B08A67D261fD8343c2061a269A2296c8469")
var TestAgentPoliceV2 = common.HexToAddress("0x55ffF1E383C02C51c06B2A041cb944fDaDe87D7c")

var TestProtoMeta = types.ProtocolMeta{
AgentFactory: common.HexToAddress("0x1EbF54537437a372614Eb50B6EDCbDB643603Fe7"),
AgentPolice: common.HexToAddress("0xDA730B08A67D261fD8343c2061a269A2296c8469"),
AgentPolice: TestAgentPoliceV2,
IFIL: common.HexToAddress("0x8c97F94b2cDbF7Dc0098057334d9908C4dC0a885"),
InfinityPool: common.HexToAddress("0x37621D9a49FFE8f13BF2Adcb87f1b115D82ac378"),
SimpleRamp: common.HexToAddress("0xbf3dE3D08A8C93fFB87A02E3Cb9758746450246e"),
InfinityPool: TestInfinityPoolV2,
MinerRegistry: common.HexToAddress("0xac13FF4743594344152Af8d1626cC80257bdf5C0"),
PoolRegistry: common.HexToAddress("0x9de147901aA6a87bFC6BC1c84b2fE1a60bf38e1A"),
Router: common.HexToAddress("0xa0770911848Bd1A0930Fc6782522381Bb6256617"),
WFIL: common.HexToAddress("0xaC26a4Ab9cF2A8c5DBaB6fb4351ec0F4b07356c4"),
ChainID: big.NewInt(314159),
Expand All @@ -57,3 +71,15 @@ var (
ProtocolDeployEpoch = big.NewInt(2890957)
TProtocolDeployEpoch = big.NewInt(559673)
)

var (
TProtocolV2DeployEpoch = big.NewInt(1912401)
TProtocolV2UpgradeEpoch = big.NewInt(0)
)

var (
// the agent police v2 and infinity pool v2 were deployed prior to this epoch, so it is safe to fetch events from this height
ProtocolV2DeployEpoch = big.NewInt(4215971)
// no events will occur on these contracts before this epoch
ProtocolV2UpgradeEpoch = big.NewInt(0)
)
133 changes: 133 additions & 0 deletions econ/agentdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package econ

import (
"context"
"errors"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/chain/types"
pooltypes "github.com/glifio/go-pools/types"
"github.com/glifio/go-pools/util"
"github.com/glifio/go-pools/vc"
)

func GetAgentEcon(ctx context.Context, agentAddr common.Address, psdk pooltypes.PoolsSDK, ts *types.TipSet) (*big.Int, *big.Int, error) {
query := psdk.Query()
blockNumber := big.NewInt(int64(ts.Height()))

agentAvailableBal, err := query.AgentLiquidAssets(ctx, agentAddr, blockNumber)
if err != nil {
return nil, nil, err
}

principal, err := getAgentPrincipal(ctx, agentAddr, psdk, ts)
if err != nil {
return nil, nil, err
}

return agentAvailableBal, principal, nil
}

func getAgentPrincipal(ctx context.Context, agentAddr common.Address, psdk pooltypes.PoolsSDK, ts *types.TipSet) (*big.Int, error) {
blockNumber := big.NewInt(int64(ts.Height()))
principal, err := psdk.Query().AgentPrincipal(ctx, agentAddr, blockNumber)
if err != nil {
return nil, err
}

return principal, nil
}

func ComputeBorrowAgentData(ctx context.Context, agentAddr common.Address, amount *big.Int, psdk pooltypes.PoolsSDK, ts *types.TipSet) (*vc.AgentData, error) {
principal, err := getAgentPrincipal(ctx, agentAddr, psdk, ts)
if err != nil {
return nil, err
}

newPrincipal := big.NewInt(0).Add(principal, amount)

data, err := ComputeAgentData(ctx, agentAddr, amount, newPrincipal, address.Undef, psdk, ts)
if err != nil {
return nil, err
}

return data, err
}

func ComputePayAgentData(ctx context.Context, agentAddr common.Address, value *big.Int, psdk pooltypes.PoolsSDK, ts *types.TipSet) (*vc.AgentData, error) {
agentAvailableBal, principal, err := GetAgentEcon(ctx, agentAddr, psdk, ts)
if err != nil {
return nil, err
}

if condition := value.Cmp(agentAvailableBal); condition == 1 {
return nil, errors.New("pay amount is greater than agent's available balance")
}

removingBal := big.NewInt(0).Sub(big.NewInt(0), value)

return ComputeAgentData(ctx, agentAddr, removingBal, principal, address.Undef, psdk, ts)
}

func ComputeWithdrawAgentData(ctx context.Context, agentAddr common.Address, withdrawAmount *big.Int, psdk pooltypes.PoolsSDK, ts *types.TipSet) (*vc.AgentData, error) {
agentAvailableBal, principal, err := GetAgentEcon(ctx, agentAddr, psdk, ts)
if err != nil {
return nil, err
}

// check if amount is greater than agentAvailableBal
if condition := withdrawAmount.Cmp(agentAvailableBal); condition == 1 {
return nil, errors.New("withdraw amount is greater than agent's available balance")
}

// here we want to pass a negative value to ComputeAgentData to indicate that the agent's balance is being withdrawn
removingVal := big.NewInt(0).Sub(big.NewInt(0), withdrawAmount)

data, err := ComputeAgentData(ctx, agentAddr, removingVal, principal, address.Undef, psdk, ts)
if err != nil {
return nil, err
}

return data, err
}

func ComputePushFundsAgentData(ctx context.Context, agentAddr common.Address, minerAddr address.Address, psdk pooltypes.PoolsSDK, ts *types.TipSet) (*vc.AgentData, error) {
principal, err := getAgentPrincipal(ctx, agentAddr, psdk, ts)
if err != nil {
return nil, err
}

lapi, closer, err := psdk.Extern().ConnectLotusClient()
if err != nil {
return nil, err
}
defer closer()

// make sure we aren't pushing a miner with fee debt
_, mstate, err := util.LoadMinerActor(ctx, lapi, minerAddr, ts)
if err != nil {
return nil, err
}

feeDebt, err := mstate.FeeDebt()
if err != nil {
return nil, err
}

if feeDebt.Sign() == 1 {
return nil, errors.New("cannot push funds to miner with fee debt")
}

return ComputeAgentData(ctx, agentAddr, big.NewInt(0), principal, address.Undef, psdk, ts)
}

func ComputeRmMinerAgentData(ctx context.Context, agentAddr common.Address, miner address.Address, psdk pooltypes.PoolsSDK, ts *types.TipSet) (*vc.AgentData, error) {
principal, err := getAgentPrincipal(ctx, agentAddr, psdk, ts)
if err != nil {
return nil, err
}

return ComputeAgentData(ctx, agentAddr, big.NewInt(0), principal, miner, psdk, ts)
}
Loading

0 comments on commit 028a585

Please sign in to comment.