Skip to content

Commit

Permalink
feat(worker): merge vsl staking and bridge to vsl core (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoyu authored Aug 20, 2024
1 parent ab1a86a commit 1d4c742
Show file tree
Hide file tree
Showing 11 changed files with 759 additions and 946 deletions.
146 changes: 4 additions & 142 deletions internal/engine/worker/decentralized/contract/rss3/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ import (
var _ engine.Worker = (*worker)(nil)

type worker struct {
config *config.Module
ethereumClient ethereum.Client
tokenClient token.Client
stakingFilterer *rss3.StakingFilterer
stakingVSLFilterer *rss3.StakingVSLFilterer
chipsFilterer *rss3.ChipsFilterer
config *config.Module
ethereumClient ethereum.Client
tokenClient token.Client
stakingFilterer *rss3.StakingFilterer
}

func (w *worker) Name() string {
Expand Down Expand Up @@ -70,21 +68,12 @@ func (w *worker) Filter() engine.DataSourceFilter {
LogAddresses: []common.Address{
// Ethereum Mainnet
rss3.AddressStaking,

// RSS3 VSL Mainnet
rss3.AddressStakingVSL,
rss3.AddressChipsVSL,
rss3.AddressSettlementVSL,
},
LogTopics: []common.Hash{
// Ethereum Mainnet
rss3.EventHashStakingDeposited,
rss3.EventHashStakingWithdrawn,
rss3.EventHashRewardsClaimed,

// RSS3 VSL Mainnet
rss3.EventHashStakingVSLDeposited,
rss3.EventHashStakingVSLStaked,
},
}
}
Expand Down Expand Up @@ -121,14 +110,6 @@ func (w *worker) Transform(ctx context.Context, task engine.Task) (*activityx.Ac
actions, err = w.transformStakingWithdrawn(ctx, ethereumTask, log)
case w.matchStakingRewardsClaimed(ethereumTask, log):
actions, err = w.transformStakingRewardsClaimed(ctx, ethereumTask, log)

// RSS3 VSL Mainnet
case w.matchStakingVSLDeposited(ethereumTask, log):
actions, err = w.transformStakingVSLDeposited(ctx, ethereumTask, log)
case w.matchStakingVSLStaked(ethereumTask, log):
actions, err = w.transformStakingVSLStaked(ctx, ethereumTask, log)
case w.matchTransfer(ethereumTask, log):
actions, err = w.transformChipsMint(ctx, ethereumTask, log)
default:
continue
}
Expand Down Expand Up @@ -163,21 +144,6 @@ func (w *worker) matchStakingRewardsClaimed(_ *source.Task, log *ethereum.Log) b
return log.Address == rss3.AddressStaking && len(log.Topics) == 4 && contract.MatchEventHashes(log.Topics[0], rss3.EventHashRewardsClaimed)
}

// matchStakingVSLDeposited matches the staking VSL deposited event.
func (w *worker) matchStakingVSLDeposited(_ *source.Task, log *ethereum.Log) bool {
return log.Address == rss3.AddressStakingVSL && contract.MatchEventHashes(log.Topics[0], rss3.EventHashStakingVSLDeposited)
}

// matchStakingVSLStaked matches the staking VSL staked event.
func (w *worker) matchStakingVSLStaked(_ *source.Task, log *ethereum.Log) bool {
return log.Address == rss3.AddressStakingVSL && contract.MatchEventHashes(log.Topics[0], rss3.EventHashStakingVSLStaked)
}

// matchTransfer matches the transfer event.
func (w *worker) matchTransfer(_ *source.Task, log *ethereum.Log) bool {
return log.Address == rss3.AddressChipsVSL && contract.MatchEventHashes(log.Topics[0], rss3.EventHashTransfer)
}

// transformStakingDeposited transforms the staking deposited event.
func (w *worker) transformStakingDeposited(ctx context.Context, task *source.Task, log *ethereum.Log) ([]*activityx.Action, error) {
event, err := w.stakingFilterer.ParseDeposited(log.Export())
Expand Down Expand Up @@ -240,62 +206,6 @@ func (w *worker) transformStakingRewardsClaimed(ctx context.Context, task *sourc
return actions, nil
}

// transformStakingVSLDeposited transforms the staking VSL deposited event.
func (w *worker) transformStakingVSLDeposited(ctx context.Context, task *source.Task, log *ethereum.Log) ([]*activityx.Action, error) {
event, err := w.stakingVSLFilterer.ParseDeposited(log.Export())
if err != nil {
return nil, fmt.Errorf("parse Deposited event: %w", err)
}

action, err := w.buildExchangeStakingVSLAction(ctx, task, task.Transaction.From, event.NodeAddr, event.Amount, metadata.ActionExchangeStakingStake)
if err != nil {
return nil, fmt.Errorf("build exchange staking action: %w", err)
}

actions := []*activityx.Action{
action,
}

return actions, nil
}

// transformStakingVSLStaked transforms the staking VSL staked event.
func (w *worker) transformStakingVSLStaked(ctx context.Context, task *source.Task, log *ethereum.Log) ([]*activityx.Action, error) {
event, err := w.stakingVSLFilterer.ParseStaked(log.Export())
if err != nil {
return nil, fmt.Errorf("parse Deposited event: %w", err)
}

stakingAction, err := w.buildExchangeStakingVSLAction(ctx, task, task.Transaction.From, event.NodeAddr, event.Amount, metadata.ActionExchangeStakingStake)
if err != nil {
return nil, fmt.Errorf("build exchange staking action: %w", err)
}

actions := []*activityx.Action{
stakingAction,
}

return actions, nil
}

// transformChipsMint transforms the transfer event.
func (w *worker) transformChipsMint(ctx context.Context, task *source.Task, log *ethereum.Log) ([]*activityx.Action, error) {
// Parse Transfer event.
event, err := w.chipsFilterer.ParseTransfer(log.Export())
if err != nil {
return nil, fmt.Errorf("parse Transfer event: %w", err)
}

action, err := w.buildChipsMintAction(ctx, task, event.From, event.To, log.Address, event.TokenId, big.NewInt(1))
if err != nil {
return nil, fmt.Errorf("build ChipsMint action: %w", err)
}

return []*activityx.Action{
action,
}, nil
}

// buildExchangeStakingAction builds the exchange staking action.
func (w *worker) buildExchangeStakingAction(ctx context.Context, task *source.Task, from, to common.Address, tokenValue *big.Int, stakingAction metadata.ExchangeStakingAction, period *metadata.ExchangeStakingPeriod) (*activityx.Action, error) {
// The Token always is $RSS3.
Expand All @@ -321,48 +231,6 @@ func (w *worker) buildExchangeStakingAction(ctx context.Context, task *source.Ta
return &action, nil
}

// buildExchangeStakingVSLAction builds the exchange staking VSL action.
func (w *worker) buildExchangeStakingVSLAction(ctx context.Context, task *source.Task, from, to common.Address, tokenValue *big.Int, stakingAction metadata.ExchangeStakingAction) (*activityx.Action, error) {
// The Token always is $RSS3.
tokenMetadata, err := w.tokenClient.Lookup(ctx, task.ChainID, nil, nil, task.Header.Number)
if err != nil {
return nil, fmt.Errorf("lookup token: %w", err)
}

tokenMetadata.Value = lo.ToPtr(decimal.NewFromBigInt(tokenValue, 0))

action := activityx.Action{
Type: typex.ExchangeStaking,
Platform: w.Platform(),
From: from.String(),
To: to.String(),
Metadata: metadata.ExchangeStaking{
Action: stakingAction,
Token: *tokenMetadata,
},
}

return &action, nil
}

// buildChipsMintAction builds the ChipsMint action.
func (w *worker) buildChipsMintAction(ctx context.Context, task *source.Task, from, to common.Address, contract common.Address, id *big.Int, value *big.Int) (*activityx.Action, error) {
tokenMetadata, err := w.tokenClient.Lookup(ctx, task.ChainID, &contract, id, task.Header.Number)
if err != nil {
return nil, fmt.Errorf("lookup token metadata: %w", err)
}

tokenMetadata.Value = lo.ToPtr(decimal.NewFromBigInt(value, 0))

return &activityx.Action{
Type: typex.CollectibleMint,
Platform: w.Platform(),
From: from.String(),
To: to.String(),
Metadata: metadata.CollectibleTransfer(*tokenMetadata),
}, nil
}

// NewWorker creates a new RSS3 worker.
func NewWorker(config *config.Module) (engine.Worker, error) {
var (
Expand All @@ -381,11 +249,5 @@ func NewWorker(config *config.Module) (engine.Worker, error) {
// Initialize RSS3 staking filterer.
instance.stakingFilterer = lo.Must(rss3.NewStakingFilterer(ethereum.AddressGenesis, nil))

// Initialize RSS3 VSL staking filterer.
instance.stakingVSLFilterer = lo.Must(rss3.NewStakingVSLFilterer(ethereum.AddressGenesis, nil))

// Initialize RSS3 VSL chips filterer.
instance.chipsFilterer = lo.Must(rss3.NewChipsFilterer(ethereum.AddressGenesis, nil))

return &instance, nil
}
Loading

0 comments on commit 1d4c742

Please sign in to comment.