Skip to content

Commit fd1276f

Browse files
committed
fix(erc20): guard unwired read helpers
1 parent 999782e commit fd1276f

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

blockchain/erc20_abi.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ func (c *ERC20Client) Erc20Allowance(ctx context.Context, contract, owner, spend
6868

6969
// Erc20Metadata returns name, symbol, decimals for the ERC20 contract.
7070
func (c *ERC20Client) Erc20Metadata(ctx context.Context, contract common.Address) (Erc20Metadata, error) {
71-
if c.client == nil {
72-
return Erc20Metadata{}, fmt.Errorf("ERC20Client not wired to a base.Client")
73-
}
74-
7571
name, err := c.callString(ctx, contract, "name")
7672
if err != nil {
7773
return Erc20Metadata{}, fmt.Errorf("name: %w", err)
@@ -87,11 +83,15 @@ func (c *ERC20Client) Erc20Metadata(ctx context.Context, contract common.Address
8783
return Erc20Metadata{Name: name, Symbol: symbol, Decimals: decimals}, nil
8884
}
8985

90-
func (c *ERC20Client) callUint256(ctx context.Context, contract common.Address, method string, data []byte) (*big.Int, error) {
91-
if c.client == nil {
92-
return nil, fmt.Errorf("ERC20Client not wired to a base.Client")
86+
func (c *ERC20Client) callContract(ctx context.Context, contract common.Address, data []byte) ([]byte, error) {
87+
if c.client == nil || c.client.EVM == nil {
88+
return nil, fmt.Errorf("ERC20Client not wired to an EVM client")
9389
}
94-
ret, err := c.client.EVM.CallContract(ctx, contract, data)
90+
return c.client.EVM.CallContract(ctx, contract, data)
91+
}
92+
93+
func (c *ERC20Client) callUint256(ctx context.Context, contract common.Address, method string, data []byte) (*big.Int, error) {
94+
ret, err := c.callContract(ctx, contract, data)
9595
if err != nil {
9696
return nil, err
9797
}
@@ -114,7 +114,7 @@ func (c *ERC20Client) callString(ctx context.Context, contract common.Address, m
114114
if err != nil {
115115
return "", fmt.Errorf("pack %s: %w", method, err)
116116
}
117-
ret, err := c.client.EVM.CallContract(ctx, contract, data)
117+
ret, err := c.callContract(ctx, contract, data)
118118
if err != nil {
119119
return "", err
120120
}
@@ -137,7 +137,7 @@ func (c *ERC20Client) callUint8(ctx context.Context, contract common.Address, me
137137
if err != nil {
138138
return 0, fmt.Errorf("pack %s: %w", method, err)
139139
}
140-
ret, err := c.client.EVM.CallContract(ctx, contract, data)
140+
ret, err := c.callContract(ctx, contract, data)
141141
if err != nil {
142142
return 0, err
143143
}

blockchain/erc20_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@ func TestErc20Reads_EmptyReturnData(t *testing.T) {
101101
}
102102
}
103103

104+
func TestErc20Reads_UnwiredClientReturnsError(t *testing.T) {
105+
ctx := context.Background()
106+
contract := common.HexToAddress("0x0000000000000000000000000000000000000abc")
107+
holder := common.HexToAddress("0x0000000000000000000000000000000000000def")
108+
109+
for name, c := range map[string]*ERC20Client{
110+
"nil base client": {},
111+
"nil EVM client": {client: &Client{}},
112+
} {
113+
if _, err := c.Erc20Balance(ctx, contract, holder); err == nil || !strings.Contains(err.Error(), "not wired") {
114+
t.Fatalf("%s Erc20Balance: got %v, want not-wired error", name, err)
115+
}
116+
if _, err := c.Erc20Metadata(ctx, contract); err == nil || !strings.Contains(err.Error(), "not wired") {
117+
t.Fatalf("%s Erc20Metadata: got %v, want not-wired error", name, err)
118+
}
119+
}
120+
}
121+
104122
func TestNewMsgConvertCoin(t *testing.T) {
105123
coin := sdk.NewCoin("ulume", sdkmath.NewInt(1_000_000))
106124
receiver := common.HexToAddress("0x1234567890123456789012345678901234567890")

0 commit comments

Comments
 (0)