-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththeta_utils.go
278 lines (260 loc) · 9.68 KB
/
theta_utils.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"strings"
"github.com/thetatoken/theta/blockchain"
"github.com/thetatoken/theta/common"
tcommon "github.com/thetatoken/theta/common"
"github.com/thetatoken/theta/common/hexutil"
"github.com/thetatoken/theta/core"
"github.com/thetatoken/theta/ledger/types"
"github.com/thetatoken/theta/rpc"
"github.com/thetatoken/thetasubchain/eth/abi"
rpcc "github.com/ybbus/jsonrpc"
)
type GetStatusArgs struct{}
type GetStatusResult struct {
Address string `json:"address"`
ChainID string `json:"chain_id"`
PeerID string `json:"peer_id"`
LatestFinalizedBlockHash common.Hash `json:"latest_finalized_block_hash"`
LatestFinalizedBlockHeight common.JSONUint64 `json:"latest_finalized_block_height"`
LatestFinalizedBlockTime *common.JSONBig `json:"latest_finalized_block_time"`
LatestFinalizedBlockEpoch common.JSONUint64 `json:"latest_finalized_block_epoch"`
CurrentEpoch common.JSONUint64 `json:"current_epoch"`
CurrentHeight common.JSONUint64 `json:"current_height"`
CurrentTime *common.JSONBig `json:"current_time"`
Syncing bool `json:"syncing"`
GenesisBlockHash common.Hash `json:"genesis_block_hash"`
SnapshotBlockHeight common.JSONUint64 `json:"snapshot_block_height"`
SnapshotBlockHash common.Hash `json:"snapshot_block_hash"`
}
func HandleThetaRPCResponse(rpcRes *rpcc.RPCResponse, rpcErr error, parse func(jsonBytes []byte) (interface{}, error)) (result interface{}, err error) {
if rpcErr != nil {
return nil, fmt.Errorf("failed to get theta RPC response: %v", rpcErr)
}
if rpcRes.Error != nil {
return nil, fmt.Errorf("theta RPC returns an error: %v", rpcRes.Error)
}
var jsonBytes []byte
jsonBytes, err = json.MarshalIndent(rpcRes.Result, "", " ")
if err != nil {
return nil, fmt.Errorf("failed to parse theta RPC response: %v, %s", err, string(jsonBytes))
}
//logger.Infof("HandleThetaRPCResponse, jsonBytes: %v", string(jsonBytes))
result, err = parse(jsonBytes)
if err != nil {
logger.Warn("Failed to parse theta RPC response: " + err.Error())
}
return
}
type EthGetBlockResult struct {
Height hexutil.Uint64 `json:"number"`
Hash common.Hash `json:"hash"`
Parent common.Hash `json:"parentHash"`
Timestamp hexutil.Uint64 `json:"timestamp"`
Proposer common.Address `json:"miner"`
TxHash common.Hash `json:"transactionsRoot"`
StateHash common.Hash `json:"stateRoot"`
ReiceptHash common.Hash `json:"receiptsRoot"`
Nonce string `json:"nonce"`
Sha3Uncles common.Hash `json:"sha3Uncles"`
LogsBloom string `json:"logsBloom"`
Difficulty hexutil.Uint64 `json:"difficulty"`
TotalDifficulty hexutil.Uint64 `json:"totalDifficulty"`
Size hexutil.Uint64 `json:"size"`
GasLimit hexutil.Uint64 `json:"gasLimit"`
GasUsed hexutil.Uint64 `json:"gasUsed"`
ExtraData string `json:"extraData"`
Uncles []common.Hash `json:"uncles"`
Transactions []interface{} `json:"transactions"`
}
type ThetaGetBlockResult struct {
*ThetaGetBlockResultInner
}
type ThetaGetBlocksResult []*ThetaGetBlockResultInner
type ThetaGetBlockResultInner struct {
ChainID string `json:"chain_id"`
Epoch common.JSONUint64 `json:"epoch"`
Height common.JSONUint64 `json:"height"`
Parent common.Hash `json:"parent"`
TxHash common.Hash `json:"transactions_hash"`
StateHash common.Hash `json:"state_hash"`
Timestamp *common.JSONBig `json:"timestamp"`
Proposer common.Address `json:"proposer"`
HCC core.CommitCertificate `json:"hcc"`
GuardianVotes *core.AggregatedVotes `json:"guardian_votes"`
EliteEdgeNodeVotes *core.AggregatedEENVotes `json:"elite_edge_node_votes"`
Children []common.Hash `json:"children"`
Status core.BlockStatus `json:"status"`
Hash common.Hash `json:"hash"`
Txs []rpc.Tx `json:"transactions"`
}
type LogData struct {
Address common.Address `json:"address" gencodec:"required"`
// list of topics provided by the contract.
Topics []common.Hash `json:"topics" gencodec:"required"`
// supplied by the contract, usually ABI-encoded
Data []byte `json:"data" gencodec:"required"`
}
type RPCResult struct {
Result []LogData `json:"logs"`
Address string `json:"contractAddress"`
}
type GetBlockByHeightArgs struct {
Height common.JSONUint64 `json:"height"`
IncludeEthTxHashes bool `json:"include_eth_tx_hashes"`
}
type Tx struct {
types.Tx `json:"raw"`
Type byte `json:"type"`
Hash common.Hash `json:"hash"`
Receipt *blockchain.TxReceiptEntry `json:"receipt"`
BalanceChanges *blockchain.TxBalanceChangesEntry `json:"balance_changes"`
}
type TxTmp struct {
Tx json.RawMessage `json:"raw"`
Type byte `json:"type"`
Hash tcommon.Hash `json:"hash"`
}
type GetAccountArgs struct {
Name string `json:"name"`
Address string `json:"address"`
Height common.JSONUint64 `json:"height"`
Preview bool `json:"preview"` // preview the account balance from the ScreenedView
}
type GetAccountResult struct {
*types.Account
Address string `json:"address"`
}
type EthGetTransactionResult struct {
BlockHash tcommon.Hash `json:"blockHash"`
BlockHeight hexutil.Uint64 `json:"blockNumber"`
From tcommon.Address `json:"from"`
To *tcommon.Address `json:"to"`
Gas hexutil.Uint64 `json:"gas"`
GasPrice string `json:"gasPrice"`
TxHash tcommon.Hash `json:"hash"`
Nonce hexutil.Uint64 `json:"nonce"`
Input string `json:"input"`
TransactionIndex hexutil.Uint64 `json:"transactionIndex"`
Value string `json:"value"`
V hexutil.Uint64 `json:"v"` //ECDSA recovery id
R tcommon.Hash `json:"r"` //ECDSA signature r
S tcommon.Hash `json:"s"` //ECDSA signature s
}
type GetTransactionResult struct {
BlockHash common.Hash `json:"block_hash"`
BlockHeight common.JSONUint64 `json:"block_height"`
Status TxStatus `json:"status"`
TxHash common.Hash `json:"hash"`
Type byte `json:"type"`
Tx types.Tx `json:"transaction"`
Receipt *blockchain.TxReceiptEntry `json:"receipt"`
BalanceChanges *blockchain.TxBalanceChangesEntry `json:"blance_changes"`
}
type TxStatus string
type EthLogObj struct {
Address tcommon.Address `json:"address"`
BlockHash tcommon.Hash `json:"blockHash"`
BlockHeight hexutil.Uint64 `json:"blockNumber"`
LogIndex hexutil.Uint64 `json:"logIndex"`
Topics []tcommon.Hash `json:"topics"`
TxHash tcommon.Hash `json:"transactionHash"`
TransactionIndex hexutil.Uint64 `json:"transactionIndex"`
Data string `json:"data"`
Type string `json:"type"`
//Removed bool `json:"removed"`
}
func ThetaLogToEthLog(log *types.Log) EthLogObj {
result := EthLogObj{}
result.Address = log.Address
result.Data = "0x" + hex.EncodeToString(log.Data)
result.Type = "mined"
result.Topics = log.Topics
return result
}
type EthGetReceiptResult struct {
BlockHash tcommon.Hash `json:"blockHash"`
BlockHeight hexutil.Uint64 `json:"blockNumber"`
TxHash tcommon.Hash `json:"transactionHash"`
TransactionIndex hexutil.Uint64 `json:"transactionIndex"`
ContractAddress tcommon.Address `json:"contractAddress"`
From tcommon.Address `json:"from"`
To tcommon.Address `json:"to"`
GasUsed hexutil.Uint64 `json:"gasUsed"`
CumulativeGasUsed hexutil.Uint64 `json:"cumulativeGasUsed"`
Logs []EthLogObj `json:"logs"`
LogsBloom string `json:"logsBloom"`
Status hexutil.Uint64 `json:"status"`
}
const RawABI = `
[
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "string",
"name": "denom",
"type": "string"
},
{
"indexed": false,
"internalType": "address",
"name": "targetChainVoucherReceiver",
"type": "address"
},
{
"indexed": false,
"internalType": "address",
"name": "voucherContact",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "mintedAmount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "sourceChainTokenLockNonce",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "voucherMintNonce",
"type": "uint256"
}
],
"name": "TNT20VoucherMinted",
"type": "event"
}
]`
func Resolve(data []byte) big.Int {
contractAbi, err := abi.JSON(strings.NewReader(RawABI))
if err != nil {
fmt.Println(err)
}
type TransferEvt struct {
Denom string
TargetChainVoucherReceiver common.Address
VoucherContact common.Address
MintedAmount *big.Int
SourceChainTokenLockNonce *big.Int
VoucherMintNonce *big.Int
}
var event TransferEvt
h := data
err = contractAbi.UnpackIntoInterface(&event, "TNT20VoucherMinted", h)
if err != nil {
fmt.Println(err)
}
return *event.VoucherMintNonce
}