Skip to content

Commit

Permalink
working slashing
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Sep 21, 2024
1 parent 3c6cac7 commit a839dc5
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 121 deletions.
8 changes: 5 additions & 3 deletions pkg/internal/common/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ func ShortEthAddress(address common.Address) string {
return fmt.Sprintf("%s...%s", address.Hex()[:6], address.Hex()[len(address.Hex())-4:])
}

func FormatNumberWithUnderscores(n uint64) string {
// Convert the number to a string
numStr := strconv.FormatUint(n, 10)
func Uint64ToString(num uint64) string {
return strconv.FormatUint(num, 10)
}

func FormatNumberWithUnderscores(numStr string) string {

// If the number is less than 1000, no formatting is needed
if len(numStr) <= 3 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/allocations/initializedelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func initializeDelayAction(cCtx *cli.Context, p utils.Prompter) error {
}

// Temp to test modify Allocations
config.delegationManagerAddress = gethcommon.HexToAddress("0x1a597729A7dCfeDDD1f6130fBb099892B7623FAd")
config.delegationManagerAddress = gethcommon.HexToAddress("0xFF30144A9A749144e88bEb4FAbF020Cc7F71d2dC")

if config.broadcast {
confirm, err := p.Confirm(
Expand Down
119 changes: 83 additions & 36 deletions pkg/operator/allocations/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package allocations

import (
"fmt"
"math/big"
"sort"

"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common"
Expand All @@ -21,6 +22,11 @@ import (
"github.com/urfave/cli/v2"
)

var (
// PrecisionFactor comes from the allocation manager contract
PrecisionFactor = big.NewInt(1e18)
)

func ShowCmd(p utils.Prompter) *cli.Command {
showCmd := &cli.Command{
Name: "show",
Expand Down Expand Up @@ -53,7 +59,7 @@ func showAction(cCtx *cli.Context, p utils.Prompter) error {
}

// Temp to test modify allocations
config.delegationManagerAddress = gethcommon.HexToAddress("0x1a597729A7dCfeDDD1f6130fBb099892B7623FAd")
config.delegationManagerAddress = gethcommon.HexToAddress("0xFF30144A9A749144e88bEb4FAbF020Cc7F71d2dC")

elReader, err := elcontracts.NewReaderFromConfig(
elcontracts.Config{
Expand All @@ -76,7 +82,25 @@ func showAction(cCtx *cli.Context, p utils.Prompter) error {
if err != nil {
return eigenSdkUtils.WrapError("failed to get allocatable magnitude", err)
}
logger.Debugf("Allocatable magnitude for strategy %v: %s", strategyAddress, common.FormatNumberWithUnderscores(allocatableMagnitude))
logger.Debugf(
"Allocatable magnitude for strategy %v: %s",
strategyAddress,
common.FormatNumberWithUnderscores(common.Uint64ToString(allocatableMagnitude)),
)
}

// for each strategy address, get the total magnitude
totalMagnitudeMap := make(map[string]uint64)
totalMagnitudes, err := elReader.GetTotalMagnitudes(
&bind.CallOpts{Context: ctx},
config.operatorAddress,
config.strategyAddresses,
)
if err != nil {
return eigenSdkUtils.WrapError("failed to get allocatable magnitude", err)
}
for i, strategyAddress := range config.strategyAddresses {
totalMagnitudeMap[strategyAddress.String()] = totalMagnitudes[i]
}

opSets, slashableMagnitudes, err := elReader.GetCurrentSlashableMagnitudes(
Expand All @@ -89,7 +113,6 @@ func showAction(cCtx *cli.Context, p utils.Prompter) error {
}

// Get Pending allocations
//pendingAllocationsDetails := make(AllocationDetailsHolder, 0)
pendingAllocationMap := make(map[string]AllocDetails)
for _, strategyAddress := range config.strategyAddresses {
pendingAllocations, timestamps, err := elReader.GetPendingAllocations(
Expand All @@ -107,21 +130,13 @@ func showAction(cCtx *cli.Context, p utils.Prompter) error {
if pendingAllocation == 0 && timestamp == 0 {
continue
}
//pendingAllocationsDetails = append(pendingAllocationsDetails, AllocationDetails{
// StrategyAddress: strategyAddress,
// AVSAddress: opSet.Avs,
// OperatorSetId: opSet.OperatorSetId,
// Allocation: pendingAllocation,
// Timestamp: timestamp,
//})
pendingAllocationMap[getUniqueKey(strategyAddress, opSet)] = AllocDetails{
Magnitude: pendingAllocation,
Timestamp: timestamp,
}
}
}

//pendingDeallocationsDetails := make(AllocationDetailsHolder, 0)
pendingdeAllocationMap := make(map[string]AllocDetails)
for _, strategyAddress := range config.strategyAddresses {
pendingDeallocations, err := elReader.GetPendingDeallocations(
Expand All @@ -138,20 +153,23 @@ func showAction(cCtx *cli.Context, p utils.Prompter) error {
if pendingAllocation.MagnitudeDiff == 0 && pendingAllocation.CompletableTimestamp == 0 {
continue
}
//pendingDeallocationsDetails = append(pendingDeallocationsDetails, AllocationDetails{
// StrategyAddress: strategyAddress,
// AVSAddress: opSet.Avs,
// OperatorSetId: opSet.OperatorSetId,
// Allocation: pendingAllocation.MagnitudeDiff,
// Timestamp: pendingAllocation.CompletableTimestamp,
//})
pendingdeAllocationMap[getUniqueKey(strategyAddress, opSet)] = AllocDetails{
Magnitude: pendingAllocation.MagnitudeDiff,
Timestamp: pendingAllocation.CompletableTimestamp,
}
}
}

// Get Operator Shares
operatorScaledSharesMap := make(map[string]*big.Int)
for _, strategyAddress := range config.strategyAddresses {
shares, err := elReader.GetOperatorScaledShares(&bind.CallOpts{}, config.operatorAddress, strategyAddress)
if err != nil {
return err
}
operatorScaledSharesMap[strategyAddress.String()] = shares
}

slashableMagnitudeHolders := make(SlashableMagnitudeHolders, 0)
for i, strategyAddress := range config.strategyAddresses {
slashableMagnitude := slashableMagnitudes[i]
Expand All @@ -171,36 +189,54 @@ func showAction(cCtx *cli.Context, p utils.Prompter) error {
newAllocation = currSlashableMag
currSlashableMag = currSlashableMag + newAllocationDiff
}

operatorScaledShares := operatorScaledSharesMap[strategyAddress.String()]

currSlashableMagBigInt := big.NewInt(1)
currSlashableMagBigInt = currSlashableMagBigInt.SetUint64(currSlashableMag)

scaledOpShares := big.NewInt(1)
scaledOpShares = scaledOpShares.Set(operatorScaledShares)
scaledOpShares = scaledOpShares.Div(scaledOpShares, PrecisionFactor)
shares := scaledOpShares.Mul(scaledOpShares, currSlashableMagBigInt)

newShares := getSharesFromMagnitude(operatorScaledShares, newAllocation)

percentageShares := big.NewInt(1)
percentageShares = percentageShares.Mul(scaledOpShares, big.NewInt(100))
percentageSharesFloat := new(
big.Float,
).Quo(new(big.Float).SetInt(percentageShares), new(big.Float).SetInt(operatorScaledShares))
slashableMagnitudeHolders = append(slashableMagnitudeHolders, SlashableMagnitudesHolder{
StrategyAddress: strategyAddress,
AVSAddress: opSet.Avs,
OperatorSetId: opSet.OperatorSetId,
SlashableMagnitude: currSlashableMag,
NewMagnitude: newAllocation,
NewMagnitudeTimestamp: newTimestamp,
Shares: shares,
SharesPercentage: percentageSharesFloat.String(),
NewAllocationShares: newShares,
})
}
}

// Get Operator Shares
operatorSharesMap := make(map[string]*big.Int)
for _, strategyAddress := range config.strategyAddresses {
shares, err := elReader.GetOperatorShares(&bind.CallOpts{}, config.operatorAddress, strategyAddress)
if err != nil {
return err
}
operatorSharesMap[strategyAddress.String()] = shares
}

for key, val := range operatorSharesMap {
fmt.Printf("Strategy Address: %s, Shares %s\n", key, val.String())
}

fmt.Println()
//fmt.Println("------------------Pending Allocations---------------------")
//if config.outputType == string(common.OutputType_Json) {
// pendingAllocationsDetails.PrintJSON()
//} else {
// pendingAllocationsDetails.PrintPretty()
//}
//fmt.Println()
//
//fmt.Println()
//fmt.Println("------------------Pending Deallocations---------------------")
//if config.outputType == string(common.OutputType_Json) {
// pendingDeallocationsDetails.PrintJSON()
//} else {
// pendingDeallocationsDetails.PrintPretty()
//}
//fmt.Println()

fmt.Println("------------------Current Slashable Magnitudes---------------------")
fmt.Printf("------------------ Allocation State for %s ---------------------\n", config.operatorAddress.String())
if config.outputType == string(common.OutputType_Json) {
slashableMagnitudeHolders.PrintJSON()
} else {
Expand All @@ -210,6 +246,17 @@ func showAction(cCtx *cli.Context, p utils.Prompter) error {
return nil
}

func getSharesFromMagnitude(totalScaledShare *big.Int, magnitude uint64) *big.Int {
slashableMagBigInt := big.NewInt(1)
slashableMagBigInt = slashableMagBigInt.SetUint64(magnitude)

scaledOpShares := big.NewInt(1)
scaledOpShares = scaledOpShares.Set(totalScaledShare)
scaledOpShares = scaledOpShares.Div(scaledOpShares, PrecisionFactor)
shares := scaledOpShares.Mul(scaledOpShares, slashableMagBigInt)
return shares
}

func getUniqueKey(strategyAddress gethcommon.Address, opSet contractIAllocationManager.OperatorSet) string {
return fmt.Sprintf("%s-%s-%d", strategyAddress.String(), opSet.Avs.String(), opSet.OperatorSetId)
}
Expand Down
Loading

0 comments on commit a839dc5

Please sign in to comment.