OSDN Git Service

Voter reward (#344)
[bytom/vapor.git] / toolbar / apinode / node_test.go
1 package apinode
2
3 import (
4         "encoding/json"
5         "testing"
6
7         "github.com/vapor/consensus"
8         "github.com/vapor/errors"
9         "github.com/vapor/protocol/bc"
10 )
11
12 func buildTxRequest(accountID string, outputs map[string]uint64) ([]byte, error) {
13         totalBTM := uint64(10000000)
14         actions := []interface{}{}
15         for address, amount := range outputs {
16                 actions = append(actions, &ControlAddressAction{
17                         Address:     address,
18                         AssetAmount: &bc.AssetAmount{AssetId: consensus.BTMAssetID, Amount: amount},
19                 })
20                 totalBTM += amount
21         }
22
23         actions = append(actions, &SpendAccountAction{
24                 AccountID:   accountID,
25                 AssetAmount: &bc.AssetAmount{AssetId: consensus.BTMAssetID, Amount: totalBTM},
26         })
27         payload, err := json.Marshal(&buildTxReq{Actions: actions})
28         if err != nil {
29                 return nil, errors.Wrap(err, "Marshal spend request")
30         }
31
32         return payload, nil
33 }
34
35 type args struct {
36         accountID string
37         outputs   map[string]uint64
38 }
39
40 func TestBuildTxRequest(t *testing.T) {
41         cases := []struct {
42                 args args
43                 want string
44         }{
45                 {
46                         args: args{
47                                 accountID: "9bb77612-350e-4d53-81e2-525b28247ba5",
48                                 outputs:   map[string]uint64{"sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf": 100},
49                         },
50                         want: `{"actions":[{"type":"control_address","address":"sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":100},{"type":"spend_account","account_id":"9bb77612-350e-4d53-81e2-525b28247ba5","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":10000100}]}`,
51                 },
52                 {
53                         args: args{
54                                 accountID: "9bb77612-350e-4d53-81e2-525b28247ba5",
55                                 outputs:   map[string]uint64{"sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf": 100, "sp1qcgtxkhfzytul4lfttwex3skfqhm0tg6ms9da28": 200},
56                         },
57                         want: `{"actions":[{"type":"control_address","address":"sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":100},{"type":"control_address","address":"sp1qcgtxkhfzytul4lfttwex3skfqhm0tg6ms9da28","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":200},{"type":"spend_account","account_id":"9bb77612-350e-4d53-81e2-525b28247ba5","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":10000300}]}`,
58                 },
59         }
60
61         for i, c := range cases {
62                 tx, err := buildTxRequest(c.args.accountID, c.args.outputs)
63                 if err != nil {
64                         t.Fatal(err)
65                 }
66                 if string(tx) != string(c.want) {
67                         t.Fatal(i, string(tx))
68                 }
69         }
70 }