Skip to content

Commit 05c7637

Browse files
feat: add token name to show cmd (#230)
* feat: add token name to show cmd
1 parent e873f52 commit 05c7637

File tree

3 files changed

+101
-17
lines changed

3 files changed

+101
-17
lines changed

pkg/internal/erc20/erc20.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package erc20
2+
3+
import (
4+
"strings"
5+
6+
"github.com/ethereum/go-ethereum/accounts/abi"
7+
"github.com/ethereum/go-ethereum/accounts/abi/bind"
8+
"github.com/ethereum/go-ethereum/common"
9+
"github.com/ethereum/go-ethereum/ethclient"
10+
)
11+
12+
const (
13+
UnknownTokenName = "Unknown"
14+
)
15+
16+
// ABI is a simplified ABI for the ERC20 token standard
17+
var ABI = `[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"type":"function"}]`
18+
19+
// ERC20 is the Go binding of the ERC20 contract
20+
type ERC20 struct {
21+
Caller // Read-only binding to the contract
22+
}
23+
24+
// Caller is an auto generated read-only Go binding around an Ethereum contract.
25+
type Caller struct {
26+
contract *bind.BoundContract // Generic contract wrapper for the low level calls
27+
}
28+
29+
// Name is a free data retrieval call binding the contract method 0x06fdde03.
30+
func (c *Caller) Name(opts *bind.CallOpts) (string, error) {
31+
var out []interface{}
32+
err := c.contract.Call(opts, &out, "name")
33+
if err != nil {
34+
return "", err
35+
}
36+
return out[0].(string), err
37+
}
38+
39+
// NewERC20 creates a new instance of ERC20, bound to a specific deployed contract.
40+
func NewERC20(address common.Address, backend bind.ContractBackend) (*ERC20, error) {
41+
contract, err := bindERC20(address, backend, backend, backend)
42+
if err != nil {
43+
return nil, err
44+
}
45+
return &ERC20{Caller: Caller{contract: contract}}, nil
46+
}
47+
48+
// bindERC20 binds a generic wrapper to an already deployed contract.
49+
func bindERC20(
50+
address common.Address,
51+
caller bind.ContractCaller,
52+
transactor bind.ContractTransactor,
53+
filterer bind.ContractFilterer,
54+
) (*bind.BoundContract, error) {
55+
parsed, err := abi.JSON(strings.NewReader(ABI))
56+
if err != nil {
57+
return nil, err
58+
}
59+
return bind.NewBoundContract(address, parsed, caller, transactor, filterer), nil
60+
}
61+
62+
func GetTokenName(tokenAddress common.Address, client *ethclient.Client) string {
63+
erc20Client, err := NewERC20(tokenAddress, client)
64+
if err != nil {
65+
return UnknownTokenName
66+
}
67+
68+
name, err := erc20Client.Name(&bind.CallOpts{})
69+
if err != nil {
70+
return UnknownTokenName
71+
}
72+
73+
return name
74+
}

pkg/rewards/show.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common"
1414
"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common/flags"
15+
"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/erc20"
1516
"github.com/Layr-Labs/eigenlayer-cli/pkg/telemetry"
1617
"github.com/Layr-Labs/eigenlayer-cli/pkg/utils"
1718

@@ -104,7 +105,8 @@ func ShowRewards(cCtx *cli.Context) error {
104105
elcontracts.Config{
105106
RewardsCoordinatorAddress: config.RewardsCoordinatorAddress,
106107
},
107-
ethClient, logger,
108+
ethClient,
109+
logger,
108110
)
109111
if err != nil {
110112
return eigenSdkUtils.WrapError("failed to create new reader from config", err)
@@ -197,14 +199,19 @@ func handleRewardsOutput(
197199
rewards map[gethcommon.Address]*big.Int,
198200
msg string,
199201
) error {
202+
client, err := ethclient.Dial(cfg.RPCUrl)
203+
if err != nil {
204+
return err
205+
}
206+
allRewards := make(allRewardsJson, 0)
207+
for address, amount := range rewards {
208+
allRewards = append(allRewards, rewardsJson{
209+
TokenName: erc20.GetTokenName(address, client),
210+
Address: address.Hex(),
211+
Amount: amount.String(),
212+
})
213+
}
200214
if cfg.OutputType == "json" {
201-
allRewards := make(allRewardsJson, 0)
202-
for address, amount := range rewards {
203-
allRewards = append(allRewards, rewardsJson{
204-
Address: address.Hex(),
205-
Amount: amount.String(),
206-
})
207-
}
208215
out, err := json.MarshalIndent(allRewards, "", " ")
209216
if err != nil {
210217
return err
@@ -223,18 +230,19 @@ func handleRewardsOutput(
223230
}
224231
fmt.Println()
225232
fmt.Println(strings.Repeat("-", 30), msg, strings.Repeat("-", 30))
226-
printRewards(rewards)
233+
printRewards(allRewards)
227234
}
228235
return nil
229236
}
230237

231-
func printRewards(rewards map[gethcommon.Address]*big.Int) {
238+
func printRewards(allRewards allRewardsJson) {
232239
// Define column headers and widths
233240
headers := []string{
241+
"Token Name",
234242
"Token Address",
235243
"Amount (Wei)",
236244
}
237-
widths := []int{46, 30}
245+
widths := []int{20, 46, 30}
238246

239247
// print dashes
240248
for _, width := range widths {
@@ -255,10 +263,11 @@ func printRewards(rewards map[gethcommon.Address]*big.Int) {
255263
fmt.Println("|")
256264

257265
// Print data rows
258-
for address, amount := range rewards {
259-
fmt.Printf("| %-*s| %-*s|\n",
260-
widths[0], address.Hex(),
261-
widths[1], amount.String(),
266+
for _, rewards := range allRewards {
267+
fmt.Printf("| %-*s| %-*s| %-*s|\n",
268+
widths[0], rewards.TokenName,
269+
widths[1], rewards.Address,
270+
widths[2], rewards.Amount,
262271
)
263272
}
264273

pkg/rewards/types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88
)
99

1010
type rewardsJson struct {
11-
Address string `json:"tokenAddress"`
12-
Amount string `json:"amount"`
11+
Address string `json:"tokenAddress"`
12+
TokenName string `json:"tokenName"`
13+
Amount string `json:"amount"`
1314
}
1415

1516
type allRewardsJson []rewardsJson

0 commit comments

Comments
 (0)