frostfs-node/pkg/morph/client/balance/balanceOf.go

37 lines
1 KiB
Go
Raw Normal View History

2020-07-24 13:54:03 +00:00
package balance
import (
"fmt"
"math/big"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
2020-07-24 13:54:03 +00:00
)
// BalanceOf receives the amount of funds in the client's account
// through the Balance contract call, and returns it.
func (c *Client) BalanceOf(id user.ID) (*big.Int, error) {
h, err := address.StringToUint160(id.EncodeToString())
if err != nil {
return nil, err
}
2020-07-24 13:54:03 +00:00
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(balanceOfMethod)
invokePrm.SetArgs(h)
prms, err := c.client.TestInvoke(invokePrm)
2020-07-24 13:54:03 +00:00
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", balanceOfMethod, err)
2020-07-24 13:54:03 +00:00
} else if ln := len(prms); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", balanceOfMethod, ln)
2020-07-24 13:54:03 +00:00
}
amount, err := client.BigIntFromStackItem(prms[0])
2020-07-24 13:54:03 +00:00
if err != nil {
return nil, fmt.Errorf("could not get integer stack item from stack item (%s): %w", balanceOfMethod, err)
2020-07-24 13:54:03 +00:00
}
return amount, nil
2020-07-24 13:54:03 +00:00
}