viem-goviem-go

watchAsset

Requests to watch an asset in wallet

Import

Import the wallet actions package so you can call this action.

import "github.com/ChefBingbong/viem-go/actions/wallet"

Usage

See how to construct a wallet client and call this action.

import (
"context"
"log"
"github.com/ChefBingbong/viem-go/actions/wallet"
"github.com/ChefBingbong/viem-go/client"
"github.com/ChefBingbong/viem-go/client/transport"
"github.com/ChefBingbong/viem-go/chain/definitions"
)
ctx := context.Background()
walletClient, err := client.CreateWalletClient(client.WalletClientConfig{
Chain: definitions.Mainnet,
Transport: transport.HTTP("https://eth.llamarpc.com"),
})
if err != nil {
log.Fatal(err)
}
defer func() { _ = walletClient.Close() }()
success, err := wallet.WatchAsset(ctx, walletClient, wallet.WatchAssetParameters{
Type: "ERC20",
Options: wallet.WatchAssetOptions{
Address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
Decimals: 18,
Symbol: "WETH",
},
})
if err != nil {
log.Fatal(err)
}
log.Printf("Asset added: %v", success)

Returns

bool

Boolean indicating if the token was successfully added.

Parameters

Configuration options accepted by this action.

Type

  • Type: string

  • Required

The token type (e.g., "ERC20").

success, err := wallet.WatchAsset(ctx, walletClient, wallet.WatchAssetParameters{
Type: "ERC20",
Options: wallet.WatchAssetOptions{
Address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
Decimals: 18,
Symbol: "WETH",
},
})

Options

  • Type: WatchAssetOptions

  • Required

The token details.

Options.Address

Specify the token contract address for the asset to watch.

  • Type: string

  • Required

The address of the token contract.

success, err := wallet.WatchAsset(ctx, walletClient, wallet.WatchAssetParameters{
Type: "ERC20",
Options: wallet.WatchAssetOptions{
Address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
Decimals: 18,
Symbol: "WETH",
},
})

Options.Symbol

Provide the symbol that will be shown for this token.

  • Type: string

  • Required

A ticker symbol or shorthand, up to 11 characters.

success, err := wallet.WatchAsset(ctx, walletClient, wallet.WatchAssetParameters{
Type: "ERC20",
Options: wallet.WatchAssetOptions{
Address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
Decimals: 18,
Symbol: "WETH",
},
})

Options.Decimals

Define how many decimal places the token uses.

  • Type: uint8

  • Required

The number of token decimals.

success, err := wallet.WatchAsset(ctx, walletClient, wallet.WatchAssetParameters{
Type: "ERC20",
Options: wallet.WatchAssetOptions{
Address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
Decimals: 18,
Symbol: "WETH",
},
})

Options.Image

Optionally supply a URL for the token logo.

  • Type: string

  • Optional

A string URL of the token logo.

success, err := wallet.WatchAsset(ctx, walletClient, wallet.WatchAssetParameters{
Type: "ERC20",
Options: wallet.WatchAssetOptions{
Address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
Decimals: 18,
Symbol: "WETH",
Image: "https://weth.com/icon.png",
},
})

JSON-RPC Method