Skip to content

Commit

Permalink
Persist 0xv3 Contract Address (#104)
Browse files Browse the repository at this point in the history
* persist 0xv3 contract address

* update unit test

* refactor deploy parser
  • Loading branch information
linhnt3400 authored Dec 4, 2024
1 parent 33be191 commit f623758
Show file tree
Hide file tree
Showing 11 changed files with 626 additions and 65 deletions.
21 changes: 21 additions & 0 deletions v2/cmd/migrations/00008_add_zerox_v3_deployment.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
create table zerox_v3_deployment
(
block_number bigint not null,
contract_type int not null,
contract_address text not null,
primary key (contract_type, contract_address)
);

insert into zerox_v3_deployment (block_number, contract_type, contract_address) values
(19921333, 2, '0xecf4248a682ffc676f4c596214cd6a4b463d8d2e'),
(19921333, 3, '0x6d837c5f39d609b996ad4ad45f744ad2df93fb57'),
(20027105, 2, '0xbcd5e096c749bf7dd8cdb5ae90a2186866b67d77'),
(20027105, 3, '0x4ee2d0b91084821b96365f93da61cf86e10c42bb'),
(20064192, 2, '0x7f6cee965959295cc64d0e6c00d99d6532d8e86b'),
(20064192, 3, '0x7c39a136ea20b3483e402ea031c1f3c019bab24b'),
(20412702, 2, '0x07e594aa718bb872b526e93eed830a8d2a6a1071'),
(20412702, 3, '0x25b81ce58ab0c4877d25a96ad644491ceab81048'),
(20514759, 2, '0x2c4b05349418ef279184f07590e61af27cf3a86b'),
(20514759, 3, '0xae11b95c8ebb5247548c279a00120b0acadc7451'),
(20616430, 2, '0x70bf6634ee8cb27d04478f184b9b8bb13e5f4710'),
(20616430, 3, '0x12d737470fb3ec6c3deec9b518100bec9d520144');
6 changes: 5 additions & 1 deletion v2/cmd/parse_log/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
uniswapxStorage "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/uniswapx"
zxotcStorage "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/zxotc"
zxrfqv3Storage "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/zxrfqv3"
"github.com/KyberNetwork/tradelogs/v2/pkg/storage/zerox_deployment"
"github.com/KyberNetwork/tradinglib/pkg/dbutil"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
Expand Down Expand Up @@ -102,6 +103,9 @@ func run(c *cli.Context) error {
// state storage
s := state.New(l, db)

// zerox deployment storage
zxv3DeployStorage := zerox_deployment.NewStorage(l, db)

// rpc node to query trace call
rpcURL := c.StringSlice(libapp.RPCUrlFlagName)
if len(rpcURL) == 0 {
Expand All @@ -127,7 +131,7 @@ func run(c *cli.Context) error {
oneinchv6.MustNewParser(),
uniswapx.MustNewParser(),
bebop.MustNewParser(),
zxrfqv3.MustNewParserWithDeployer(ethClients[0], common.HexToAddress(constant.Deployer0xV3)),
zxrfqv3.MustNewParserWithDeployer(l, zxv3DeployStorage, ethClients[0], common.HexToAddress(constant.Deployer0xV3)),
pancakeswap.MustNewParser(),
}

Expand Down
75 changes: 75 additions & 0 deletions v2/mocks/DeployStorage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions v2/pkg/handler/trade_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ type TradeLogHandler struct {
l *zap.SugaredLogger
rpcClient rpcnode.IClient
storage *tradelogs.Manager
promoteestorage *promoteeTypes.Storage
promoteeStorage *promoteeTypes.Storage
parsers []parser.Parser
promotionparsers []promotionparser.Parser
promotionParsers []promotionparser.Parser
kafkaTopic string
publisher kafka.Publisher
}
Expand All @@ -47,9 +47,9 @@ func NewTradeLogHandler(l *zap.SugaredLogger, rpc rpcnode.IClient,
l: l,
rpcClient: rpc,
storage: storage,
promoteestorage: promoteeStorage,
promoteeStorage: promoteeStorage,
parsers: parsers,
promotionparsers: promotionParsers,
promotionParsers: promotionParsers,
kafkaTopic: kafkaTopic,
publisher: publisher,
}
Expand Down Expand Up @@ -155,7 +155,7 @@ func (h *TradeLogHandler) processForPromotion(calls []types.TransactionCallFrame
continue
}

err := h.promoteestorage.Insert(promotees)
err := h.promoteeStorage.Insert(promotees)
if err != nil {
return fmt.Errorf("write to storage error: %w", err)
}
Expand Down Expand Up @@ -258,7 +258,7 @@ func (h *TradeLogHandler) findMatchingParser(log ethereumTypes.Log) parser.Parse
}

func (h *TradeLogHandler) findMatchingPromotionParser(log ethereumTypes.Log) promotionparser.Parser {
for _, p := range h.promotionparsers {
for _, p := range h.promotionParsers {
if p.LogFromContract(log) {
return p
}
Expand All @@ -276,7 +276,7 @@ func (h *TradeLogHandler) RevertBlock(blocks []uint64) error {
return fmt.Errorf("delete blocks error: %w", err)
}

err = h.promoteestorage.Delete((blocks))
err = h.promoteeStorage.Delete(blocks)
if err != nil {
return fmt.Errorf("delete blocks error: %w", err)
}
Expand Down
159 changes: 159 additions & 0 deletions v2/pkg/parser/zxrfqv3/deploy_parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package zxrfqv3

import (
"context"
"fmt"
"math/big"
"strings"
"time"

"github.com/KyberNetwork/tradelogs/v2/pkg/constant"
"github.com/KyberNetwork/tradelogs/v2/pkg/parser/zxrfqv3/deployer"
"github.com/KyberNetwork/tradelogs/v2/pkg/storage/zerox_deployment"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
ethereumTypes "github.com/ethereum/go-ethereum/core/types"
"go.uber.org/zap"
)

type DeployParser struct {
l *zap.SugaredLogger
contractABIs *ContractABIs
deployer *deployer.Deployer
deployEventHash string
storage zerox_deployment.IStorage
}

const (
deployedEventName = "Deployed"
)

func NewDeployParser(l *zap.SugaredLogger, contractABIs *ContractABIs, d *deployer.Deployer, storage zerox_deployment.IStorage) (*DeployParser, error) {
deployerABI, err := deployer.DeployerMetaData.GetAbi()
if err != nil {
return nil, fmt.Errorf("failed to get deployer abi: %w", err)
}
p := &DeployParser{
l: l,
contractABIs: contractABIs,
deployer: d,
deployEventHash: deployerABI.Events[deployedEventName].ID.String(),
storage: storage,
}
return p, p.loadDeployments()
}

func (p *DeployParser) loadDeployments() error {
deployments, err := p.storage.Get()
if err != nil {
p.l.Errorw("failed to get deployments", "err", err)
return fmt.Errorf("failed to get deployments: %w", err)
}
p.l.Infow("get deployments", "deployments", deployments)
for _, d := range deployments {
address := common.HexToAddress(d.Address)
if p.contractABIs.containAddress(address) || isZeroAddress(address) {
continue
}

abi, err := NewABI(ContractABI{Address: address, ContractType: ContractType(d.ContractType)})
if err != nil {
p.l.Errorw("failed to create abi for contract", "err", err)
return fmt.Errorf("create abi error: %w", err)
}

p.contractABIs.addContractABI(address, abi)
}

go p.syncContractAddress()
return nil
}

func (p *DeployParser) syncContractAddress() {
contractTypeSupported := []ContractType{SwapContract, GaslessContract}
ticker := time.NewTicker(time.Hour)
defer ticker.Stop()
for ; ; <-ticker.C {
callOpts := &bind.CallOpts{Context: context.Background()}
for _, contractType := range contractTypeSupported {
contractAddress, err := p.deployer.OwnerOf(callOpts, big.NewInt(int64(contractType)))
if err != nil {
p.l.Errorw("error to get contract address", "err", err, "contractType", contractType)
continue
}

if p.contractABIs.containAddress(contractAddress) || isZeroAddress(contractAddress) {
continue
}

abi, err := NewABI(ContractABI{Address: contractAddress, ContractType: contractType})
if err != nil {
p.l.Errorw("error to create abi", "err", err, "contractAddress", contractAddress, "contractType", contractType)
continue
}

p.l.Infow("add contract abi", "contractAddress", contractAddress)
p.contractABIs.addContractABI(contractAddress, abi)

err = p.storage.Insert(zerox_deployment.Deployment{
Address: contractAddress.String(),
ContractType: int(contractType),
})
if err != nil {
p.l.Errorw("failed to insert deployment", "err", err)
}
}
}
}

func (p *DeployParser) isDeployLog(log ethereumTypes.Log) bool {
return log.Address.Hex() == constant.Deployer0xV3 && strings.EqualFold(log.Topics[0].String(), p.deployEventHash)
}

func (p *DeployParser) handlerDeployLog(log ethereumTypes.Log) {
if p.deployer == nil {
p.l.Errorw("deployer is nil")
return
}
if !p.isDeployLog(log) {
return
}
deployment, err := p.parseDeployLog(log)
if err != nil {
p.l.Errorw("failed to parse log", "log", log, "err", err)
return
}
if len(deployment.Address) == 0 {
return
}
err = p.storage.Insert(deployment)
if err != nil {
p.l.Errorw("failed to insert deployment", "deployment", deployment, "err", err)
}
}

func (p *DeployParser) parseDeployLog(log ethereumTypes.Log) (zerox_deployment.Deployment, error) {
var result zerox_deployment.Deployment
event, err := p.deployer.ParseDeployed(log)
if err != nil {
return result, fmt.Errorf("parse deploy log error %w", err)
}
feature, address := event.Arg0, event.Arg2

contractType := ContractType(feature.Int64())

if !p.contractABIs.containAddress(address) && !isZeroAddress(address) {
abi, err := NewABI(ContractABI{Address: address, ContractType: contractType})
if err != nil {
return result, fmt.Errorf("create abi error: %w", err)
}
p.contractABIs.addContractABI(address, abi)
}

result = zerox_deployment.Deployment{
BlockNumber: log.BlockNumber,
ContractType: int(contractType),
Address: address.String(),
}
return result, nil
}
25 changes: 25 additions & 0 deletions v2/pkg/parser/zxrfqv3/deployer/abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,30 @@
],
"stateMutability": "view",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "Feature",
"name": "",
"type": "uint128"
},
{
"indexed": true,
"internalType": "Nonce",
"name": "",
"type": "uint32"
},
{
"indexed": true,
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "Deployed",
"type": "event"
}
]
Loading

0 comments on commit f623758

Please sign in to comment.