Skip to content

Commit 7e435ba

Browse files
authored
Merge pull request #18 from everFinance/refactor
feat(): refactor get transaction api
2 parents 31b82db + 6f3ed34 commit 7e435ba

File tree

6 files changed

+60
-33
lines changed

6 files changed

+60
-33
lines changed

client.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,15 @@ func (c *Client) GetInfo() (info *types.NetworkInfo, err error) {
5151
return
5252
}
5353

54-
// status: Pending/Invalid hash/overspend
55-
func (c *Client) GetTransactionByID(id string) (tx *types.Transaction, status string, code int, err error) {
54+
// GetTransactionByID status: Pending/Invalid hash/overspend
55+
func (c *Client) GetTransactionByID(id string) (tx *types.Transaction, err error) {
5656
body, statusCode, err := c.httpGet(fmt.Sprintf("tx/%s", id))
5757
if err != nil {
5858
return
5959
}
6060

61-
code = statusCode
6261
if statusCode != 200 {
63-
status = string(body)
62+
err = errors.New(string(body))
6463
return
6564
}
6665

@@ -71,15 +70,19 @@ func (c *Client) GetTransactionByID(id string) (tx *types.Transaction, status st
7170
}
7271

7372
// GetTransactionStatus
74-
func (c *Client) GetTransactionStatus(id string) (status string, code int, err error) {
73+
func (c *Client) GetTransactionStatus(id string) (*types.TxStatus, error) {
7574
body, code, err := c.httpGet(fmt.Sprintf("tx/%s/status", id))
76-
if code == 200 {
77-
return types.SuccessTxStatus, code, nil
78-
} else if code == 202 {
79-
return types.PendingTxStatus, code, nil
80-
} else {
81-
return string(body), code, err
75+
if err != nil {
76+
return nil, err
8277
}
78+
if code != 200 {
79+
return nil, errors.New(string(body))
80+
}
81+
82+
// json unmarshal
83+
txStatus := &types.TxStatus{}
84+
err = json.Unmarshal(body, txStatus)
85+
return txStatus, err
8386
}
8487

8588
func (c *Client) GetTransactionField(id string, field string) (f string, err error) {

client_test.go

+33-2
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,42 @@ func TestClient_VerifyTx(t *testing.T) {
134134
// txId := "XOzxw5kaYJrt9Vljj23pA5_6b63kY2ydQ0lPfnhksMA"
135135
txId := "_fVj-WyEtXV3URXlNkSnHVGupl7_DM1UWZ64WMdhPkU"
136136
client := NewClient("https://arweave.net")
137-
tx, status, code, err := client.GetTransactionByID(txId)
137+
tx, err := client.GetTransactionByID(txId)
138138
assert.NoError(t, err)
139-
t.Log(status, code)
140139
t.Log(tx.Format)
141140
t.Log(utils.TagsDecode(tx.Tags))
142141
err = utils.VerifyTransaction(*tx)
143142
assert.NoError(t, err)
144143
}
144+
145+
func TestGetTransaction(t *testing.T) {
146+
arNode := "https://arweave.net"
147+
cli := NewClient(arNode)
148+
149+
// on chain tx
150+
txId := "ggt-x5Q_niHifdNzMxZrhiibKf0KQ-cJun0UIBBa-yA"
151+
txStatus, err := cli.GetTransactionStatus(txId)
152+
assert.NoError(t, err)
153+
assert.Equal(t, 575660, txStatus.BlockHeight)
154+
tx, err := cli.GetTransactionByID(txId)
155+
assert.NoError(t, err)
156+
assert.Equal(t, "0pu7-Otb-AH6SSSX_rfUmpTkwh3Nmhpztd_IT8nYXDwBE6P3B-eJSBuaTBeLypx4", tx.LastTx)
157+
158+
// not exist tx
159+
txId = "KPlEyCrcs2rDHBFn2f0UUn2NZQKfawGb_EnBfip8ayA"
160+
txStatus, err = cli.GetTransactionStatus(txId)
161+
assert.Equal(t, `{"status":404,"error":"Not Found"}`, err.Error())
162+
assert.Nil(t, txStatus)
163+
tx, err = cli.GetTransactionByID(txId)
164+
assert.Equal(t, `{"status":404,"error":"Not Found"}`, err.Error())
165+
assert.Nil(t, tx)
166+
167+
// // pending tx
168+
// txId = "muANv_lsyZKC5C8fTxQaC2dCCyGDao8z35ECuGdIBP8" // need send a new tx create pending status
169+
// txStatus, err = cli.GetTransactionStatus(txId)
170+
// assert.Equal(t, "Pending",err.Error())
171+
// assert.Nil(t, txStatus)
172+
// tx, err = cli.GetTransactionByID(txId)
173+
// assert.Equal(t, "Pending",err.Error())
174+
// assert.Nil(t, txStatus)
175+
}

example/api_example_test.go

+1-13
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ func Test_Client(t *testing.T) {
1919
t.Logf("%v", nodeInfo)
2020

2121
// 2. full transaction via Id
22-
tx, state, _, err := c.GetTransactionByID(txId)
22+
tx, err := c.GetTransactionByID(txId)
2323
assert.NoError(t, err)
24-
t.Logf("state: %s", state)
2524
t.Log(tx)
2625

2726
// 3. get transaction field by id
@@ -53,17 +52,6 @@ func Test_Client(t *testing.T) {
5352

5453
}
5554

56-
func TestGetTransactionsStatus(t *testing.T) {
57-
arNode := "https://arweave.net"
58-
wallet, err := goar.NewWalletFromPath("./testKey.json", arNode)
59-
assert.NoError(t, err)
60-
61-
status, code, err := wallet.Client.GetTransactionStatus("ggt-x5Q_niHifdNzMxZrhiibKf0KQ-cJun0UIBBa-yA")
62-
assert.Equal(t, "Success", status)
63-
assert.Equal(t, 200, code)
64-
assert.NoError(t, err)
65-
}
66-
6755
func Test_Arq(t *testing.T) {
6856
arqStr := `{
6957
"op": "and",

example/chunks_tx_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,13 @@ func Test_RetryUploadDataByTxId(t *testing.T) {
109109

110110
// 2. watcher this tx from ar chain and must be sure the tx on chain
111111
getTxOnchain := func() bool {
112-
_, body, statusCode, _ := wallet.Client.GetTransactionByID(tx.ID)
113-
if statusCode/100 == 2 && string(body) == "Pending" {
112+
_, err := wallet.Client.GetTransactionByID(tx.ID)
113+
if err != nil {
114114
t.Log("watcher tx status: ", string(body))
115115
return false
116-
} else if statusCode/100 == 2 {
116+
} else {
117117
return true
118118
}
119-
return false
120119
}
121120

122121
// watcher tx

types/types.go

+6
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ type TransactionOffset struct {
4040
Size string `json:"size"`
4141
Offset string `json:"offset"`
4242
}
43+
44+
type TxStatus struct {
45+
BlockHeight int `json:"block_height"`
46+
BlockIndepHash string `json:"block_indep_hash"`
47+
NumberOfConfirmations int `json:"number_of_confirmations"`
48+
}

uploader.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ func (tt *TransactionUploader) FromSerialized(serialized *SerializedUploader, da
262262
* @param data
263263
*/
264264
func (tt *TransactionUploader) FromTransactionId(id string) (*SerializedUploader, error) {
265-
tx, state, code, err := tt.Client.GetTransactionByID(id)
266-
if err != nil || state == "Pending" || code/100 != 2 {
267-
return nil, errors.New(fmt.Sprintf("Tx %s not found: %d, error: %v", id, code, err))
265+
tx, err := tt.Client.GetTransactionByID(id)
266+
if err != nil {
267+
return nil, errors.New(fmt.Sprintf("Tx %s not found; error: %v", id, err))
268268
}
269269
transaction := tx
270270
transaction.Data = ""

0 commit comments

Comments
 (0)