Skip to content

Commit

Permalink
feat: core account methods
Browse files Browse the repository at this point in the history
  • Loading branch information
asolovov committed May 2, 2024
1 parent 172443c commit adfc65b
Show file tree
Hide file tree
Showing 11 changed files with 477 additions and 0 deletions.
35 changes: 35 additions & 0 deletions models/account.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

import (
"github.com/gateway-fm/perpsv3-Go/contracts/core"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -30,6 +31,26 @@ type AccountLiquidated struct {
FullLiquidated bool
}

type AccountCollateral struct {
TotalDeposited *big.Int
TotalAssigned *big.Int
TotalLocked *big.Int
}

func GetAccountCollateralFromContract(res struct {
TotalDeposited *big.Int
TotalAssigned *big.Int
TotalLocked *big.Int
}) *AccountCollateral {
ac := &AccountCollateral{}

ac.TotalLocked = res.TotalLocked
ac.TotalAssigned = res.TotalAssigned
ac.TotalDeposited = res.TotalDeposited

return ac
}

// FormatAccount is used to get account from given data
func FormatAccount(
id *big.Int,
Expand All @@ -44,3 +65,17 @@ func FormatAccount(
LastInteraction: lastInteraction,
}
}

func FormatAccountCore(
id *big.Int,
owner common.Address,
lastInteraction uint64,
permissions []core.IAccountModuleAccountPermissions,
) *Account {
return &Account{
ID: id,
Permissions: getUserPermissionsCore(permissions),
Owner: owner,
LastInteraction: lastInteraction,
}
}
42 changes: 42 additions & 0 deletions models/collateral.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import (
"github.com/gateway-fm/perpsv3-Go/pkg/logger"
)

type CollateralConfiguration struct {
DepositingEnabled bool
IssuanceRatioD18 *big.Int
LiquidationRatioD18 *big.Int
LiquidationRewardD18 *big.Int
OracleNodeId [32]byte
TokenAddress common.Address
MinDelegationD18 *big.Int
}

// CollateralDeposited is a `Deposited` Core smart-contract event struct
type CollateralDeposited struct {
AccountId *big.Int
Expand All @@ -34,6 +44,38 @@ type CollateralPrice struct {
Price *big.Int
}

// DepositingEnabled bool
// IssuanceRatioD18 *big.Int
// LiquidationRatioD18 *big.Int
// LiquidationRewardD18 *big.Int
// OracleNodeId [32]byte
// TokenAddress common.Address
// MinDelegationD18 *big.Int

func GetCollateralConfigurations(data []core.CollateralConfigurationData) []*CollateralConfiguration {
cc := make([]*CollateralConfiguration, len(data))

for i, c := range data {
cc[i] = GetCollateralConfiguration(c)
}

return cc
}

func GetCollateralConfiguration(data core.CollateralConfigurationData) *CollateralConfiguration {
c := &CollateralConfiguration{}

c.DepositingEnabled = data.DepositingEnabled
c.IssuanceRatioD18 = data.IssuanceRatioD18
c.LiquidationRatioD18 = data.LiquidationRatioD18
c.LiquidationRewardD18 = data.LiquidationRewardD18
c.OracleNodeId = data.OracleNodeId
c.TokenAddress = data.TokenAddress
c.MinDelegationD18 = data.MinDelegationD18

return c
}

// GetCollateralDepositedFromEvent is used to get CollateralDeposited struct from given contract event
func GetCollateralDepositedFromEvent(event *core.CoreDeposited, time uint64) *CollateralDeposited {
if event == nil {
Expand Down
38 changes: 38 additions & 0 deletions models/coreLiquidation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package models

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/gateway-fm/perpsv3-Go/contracts/core"
)

type CoreLiquidation struct {
AccountId *big.Int
PoolId *big.Int
CollateralType common.Address
DebtLiquidated *big.Int
CollateralLiquidated *big.Int
AmountRewarded *big.Int
LiquidateAsAccountId *big.Int
Sender common.Address
BlockNumber uint64
BlockTimestamp uint64
}

func GetCoreLiquidationFromEvent(event *core.CoreLiquidation, time uint64) *CoreLiquidation {
c := &CoreLiquidation{}

c.AccountId = event.AccountId
c.PoolId = event.PoolId
c.CollateralType = event.CollateralType
c.DebtLiquidated = event.LiquidationData.DebtLiquidated
c.CollateralLiquidated = event.LiquidationData.CollateralLiquidated
c.AmountRewarded = event.LiquidationData.AmountRewarded
c.LiquidateAsAccountId = event.LiquidateAsAccountId
c.Sender = event.Sender
c.BlockNumber = event.Raw.BlockNumber
c.BlockTimestamp = time

return c
}
14 changes: 14 additions & 0 deletions models/permissions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

import (
"github.com/gateway-fm/perpsv3-Go/contracts/core"
"strings"

"github.com/gateway-fm/perpsv3-Go/contracts/perpsMarket"
Expand Down Expand Up @@ -63,3 +64,16 @@ func decodePermissions(perm perpsMarket.IAccountModuleAccountPermissions) (res [

return res
}

func decodePermissionsCore(perm core.IAccountModuleAccountPermissions) (res []Permission) {
for _, b := range perm.Permissions {
p, err := PermissionFromString(strings.TrimRight(string(b[:]), string(rune(0))))
if err != nil {
logger.Log().WithField("layer", "Model-decodePermissions").Warningf("received unsupported bytes value %v", string(b[:]))
} else {
res = append(res, p)
}
}

return res
}
14 changes: 14 additions & 0 deletions models/userPermissions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

import (
"github.com/gateway-fm/perpsv3-Go/contracts/core"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -33,3 +34,16 @@ func getUserPermissions(perms []perpsMarket.IAccountModuleAccountPermissions) (r

return res
}

func getUserPermissionsCore(perms []core.IAccountModuleAccountPermissions) (res []*UserPermissions) {
for _, p := range perms {
perm := &UserPermissions{
User: p.User,
Permissions: decodePermissionsCore(p),
}

res = append(res, perm)
}

return res
}
36 changes: 36 additions & 0 deletions models/vaultLiquidation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package models

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/gateway-fm/perpsv3-Go/contracts/core"
)

type CoreVaultLiquidation struct {
PoolId *big.Int
CollateralType common.Address
DebtLiquidated *big.Int
CollateralLiquidated *big.Int
AmountRewarded *big.Int
LiquidateAsAccountId *big.Int
Sender common.Address
BlockNumber uint64
BlockTimestamp uint64
}

func GetCoreVaultLiquidationFromEvent(event *core.CoreVaultLiquidation, time uint64) *CoreVaultLiquidation {
c := &CoreVaultLiquidation{}

c.PoolId = event.PoolId
c.CollateralType = event.CollateralType
c.DebtLiquidated = event.LiquidationData.DebtLiquidated
c.CollateralLiquidated = event.LiquidationData.CollateralLiquidated
c.AmountRewarded = event.LiquidationData.AmountRewarded
c.LiquidateAsAccountId = event.LiquidateAsAccountId
c.Sender = event.Sender
c.BlockNumber = event.Raw.BlockNumber
c.BlockTimestamp = time

return c
}
45 changes: 45 additions & 0 deletions perpsv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ type IPerpsv3 interface {
// limit. For most public RPC providers the value for limit is 20 000 blocks
RetrievePoolCreated(limit uint64) ([]*models.PoolCreated, error)

// RetrieveLiquidationsCore is used to get all `Liquidation` events from the Core contract with given block search
// limit. For most public RPC providers the value for limit is 20 000 blocks
RetrieveLiquidationsCore(limit uint64) ([]*models.CoreLiquidation, error)

// RetrieveVaultLiquidationsCore is used to get all `VaultLiquidation` events from the Core contract with given block search
// limit. For most public RPC providers the value for limit is 20 000 blocks
RetrieveVaultLiquidationsCore(limit uint64) ([]*models.CoreVaultLiquidation, error)

// ListenTrades is used to subscribe on the contract "OrderSettled" event. The goroutine will return events on the
// TradesChan chanel and errors on the ErrChan chanel.
// To close the subscription use events.TradeSubscription `Close` function
Expand Down Expand Up @@ -260,6 +268,15 @@ type IPerpsv3 interface {
// GetPoolName is used to get pool name from given PoolID
GetPoolName(poolID *big.Int) (string, error)

// GetAccountCollateralCore is used to get account collateral data for given account ID and collateral type
GetAccountCollateralCore(accountId *big.Int, collateralType common.Address) (*models.AccountCollateral, error)

// GetAccountAvailableCollateral is used to get account available collateral data for given account ID and collateral type
GetAccountAvailableCollateral(accountId *big.Int, collateralType common.Address) (*big.Int, error)

// GetCollateralConfigurations is used to get CollateralConfiguration data
GetCollateralConfigurations(hideDisabled bool) ([]*models.CollateralConfiguration, error)

// FormatAccount is used to get account, and it's additional data from the contract by given account id
FormatAccount(id *big.Int) (*models.Account, error)

Expand All @@ -270,6 +287,10 @@ type IPerpsv3 interface {
// limit. For most public RPC providers the value for limit is 20 000 blocks
FormatAccountsLimit(limit uint64) ([]*models.Account, error)

// FormatAccountCore is used to get all accounts and their additional data from the core contract with given block search
// limit. For most public RPC providers the value for limit is 20 000 blocks
FormatAccountCore(id *big.Int) (*models.Account, error)

// Config is used to get current lib config
Config() *config.PerpsvConfig

Expand Down Expand Up @@ -402,6 +423,14 @@ func (p *Perpsv3) RetrievePoolCreated(limit uint64) ([]*models.PoolCreated, erro
return p.service.RetrievePoolCreated(limit)
}

func (p *Perpsv3) RetrieveLiquidationsCore(limit uint64) ([]*models.CoreLiquidation, error) {
return p.service.RetrieveLiquidationsCore(limit)
}

func (p *Perpsv3) RetrieveVaultLiquidationsCore(limit uint64) ([]*models.CoreVaultLiquidation, error) {
return p.service.RetrieveVaultLiquidationsCore(limit)
}

func (p *Perpsv3) ListenTrades() (*events.TradeSubscription, error) {
return p.events.ListenTrades()
}
Expand Down Expand Up @@ -558,6 +587,18 @@ func (p *Perpsv3) GetPoolName(poolID *big.Int) (string, error) {
return p.service.GetPoolName(poolID)
}

func (p *Perpsv3) GetAccountCollateralCore(accountId *big.Int, collateralType common.Address) (*models.AccountCollateral, error) {
return p.service.GetAccountCollateralCore(accountId, collateralType)
}

func (p *Perpsv3) GetAccountAvailableCollateral(accountId *big.Int, collateralType common.Address) (*big.Int, error) {
return p.service.GetAccountAvailableCollateral(accountId, collateralType)
}

func (p *Perpsv3) GetCollateralConfigurations(hideDisabled bool) ([]*models.CollateralConfiguration, error) {
return p.service.GetCollateralConfigurations(hideDisabled)
}

func (p *Perpsv3) FormatAccounts() ([]*models.Account, error) {
return p.service.FormatAccounts()
}
Expand All @@ -570,6 +611,10 @@ func (p *Perpsv3) FormatAccountsLimit(limit uint64) ([]*models.Account, error) {
return p.service.FormatAccountsLimit(limit)
}

func (p *Perpsv3) FormatAccountCore(id *big.Int) (*models.Account, error) {
return p.service.FormatAccountCore(id)
}

func (p *Perpsv3) Config() *config.PerpsvConfig {
return p.config
}
Expand Down
47 changes: 47 additions & 0 deletions services/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services

import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"math/big"
"time"

Expand Down Expand Up @@ -508,3 +509,49 @@ func (s *Service) formatAccount(id *big.Int) (*models.Account, error) {

return models.FormatAccount(id, owner, time.Uint64(), permissions), nil
}

func (s *Service) FormatAccountCore(id *big.Int) (*models.Account, error) {
return s.formatAccountCore(id)
}

func (s *Service) formatAccountCore(id *big.Int) (*models.Account, error) {
owner, err := s.core.GetAccountOwner(nil, id)
if err != nil {
logger.Log().WithField("layer", "Service-formatAccount").Errorf("get account owner error: %v", err.Error())
return nil, errors.GetReadContractErr(err, "core", "GetAccountOwner")
}

time, err := s.core.GetAccountLastInteraction(nil, id)
if err != nil {
logger.Log().WithField("layer", "Service-formatAccount").Errorf("get account last interaction error: %v", err.Error())
return nil, errors.GetReadContractErr(err, "core", "GetAccountLastInteraction")
}

permissions, err := s.core.GetAccountPermissions(nil, id)
if err != nil {
logger.Log().WithField("layer", "Service-formatAccount").Errorf("get account permissions error: %v", err.Error())
return nil, errors.GetReadContractErr(err, "core", "GetAccountPermissions")
}

return models.FormatAccountCore(id, owner, time.Uint64(), permissions), nil
}

func (s *Service) GetAccountCollateralCore(accountId *big.Int, collateralType common.Address) (*models.AccountCollateral, error) {
res, err := s.core.GetAccountCollateral(nil, accountId, collateralType)
if err != nil {
logger.Log().WithField("layer", "Service-GetAccountCollateralCore").Errorf("get account collateral error: %v", err.Error())
return nil, errors.GetReadContractErr(err, "core", "GetAccountCollateral")
}

return models.GetAccountCollateralFromContract(res), nil
}

func (s *Service) GetAccountAvailableCollateral(accountId *big.Int, collateralType common.Address) (*big.Int, error) {
res, err := s.core.GetAccountAvailableCollateral(nil, accountId, collateralType)
if err != nil {
logger.Log().WithField("layer", "Service-GetAccountAvailableCollateral").Errorf("get account available collateral error: %v", err.Error())
return nil, errors.GetReadContractErr(err, "core", "GetAccountAvailableCollateral")
}

return res, nil
}
10 changes: 10 additions & 0 deletions services/collateral.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,13 @@ func (s *Service) getCollateralDeposited(event *core.CoreDeposited, blockN uint6

return models.GetCollateralDepositedFromEvent(event, block.Time), nil
}

func (s *Service) GetCollateralConfigurations(hideDisabled bool) ([]*models.CollateralConfiguration, error) {
data, err := s.core.GetCollateralConfigurations(nil, hideDisabled)
if err != nil {
logger.Log().WithField("layer", "Service-GetCollateralConfigurations").Errorf("get collateral configurations error: %v", err.Error())
return nil, errors.GetReadContractErr(err, "core", "GetCollateralConfigurations")
}

return models.GetCollateralConfigurations(data), nil
}
Loading

0 comments on commit adfc65b

Please sign in to comment.