Skip to content

Commit 230cfa5

Browse files
Merge pull request #32 from everFinance/fix-submintTx
fix(): fix wallet submit tx api
2 parents 3d23d76 + 7f5cfb0 commit 230cfa5

8 files changed

+81
-50
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ func main() {
2626
panic(err)
2727
}
2828

29-
id, err := wallet.SendAR(
29+
tx, err := wallet.SendAR(
3030
//id, err := wallet.SendWinston(
3131
big.NewFloat(1.0), // AR amount
3232
{{target}}, // target address
3333
[]types.Tag{},
3434
)
3535

36-
fmt.Println(id, err) // {{id}}, nil
36+
fmt.Println(tx.ID, err)
3737
}
3838

3939
```
@@ -69,7 +69,7 @@ tx, err := wallet.SendDataSpeedUp(
6969
},
7070
},speedUp)
7171

72-
fmt.Println(id, err) // {{id}}, nil
72+
fmt.Println(tx.ID, err)
7373
```
7474
### Components
7575

@@ -334,7 +334,7 @@ resp, err := w.Client.BatchSendItemToBundler(items,"") // The second parameter i
334334

335335
#### Send Bundle Tx
336336
```go
337-
txId, err := w.SendBundleTx(bd.BundleBinary, arTxtags)
337+
tx, err := w.SendBundleTx(bd.BundleBinary, arTxtags)
338338
```
339339

340340
#### Get Bundle and Verify

client.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -178,27 +178,30 @@ func (c *Client) GetTransactionTags(id string) ([]types.Tag, error) {
178178
return tags, nil
179179
}
180180

