This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
tfc.go
468 lines (423 loc) · 15.2 KB
/
tfc.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package sdk
import (
"context"
"fmt"
"github.com/Troublor/jasmine-eth-go/token"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"math/big"
"strings"
)
type TFC struct {
*provider
address Address
contract *token.TFCToken
}
/**
Create a new TFC instance by providing the sdk object and the Address of TFC ERC20 contract
*/
func NewTFC(backend Backend, tfcAddress Address) (tfc *TFC, err error) {
tfc = &TFC{
provider: NewProvider(backend),
address: tfcAddress,
}
tfc.contract, err = token.NewTFCToken(common.HexToAddress(string(tfcAddress)), backend)
if err != nil {
return nil, err
}
return tfc, nil
}
/* Call wrappers */
/**
Returns the name of the token, i.e. TFCToken
*/
func (tfc *TFC) Name() (name string, err error) {
return tfc.contract.Name(nil)
}
/**
Returns the symbol of the token, i.e. TFC
*/
func (tfc *TFC) Symbol() (symbol string, err error) {
return tfc.contract.Symbol(nil)
}
/**
Returns the number of decimals the token uses - e.g. 8, means to divide the token amount by 100000000 to get its user representation.
*/
func (tfc *TFC) Decimals() (decimals uint8, err error) {
return tfc.contract.Decimals(nil)
}
/**
Returns the total token supply.
*/
func (tfc *TFC) TotalSupply() (totalSupply *big.Int, err error) {
return tfc.contract.TotalSupply(nil)
}
/**
Returns the Account balance with the provided Address.
*/
func (tfc *TFC) BalanceOf(address Address) (balance *big.Int, err error) {
if !address.IsValid() {
return nil, InvalidAddressError
}
return tfc.contract.BalanceOf(nil, common.HexToAddress(string(address)))
}
/**
Returns the amount which spender is still allowed to withdraw from owner.
*/
func (tfc *TFC) Allowance(owner Address, spender Address) (amount *big.Int, err error) {
if !owner.IsValid() {
return nil, InvalidAddressError
}
if !spender.IsValid() {
return nil, InvalidAddressError
}
return tfc.contract.Allowance(nil, common.HexToAddress(string(owner)), common.HexToAddress(string(spender)))
}
/* Send wrappers */
/**
Transfer the amount of balance from current Account (specified in SDK) to the given "to" Account.
This function requires privateKey has been set in SDK.
*/
func (tfc *TFC) Transfer(ctx context.Context, to Address, amount *big.Int, sender *Account) (doneCh chan interface{}, errCh chan error) {
doneCh = make(chan interface{}, 0)
errCh = make(chan error, 0)
auth := bind.NewKeyedTransactor(sender.privateKey)
tx, err := tfc.contract.Transfer(auth, to.address(), amount)
if err != nil {
errCh <- err
return doneCh, errCh
}
receiptCh, eCh := tfc.AsyncTransaction(ctx, tx.Hash(), ConfirmationRequirement)
go func() {
select {
case <-receiptCh:
close(doneCh)
case err := <-eCh:
errCh <- err
}
}()
return doneCh, errCh
}
func (tfc *TFC) TransferSync(ctx context.Context, to Address, amount *big.Int, sender *Account) (err error) {
doneCh, errCh := tfc.Transfer(ctx, to, amount, sender)
select {
case <-doneCh:
return nil
case err := <-errCh:
return err
}
}
/**
Transfer the amount of balance from the given "from" Account to the given "to" Account.
This function requires privateKey has been set in SDK, which will be used to sign the ethereum transaction.
*/
func (tfc *TFC) TransferFrom(ctx context.Context, from Address, to string, amount *big.Int, sender *Account) (doneCh chan interface{}, errCh chan error) {
panic(UnimplementedError)
}
/**
Allows spender to withdraw from the current Account (specified in SDK) multiple times, up to the given amount.
This function requires privateKey has been set in SDK.
*/
func (tfc *TFC) Approve(ctx context.Context, spender Address, amount *big.Int, sender *Account) (doneCh chan interface{}, errCh chan error) {
panic(UnimplementedError)
}
/**
Generate the amount of tokens and put them in the balance of the given "to" Account.
This function can only be called by Account (specified in SDK) which has MINTER_ROLE of smart contract.
This function requires privateKey has been set in SDK.
*/
func (tfc *TFC) Mint(ctx context.Context, to Address, amount *big.Int, sender *Account) (doneCh chan interface{}, errCh chan error) {
doneCh = make(chan interface{}, 0)
errCh = make(chan error, 0)
auth := bind.NewKeyedTransactor(sender.privateKey)
tx, err := tfc.contract.Mint(auth, to.address(), amount)
if err != nil {
errCh <- err
return doneCh, errCh
}
receiptCh, eCh := tfc.AsyncTransaction(ctx, tx.Hash(), ConfirmationRequirement)
go func() {
select {
case <-receiptCh:
close(doneCh)
case err := <-eCh:
errCh <- err
}
}()
return doneCh, errCh
}
func (tfc *TFC) MintSync(ctx context.Context, to Address, amount *big.Int, sender *Account) (err error) {
doneCh, errCh := tfc.Mint(ctx, to, amount, sender)
select {
case <-doneCh:
return nil
case err := <-errCh:
return err
}
}
/* Anonymous wrappers */
func (tfc *TFC) BridgeTFCExchange(ctx context.Context, depositTransactionHash string, amount *big.Int, minter *Account, depositTransactionConfirmationRequirement int) (recipient Address, transactionHashErr error, doneCh chan interface{}, errCh chan error) {
chainID, err := tfc.backend.NetworkID(context.Background())
if err != nil {
return "", err, nil, nil
}
tx, pending, err := tfc.backend.TransactionByHash(ctx, common.HexToHash(depositTransactionHash))
if err == ethereum.NotFound {
return "", UnknownTransactionHashErr, nil, nil
} else if err != nil {
return "", err, nil, nil
}
if pending {
return "", UnconfirmedTransactionErr, nil, nil
}
msg, err := tx.AsMessage(types.NewEIP155Signer(chainID))
if err != nil {
return "", err, nil, nil
}
receipt, err := tfc.backend.TransactionReceipt(ctx, common.HexToHash(depositTransactionHash))
if err == ethereum.NotFound {
return "", UnknownTransactionHashErr, nil, nil
} else if err != nil {
return "", err, nil, nil
}
// check if receipt is on canonical chain
blockHash := receipt.BlockHash
canonicalBlock, err := tfc.backend.BlockByNumber(ctx, receipt.BlockNumber)
if err != nil {
return "", err, nil, nil
}
if blockHash != canonicalBlock.Hash() {
return "", UnconfirmedTransactionErr, nil, nil
}
currentBlock, err := tfc.backend.BlockByNumber(ctx, nil)
if err != nil {
return "", err, nil, nil
}
if currentBlock.Number().Sub(currentBlock.Number(), receipt.BlockNumber).Cmp(big.NewInt(int64(depositTransactionConfirmationRequirement))) < 0 {
return "", UnconfirmedTransactionErr, nil, nil
}
recipient = Address(msg.From().Hex())
// transaction confirmed
doneCh, errCh = tfc.Mint(ctx, recipient, amount, minter)
return recipient, nil, doneCh, errCh
}
func (tfc *TFC) EstimateTFCExchangeFee(ctx context.Context, recipient Address, amount *big.Int, bridgeAccount *Account, minGas uint64, transactionFeeRate float64) (requiredTransferAmount *big.Int, estimatedGas uint64, gasPrice *big.Int, err error) {
gasPrice, err = tfc.backend.SuggestGasPrice(ctx)
if err != nil {
return nil, 0, nil, err
}
parsedABI, err := abi.JSON(strings.NewReader(token.TFCTokenABI))
if err != nil {
return nil, 0, nil, err
}
// encode input
input, err := parsedABI.Pack("mint", recipient.address(), amount)
if err != nil {
return nil, 0, nil, err
}
// estimate gas
// Gas estimation cannot succeed without code for method invocations
if code, err := tfc.backend.PendingCodeAt(ctx, tfc.address.address()); err != nil {
return nil, 0, nil, err
} else if len(code) == 0 {
return nil, 0, nil, bind.ErrNoCode
}
// If the contract surely has code (or code is not needed), estimate the transaction
tfcAddress := tfc.address.address()
msg := ethereum.CallMsg{From: bridgeAccount.address, To: &tfcAddress, GasPrice: gasPrice, Value: big.NewInt(0), Data: input}
estimatedGas, err = tfc.backend.EstimateGas(ctx, msg)
if err != nil && (strings.Contains(err.Error(), "insufficient funds") || strings.Contains(err.Error(), "gas required exceeds allowance")) {
estimatedGas = 60000 // if estimate gas fails due to bridge account has no balance, assign a default safe gasLimit for ERC20 mint
} else if err != nil {
return nil, 0, nil, fmt.Errorf("failed to estimate gas needed: %v", err)
}
if estimatedGas < minGas {
estimatedGas = minGas
}
// calculate fee with rate
fee := new(big.Int).Mul(gasPrice, big.NewInt(int64(estimatedGas)))
interestedFee := new(big.Float).Mul(new(big.Float).SetInt(fee), big.NewFloat(1+transactionFeeRate))
requiredTransferAmount, accuracy := interestedFee.Int(nil)
if accuracy == big.Below &&
interestedFee.Cmp(new(big.Float).SetInt(requiredTransferAmount)) > 0 {
// add one gas
requiredTransferAmount = requiredTransferAmount.Add(requiredTransferAmount, gasPrice)
}
return requiredTransferAmount, estimatedGas, gasPrice, nil
}
func (tfc *TFC) CheckTransactionFeeDeposit(ctx context.Context, depositTransactionHash string, bridgeAccountAddress Address, depositTransactionConfirmationRequirement int) (recipient Address, depositAmount *big.Int, err error) {
chainID, err := tfc.backend.NetworkID(context.Background())
if err != nil {
return "", nil, err
}
tx, pending, err := tfc.backend.TransactionByHash(ctx, common.HexToHash(depositTransactionHash))
if err == ethereum.NotFound {
return "", nil, UnknownTransactionHashErr
} else if err != nil {
return "", nil, err
}
if pending {
return "", tx.Value(), UnconfirmedTransactionErr
}
msg, err := tx.AsMessage(types.NewEIP155Signer(chainID))
if err != nil {
return "", nil, err
}
if *msg.To() != bridgeAccountAddress.address() {
return "", nil, InvalidDepositErr
}
receipt, err := tfc.backend.TransactionReceipt(ctx, common.HexToHash(depositTransactionHash))
if err == ethereum.NotFound {
return "", nil, UnknownTransactionHashErr
} else if err != nil {
return "", nil, err
}
// check if receipt is on canonical chain
blockHash := receipt.BlockHash
canonicalBlock, err := tfc.backend.BlockByNumber(ctx, receipt.BlockNumber)
if err != nil {
return "", nil, err
}
if blockHash != canonicalBlock.Hash() {
return "", tx.Value(), UnconfirmedTransactionErr
}
currentBlock, err := tfc.backend.BlockByNumber(ctx, nil)
if err != nil {
return "", nil, err
}
if currentBlock.Number().Sub(currentBlock.Number(), receipt.BlockNumber).Cmp(big.NewInt(int64(depositTransactionConfirmationRequirement))) < 0 {
return "", tx.Value(), UnconfirmedTransactionErr
}
recipient = Address(msg.From().Hex())
return recipient, tx.Value(), nil
}
func (tfc *TFC) SendMintTransaction(ctx context.Context, recipient Address, amount *big.Int, minter *Account, depositAmount *big.Int, estimatedGas uint64, gasPrice *big.Int, transactionFeeRate float64) (mintTransactionHash string, err error) {
// get the fee received from user
receivedFee := depositAmount
// make sure the minter account has at least receivedFee amount of ETH
balance, err := tfc.backend.BalanceAt(ctx, minter.address, nil)
if err != nil {
return "", err
}
if balance.Cmp(receivedFee) < 0 {
return "", InsufficientBalanceErr
}
// deduct transaction fee rate
txFee := new(big.Float).Quo(new(big.Float).SetInt(receivedFee), big.NewFloat(1+transactionFeeRate))
// encode input
parsedABI, err := abi.JSON(strings.NewReader(token.TFCTokenABI))
if err != nil {
return "", err
}
// pack mint transaction input
input, err := parsedABI.Pack("mint", recipient.address(), amount)
if err != nil {
return "", err
}
// estimate gas if estimatedGas == 0
// Gas estimation cannot succeed without code for method invocations
if code, err := tfc.backend.PendingCodeAt(ctx, tfc.address.address()); err != nil {
return "", err
} else if len(code) == 0 {
return "", bind.ErrNoCode
}
// If the contract surely has code (or code is not needed), estimate the transaction
tfcAddress := tfc.address.address()
msg := ethereum.CallMsg{From: minter.address, To: &tfcAddress, GasPrice: gasPrice, Value: big.NewInt(0), Data: input}
gas, err := tfc.backend.EstimateGas(ctx, msg)
if err != nil && strings.Contains(err.Error(), "insufficient funds") {
estimatedGas = 60000 // if estimate gas fails due to bridge account does not have enough balance, assign a default safe gasLimit for ERC20 mint
} else if err != nil {
return "", fmt.Errorf("failed to estimate gas needed: %v", err)
} else {
if estimatedGas == 0 {
estimatedGas = gas
} else if estimatedGas > 0 && estimatedGas < gas {
return "", InsufficientGasErr
}
}
// if gasPrice is zero or nil, calculate gasPrice using estimatedGas and txFee
if gasPrice == nil || gasPrice.Cmp(big.NewInt(0)) == 0 {
// calculate gasPrice
calculatedGasPrice := new(big.Float).Quo(txFee, big.NewFloat(float64(estimatedGas)))
gasPrice, _ = calculatedGasPrice.Int(nil)
}
if txFee.Cmp(new(big.Float).Mul(big.NewFloat(float64(estimatedGas)), new(big.Float).SetInt(gasPrice))) < 0 {
return "", InsufficientTransactionFeeErr
}
// send mint transaction
auth := bind.NewKeyedTransactor(minter.privateKey)
auth.GasLimit = estimatedGas
auth.GasPrice = gasPrice
tx, err := tfc.contract.Mint(auth, recipient.address(), amount)
if err != nil {
return "", err
}
return tx.Hash().Hex(), nil
}
func (tfc *TFC) BridgeTFCExchangeAsync(ctx context.Context, depositTransactionHash string, amount *big.Int, minter *Account, depositTransactionConfirmationRequirement int) (recipient Address, mintTransactionHash string, err error) {
chainID, err := tfc.backend.NetworkID(context.Background())
if err != nil {
return "", "", err
}
tx, pending, err := tfc.backend.TransactionByHash(ctx, common.HexToHash(depositTransactionHash))
if err == ethereum.NotFound {
return "", "", UnknownTransactionHashErr
} else if err != nil {
return "", "", err
}
if pending {
return "", "", UnconfirmedTransactionErr
}
msg, err := tx.AsMessage(types.NewEIP155Signer(chainID))
if err != nil {
return "", "", err
}
receipt, err := tfc.backend.TransactionReceipt(ctx, common.HexToHash(depositTransactionHash))
if err == ethereum.NotFound {
return "", "", UnknownTransactionHashErr
} else if err != nil {
return "", "", err
}
// check if receipt is on canonical chain
blockHash := receipt.BlockHash
canonicalBlock, err := tfc.backend.BlockByNumber(ctx, receipt.BlockNumber)
if err != nil {
return "", "", err
}
if blockHash != canonicalBlock.Hash() {
return "", "", UnconfirmedTransactionErr
}
currentBlock, err := tfc.backend.BlockByNumber(ctx, nil)
if err != nil {
return "", "", err
}
if currentBlock.Number().Sub(currentBlock.Number(), receipt.BlockNumber).Cmp(big.NewInt(int64(depositTransactionConfirmationRequirement))) < 0 {
return "", "", UnconfirmedTransactionErr
}
recipient = Address(msg.From().Hex())
// send mint transaction
auth := bind.NewKeyedTransactor(minter.privateKey)
tx, err = tfc.contract.Mint(auth, recipient.address(), amount)
if err != nil {
return "", "", err
}
return recipient, tx.Hash().Hex(), nil
}
func (tfc *TFC) UntilBridgeTFCExchangeComplete(ctx context.Context, mintTransactionHash string, confirmationRequirement int) (doneCh chan interface{}, errCh chan error) {
doneCh = make(chan interface{}, 0)
errCh = make(chan error, 0)
receiptCh, eCh := tfc.AsyncTransaction(ctx, common.HexToHash(mintTransactionHash), confirmationRequirement)
go func() {
select {
case <-receiptCh:
close(doneCh)
case err := <-eCh:
errCh <- err
}
}()
return doneCh, errCh
}