diff --git a/pkg/morph/client/balance/client.go b/pkg/morph/client/balance/client.go index d7c1a38b..7e4d3799 100644 --- a/pkg/morph/client/balance/client.go +++ b/pkg/morph/client/balance/client.go @@ -28,17 +28,20 @@ var ErrNilClient = errors.New("balance contract client is nil") type Option func(*cfg) type cfg struct { + transferXMethod, // transferX method name for invocation balanceOfMethod, // balanceOf method name for invocation decimalsMethod string // decimals method name for invocation } const ( + defaultTransferXMethod = "transferX" // default "transferX" method name defaultBalanceOfMethod = "balanceOf" // default "balance of" method name defaultDecimalsMethod = "decimals" // default decimals method name ) func defaultConfig() *cfg { return &cfg{ + transferXMethod: defaultTransferXMethod, balanceOfMethod: defaultBalanceOfMethod, 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 + } + } +} diff --git a/pkg/morph/client/balance/transfer.go b/pkg/morph/client/balance/transfer.go new file mode 100644 index 00000000..f3a21249 --- /dev/null +++ b/pkg/morph/client/balance/transfer.go @@ -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) +} diff --git a/pkg/morph/client/balance/wrapper/transfer.go b/pkg/morph/client/balance/wrapper/transfer.go new file mode 100644 index 00000000..7cadede9 --- /dev/null +++ b/pkg/morph/client/balance/wrapper/transfer.go @@ -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) +}