Skip to content

Commit 6ec6cb3

Browse files
committed
[dev] transaction module
1 parent 3c68361 commit 6ec6cb3

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

response.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,10 @@ type ContractSource struct {
107107
Library string `json:"Library"`
108108
SwarmSource string `json:"SwarmSource"`
109109
}
110+
111+
// ExecutionStatus holds info from query for transaction execution status
112+
type ExecutionStatus struct {
113+
// 0 = pass, 1 = error
114+
IsError int `json:"isError,string"`
115+
ErrDescription string `json:"errDescription"`
116+
}

transaction.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2018 LI Zhennan
3+
*
4+
* Use of this work is governed by a MIT License.
5+
* You may find a license copy in project root.
6+
*/
7+
8+
package etherscan
9+
10+
import "errors"
11+
12+
// ErrPreByzantiumTx transaction before 4,370,000 does not support receipt status check
13+
var ErrPreByzantiumTx = errors.New("pre-byzantium transaction does not support receipt status check")
14+
15+
// ExecutionStatus checks contract execution status (if there was an error during contract execution)
16+
// note on IsError: 0 = pass, 1 = error
17+
func (c *Client) ExecutionStatus(txHash string) (status ExecutionStatus, err error) {
18+
param := M{
19+
"txhash": txHash,
20+
}
21+
22+
err = c.call("transaction", "getstatus", param, &status)
23+
return
24+
}
25+
26+
// ReceiptStatus checks transaction receipt status
27+
// only applicable for post byzantium fork transactions, i.e. after block 4,370,000
28+
// An special err ErrPreByzantiumTx raises for the transaction before byzantium fork.
29+
// Note: receiptStatus: 0 = Fail, 1 = Pass.
30+
func (c *Client) ReceiptStatus(txHash string) (receiptStatus int, err error) {
31+
param := M{
32+
"txhash": txHash,
33+
}
34+
35+
var rawStatus = struct {
36+
Status string `json:"status"`
37+
}{}
38+
39+
err = c.call("transaction", "gettxreceiptstatus", param, &rawStatus)
40+
if err != nil {
41+
return
42+
}
43+
44+
switch rawStatus.Status {
45+
case "0":
46+
receiptStatus = 0
47+
case "1":
48+
receiptStatus = 1
49+
default:
50+
receiptStatus = -1
51+
err = ErrPreByzantiumTx
52+
}
53+
54+
return
55+
}

transaction_e2e_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2018 LI Zhennan
3+
*
4+
* Use of this work is governed by a MIT License.
5+
* You may find a license copy in project root.
6+
*/
7+
8+
package etherscan
9+
10+
import (
11+
"testing"
12+
)
13+
14+
func TestClient_ExecutionStatus(t *testing.T) {
15+
var err error
16+
17+
// bad execution
18+
bad, err := api.ExecutionStatus("0x15f8e5ea1079d9a0bb04a4c58ae5fe7654b5b2b4463375ff7ffb490aa0032f3a")
19+
noError(t, err, "api.ExecutionStatus")
20+
if bad.IsError != 1 && bad.ErrDescription != "Bad jump destination" {
21+
t.Errorf("api.ExecutionStatus not working, got\n%+v", bad)
22+
}
23+
24+
// good execution
25+
good, err := api.ExecutionStatus("0xe8253035f1a1e93be24f43a3592a2c6cdbe3360e6f738ff40d46305252b44f5c")
26+
noError(t, err, "api.ExecutionStatus")
27+
if good.IsError != 0 && good.ErrDescription != "" {
28+
t.Errorf("api.ExecutionStatus not working, got\n%+v", good)
29+
}
30+
}
31+
32+
func TestClient_ReceiptStatus(t *testing.T) {
33+
var err error
34+
35+
// bad execution
36+
bad, err := api.ReceiptStatus("0xe7bbbeb43cf863e20ec865021d63005149c133d7822e8edc1e6cb746d6728d4e")
37+
noError(t, err, "api.ReceiptStatus")
38+
if bad != 0 {
39+
t.Errorf("api.ExecutionStatus not working, got %v, want 0", bad)
40+
}
41+
42+
// good execution
43+
good, err := api.ReceiptStatus("0xe8253035f1a1e93be24f43a3592a2c6cdbe3360e6f738ff40d46305252b44f5c")
44+
noError(t, err, "api.ReceiptStatus")
45+
if good != 1 {
46+
t.Errorf("api.ExecutionStatus not working, got %v, want 1", good)
47+
}
48+
49+
// tx before byzantium fork
50+
before, err := api.ReceiptStatus("0x836b403cc1516eb1337c151ff3660c3ebd528d850e6ac20a75652c705ea769f4")
51+
if err != ErrPreByzantiumTx {
52+
t.Errorf("api.ReceiptStatus does not throw error for tx before byzantium fork")
53+
}
54+
if before != -1 {
55+
t.Errorf("api.ExecutionStatus not working, got %v, want -1", before)
56+
}
57+
}

0 commit comments

Comments
 (0)