2020-01-13 10:21:44 +00:00
|
|
|
package result
|
2019-02-20 17:39:32 +00:00
|
|
|
|
|
|
|
import (
|
2020-03-23 14:31:28 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-07-27 15:07:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2019-02-20 17:39:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TransactionOutputRaw is used as a wrapper to represents
|
|
|
|
// a Transaction.
|
|
|
|
type TransactionOutputRaw struct {
|
2020-06-18 08:47:54 +00:00
|
|
|
transaction.Transaction
|
2020-03-23 14:31:28 +00:00
|
|
|
TransactionMetadata
|
|
|
|
}
|
|
|
|
|
2020-08-14 09:16:24 +00:00
|
|
|
// TransactionMetadata is an auxiliary struct for proper TransactionOutputRaw marshaling.
|
2020-03-23 14:31:28 +00:00
|
|
|
type TransactionMetadata struct {
|
2020-02-20 16:31:16 +00:00
|
|
|
Blockhash util.Uint256 `json:"blockhash,omitempty"`
|
|
|
|
Confirmations int `json:"confirmations,omitempty"`
|
2020-04-21 11:26:57 +00:00
|
|
|
Timestamp uint64 `json:"blocktime,omitempty"`
|
2020-11-13 13:54:38 +00:00
|
|
|
VMState string `json:"vmstate,omitempty"`
|
2019-02-20 17:39:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTransactionOutputRaw returns a new ransactionOutputRaw object.
|
2022-01-13 01:06:06 +00:00
|
|
|
func NewTransactionOutputRaw(tx *transaction.Transaction, header *block.Header, appExecResult *state.AppExecResult, chain LedgerAux) TransactionOutputRaw {
|
2020-11-13 13:54:38 +00:00
|
|
|
result := TransactionOutputRaw{
|
|
|
|
Transaction: *tx,
|
|
|
|
}
|
|
|
|
if header == nil {
|
|
|
|
return result
|
|
|
|
}
|
2019-02-20 17:39:32 +00:00
|
|
|
// confirmations formula
|
2021-03-01 13:44:47 +00:00
|
|
|
confirmations := int(chain.BlockHeight() - header.Index + 1)
|
2020-11-13 13:54:38 +00:00
|
|
|
result.TransactionMetadata = TransactionMetadata{
|
|
|
|
Blockhash: header.Hash(),
|
|
|
|
Confirmations: confirmations,
|
|
|
|
Timestamp: header.Timestamp,
|
|
|
|
VMState: appExecResult.VMState.String(),
|
2020-03-23 14:31:28 +00:00
|
|
|
}
|
2020-11-13 13:54:38 +00:00
|
|
|
return result
|
2020-03-23 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler interface.
|
|
|
|
func (t TransactionOutputRaw) MarshalJSON() ([]byte, error) {
|
2020-11-13 13:54:38 +00:00
|
|
|
output, err := json.Marshal(t.TransactionMetadata)
|
2020-03-23 14:31:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-18 08:47:54 +00:00
|
|
|
txBytes, err := json.Marshal(&t.Transaction)
|
2020-03-23 14:31:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to keep both transaction.Transaction and tranactionOutputRaw at the same level in json
|
|
|
|
// in order to match C# API, so there's no way to marshall Tx correctly with standard json.Marshaller tool.
|
|
|
|
if output[len(output)-1] != '}' || txBytes[0] != '{' {
|
|
|
|
return nil, errors.New("can't merge internal jsons")
|
|
|
|
}
|
|
|
|
output[len(output)-1] = ','
|
|
|
|
output = append(output, txBytes[1:]...)
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Marshaler interface.
|
|
|
|
func (t *TransactionOutputRaw) UnmarshalJSON(data []byte) error {
|
|
|
|
// As transaction.Transaction and tranactionOutputRaw are at the same level in json,
|
|
|
|
// do unmarshalling separately for both structs.
|
|
|
|
output := new(TransactionMetadata)
|
|
|
|
err := json.Unmarshal(data, output)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-13 13:54:38 +00:00
|
|
|
t.TransactionMetadata = *output
|
2020-06-18 08:47:54 +00:00
|
|
|
return json.Unmarshal(data, &t.Transaction)
|
2019-02-20 17:39:32 +00:00
|
|
|
}
|