2020-11-19 10:00:46 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2021-07-17 12:54:52 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
2020-11-19 10:00:46 +00:00
|
|
|
"math/big"
|
|
|
|
|
2021-07-17 12:54:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-11-19 10:00:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Deposit represents GAS deposit from Notary contract.
|
|
|
|
type Deposit struct {
|
|
|
|
Amount *big.Int
|
|
|
|
Till uint32
|
|
|
|
}
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
// ToStackItem implements stackitem.Convertible interface. It never returns an
|
|
|
|
// error.
|
|
|
|
func (d *Deposit) ToStackItem() (stackitem.Item, error) {
|
2021-07-17 12:54:52 +00:00
|
|
|
return stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.NewBigInteger(d.Amount),
|
|
|
|
stackitem.Make(d.Till),
|
2021-07-17 15:37:33 +00:00
|
|
|
}), nil
|
2021-07-17 12:54:52 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
// FromStackItem implements stackitem.Convertible interface.
|
|
|
|
func (d *Deposit) FromStackItem(it stackitem.Item) error {
|
2021-07-17 12:54:52 +00:00
|
|
|
items, ok := it.Value().([]stackitem.Item)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("not a struct")
|
|
|
|
}
|
|
|
|
if len(items) != 2 {
|
|
|
|
return errors.New("wrong number of elements")
|
|
|
|
}
|
|
|
|
amount, err := items[0].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid amount: %w", err)
|
|
|
|
}
|
|
|
|
till, err := items[1].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid till: %w", err)
|
|
|
|
}
|
2021-07-19 12:15:14 +00:00
|
|
|
tiu64 := till.Uint64()
|
|
|
|
if !till.IsUint64() || tiu64 > math.MaxUint32 {
|
2021-07-17 12:54:52 +00:00
|
|
|
return errors.New("wrong till value")
|
|
|
|
}
|
|
|
|
d.Amount = amount
|
2021-07-19 12:15:14 +00:00
|
|
|
d.Till = uint32(tiu64)
|
2021-07-17 12:54:52 +00:00
|
|
|
return nil
|
2020-11-19 10:00:46 +00:00
|
|
|
}
|