-
Notifications
You must be signed in to change notification settings - Fork 3
/
msg.go
100 lines (84 loc) · 2.46 KB
/
msg.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
package pvm
import (
"errors"
ethtypes "github.com/ethereum/go-ethereum/core/types"
codectypes "github.com/oracleNetworkProtocol/plugchain-sdk-go/codec/types"
sdk "github.com/oracleNetworkProtocol/plugchain-sdk-go/types"
"github.com/oracleNetworkProtocol/plugchain-sdk-go/types/tx"
"math/big"
)
var (
_ sdk.Msg = &MsgEthereumTx{}
_ sdk.Tx = &MsgEthereumTx{}
_ codectypes.UnpackInterfacesMessage = &MsgEthereumTx{}
)
// AsTransaction creates an Ethereum Transaction type from the msg fields
func (msg MsgEthereumTx) AsTransaction() *ethtypes.Transaction {
txData, err := UnpackTxData(msg.Data)
if err != nil {
return nil
}
return ethtypes.NewTx(txData.AsEthereumData())
}
// GetMsgs returns a single MsgEthereumTx as an sdk.Msg.
func (msg *MsgEthereumTx) GetMsgs() []sdk.Msg {
return []sdk.Msg{msg}
}
func (msg MsgEthereumTx) GetGas() uint64 {
txData, err := UnpackTxData(msg.Data)
if err != nil {
return 0
}
return txData.GetGas()
}
// UnpackInterfaces implements UnpackInterfacesMesssage.UnpackInterfaces
func (msg MsgEthereumTx) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
return unpacker.UnpackAny(msg.Data, new(TxData))
}
// fromEthereumTx populates the message fields from the given ethereum transaction
func (msg *MsgEthereumTx) FromEthereumTx(tx *ethtypes.Transaction) error {
txData, err := NewTxDataFromTx(tx)
if err != nil {
return err
}
anyTxData, err := PackTxData(txData)
if err != nil {
return err
}
msg.Data = anyTxData
msg.Size_ = float64(tx.Size())
msg.Hash = tx.Hash().Hex()
return nil
}
//---------------------------------------------
func (msg *MsgEthereumTx) BuildTx(b sdk.TxBuilder, evmDenom string) (sdk.Tx, error) {
builder, ok := b.(tx.ExtensionOptionsTxBuilder)
if !ok {
return nil, errors.New("unsupported builder")
}
option, err := codectypes.NewAnyWithValue(&ExtensionOptionsEthereumTx{})
if err != nil {
return nil, err
}
txData, err := UnpackTxData(msg.Data)
if err != nil {
return nil, err
}
feeamt := GetFeeAmt(txData)
fees := sdk.Coins{{Denom: evmDenom, Amount: sdk.NewIntFromBigInt(feeamt)}}
builder.SetExtensionOptions(option)
err = builder.SetMsgs(msg)
if err != nil {
return nil, err
}
builder.SetFeeAmount(fees)
builder.SetGasLimit(msg.GetGas())
_tx := builder.GetTx()
return _tx, err
}
func GetFeeAmt(txData TxData) *big.Int {
effectiveTip := txData.GetGasPrice()
gasUsed := new(big.Int).SetUint64(txData.GetGas())
feeInit := new(big.Int).Mul(gasUsed, effectiveTip)
return feeInit
}