-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.go
181 lines (155 loc) · 4.66 KB
/
builder.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package tron
import (
"fmt"
xc "github.com/cordialsys/crosschain"
"github.com/golang/protobuf/ptypes"
"github.com/okx/go-wallet-sdk/coins/tron"
core "github.com/okx/go-wallet-sdk/coins/tron/pb"
"github.com/okx/go-wallet-sdk/crypto/base58"
"golang.org/x/crypto/sha3"
xcbuilder "github.com/cordialsys/crosschain/builder"
eABI "github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/sirupsen/logrus"
)
// TxBuilder for Template
type TxBuilder struct {
Asset xc.ITask
}
// NewTxBuilder creates a new Template TxBuilder
func NewTxBuilder(cfgI xc.ITask) (TxBuilder, error) {
return TxBuilder{
Asset: cfgI,
}, nil
}
// NewTransfer creates a new transfer for an Asset, either native or token
func (txBuilder TxBuilder) Transfer(args xcbuilder.TransferArgs, input xc.TxInput) (xc.Tx, error) {
return txBuilder.NewTransfer(args.GetFrom(), args.GetTo(), args.GetAmount(), input)
}
// Old transfer interface
func (txBuilder TxBuilder) NewTransfer(from xc.Address, to xc.Address, amount xc.AmountBlockchain, input xc.TxInput) (xc.Tx, error) {
switch asset := txBuilder.Asset.(type) {
// case *xc.TaskConfig:
// return txBuilder.NewTask(from, to, amount, input)
case *xc.ChainConfig:
return txBuilder.NewNativeTransfer(from, to, amount, input)
case *xc.TokenAssetConfig:
return txBuilder.NewTokenTransfer(from, to, amount, input)
default:
// TODO this should return error
contract := asset.GetContract()
logrus.WithFields(logrus.Fields{
"chain": asset.GetChain().Chain,
"contract": contract,
"asset_type": fmt.Sprintf("%T", asset),
}).Warn("new transfer for unknown asset type")
if contract != "" {
return txBuilder.NewTokenTransfer(from, to, amount, input)
} else {
return txBuilder.NewNativeTransfer(from, to, amount, input)
}
}
}
// NewNativeTransfer creates a new transfer for a native asset
func (txBuilder TxBuilder) NewNativeTransfer(from xc.Address, to xc.Address, amount xc.AmountBlockchain, input xc.TxInput) (xc.Tx, error) {
from_bytes, err := tron.GetAddressHash(string(from))
if err != nil {
return nil, err
}
to_bytes, err := tron.GetAddressHash(string(to))
if err != nil {
return nil, err
}
params := &core.TransferContract{}
params.Amount = amount.Int().Int64()
params.OwnerAddress = from_bytes
params.ToAddress = to_bytes
contract := &core.Transaction_Contract{}
contract.Type = core.Transaction_Contract_TransferContract
param, err := ptypes.MarshalAny(params)
if err != nil {
return nil, err
}
contract.Parameter = param
i := input.(*TxInput)
tx := new(core.Transaction)
tx.RawData = i.ToRawData(contract)
// bz, _ := json.Marshal(tx)
// fmt.Println(string(bz))
return &Tx{
tronTx: tx,
}, nil
}
// Signature of a method
func Signature(method string) []byte {
// hash method
hasher := sha3.NewLegacyKeccak256()
hasher.Write([]byte(method))
b := hasher.Sum(nil)
return b[:4]
}
// NewTokenTransfer creates a new transfer for a token asset
func (txBuilder TxBuilder) NewTokenTransfer(from xc.Address, to xc.Address, amount xc.AmountBlockchain, input xc.TxInput) (xc.Tx, error) {
from_bytes, _, err := base58.CheckDecode(string(from))
if err != nil {
return nil, err
}
to_bytes, _, err := base58.CheckDecode(string(to))
if err != nil {
return nil, err
}
contract_bytes, _, err := base58.CheckDecode(txBuilder.Asset.GetContract())
if err != nil {
return nil, err
}
addrType, err := eABI.NewType("address", "", nil)
if err != nil {
return nil, fmt.Errorf("internal type construction error: %v", err)
}
amountType, err := eABI.NewType("address", "", nil)
if err != nil {
return nil, fmt.Errorf("internal type construction error: %v", err)
}
args := eABI.Arguments{
{
Type: addrType,
},
{
Type: amountType,
},
}
paramBz, err := args.PackValues([]interface{}{
common.BytesToAddress(to_bytes),
amount.Int(),
})
methodSig := Signature("transfer(address,uint256)")
data := append(methodSig, paramBz...)
if err != nil {
return nil, err
}
params := &core.TriggerSmartContract{}
params.ContractAddress = contract_bytes
params.Data = data
params.OwnerAddress = from_bytes
params.CallValue = 0
contract := &core.Transaction_Contract{}
contract.Type = core.Transaction_Contract_TriggerSmartContract
param, err := ptypes.MarshalAny(params)
if err != nil {
return nil, err
}
contract.Parameter = param
i := input.(*TxInput)
tx := &core.Transaction{}
tx.RawData = i.ToRawData(contract)
// set limit for token contracts
maxPrice := int64(txBuilder.Asset.GetChain().ChainMaxGasPrice)
tx.RawData.FeeLimit = maxPrice
if tx.RawData.FeeLimit == 0 {
// 2k tron sanity limit
tx.RawData.FeeLimit = 2000000000
}
return &Tx{
tronTx: tx,
}, nil
}