From 5f041522215338951ad96704f6c981b068e2ba6b Mon Sep 17 00:00:00 2001 From: 0x6e616d Date: Wed, 28 Aug 2024 14:13:15 +0700 Subject: [PATCH] feat: fetch erc20 token info when missing on configuration --- internal/app/thanos-notif/app.go | 6 ++++-- internal/app/thanos-notif/deposit.go | 26 ++++++++++++++++++++----- internal/app/thanos-notif/withdrawal.go | 26 ++++++++++++++++++++----- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/internal/app/thanos-notif/app.go b/internal/app/thanos-notif/app.go index 439afe7..f6ec984 100644 --- a/internal/app/thanos-notif/app.go +++ b/internal/app/thanos-notif/app.go @@ -3,14 +3,15 @@ package thanosnotif import ( "context" "fmt" + "sync" - "github.com/tokamak-network/tokamak-thanos-event-listener/internal/pkg/redis" + redislib "github.com/go-redis/redis/v8" "golang.org/x/sync/errgroup" - redislib "github.com/go-redis/redis/v8" "github.com/tokamak-network/tokamak-thanos-event-listener/internal/pkg/bcclient" "github.com/tokamak-network/tokamak-thanos-event-listener/internal/pkg/listener" "github.com/tokamak-network/tokamak-thanos-event-listener/internal/pkg/notification" + "github.com/tokamak-network/tokamak-thanos-event-listener/internal/pkg/redis" "github.com/tokamak-network/tokamak-thanos-event-listener/internal/pkg/repository" "github.com/tokamak-network/tokamak-thanos-event-listener/internal/pkg/types" "github.com/tokamak-network/tokamak-thanos-event-listener/pkg/log" @@ -33,6 +34,7 @@ type App struct { l2Listener *listener.EventService l1Client *bcclient.Client l2Client *bcclient.Client + mu sync.Mutex } func New(ctx context.Context, cfg *Config) (*App, error) { diff --git a/internal/app/thanos-notif/deposit.go b/internal/app/thanos-notif/deposit.go index 13c3ac4..307fd4b 100644 --- a/internal/app/thanos-notif/deposit.go +++ b/internal/app/thanos-notif/deposit.go @@ -4,8 +4,10 @@ import ( "fmt" ethereumTypes "github.com/ethereum/go-ethereum/core/types" - "github.com/tokamak-network/tokamak-thanos-event-listener/pkg/log" "github.com/tokamak-network/tokamak-thanos/op-bindings/bindings" + + "github.com/tokamak-network/tokamak-thanos-event-listener/internal/pkg/erc20" + "github.com/tokamak-network/tokamak-thanos-event-listener/pkg/log" ) func (p *App) depositETHInitiatedEvent(vLog *ethereumTypes.Log) (string, string, error) { @@ -63,8 +65,15 @@ func (p *App) depositERC20InitiatedEvent(vLog *ethereumTypes.Log) (string, strin l1Token := erc20Dep.L1Token l1TokenInfo, found := p.l1TokensInfo[l1Token.Hex()] if !found { - log.GetLogger().Errorw("Token info not found for address", "l1Token", l1Token.Hex()) - return "", "", err + newToken, err := erc20.FetchTokenInfo(p.l1Client, l1Token.Hex()) + if err != nil || newToken == nil { + log.GetLogger().Errorw("Token info not found for address", "l1Token", l1Token.Hex()) + return "", "", err + } + l1TokenInfo = newToken + p.mu.Lock() + p.l1TokensInfo[l1Token.Hex()] = l1TokenInfo + p.mu.Unlock() } tokenSymbol := l1TokenInfo.Symbol @@ -114,8 +123,15 @@ func (p *App) depositFinalizedEvent(vLog *ethereumTypes.Log) (string, string, er l2TokenInfo, found := p.l2TokensInfo[l2Token.Hex()] if !found { - log.GetLogger().Errorw("Token info not found for address", "l2Token", l2Token.Hex()) - return "", "", err + newToken, err := erc20.FetchTokenInfo(p.l2Client, l2Token.Hex()) + if err != nil || newToken == nil { + log.GetLogger().Errorw("Token info not found for address", "l2Token", l2Token.Hex()) + return "", "", err + } + l2TokenInfo = newToken + p.mu.Lock() + p.l2TokensInfo[l2Token.Hex()] = l2TokenInfo + p.mu.Unlock() } amount := formatAmount(l2Dep.Amount, l2TokenInfo.Decimals) diff --git a/internal/app/thanos-notif/withdrawal.go b/internal/app/thanos-notif/withdrawal.go index eda6a8d..3ea8170 100644 --- a/internal/app/thanos-notif/withdrawal.go +++ b/internal/app/thanos-notif/withdrawal.go @@ -4,8 +4,10 @@ import ( "fmt" ethereumTypes "github.com/ethereum/go-ethereum/core/types" - "github.com/tokamak-network/tokamak-thanos-event-listener/pkg/log" "github.com/tokamak-network/tokamak-thanos/op-bindings/bindings" + + "github.com/tokamak-network/tokamak-thanos-event-listener/internal/pkg/erc20" + "github.com/tokamak-network/tokamak-thanos-event-listener/pkg/log" ) func (p *App) withdrawalETHFinalizedEvent(vLog *ethereumTypes.Log) (string, string, error) { @@ -63,8 +65,15 @@ func (p *App) withdrawalERC20FinalizedEvent(vLog *ethereumTypes.Log) (string, st l1Token := erc20With.L1Token l1TokenInfo, found := p.l1TokensInfo[l1Token.Hex()] if !found { - log.GetLogger().Errorw("Token info not found for address", "l1Token", l1Token.Hex()) - return "", "", err + newToken, err := erc20.FetchTokenInfo(p.l1Client, l1Token.Hex()) + if err != nil || newToken == nil { + log.GetLogger().Errorw("Token info not found for address", "l1Token", l1Token.Hex()) + return "", "", err + } + l1TokenInfo = newToken + p.mu.Lock() + p.l1TokensInfo[l1Token.Hex()] = l1TokenInfo + p.mu.Unlock() } tokenSymbol := l1TokenInfo.Symbol @@ -117,8 +126,15 @@ func (p *App) withdrawalInitiatedEvent(vLog *ethereumTypes.Log) (string, string, l2TokenInfo, found := p.l2TokensInfo[l2Token.Hex()] if !found { - log.GetLogger().Errorw("Token info not found for address", "l2Token", l2Token.Hex()) - return "", "", err + newToken, err := erc20.FetchTokenInfo(p.l2Client, l2Token.Hex()) + if err != nil || newToken == nil { + log.GetLogger().Errorw("Token info not found for address", "l2Token", l2Token.Hex()) + return "", "", err + } + l2TokenInfo = newToken + p.mu.Lock() + p.l2TokensInfo[l2Token.Hex()] = l2TokenInfo + p.mu.Unlock() } amount := formatAmount(l2With.Amount, tokenDecimals)