generated from KyberNetwork/go-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
33be191
commit 6c3f730
Showing
11 changed files
with
628 additions
and
65 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
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'); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,161 @@ | ||
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) | ||
} | ||
|
||
p.syncContractAddress() | ||
return nil | ||
} | ||
|
||
func (p *DeployParser) syncContractAddress() { | ||
contractTypeSupported := []ContractType{SwapContract, GaslessContract} | ||
ticker := time.NewTicker(time.Hour) | ||
defer ticker.Stop() | ||
go func() { | ||
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 | ||
} |
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.