client: remove Balancer getter/setter, make it an Option

Keep it internal to the client instance, it makes no sense exposing it to the
outside user.
This commit is contained in:
Roman Khimov 2020-04-29 18:04:05 +03:00
parent 00d6439eb2
commit a49f2bc47c
2 changed files with 30 additions and 37 deletions

View file

@ -35,7 +35,6 @@ type Client struct {
version string version string
wifMu *sync.Mutex wifMu *sync.Mutex
wif *keys.WIF wif *keys.WIF
balancerMu *sync.Mutex
balancer request.BalanceGetter balancer request.BalanceGetter
} }
@ -43,6 +42,12 @@ type Client struct {
// All Values are optional. If any duration is not specified // All Values are optional. If any duration is not specified
// a default of 3 seconds will be used. // a default of 3 seconds will be used.
type Options struct { type Options struct {
// Balancer is an implementation of request.BalanceGetter interface,
// if not set then the default Client's implementation will be used, but
// it relies on server support for `getunspents` RPC call which is
// standard for neo-go, but only implemented as a plugin for C# node. So
// you can override it here to use NeoScanServer for example.
Balancer request.BalanceGetter
Cert string Cert string
Key string Key string
CACert string CACert string
@ -86,14 +91,18 @@ func New(ctx context.Context, endpoint string, opts Options) (*Client, error) {
if opts.Cert != "" && opts.Key != "" { if opts.Cert != "" && opts.Key != "" {
} }
return &Client{ cl := &Client{
ctx: ctx, ctx: ctx,
cli: httpClient, cli: httpClient,
balancerMu: new(sync.Mutex),
wifMu: new(sync.Mutex), wifMu: new(sync.Mutex),
endpoint: url, endpoint: url,
version: opts.Version, version: opts.Version,
}, nil }
if opts.Balancer == nil {
opts.Balancer = cl
}
cl.balancer = opts.Balancer
return cl, nil
} }
// WIF returns WIF structure associated with the client. // WIF returns WIF structure associated with the client.
@ -121,26 +130,10 @@ func (c *Client) SetWIF(wif string) error {
return nil return nil
} }
// Balancer is a getter for balance field. // CalculateInputs implements request.BalanceGetter interface and returns inputs
func (c *Client) Balancer() request.BalanceGetter { // array for the specified amount of given asset belonging to specified address.
c.balancerMu.Lock() // This implementation uses GetUnspents JSON-RPC call internally, so make sure
defer c.balancerMu.Unlock() // your RPC server supports that.
return c.balancer
}
// SetBalancer is a setter for balance field.
func (c *Client) SetBalancer(b request.BalanceGetter) {
c.balancerMu.Lock()
defer c.balancerMu.Unlock()
if b != nil {
c.balancer = b
}
}
// CalculateInputs creates input transactions for the specified amount of given
// asset belonging to specified address. This implementation uses GetUnspents
// JSON-RPC call internally, so make sure your RPC server suppors that.
func (c *Client) CalculateInputs(address string, asset util.Uint256, cost util.Fixed8) ([]transaction.Input, util.Fixed8, error) { func (c *Client) CalculateInputs(address string, asset util.Uint256, cost util.Fixed8) ([]transaction.Input, util.Fixed8, error) {
var utxos state.UnspentBalances var utxos state.UnspentBalances

View file

@ -484,7 +484,7 @@ func (c *Client) TransferAsset(asset util.Uint256, address string, amount util.F
Address: address, Address: address,
Value: amount, Value: amount,
WIF: c.WIF(), WIF: c.WIF(),
Balancer: c.Balancer(), Balancer: c.balancer,
} }
resp util.Uint256 resp util.Uint256
) )