/
transform.go
65 lines (58 loc) · 1.33 KB
/
transform.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
package coin
import (
sdk "github.com/cosmos/cosmos-sdk"
"github.com/cosmos/cosmos-sdk/errors"
)
type coinTx struct {
Inputs []coinInput `json:"inputs"`
Outputs []coinOutput `json:"outputs"`
}
type coinInput struct {
Sender string `json:"sender"`
Coins Coins `json:"coins"`
}
type coinOutput struct {
Receiver string `json:"receiver"`
Coins Coins `json:"coins"`
}
// ExtractCoinTx makes nice json from raw tx bytes
func ExtractCoinTx(data []byte) (interface{}, error) {
tx, err := sdk.LoadTx(data)
if err != nil {
return nil, err
}
txl, ok := tx.Unwrap().(sdk.TxLayer)
for ok {
tx = txl.Next()
txl, ok = tx.Unwrap().(sdk.TxLayer)
}
ctx, ok := tx.Unwrap().(SendTx)
if !ok {
return nil, errors.ErrUnknownTxType(tx)
}
// now reformat this....
return &coinTx{
Inputs: transformInputs(ctx.Inputs),
Outputs: transformOutputs(ctx.Outputs),
}, nil
}
func transformInputs(input []TxInput) []coinInput {
out := make([]coinInput, 0, len(input))
for _, in := range input {
out = append(out, coinInput{
Sender: in.Address.String(),
Coins: in.Coins,
})
}
return out
}
func transformOutputs(output []TxOutput) []coinOutput {
out := make([]coinOutput, 0, len(output))
for _, val := range output {
out = append(out, coinOutput{
Receiver: val.Address.String(),
Coins: val.Coins,
})
}
return out
}