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

36 lines
971 B
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"
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 := id.ScriptHash()
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("test invoke (%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
}