forked from TrueCloudLab/frostfs-node
[#1541] morph/event: Simplify balance contract event parsing
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
7df3520d48
commit
d5d5ce2074
2 changed files with 13 additions and 58 deletions
|
@ -3,7 +3,7 @@ package balance
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-contract/rpcclient/balance"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
|
@ -47,61 +47,17 @@ func (l Lock) TxHash() util.Uint256 { return l.txHash }
|
|||
|
||||
// ParseLock from notification into lock structure.
|
||||
func ParseLock(e *state.ContainedNotificationEvent) (event.Event, error) {
|
||||
var (
|
||||
ev Lock
|
||||
err error
|
||||
)
|
||||
|
||||
params, err := event.ParseStackArray(e)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
|
||||
var le balance.LockEvent
|
||||
if err := le.FromStackItem(e.Item); err != nil {
|
||||
return nil, fmt.Errorf("parse balance.LockEvent: %w", err)
|
||||
}
|
||||
|
||||
if ln := len(params); ln != 5 {
|
||||
return nil, event.WrongNumberOfParameters(5, ln)
|
||||
}
|
||||
|
||||
// parse id
|
||||
ev.id, err = client.BytesFromStackItem(params[0])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not get lock id: %w", err)
|
||||
}
|
||||
|
||||
// parse user
|
||||
user, err := client.BytesFromStackItem(params[1])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not get lock user value: %w", err)
|
||||
}
|
||||
|
||||
ev.user, err = util.Uint160DecodeBytesBE(user)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not convert lock user value to uint160: %w", err)
|
||||
}
|
||||
|
||||
// parse lock account
|
||||
lock, err := client.BytesFromStackItem(params[2])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not get lock account value: %w", err)
|
||||
}
|
||||
|
||||
ev.lock, err = util.Uint160DecodeBytesBE(lock)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not convert lock account value to uint160: %w", err)
|
||||
}
|
||||
|
||||
// parse amount
|
||||
ev.amount, err = client.IntFromStackItem(params[3])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not get lock amount: %w", err)
|
||||
}
|
||||
|
||||
// parse until deadline
|
||||
ev.until, err = client.IntFromStackItem(params[4])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not get lock deadline: %w", err)
|
||||
}
|
||||
|
||||
ev.txHash = e.Container
|
||||
|
||||
return ev, nil
|
||||
return Lock{
|
||||
id: le.TxID,
|
||||
user: le.From,
|
||||
lock: le.To,
|
||||
amount: le.Amount.Int64(),
|
||||
until: le.Until.Int64(),
|
||||
txHash: e.Container,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"math/big"
|
||||
"testing"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
|
@ -28,7 +27,7 @@ func TestParseLock(t *testing.T) {
|
|||
}
|
||||
|
||||
_, err := ParseLock(createNotifyEventFromItems(prms))
|
||||
require.EqualError(t, err, event.WrongNumberOfParameters(5, len(prms)).Error())
|
||||
require.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("wrong id parameter", func(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue