Skip to content

Commit

Permalink
fix compile
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderZhi committed Mar 19, 2024
1 parent 695bb39 commit be4568a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
77 changes: 51 additions & 26 deletions witness-service/cmd/witness/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ import (
"github.com/iotexproject/ioTube/witness-service/witness"
)

// TokenPair defines a token pair
type TokenPair struct {
Token1 string `json:"token1" yaml:"token1"`
Token2 string `json:"token2" yaml:"token2"`
}

// Configuration defines the configuration of the witness service
type Configuration struct {
Chain string `json:"chain" yaml:"chain"`
Expand All @@ -47,19 +53,16 @@ type Configuration struct {
GrpcProxyPort int `json:"grpcProxyPort" yaml:"grpcProxyPort"`
DisableTransferSubmit bool `json:"disableTransferSubmit" yaml:"disableTransferSubmit"`
Cashiers []struct {
ID string `json:"id" yaml:"id"`
RelayerURL string `json:"relayerURL" yaml:"relayerURL"`
Version string `json:"version" yaml:"version"`
CashierContractAddress string `json:"cashierContractAddress" yaml:"cashierContractAddress"`
TokenSafeContractAddress string `json:"tokenSafeContractAddress" yaml:"tokenSafeContractAddress"`
ValidatorContractAddress string `json:"vialidatorContractAddress" yaml:"validatorContractAddress"`
TransferTableName string `json:"transferTableName" yaml:"transferTableName"`
TokenPairs []struct {
Token1 string `json:"token1" yaml:"token1"`
Token2 string `json:"token2" yaml:"token2"`
} `json:"tokenPairs" yaml:"tokenPairs"`
StartBlockHeight int `json:"startBlockHeight" yaml:"startBlockHeight"`
Reverse struct {
ID string `json:"id" yaml:"id"`
RelayerURL string `json:"relayerURL" yaml:"relayerURL"`
Version witness.Version `json:"version" yaml:"version"`
CashierContractAddress string `json:"cashierContractAddress" yaml:"cashierContractAddress"`
TokenSafeContractAddress string `json:"tokenSafeContractAddress" yaml:"tokenSafeContractAddress"`
ValidatorContractAddress string `json:"vialidatorContractAddress" yaml:"validatorContractAddress"`
TransferTableName string `json:"transferTableName" yaml:"transferTableName"`
TokenPairs []TokenPair `json:"tokenPairs" yaml:"tokenPairs"`
StartBlockHeight int `json:"startBlockHeight" yaml:"startBlockHeight"`
Reverse struct {
TransferTableName string `json:"transferTableName" yaml:"transferTableName"`
CashierContractAddress string `json:"cashierContractAddress" yaml:"cashierContractAddress"`
Tokens []string `json:"tokens" yaml:"tokens"`
Expand Down Expand Up @@ -96,6 +99,37 @@ func init() {
}
}

func parseAddress(addr string) (common.Address, error) {
if strings.HasPrefix(addr, "io") {
ioAddr, err := address.FromString(addr)
if err != nil {
log.Fatalf("failed to parse iotex address %s, %v\n", addr, err)
}
return common.BytesToAddress(ioAddr.Bytes()), nil
}
return common.HexToAddress(addr), nil
}

func parseTokenPairs(tokenPairs []TokenPair) map[common.Address]common.Address {
pairs := make(map[common.Address]common.Address)
for _, pair := range tokenPairs {
token1, err := parseAddress(pair.Token1)
if err != nil {
log.Fatalf("failed to parse token1 address %s, %v\n", pair.Token1, err)
}
if _, ok := pairs[token1]; ok {
log.Fatalf("duplicate token key %s\n", pair.Token1)
}
token2, err := parseAddress(pair.Token2)
if err != nil {
log.Fatalf("failed to parse token2 address %s, %v\n", pair.Token1, err)
}
pairs[token1] = token2
}

return pairs
}

func main() {
flag.Parse()
opts := []config.YAMLOption{config.Static(defaultConfig), config.Expand(os.LookupEnv)}
Expand Down Expand Up @@ -177,27 +211,17 @@ func main() {
if err != nil {
log.Fatalf("failed to parse cashier contract address %s, %v\n", cc.CashierContractAddress, err)
}
pairs := make(map[common.Address]common.Address)
for _, pair := range cc.TokenPairs {
ioAddr, err := address.FromString(pair.Token1)
if err != nil {
log.Fatalf("failed to parse iotex address %s, %v\n", pair.Token1, err)
}
if _, ok := pairs[common.BytesToAddress(ioAddr.Bytes())]; ok {
log.Fatalf("duplicate token key %s\n", pair.Token1)
}
pairs[common.BytesToAddress(ioAddr.Bytes())] = common.HexToAddress(pair.Token2)
}
cashier, err := witness.NewTokenCashier(
cc.ID,
cc.Version,
cc.RelayerURL,
iotexClient,
cashierContractAddr,
common.HexToAddress(cc.ValidatorContractAddress),
witness.NewRecorder(
db.NewStore(cfg.Database),
cc.TransferTableName,
pairs,
parseTokenPairs(cc.TokenPairs),
),
uint64(cc.StartBlockHeight),
)
Expand Down Expand Up @@ -244,6 +268,7 @@ func main() {
}
cashier, err := witness.NewTokenCashierOnEthereum(
cc.ID,
cc.Version,
cc.RelayerURL,
ethClient,
common.HexToAddress(cc.CashierContractAddress),
Expand All @@ -252,7 +277,7 @@ func main() {
witness.NewRecorder(
db.NewStore(cfg.Database),
cc.TransferTableName,
pairs,
parseTokenPairs(cc.TokenPairs),
),
uint64(cc.StartBlockHeight),
uint8(cfg.ConfirmBlockNumber),
Expand Down
1 change: 0 additions & 1 deletion witness-service/witness/tokencashieronethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ func NewTokenCashierOnEthereum(
}
return newTokenCashierBase(
id,
version,
recorder,
relayerURL,
validatorContractAddr,
Expand Down

0 comments on commit be4568a

Please sign in to comment.