-
Notifications
You must be signed in to change notification settings - Fork 113
/
transfer.go
128 lines (114 loc) · 3.38 KB
/
transfer.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
package transfer
import (
"context"
"fmt"
"github.com/iost-official/go-iost/sdk"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
"github.com/iost-official/go-iost/test/performance/call"
"github.com/iost-official/go-iost/account"
"github.com/iost-official/go-iost/common"
"github.com/iost-official/go-iost/core/contract"
"github.com/iost-official/go-iost/core/tx"
"github.com/iost-official/go-iost/crypto"
"github.com/iost-official/go-iost/rpc/pb"
)
func init() {
transfer := newTransferHandler()
call.Register("transfer", transfer)
iostSDK.SetChainID(chainID)
}
const (
cache = "transfer.cache"
sep = ","
chainID uint32 = 1024
)
var rootKey = "2yquS3ySrGWPEKywCPzX4RTJugqRh7kJSo5aehsLYPEWkUxBWA39oMrZ7ZxuM4fgyXYs2cPwh5n8aNNpH5x2VyK1"
var iostSDK = sdk.NewIOSTDevSDK()
type transferHandler struct {
testID string
contractID string
}
func newTransferHandler() *transferHandler {
ret := &transferHandler{}
ret.readCache()
return ret
}
func (t *transferHandler) readCache() {
content, err := ioutil.ReadFile(cache)
if err == nil {
strs := strings.Split(string(content), sep)
if len(strs) > 1 {
t.testID, t.contractID = strs[0], strs[1]
}
}
}
func (t *transferHandler) writeCache() {
err := ioutil.WriteFile(cache, []byte(t.testID+sep+t.contractID), os.ModePerm)
if err != nil {
fmt.Println("write cache error: ", err)
panic(err)
}
}
// Prepare ...
func (t *transferHandler) Prepare() error {
acc, _ := account.NewKeyPair(common.Base58Decode(rootKey), crypto.Ed25519)
codePath := os.Getenv("GOPATH") + "/src/github.com/iost-official/go-iost/test/performance/handles/transfer/transfer.js"
abiPath := codePath + ".abi"
client := call.GetClient(0)
iostSDK.SetServer(client.Addr())
iostSDK.SetAccount("admin", acc)
iostSDK.SetTxInfo(500000.0, 1.0, 90, 0, nil)
iostSDK.SetCheckResult(true, 3, 10)
testKp, err := account.NewKeyPair(nil, crypto.Ed25519)
if err != nil {
return err
}
testID := "i" + strconv.FormatInt(time.Now().Unix(), 10)
k := testKp.ReadablePubkey()
_, err = iostSDK.CreateNewAccount(testID, k, k, 1000000, 10000, 100000)
if err != nil {
return err
}
err = iostSDK.PledgeForGasAndRAM(1500000, 0)
if err != nil {
return err
}
iostSDK.SetAccount(testID, testKp)
_, txHash, err := iostSDK.PublishContract(codePath, abiPath, "", false, "")
if err != nil {
return err
}
time.Sleep(time.Duration(30) * time.Second)
resp, err := client.GetTxReceiptByTxHash(context.Background(), &rpcpb.TxHashRequest{Hash: txHash})
if err != nil {
return err
}
if tx.StatusCode(resp.StatusCode) != tx.Success {
return fmt.Errorf("publish contract fail " + (resp.String()))
}
t.testID = testID
t.contractID = "Contract" + txHash
t.writeCache()
return nil
}
// Run ...
func (t *transferHandler) Run(i int) (interface{}, error) {
action := tx.NewAction(t.contractID, "transfer", fmt.Sprintf(`["admin","%v",1]`, t.testID))
acc, _ := account.NewKeyPair(common.Base58Decode(rootKey), crypto.Ed25519)
trx := tx.NewTx([]*tx.Action{action}, []string{}, 6000000, 100, time.Now().Add(time.Second*time.Duration(10000)).UnixNano(), 0, chainID)
trx.AmountLimit = []*contract.Amount{{Token: "*", Val: "unlimited"}}
stx, err := tx.SignTx(trx, "admin", []*account.KeyPair{acc})
if err != nil {
return nil, fmt.Errorf("sign tx error: %v", err)
}
var txHash string
txHash, err = call.SendTx(stx, i)
if err != nil {
return nil, err
}
return txHash, nil
}