neo-go icon indicating copy to clipboard operation
neo-go copied to clipboard

Wrap contracts into another structure in RPC client

Open roman-khimov opened this issue 3 years ago • 1 comments

We have a number of methods for native and non-native contracts in RPC client, but they all use the same (*Client) namespace: https://pkg.go.dev/github.com/nspcc-dev/[email protected]/pkg/rpc/client

This kinda works, but at the same time:

  • we have a lot of methods for (*Client)
  • we have to pass contract's hash to every call to non-native and even some methods of native contracts (like NEP17 methods)

What we can do instead is wrap contract-specific functions into contract-specific structures (tied to (*Client) of course), so that instead of

b, err = c.NEP17BalanceOf(token, acc)
s, err = c.NEP17Symbol(token)

we'd do something more like

tok := nep17.Init(c, token) // or client.GetNEP17(token)
b, err = tok.BalanceOf(acc)
s, err = tok.Symbol()

Exact packaging can be discussed, but the main idea is to move functionality out of the (ever-growing) Client and simplify interface.

Refs. #1965.

roman-khimov avatar May 17 '21 14:05 roman-khimov

I like the idea. Another variant is (naming can be improved):

ctr := c.Static(hash) // returns interface { Invoke(args...) }
tok := c.NEP17(hash) // returns NEP17 interface

NEP17 wiil also cache decimals and totalSupply. Static can have separate InvokeBool(args) (bool, error) for method returning bool (int etc.).

fyrchik avatar Jun 18 '21 11:06 fyrchik

I think it's done now after a set of #2597 commits.

roman-khimov avatar Sep 07 '22 19:09 roman-khimov