-
Notifications
You must be signed in to change notification settings - Fork 2
/
construction_submit.go
110 lines (100 loc) · 4.15 KB
/
construction_submit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package services
import (
"context"
"fmt"
hmyTypes "github.com/PositionExchange/posichain/core/types"
"github.com/PositionExchange/posichain/rosetta/common"
stakingTypes "github.com/PositionExchange/posichain/staking/types"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/pkg/errors"
)
// ConstructionHash implements the /construction/hash endpoint.
func (s *ConstructAPI) ConstructionHash(
ctx context.Context, request *types.ConstructionHashRequest,
) (*types.TransactionIdentifierResponse, *types.Error) {
if err := assertValidNetworkIdentifier(request.NetworkIdentifier, s.hmy.ShardID); err != nil {
return nil, err
}
_, tx, rosettaError := unpackWrappedTransactionFromString(request.SignedTransaction, true)
if rosettaError != nil {
return nil, rosettaError
}
if tx == nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": "nil transaction",
})
}
if tx.ShardID() != s.hmy.ShardID {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": fmt.Sprintf("transaction is for shard %v != shard %v", tx.ShardID(), s.hmy.ShardID),
})
}
return &types.TransactionIdentifierResponse{
TransactionIdentifier: &types.TransactionIdentifier{Hash: tx.Hash().String()},
}, nil
}
// ConstructionSubmit implements the /construction/submit endpoint.
func (s *ConstructAPI) ConstructionSubmit(
ctx context.Context, request *types.ConstructionSubmitRequest,
) (*types.TransactionIdentifierResponse, *types.Error) {
if err := assertValidNetworkIdentifier(request.NetworkIdentifier, s.hmy.ShardID); err != nil {
return nil, err
}
wrappedTransaction, tx, rosettaError := unpackWrappedTransactionFromString(request.SignedTransaction, true)
if rosettaError != nil {
return nil, rosettaError
}
if wrappedTransaction == nil || tx == nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": "nil wrapped transaction or nil unwrapped transaction",
})
}
if tx.ShardID() != s.hmy.ShardID {
return nil, common.NewError(common.StakingTransactionSubmissionError, map[string]interface{}{
"message": fmt.Sprintf("transaction is for shard %v != shard %v", tx.ShardID(), s.hmy.ShardID),
})
}
wrappedSenderAddress, err := getAddress(wrappedTransaction.From)
if err != nil {
return nil, common.NewError(common.StakingTransactionSubmissionError, map[string]interface{}{
"message": errors.WithMessage(err, "unable to get address from wrapped transaction"),
})
}
var signedTx hmyTypes.PoolTransaction
if stakingTx, ok := tx.(*stakingTypes.StakingTransaction); ok && wrappedTransaction.IsStaking {
signedTx = stakingTx
} else if plainTx, ok := tx.(*hmyTypes.Transaction); ok && !wrappedTransaction.IsStaking {
signedTx = plainTx
} else {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": "invalid/inconsistent type or unknown transaction type stored in wrapped transaction",
})
}
txSenderAddress, err := signedTx.SenderAddress()
if err != nil {
return nil, common.NewError(common.StakingTransactionSubmissionError, map[string]interface{}{
"message": errors.WithMessage(err, "unable to get sender address from transaction").Error(),
})
}
if wrappedSenderAddress != txSenderAddress {
return nil, common.NewError(common.StakingTransactionSubmissionError, map[string]interface{}{
"message": "transaction sender address does not match wrapped transaction sender address",
})
}
if wrappedTransaction.IsStaking {
if err := s.hmy.SendStakingTx(ctx, signedTx.(*stakingTypes.StakingTransaction)); err != nil {
return nil, common.NewError(common.StakingTransactionSubmissionError, map[string]interface{}{
"message": fmt.Sprintf("error is: %s, gas price is: %s, gas limit is: %d", err.Error(), signedTx.GasPrice().String(), signedTx.GasLimit()),
})
}
} else {
if err := s.hmy.SendTx(ctx, signedTx.(*hmyTypes.Transaction)); err != nil {
return nil, common.NewError(common.TransactionSubmissionError, map[string]interface{}{
"message": err.Error(),
})
}
}
return &types.TransactionIdentifierResponse{
TransactionIdentifier: &types.TransactionIdentifier{Hash: tx.Hash().String()},
}, nil
}