/
service_broadcaster.go
295 lines (250 loc) · 7.2 KB
/
service_broadcaster.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package blockchain
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"sync"
"github.com/antihax/optional"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
kms "github.com/oxygenpay/oxygen/internal/kms/wallet"
"github.com/oxygenpay/oxygen/internal/money"
"github.com/oxygenpay/oxygen/internal/provider/tatum"
client "github.com/oxygenpay/tatum-sdk/tatum"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
type Broadcaster interface {
BroadcastTransaction(ctx context.Context, blockchain money.Blockchain, hex string, isTest bool) (string, error)
GetTransactionReceipt(ctx context.Context, blockchain money.Blockchain, transactionID string, isTest bool) (*TransactionReceipt, error)
}
func (s *Service) BroadcastTransaction(ctx context.Context, blockchain money.Blockchain, rawTX string, isTest bool) (string, error) {
api := s.providers.Tatum.Main()
if isTest {
api = s.providers.Tatum.Test()
}
var (
txHash client.TransactionHash
err error
)
switch kms.Blockchain(blockchain) {
case kms.ETH:
opts := &client.EthereumApiEthBroadcastOpts{}
if isTest {
opts.XTestnetType = optional.NewString(tatum.EthTestnet)
}
txHash, _, err = api.EthereumApi.EthBroadcast(ctx, client.BroadcastKms{TxData: rawTX}, opts)
case kms.MATIC:
txHash, _, err = api.PolygonApi.PolygonBroadcast(ctx, client.BroadcastKms{TxData: rawTX})
case kms.BSC:
txHash, _, err = api.BNBSmartChainApi.BscBroadcast(ctx, client.BroadcastKms{TxData: rawTX})
case kms.TRON:
hashID, errTron := s.providers.Trongrid.BroadcastTransaction(ctx, []byte(rawTX), isTest)
if errTron != nil {
err = errTron
} else {
txHash.TxId = hashID
}
default:
return "", fmt.Errorf("broadcast for %q is not implemented yet", blockchain)
}
if err != nil {
errSwagger, ok := err.(client.GenericSwaggerError)
if !ok {
return "", errors.Wrap(err, "unknown swagger error")
}
s.logger.Error().Err(errSwagger).
Str("raw_tx", rawTX).
Str("response", string(errSwagger.Body())).
Bool("is_test", isTest).
Msg("unable to broadcast transaction")
return "", parseBroadcastError(blockchain, errSwagger.Body())
}
return txHash.TxId, nil
}
type TransactionReceipt struct {
Blockchain money.Blockchain
IsTest bool
Sender string
Recipient string
Hash string
Nonce uint64
NetworkFee money.Money
Success bool
Confirmations int64
IsConfirmed bool
}
func (s *Service) GetTransactionReceipt(
ctx context.Context,
blockchain money.Blockchain,
transactionID string,
isTest bool,
) (*TransactionReceipt, error) {
receipt, err := s.getTransactionReceipt(ctx, blockchain, transactionID, isTest)
if err != nil {
errSwagger, ok := err.(client.GenericSwaggerError)
if ok {
err = errors.Errorf(string(errSwagger.Body()))
}
s.logger.Error().Err(err).Msg("unable to get transaction receipt")
}
return receipt, err
}
func (s *Service) getTransactionReceipt(
ctx context.Context,
blockchain money.Blockchain,
transactionID string,
isTest bool,
) (*TransactionReceipt, error) {
const (
ethConfirmations = 12
maticConfirmations = 30
bscConfirmations = 15
)
nativeCoin, err := s.GetNativeCoin(blockchain)
if err != nil {
return nil, errors.Wrapf(err, "native coin for %q is not found", blockchain)
}
switch kms.Blockchain(blockchain) {
case kms.ETH:
rpc, err := s.providers.Tatum.EthereumRPC(ctx, isTest)
if err != nil {
return nil, err
}
return s.getEthReceipt(ctx, rpc, nativeCoin, transactionID, ethConfirmations, isTest)
case kms.MATIC:
rpc, err := s.providers.Tatum.MaticRPC(ctx, isTest)
if err != nil {
return nil, err
}
return s.getEthReceipt(ctx, rpc, nativeCoin, transactionID, maticConfirmations, isTest)
case kms.BSC:
rpc, err := s.providers.Tatum.BinanceSmartChainRPC(ctx, isTest)
if err != nil {
return nil, err
}
return s.getEthReceipt(ctx, rpc, nativeCoin, transactionID, bscConfirmations, isTest)
case kms.TRON:
receipt, err := s.providers.Trongrid.GetTransactionReceipt(ctx, transactionID, isTest)
if err != nil {
return nil, errors.Wrap(err, "unable to get tron transaction receipt")
}
networkFee, err := nativeCoin.MakeAmount(strconv.Itoa(int(receipt.Fee)))
if err != nil {
return nil, errors.Wrap(err, "unable to calculate network fee")
}
return &TransactionReceipt{
Blockchain: blockchain,
IsTest: isTest,
Sender: receipt.Sender,
Recipient: receipt.Recipient,
Hash: transactionID,
NetworkFee: networkFee,
Success: receipt.Success,
Confirmations: receipt.Confirmations,
IsConfirmed: receipt.IsConfirmed,
}, nil
}
return nil, kms.ErrUnknownBlockchain
}
func (s *Service) getEthReceipt(
ctx context.Context,
rpc *ethclient.Client,
nativeCoin money.CryptoCurrency,
txID string,
requiredConfirmations int64,
isTest bool,
) (*TransactionReceipt, error) {
hash := common.HexToHash(txID)
var (
tx *types.Transaction
receipt *types.Receipt
latestBlock int64
mu sync.Mutex
group errgroup.Group
)
group.Go(func() error {
txByHash, _, err := rpc.TransactionByHash(ctx, hash)
if err != nil {
return err
}
mu.Lock()
tx = txByHash
mu.Unlock()
return nil
})
group.Go(func() error {
r, err := rpc.TransactionReceipt(ctx, hash)
if err != nil {
return err
}
mu.Lock()
receipt = r
mu.Unlock()
return nil
})
group.Go(func() error {
num, err := rpc.BlockNumber(ctx)
if err != nil {
return err
}
mu.Lock()
latestBlock = int64(num)
mu.Unlock()
return nil
})
if err := group.Wait(); err != nil {
return nil, err
}
gasPrice, err := nativeCoin.MakeAmountFromBigInt(receipt.EffectiveGasPrice)
if err != nil {
return nil, errors.Wrap(err, "unable to construct network fee")
}
networkFee, err := gasPrice.MultiplyInt64(int64(receipt.GasUsed))
if err != nil {
return nil, errors.Wrap(err, "unable to calculate network fee")
}
sender, err := types.Sender(types.LatestSignerForChainID(tx.ChainId()), tx)
if err != nil {
return nil, errors.Wrap(err, "unable to get sender")
}
confirmations := latestBlock - receipt.BlockNumber.Int64()
return &TransactionReceipt{
Blockchain: nativeCoin.Blockchain,
IsTest: isTest,
Sender: sender.String(),
Recipient: tx.To().String(),
Hash: txID,
Nonce: tx.Nonce(),
NetworkFee: networkFee,
Success: receipt.Status == 1,
Confirmations: confirmations,
IsConfirmed: confirmations >= requiredConfirmations,
}, nil
}
func parseBroadcastError(_ money.Blockchain, body []byte) error {
// Sample response:
//{
// "statusCode": 403,
// "errorCode": "eth.broadcast.failed",
// "message": "Unable to broadcast transaction.",
// "cause": "insufficient funds for gas * price + value [-32000]"
//}
type tatumErrObj struct {
Message string `json:"message"`
Cause string `json:"cause"`
}
msg := &tatumErrObj{}
_ = json.Unmarshal(body, msg)
switch {
case strings.Contains(msg.Message, "insufficient funds"):
return ErrInsufficientFunds
case strings.Contains(msg.Cause, "insufficient funds"):
return ErrInsufficientFunds
default:
return errors.Wrap(ErrInvalidTransaction, msg.Message)
}
}