Skip to content

Commit 752331b

Browse files
committed
added code to generate and validate config
1 parent 9c0f351 commit 752331b

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

x/usvl/types/chain_config.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package types
2+
3+
import "fmt"
4+
5+
type ChainConfig struct {
6+
// ChainId of the chain
7+
ChainID string `json:"chainId"`
8+
// Chain name, ethereum-mainnet, ethereum-sepolia..
9+
ChainName string `json:"chainName"`
10+
// Chain rpc url
11+
ChainRpcURL string `json:"chainRpcUrl"`
12+
//Chain wss socket
13+
ChainWssSocket string `json:"chainWssSocket"`
14+
//Locker Contract Address
15+
LockerContractAddress string `json:"lockerContractAddress"`
16+
// Block confirmation number
17+
BlockConfirmationNumber uint8 `json:"blockConfirmationNumber"`
18+
//Caip10 prefix
19+
CAIP10Prefix string `json:"caip10Prefix"`
20+
21+
}
22+
23+
func (c *ChainConfig) Validate() ( error) {
24+
if c.ChainID == "" {
25+
return fmt.Errorf("ChainID cannot be empty")
26+
}
27+
if c.ChainName == "" {
28+
return fmt.Errorf("ChainName cannot be empty")
29+
}
30+
if c.ChainRpcURL == "" {
31+
return fmt.Errorf("ChainRpcURL cannot be empty")
32+
}
33+
if c.ChainWssSocket == "" {
34+
return fmt.Errorf("ChainWssSocket cannot be empty")
35+
}
36+
if c.LockerContractAddress == "" {
37+
return fmt.Errorf("LockerContractAddress cannot be empty")
38+
}
39+
if c.BlockConfirmationNumber == 0 {
40+
return fmt.Errorf("BlockConfirmationNumber cannot be 0")
41+
}
42+
if c.CAIP10Prefix == "" {
43+
return fmt.Errorf("CAIP10Prefix cannot be empty")
44+
}
45+
return nil
46+
}
47+
48+
func generateConfigForEthereumSepolia() ChainConfig {
49+
return ChainConfig{
50+
ChainID: "11155111",
51+
ChainName: "ethereum-sepolia",
52+
ChainRpcURL: "https://sepolia.infura.io/v3/XXXXXXXXXX",
53+
ChainWssSocket: "wss://sepolia.infura.io/ws/v3/XXXXXXXXXX",
54+
LockerContractAddress: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd",
55+
BlockConfirmationNumber: 12,
56+
CAIP10Prefix: "eip155:11155111",
57+
}
58+
}
59+
60+
func GenerateConfig() []ChainConfig {
61+
configs := []ChainConfig{generateConfigForEthereumSepolia()}
62+
return configs
63+
}

x/usvl/types/chain_config_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package types
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestConfig(t *testing.T) {
8+
originalConfig := ChainConfig{
9+
ChainID: "1",
10+
ChainName: "ethereum-mainnet",
11+
ChainRpcURL: "https://mainnet.infura.io/v3/XXXXXXXXXX",
12+
ChainWssSocket: "wss://mainnet.infura.io/ws/v3/XXXXXXXXXX",
13+
LockerContractAddress: "0x1234567890abcdef1234567890abcdef12345678",
14+
BlockConfirmationNumber: 12,
15+
CAIP10Prefix: "eip155:1",
16+
}
17+
18+
err := originalConfig.Validate()
19+
if err != nil {
20+
t.Errorf("Validation error: %v", err)
21+
return
22+
}
23+
24+
}
25+
26+
func TestEmptyConfig(t *testing.T) {
27+
// Arrange
28+
partialConfig := ChainConfig{
29+
ChainName: "ethereum-mainnet",
30+
ChainRpcURL: "https://mainnet.infura.io/v3/XXXXXXXXXX",
31+
ChainWssSocket: "wss://mainnet.infura.io/ws/v3/XXXXXXXXXX",
32+
LockerContractAddress: "0x1234567890abcdef1234567890abcdef12345678",
33+
BlockConfirmationNumber: 12,
34+
}
35+
36+
err := partialConfig.Validate()
37+
if err == nil {
38+
t.Error("Expected validation to fail, but it succeeded")
39+
return
40+
}
41+
42+
}
43+
44+
func TestGenerateConfig(t *testing.T) {
45+
chainConfigs := GenerateConfig()
46+
if len(chainConfigs) == 0 {
47+
t.Error("Expected non-empty config, but got empty")
48+
return
49+
}
50+
}

0 commit comments

Comments
 (0)