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"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
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
|
|
|
)
|
|
|
|
|
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.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (c *Client) BalanceOf(id user.ID) (*big.Int, error) {
|
2022-05-17 13:59:46 +00:00
|
|
|
h, err := address.StringToUint160(id.EncodeToString())
|
2022-01-31 09:24:25 +00:00
|
|
|
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
|
|
|
}
|