2020-07-24 13:54:03 +00:00
|
|
|
package balance
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-10-28 13:01:29 +00:00
|
|
|
"math/big"
|
|
|
|
|
2022-01-31 09:24:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2022-01-31 09:24:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
2022-01-31 09:24:25 +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 *owner.ID) (*big.Int, error) {
|
|
|
|
h, err := address.StringToUint160(id.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2021-11-09 20:52:29 +00:00
|
|
|
invokePrm := client.TestInvokePrm{}
|
2022-01-29 13:06:36 +00:00
|
|
|
invokePrm.SetMethod(balanceOfMethod)
|
2022-03-31 08:08:27 +00:00
|
|
|
invokePrm.SetArgs(h)
|
2021-11-09 20:52:29 +00:00
|
|
|
|
|
|
|
prms, err := c.client.TestInvoke(invokePrm)
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
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 {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("unexpected stack item count (%s): %d", balanceOfMethod, ln)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 13:01:29 +00:00
|
|
|
amount, err := client.BigIntFromStackItem(prms[0])
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
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
|
|
|
}
|
2022-01-31 09:24:25 +00:00
|
|
|
return amount, nil
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|