181-
func (c *Client) GetTransactionData(id string, extension ...string) (body []byte, err error) {
181+
func (c *Client) GetTransactionData(id string, extension ...string) ([]byte, error) {
182182
urlPath := fmt.Sprintf("tx/%v/%v", id, "data")
183183
if extension != nil {
184184
urlPath = urlPath + "." + extension[0]
185185
}
186-
body, statusCode, err := c.httpGet(urlPath)
186+
data, statusCode, err := c.httpGet(urlPath)
187+
if err != nil {
188+
return nil, fmt.Errorf("httpGet error: %v", err)
189+
}
187190

188191
// When data is bigger than 12MiB statusCode == 400 NOTE: Data bigger than that has to be downloaded chunk by chunk.
189-
if statusCode == 400 {
190-
body, err = c.DownloadChunkData(id)
191-
return
192-
} else if statusCode == 200 {
193-
if len(body) == 0 {
192+
switch statusCode {
193+
case 200:
194+
if len(data) == 0 {
194195
return c.DownloadChunkData(id)
195196
}
196-
return body, nil
197-
} else if statusCode == 202 {
197+
return data, nil
198+
case 400:
199+
return c.DownloadChunkData(id)
200+
case 202:
198201
return nil, ErrPendingTx
199-
} else if statusCode == 404 {
202+
case 404:
200203
return nil, ErrNotFound
201-
} else {
204+
default:
202205
return nil, ErrBadGateway
203206
}
204207
}

example/api_example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func Test_Arq(t *testing.T) {
8888
t.Log(ids)
8989
}
9090

91-
func Test_SendFormat1Tx(t *testing.T) {
91+
func Test_SendFormatTx(t *testing.T) {
9292
// arNode := "https://arweave.net"
9393
// wallet, err := goar.NewWalletFromPath("./testKey.json", arNode)
9494
// assert.NoError(t, err)

example/bundle_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ func TestBundle_SendBundleTx(t *testing.T) {
6363
bd, err := utils.NewBundle(items...)
6464
assert.NoError(t, err)
6565

66-
txId, err := w.SendBundleTx(bd.BundleBinary, arTxtags)
66+
signedTx, err := w.SendBundleTx(bd.BundleBinary, arTxtags)
6767
assert.NoError(t, err)
68-
t.Log(txId)
68+
t.Log(signedTx.ID)
6969
}
7070

7171
func TestVerifyBundleItem(t *testing.T) {

example/local_data_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package example
2+
3+
import (
4+
"github.com/everFinance/goar"
5+
"github.com/everFinance/goar/types"
6+
"github.com/stretchr/testify/assert"
7+
"io/ioutil"
8+
"testing"
9+
)
10+
11+
func Test_SendData(t *testing.T) {
12+
arNode := "https://arweave.net"
13+
w, err := goar.NewWalletFromPath("./wallet/account1.json", arNode) // your wallet private key
14+
assert.NoError(t, err)
15+
16+
data, err := ioutil.ReadFile("/Users/local/Downloads/abc.jpeg") // local file path
17+
if err != nil {
18+
panic(err)
19+
}
20+
tags := []types.Tag{
21+
{Name: "xxxx", Value: "sssss"},
22+
{Name: "yyyyyy", Value: "kkkkkk"},
23+
}
24+
tx, err := w.SendDataSpeedUp(data, tags, 10)
25+
assert.NoError(t, err)
26+
t.Logf("tx hash: %s", tx.ID)
27+
}
28+
29+
func Test_LoadData(t *testing.T) {
30+
arCli := goar.NewClient("https://arweave.net")
31+
32+
arId := "r90Z_PuhD-louq6uzLTI-xWMfB5TzIti30o7QvW-6A4"
33+
data, err := arCli.GetTransactionData(arId)
34+
assert.NoError(t, err)
35+
t.Log(len(data))
36+
}

wallet.go

+14-18
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,22 @@ func (w *Wallet) Owner() string {
7272
return utils.Base64Encode(w.PubKey.N.Bytes())
7373
}
7474

75-
func (w *Wallet) SendAR(amount *big.Float, target string, tags []types.Tag) (id string, err error) {
75+
func (w *Wallet) SendAR(amount *big.Float, target string, tags []types.Tag) (types.Transaction, error) {
7676
return w.SendWinstonSpeedUp(utils.ARToWinston(amount), target, tags, 0)
7777
}
7878

79-
func (w *Wallet) SendARSpeedUp(amount *big.Float, target string, tags []types.Tag, speedFactor int64) (id string, err error) {
79+
func (w *Wallet) SendARSpeedUp(amount *big.Float, target string, tags []types.Tag, speedFactor int64) (types.Transaction, error) {
8080
return w.SendWinstonSpeedUp(utils.ARToWinston(amount), target, tags, speedFactor)
8181
}
8282

83-
func (w *Wallet) SendWinston(amount *big.Int, target string, tags []types.Tag) (id string, err error) {
83+
func (w *Wallet) SendWinston(amount *big.Int, target string, tags []types.Tag) (types.Transaction, error) {
8484
return w.SendWinstonSpeedUp(amount, target, tags, 0)
8585
}
8686

87-
func (w *Wallet) SendWinstonSpeedUp(amount *big.Int, target string, tags []types.Tag, speedFactor int64) (id string, err error) {
87+
func (w *Wallet) SendWinstonSpeedUp(amount *big.Int, target string, tags []types.Tag, speedFactor int64) (types.Transaction, error) {
8888
reward, err := w.Client.GetTransactionPrice(nil, &target)
8989
if err != nil {
90-
return
90+
return types.Transaction{}, err
9191
}
9292

9393
tx := &types.Transaction{
@@ -103,16 +103,16 @@ func (w *Wallet) SendWinstonSpeedUp(amount *big.Int, target string, tags []types
103103
return w.SendTransaction(tx)
104104
}
105105

106-
func (w *Wallet) SendData(data []byte, tags []types.Tag) (tx types.Transaction, err error) {
106+
func (w *Wallet) SendData(data []byte, tags []types.Tag) (types.Transaction, error) {
107107
return w.SendDataSpeedUp(data, tags, 0)
108108
}
109109

110110
// SendDataSpeedUp set speedFactor for speed up
111111
// eg: speedFactor = 10, reward = 1.1 * reward
112-
func (w *Wallet) SendDataSpeedUp(data []byte, tags []types.Tag, speedFactor int64) (signedTx types.Transaction, err error) {
112+
func (w *Wallet) SendDataSpeedUp(data []byte, tags []types.Tag, speedFactor int64) (types.Transaction, error) {
113113
reward, err := w.Client.GetTransactionPrice(data, nil)
114114
if err != nil {
115-
return
115+
return types.Transaction{}, err
116116
}
117117

118118
tx := &types.Transaction{
@@ -125,31 +125,27 @@ func (w *Wallet) SendDataSpeedUp(data []byte, tags []types.Tag, speedFactor int6
125125
Reward: fmt.Sprintf("%d", reward*(100+speedFactor)/100),
126126
}
127127

128-
_, err = w.SendTransaction(tx)
129-
signedTx = *tx
130-
return
128+
return w.SendTransaction(tx)
131129
}
132130

133131
// SendTransaction: if send success, should return pending
134-
func (w *Wallet) SendTransaction(tx *types.Transaction) (id string, err error) {
132+
func (w *Wallet) SendTransaction(tx *types.Transaction) (types.Transaction, error) {
135133
anchor, err := w.Client.GetTransactionAnchor()
136134
if err != nil {
137-
return
135+
return types.Transaction{}, err
138136
}
139137
tx.LastTx = anchor
140138
tx.Owner = w.Owner()
141139
if err = utils.SignTransaction(tx, w.PrvKey); err != nil {
142-
return
140+
return types.Transaction{}, err
143141
}
144142

145-
id = tx.ID
146-
147143
uploader, err := CreateUploader(w.Client, tx, nil)
148144
if err != nil {
149-
return
145+
return types.Transaction{}, err
150146
}
151147
err = uploader.Once()
152-
return
148+
return *tx, err
153149
}
154150

155151
func (w *Wallet) SendPst(contractId string, target string, qty *big.Int, customTags []types.Tag, speedFactor int64) (types.Transaction, error) {

wallet_bundle.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (w *Wallet) CreateAndSignBundleItem(data []byte, signatureType int, target
2222
return *bundleItem, nil
2323
}
2424

25-
func (w *Wallet) SendBundleTxSpeedUp(bundleBinary []byte, tags []types.Tag, txSpeed int64) (txId string, err error) {
25+
func (w *Wallet) SendBundleTxSpeedUp(bundleBinary []byte, tags []types.Tag, txSpeed int64) (types.Transaction, error) {
2626
bundleTags := []types.Tag{
2727
{Name: "Bundle-Format", Value: "binary"},
2828
{Name: "Bundle-Version", Value: "2.0.0"},
@@ -34,19 +34,15 @@ func (w *Wallet) SendBundleTxSpeedUp(bundleBinary []byte, tags []types.Tag, txSp
3434
}
3535
for _, tag := range tags {
3636
if _, ok := mmap[tag.Name]; ok {
37-
return "", errors.New("tags can not set bundleTags")
37+
return types.Transaction{}, errors.New("tags can not set bundleTags")
3838
}
3939
}
4040

4141
txTags := make([]types.Tag, 0)
4242
txTags = append(bundleTags, tags...)
43-
sentTx, err := w.SendDataSpeedUp(bundleBinary, txTags, txSpeed)
44-
if err != nil {
45-
return "", err
46-
}
47-
return sentTx.ID, err
43+
return w.SendDataSpeedUp(bundleBinary, txTags, txSpeed)
4844
}
4945

50-
func (w *Wallet) SendBundleTx(bundleBinary []byte, tags []types.Tag) (txId string, err error) {
46+
func (w *Wallet) SendBundleTx(bundleBinary []byte, tags []types.Tag) (types.Transaction, error) {
5147
return w.SendBundleTxSpeedUp(bundleBinary, tags, 0)
5248
}

wallet_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ func TestWallet_SendAR(t *testing.T) {
5656
// tags := []types.Tag{
5757
// {Name: "GOAR", Value: "sendAR"},
5858
// }
59-
// id, err := w.SendAR(amount, target, tags)
59+
// tx, err := w.SendAR(amount, target, tags)
6060
// assert.NoError(t, err)
61-
// t.Logf("tx hash: %s \n", id)
61+
// t.Logf("tx hash: %s \n", tx.ID)
6262
}
6363

6464
// test send small size file
@@ -75,9 +75,9 @@ func TestWallet_SendDataSpeedUp01(t *testing.T) {
7575
// tags := []types.Tag{
7676
// {Name: "GOAR", Value: "SMDT"},
7777
// }
78-
// id, err := w.SendDataSpeedUp(data, tags, 50)
78+
// tx, err := w.SendDataSpeedUp(data, tags, 50)
7979
// assert.NoError(t, err)
80-
// t.Logf("tx hash: %s", id)
80+
// t.Logf("tx hash: %s", tx.ID)
8181
}
8282

8383
// test send big size file
@@ -95,9 +95,9 @@ func TestWallet_SendDataSpeedUp02(t *testing.T) {
9595
// {Name: "Sender", Value: "Jie"},
9696
// {Name: "Data-Introduce", Value: "Happy anniversary, my google and dearest! I‘m so grateful to have you in my life. I love you to infinity and beyond! (⁎⁍̴̛ᴗ⁍̴̛⁎)"},
9797
// }
98-
// id, err := w.SendDataSpeedUp(data, tags, 10)
98+
// tx, err := w.SendDataSpeedUp(data, tags, 10)
9999
// assert.NoError(t, err)
100-
// t.Logf("tx hash: %s", id)
100+
// t.Logf("tx hash: %s", tx.ID)
101101
}
102102

103103
func Test_SendPstTransfer(t *testing.T) {

0 commit comments

Comments
 (0)