[#326] morph/balance: Provide interface to "transferX" method's invocations
Implement TransferX method on Balance contract client. Implement TransferX method on Balance client's wrapper that encapsulates it's TransferX call. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
f7a9e43165
commit
20ad0ae82a
3 changed files with 110 additions and 0 deletions
|
@ -28,17 +28,20 @@ var ErrNilClient = errors.New("balance contract client is nil")
|
||||||
type Option func(*cfg)
|
type Option func(*cfg)
|
||||||
|
|
||||||
type cfg struct {
|
type cfg struct {
|
||||||
|
transferXMethod, // transferX method name for invocation
|
||||||
balanceOfMethod, // balanceOf method name for invocation
|
balanceOfMethod, // balanceOf method name for invocation
|
||||||
decimalsMethod string // decimals method name for invocation
|
decimalsMethod string // decimals method name for invocation
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
defaultTransferXMethod = "transferX" // default "transferX" method name
|
||||||
defaultBalanceOfMethod = "balanceOf" // default "balance of" method name
|
defaultBalanceOfMethod = "balanceOf" // default "balance of" method name
|
||||||
defaultDecimalsMethod = "decimals" // default decimals method name
|
defaultDecimalsMethod = "decimals" // default decimals method name
|
||||||
)
|
)
|
||||||
|
|
||||||
func defaultConfig() *cfg {
|
func defaultConfig() *cfg {
|
||||||
return &cfg{
|
return &cfg{
|
||||||
|
transferXMethod: defaultTransferXMethod,
|
||||||
balanceOfMethod: defaultBalanceOfMethod,
|
balanceOfMethod: defaultBalanceOfMethod,
|
||||||
decimalsMethod: defaultDecimalsMethod,
|
decimalsMethod: defaultDecimalsMethod,
|
||||||
}
|
}
|
||||||
|
@ -100,3 +103,17 @@ func WithDecimalsMethod(n string) Option {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithTransferXMethod returns a client constructor option that
|
||||||
|
// specifies the "transferX" method name.
|
||||||
|
//
|
||||||
|
// Ignores empty value.
|
||||||
|
//
|
||||||
|
// If option not provided, "transferX" is used.
|
||||||
|
func WithTransferXMethod(n string) Option {
|
||||||
|
return func(c *cfg) {
|
||||||
|
if n != "" {
|
||||||
|
c.transferXMethod = n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
53
pkg/morph/client/balance/transfer.go
Normal file
53
pkg/morph/client/balance/transfer.go
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
package balance
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TransferXArgs groups the arguments
|
||||||
|
// of "transferX" invocation call.
|
||||||
|
type TransferXArgs struct {
|
||||||
|
amount int64 // amount in GASe-12
|
||||||
|
|
||||||
|
sender []byte // sender's wallet script hash
|
||||||
|
|
||||||
|
recipient []byte // recipient's wallet script hash
|
||||||
|
|
||||||
|
details []byte // transfer details
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAmount sets amount of funds to transfer
|
||||||
|
// in GASe-12.
|
||||||
|
func (t *TransferXArgs) SetAmount(v int64) {
|
||||||
|
t.amount = v
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetSender sets wallet script hash
|
||||||
|
// of the sender of funds in a binary format.
|
||||||
|
func (t *TransferXArgs) SetSender(v []byte) {
|
||||||
|
t.sender = v
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRecipient sets wallet script hash
|
||||||
|
// of the recipient of funds in a binary format.
|
||||||
|
func (t *TransferXArgs) SetRecipient(v []byte) {
|
||||||
|
t.recipient = v
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDetails sets details of the money transaction
|
||||||
|
// in a binary format.
|
||||||
|
func (t *TransferXArgs) SetDetails(v []byte) {
|
||||||
|
t.details = v
|
||||||
|
}
|
||||||
|
|
||||||
|
// TransferX invokes the call of "transferX" method
|
||||||
|
// of NeoFS Balance contract.
|
||||||
|
func (c *Client) TransferX(args TransferXArgs) error {
|
||||||
|
return errors.Wrapf(c.client.Invoke(
|
||||||
|
c.transferXMethod,
|
||||||
|
args.sender,
|
||||||
|
args.recipient,
|
||||||
|
args.amount,
|
||||||
|
args.details,
|
||||||
|
), "could not invoke method (%s)", c.transferXMethod)
|
||||||
|
}
|
40
pkg/morph/client/balance/wrapper/transfer.go
Normal file
40
pkg/morph/client/balance/wrapper/transfer.go
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
package wrapper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
||||||
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client/balance"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TransferPrm groups parameters of TransferX method.
|
||||||
|
type TransferPrm struct {
|
||||||
|
Amount int64
|
||||||
|
|
||||||
|
From, To *owner.ID
|
||||||
|
|
||||||
|
Details []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// TransferX transfers Amound of GASe-12 from p.From to p.To
|
||||||
|
// with details p.Details through smart contract call.
|
||||||
|
func (w *Wrapper) TransferX(p TransferPrm) error {
|
||||||
|
from, err := owner.ScriptHashBE(p.From)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "invalid sender")
|
||||||
|
}
|
||||||
|
|
||||||
|
to, err := owner.ScriptHashBE(p.To)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "invalid recipient")
|
||||||
|
}
|
||||||
|
|
||||||
|
// prepare invocation arguments
|
||||||
|
args := balance.TransferXArgs{}
|
||||||
|
args.SetSender(from)
|
||||||
|
args.SetRecipient(to)
|
||||||
|
args.SetAmount(p.Amount)
|
||||||
|
args.SetDetails(p.Details)
|
||||||
|
|
||||||
|
// invoke smart contract call
|
||||||
|
return w.client.TransferX(args)
|
||||||
|
}
|
Loading…
Reference in a new issue