-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from gateway-fm/SNX-31
SNX-31: add Core contract usd minted event
- Loading branch information
Showing
6 changed files
with
257 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package events | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/ethclient" | ||
"github.com/ethereum/go-ethereum/event" | ||
|
||
"github.com/gateway-fm/perpsv3-Go/contracts/core" | ||
"github.com/gateway-fm/perpsv3-Go/errors" | ||
"github.com/gateway-fm/perpsv3-Go/models" | ||
"github.com/gateway-fm/perpsv3-Go/pkg/logger" | ||
) | ||
|
||
// USDMintedSubscription is a struct for listening to all 'PositionLiquidated' contract events and return them as models.USDMinted struct | ||
type USDMintedSubscription struct { | ||
*basicSubscription | ||
USDMintedsChan chan *models.USDMinted | ||
contractEventChan chan *core.CoreUsdMinted | ||
} | ||
|
||
func (e *Events) ListenUSDMinted() (*USDMintedSubscription, error) { | ||
contractEventChan := make(chan *core.CoreUsdMinted) | ||
|
||
contractSub, err := e.core.WatchUsdMinted(nil, contractEventChan, nil, nil, nil) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-ListenUSDMinteds").Errorf("error watch usd minted: %v", err.Error()) | ||
return nil, errors.GetEventListenErr(err, "USDMinted") | ||
} | ||
|
||
orderSub := newUSDMintedSubscription(contractSub, contractEventChan) | ||
|
||
go orderSub.listen(e.rpcClient) | ||
|
||
return orderSub, nil | ||
} | ||
|
||
// newUSDMintedSubscription is used to create new USDMintedSubscription instance | ||
func newUSDMintedSubscription(eventSub event.Subscription, contractEventChan chan *core.CoreUsdMinted) *USDMintedSubscription { | ||
return &USDMintedSubscription{ | ||
basicSubscription: newBasicSubscription(eventSub), | ||
contractEventChan: contractEventChan, | ||
USDMintedsChan: make(chan *models.USDMinted), | ||
} | ||
} | ||
|
||
// listen is used to run a goroutine | ||
func (s *USDMintedSubscription) listen(rpcClient *ethclient.Client) { | ||
defer func() { | ||
close(s.USDMintedsChan) | ||
close(s.contractEventChan) | ||
}() | ||
|
||
for { | ||
select { | ||
case <-s.stop: | ||
return | ||
case err := <-s.eventSub.Err(): | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-PositionLiquidated").Errorf("error listening position liquidated: %v", err.Error()) | ||
s.ErrChan <- err | ||
} | ||
return | ||
case positionLiquidated := <-s.contractEventChan: | ||
block, err := rpcClient.HeaderByNumber(context.Background(), big.NewInt(int64(positionLiquidated.Raw.BlockNumber))) | ||
time := uint64(0) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-PositionLiquidated").Warningf( | ||
"error fetching block number %v: %v; order event time set to 0 ", | ||
positionLiquidated.Raw.BlockNumber, err.Error(), | ||
) | ||
s.ErrChan <- err | ||
} else { | ||
time = block.Time | ||
} | ||
|
||
order := models.GetUSDMintedFromEvent(positionLiquidated, time) | ||
|
||
s.USDMintedsChan <- order | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package models | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
|
||
"github.com/gateway-fm/perpsv3-Go/contracts/core" | ||
"github.com/gateway-fm/perpsv3-Go/pkg/logger" | ||
) | ||
|
||
// USDMinted is a `usdMinted` Core smart-contract event struct | ||
type USDMinted struct { | ||
AccountId *big.Int | ||
PoolId *big.Int | ||
CollateralType common.Address | ||
Amount *big.Int | ||
Sender common.Address | ||
BlockNumber uint64 | ||
BlockTimestamp uint64 | ||
} | ||
|
||
// GetUSDMintedFromEvent is used to get USDMinted struct from given contract event | ||
func GetUSDMintedFromEvent(event *core.CoreUsdMinted, time uint64) *USDMinted { | ||
if event == nil { | ||
logger.Log().WithField("layer", "Models-USDMinted").Warning("nil event received") | ||
return &USDMinted{} | ||
} | ||
|
||
return &USDMinted{ | ||
AccountId: event.AccountId, | ||
PoolId: event.PoolId, | ||
CollateralType: event.CollateralType, | ||
Amount: event.Amount, | ||
Sender: event.Sender, | ||
BlockNumber: event.Raw.BlockNumber, | ||
BlockTimestamp: time, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
|
||
"github.com/gateway-fm/perpsv3-Go/contracts/core" | ||
"github.com/gateway-fm/perpsv3-Go/errors" | ||
"github.com/gateway-fm/perpsv3-Go/models" | ||
"github.com/gateway-fm/perpsv3-Go/pkg/logger" | ||
) | ||
|
||
func (s *Service) RetrieveUSDMintedLimit(limit uint64) ([]*models.USDMinted, error) { | ||
iterations, last, err := s.getIterationsForLimitQuery(limit) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var mints []*models.USDMinted | ||
|
||
logger.Log().WithField("layer", "Service-RetrieveUSDMintedLimit").Infof( | ||
"fetching USDMinted with limit: %v to block: %v total iterations: %v...", | ||
limit, last, iterations, | ||
) | ||
|
||
fromBlock := s.coreFirstBlock | ||
toBlock := fromBlock + limit | ||
for i := uint64(1); i <= iterations; i++ { | ||
if i%10 == 0 || i == iterations { | ||
logger.Log().WithField("layer", "Service-RetrieveUSDMintedLimit").Infof("-- iteration %v", i) | ||
} | ||
opts := s.getFilterOptsCore(fromBlock, &toBlock) | ||
|
||
res, err := s.retrieveUSDMinted(opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mints = append(mints, res...) | ||
|
||
fromBlock = toBlock + 1 | ||
|
||
if i == iterations-1 { | ||
toBlock = last | ||
} else { | ||
toBlock = fromBlock + limit | ||
} | ||
} | ||
|
||
logger.Log().WithField("layer", "Service-RetrieveLiquidationsLimit").Infof("task completed successfully") | ||
|
||
return mints, nil | ||
} | ||
|
||
func (s *Service) retrieveUSDMinted(opts *bind.FilterOpts) ([]*models.USDMinted, error) { | ||
iterator, err := s.core.FilterUsdMinted(opts, nil, nil, nil) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Service-RetrieveUSDMintedLimit").Errorf("error get iterator: %v", err.Error()) | ||
return nil, errors.GetFilterErr(err, "core") | ||
} | ||
|
||
var mints []*models.USDMinted | ||
|
||
for iterator.Next() { | ||
if iterator.Error() != nil { | ||
logger.Log().WithField("layer", "Service-RetrieveUSDMintedLimit").Errorf("iterator error: %v", iterator.Error().Error()) | ||
return nil, errors.GetFilterErr(iterator.Error(), "core") | ||
} | ||
|
||
mint, err := s.getUSDMinted(iterator.Event, iterator.Event.Raw.BlockNumber) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mints = append(mints, mint) | ||
} | ||
|
||
return mints, nil | ||
} | ||
|
||
// getLiquidation is used to get models.Liquidation from given event and block number | ||
func (s *Service) getUSDMinted(event *core.CoreUsdMinted, blockN uint64) (*models.USDMinted, error) { | ||
block, err := s.rpcClient.HeaderByNumber(context.Background(), big.NewInt(int64(blockN))) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Service-RetrieveUSDMintedLimit").Errorf( | ||
"get block:%v by number error: %v", blockN, err.Error(), | ||
) | ||
return nil, errors.GetRPCProviderErr(err, "HeaderByNumber") | ||
} | ||
|
||
return models.GetUSDMintedFromEvent(event, block.Time), nil | ||
} |
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