-
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.
- Loading branch information
Showing
9 changed files
with
627 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,82 @@ | ||
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" | ||
) | ||
|
||
// MarketUSDDepositedSubscription is a struct for listening to all 'MarketUSDDeposited' contract events and return them as models.MarketUSDDeposited struct | ||
type MarketUSDDepositedSubscription struct { | ||
*basicSubscription | ||
MarketUSDDepositedChan chan *models.MarketUSDDeposited | ||
contractEventChan chan *core.CoreMarketUsdDeposited | ||
} | ||
|
||
func (e *Events) ListenMarketUSDDeposited() (*MarketUSDDepositedSubscription, error) { | ||
contractEventChan := make(chan *core.CoreMarketUsdDeposited) | ||
|
||
contractSub, err := e.core.WatchMarketUsdDeposited(nil, contractEventChan, nil, nil, nil) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-ListenMarketUSDDeposited").Errorf("error watch deposit: %v", err.Error()) | ||
return nil, errors.GetEventListenErr(err, "MarketUSDDeposited") | ||
} | ||
|
||
depositSub := newMarketUSDDepositedSubscription(contractSub, contractEventChan) | ||
|
||
go depositSub.listen(e.rpcClient) | ||
|
||
return depositSub, nil | ||
} | ||
|
||
// newMarketUSDDepositedSubscription is used to create new MarketUSDDepositedSubscription instance | ||
func newMarketUSDDepositedSubscription(eventSub event.Subscription, contractEventChan chan *core.CoreMarketUsdDeposited) *MarketUSDDepositedSubscription { | ||
return &MarketUSDDepositedSubscription{ | ||
basicSubscription: newBasicSubscription(eventSub), | ||
contractEventChan: contractEventChan, | ||
MarketUSDDepositedChan: make(chan *models.MarketUSDDeposited), | ||
} | ||
} | ||
|
||
// listen is used to run a goroutine | ||
func (s *MarketUSDDepositedSubscription) listen(rpcClient *ethclient.Client) { | ||
defer func() { | ||
close(s.MarketUSDDepositedChan) | ||
close(s.contractEventChan) | ||
}() | ||
|
||
for { | ||
select { | ||
case <-s.stop: | ||
return | ||
case err := <-s.eventSub.Err(): | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-MarketUSDDeposited").Errorf("error listening deposit: %v", err.Error()) | ||
s.ErrChan <- err | ||
} | ||
return | ||
case eventMarketUSDDeposited := <-s.contractEventChan: | ||
block, err := rpcClient.HeaderByNumber(context.Background(), big.NewInt(int64(eventMarketUSDDeposited.Raw.BlockNumber))) | ||
time := uint64(0) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-MarketUSDDeposited").Warningf( | ||
"error fetching block number %v: %v; order event time set to 0 ", | ||
eventMarketUSDDeposited.Raw.BlockNumber, err.Error(), | ||
) | ||
s.ErrChan <- err | ||
} else { | ||
time = block.Time | ||
} | ||
|
||
deposited := models.GetMarketUSDDepositedFromEvent(eventMarketUSDDeposited, time) | ||
|
||
s.MarketUSDDepositedChan <- deposited | ||
} | ||
} | ||
} |
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,82 @@ | ||
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" | ||
) | ||
|
||
// MarketUSDWithdrawnSubscription is a struct for listening to all 'MarketUSDWithdrawn' contract events and return them as models.MarketUSDWithdrawn struct | ||
type MarketUSDWithdrawnSubscription struct { | ||
*basicSubscription | ||
MarketUSDWithdrawnChan chan *models.MarketUSDWithdrawn | ||
contractEventChan chan *core.CoreMarketUsdWithdrawn | ||
} | ||
|
||
func (e *Events) ListenMarketUSDWithdrawn() (*MarketUSDWithdrawnSubscription, error) { | ||
contractEventChan := make(chan *core.CoreMarketUsdWithdrawn) | ||
|
||
contractSub, err := e.core.WatchMarketUsdWithdrawn(nil, contractEventChan, nil, nil, nil) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-ListenMarketUSDWithdrawn").Errorf("error watch withdraw: %v", err.Error()) | ||
return nil, errors.GetEventListenErr(err, "MarketUSDWithdrawn") | ||
} | ||
|
||
withdrawSub := newMarketUSDWithdrawnSubscription(contractSub, contractEventChan) | ||
|
||
go withdrawSub.listen(e.rpcClient) | ||
|
||
return withdrawSub, nil | ||
} | ||
|
||
// newMarketUSDWithdrawnSubscription is used to create new MarketUSDWithdrawnSubscription instance | ||
func newMarketUSDWithdrawnSubscription(eventSub event.Subscription, contractEventChan chan *core.CoreMarketUsdWithdrawn) *MarketUSDWithdrawnSubscription { | ||
return &MarketUSDWithdrawnSubscription{ | ||
basicSubscription: newBasicSubscription(eventSub), | ||
contractEventChan: contractEventChan, | ||
MarketUSDWithdrawnChan: make(chan *models.MarketUSDWithdrawn), | ||
} | ||
} | ||
|
||
// listen is used to run a goroutine | ||
func (s *MarketUSDWithdrawnSubscription) listen(rpcClient *ethclient.Client) { | ||
defer func() { | ||
close(s.MarketUSDWithdrawnChan) | ||
close(s.contractEventChan) | ||
}() | ||
|
||
for { | ||
select { | ||
case <-s.stop: | ||
return | ||
case err := <-s.eventSub.Err(): | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-MarketUSDWithdrawn").Errorf("error listening withdraw: %v", err.Error()) | ||
s.ErrChan <- err | ||
} | ||
return | ||
case eventMarketUSDWithdrawn := <-s.contractEventChan: | ||
block, err := rpcClient.HeaderByNumber(context.Background(), big.NewInt(int64(eventMarketUSDWithdrawn.Raw.BlockNumber))) | ||
time := uint64(0) | ||
if err != nil { | ||
logger.Log().WithField("layer", "Events-MarketUSDWithdrawn").Warningf( | ||
"error fetching block number %v: %v; order event time set to 0 ", | ||
eventMarketUSDWithdrawn.Raw.BlockNumber, err.Error(), | ||
) | ||
s.ErrChan <- err | ||
} else { | ||
time = block.Time | ||
} | ||
|
||
withdrawn := models.GetMarketUSDWithdrawnFromEvent(eventMarketUSDWithdrawn, time) | ||
|
||
s.MarketUSDWithdrawnChan <- withdrawn | ||
} | ||
} | ||
} |
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,72 @@ | ||
package models | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/gateway-fm/perpsv3-Go/contracts/core" | ||
) | ||
|
||
type MarketUSDDeposited struct { | ||
MarketId *big.Int | ||
Target common.Address | ||
Amount *big.Int | ||
Market common.Address | ||
CreditCapacity *big.Int | ||
NetIssuance *big.Int | ||
DepositedCollateralValue *big.Int | ||
ReportedDebt *big.Int | ||
BlockNumber uint64 | ||
BlockTimestamp uint64 | ||
TransactionHash string | ||
} | ||
|
||
type MarketUSDWithdrawn struct { | ||
MarketId *big.Int | ||
Target common.Address | ||
Amount *big.Int | ||
Market common.Address | ||
CreditCapacity *big.Int | ||
NetIssuance *big.Int | ||
DepositedCollateralValue *big.Int | ||
ReportedDebt *big.Int | ||
BlockNumber uint64 | ||
BlockTimestamp uint64 | ||
TransactionHash string | ||
} | ||
|
||
func GetMarketUSDDepositedFromEvent(event *core.CoreMarketUsdDeposited, time uint64) *MarketUSDDeposited { | ||
m := &MarketUSDDeposited{} | ||
|
||
m.MarketId = event.MarketId | ||
m.Target = event.Target | ||
m.Amount = event.Amount | ||
m.Market = event.Market | ||
m.CreditCapacity = event.CreditCapacity | ||
m.NetIssuance = event.NetIssuance | ||
m.DepositedCollateralValue = event.DepositedCollateralValue | ||
m.ReportedDebt = event.ReportedDebt | ||
m.BlockNumber = event.Raw.BlockNumber | ||
m.BlockTimestamp = time | ||
m.TransactionHash = event.Raw.TxHash.Hex() | ||
|
||
return m | ||
} | ||
|
||
func GetMarketUSDWithdrawnFromEvent(event *core.CoreMarketUsdWithdrawn, time uint64) *MarketUSDWithdrawn { | ||
m := &MarketUSDWithdrawn{} | ||
|
||
m.MarketId = event.MarketId | ||
m.Target = event.Target | ||
m.Amount = event.Amount | ||
m.Market = event.Market | ||
m.CreditCapacity = event.CreditCapacity | ||
m.NetIssuance = event.NetIssuance | ||
m.DepositedCollateralValue = event.DepositedCollateralValue | ||
m.ReportedDebt = event.ReportedDebt | ||
m.BlockNumber = event.Raw.BlockNumber | ||
m.BlockTimestamp = time | ||
m.TransactionHash = event.Raw.TxHash.Hex() | ||
|
||
return m | ||
} |
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
Oops, something went wrong.