X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=toolbar%2Fapi_node%2Fnode_test.go;fp=toolbar%2Fapi_node%2Fnode_test.go;h=681bf0291aea2bbcd108b5f0d9f1a5980d01b69d;hb=3aec4988213a49fba026a673801659094b7c1280;hp=0000000000000000000000000000000000000000;hpb=030b7405ccdeb9c1952f516317f6c512ab1ec873;p=bytom%2Fvapor.git diff --git a/toolbar/api_node/node_test.go b/toolbar/api_node/node_test.go new file mode 100644 index 00000000..681bf029 --- /dev/null +++ b/toolbar/api_node/node_test.go @@ -0,0 +1,70 @@ +package apinode + +import ( + "encoding/json" + "testing" + + "github.com/vapor/consensus" + "github.com/vapor/errors" + "github.com/vapor/protocol/bc" +) + +func buildTxRequest(accountID string, outputs map[string]uint64) ([]byte, error) { + totalBTM := uint64(10000000) + actions := []interface{}{} + for address, amount := range outputs { + actions = append(actions, &ControlAddressAction{ + Address: address, + AssetAmount: &bc.AssetAmount{AssetId: consensus.BTMAssetID, Amount: amount}, + }) + totalBTM += amount + } + + actions = append(actions, &SpendAccountAction{ + AccountID: accountID, + AssetAmount: &bc.AssetAmount{AssetId: consensus.BTMAssetID, Amount: totalBTM}, + }) + payload, err := json.Marshal(&buildTxReq{Actions: actions}) + if err != nil { + return nil, errors.Wrap(err, "Marshal spend request") + } + + return payload, nil +} + +type args struct { + accountID string + outputs map[string]uint64 +} + +func TestBuildTxRequest(t *testing.T) { + cases := []struct { + args args + want string + }{ + { + args: args{ + accountID: "9bb77612-350e-4d53-81e2-525b28247ba5", + outputs: map[string]uint64{"sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf": 100}, + }, + 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}]}`, + }, + { + args: args{ + accountID: "9bb77612-350e-4d53-81e2-525b28247ba5", + outputs: map[string]uint64{"sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf": 100, "sp1qcgtxkhfzytul4lfttwex3skfqhm0tg6ms9da28": 200}, + }, + 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}]}`, + }, + } + + for i, c := range cases { + tx, err := buildTxRequest(c.args.accountID, c.args.outputs) + if err != nil { + t.Fatal(err) + } + if string(tx) != string(c.want) { + t.Fatal(i, string(tx)) + } + } +}