-
Notifications
You must be signed in to change notification settings - Fork 3
/
transferOps.go
91 lines (79 loc) · 3.04 KB
/
transferOps.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
package ops
import (
"context"
"fmt"
"log"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
zapCommon "github.com/zapproject/pythia/common"
zap "github.com/zapproject/pythia/contracts"
"github.com/zapproject/pythia/rpc"
token "github.com/zapproject/pythia/token"
"github.com/zapproject/pythia/util"
)
/**
* This is the operational transfer component. Its purpose is to transfer zap tokens
*/
func prepareTransfer(amt *big.Int, ctx context.Context) (*bind.TransactOpts, error) {
instance := ctx.Value(zapCommon.MasterContractContextKey).(*zap.ZapMaster)
senderPubAddr := ctx.Value(zapCommon.PublicAddress).(common.Address)
balance, err := instance.BalanceOf(nil, senderPubAddr)
if err != nil {
return nil, fmt.Errorf("\U0001F6AB failed to get balance: %v", err)
}
fmt.Println("My balance", util.FormatERC20Balance(balance))
if balance.Cmp(amt) < 0 {
return nil, fmt.Errorf("insufficent balance (%s ZAP), requested %s ZAP",
util.FormatERC20Balance(balance),
util.FormatERC20Balance(amt))
}
auth, err := PrepareEthTransaction(ctx)
if err != nil {
return nil, fmt.Errorf("\U0001F6AB failed to prepare ethereum transaction: %s", err.Error())
}
return auth, nil
}
func Transfer(toAddress common.Address, amt *big.Int, ctx context.Context) error {
auth, err := prepareTransfer(amt, ctx)
if err != nil {
return err
}
instance2 := ctx.Value(zapCommon.TokenTransactorContractContextKey).(*token.ZapTokenBSCTransactor)
tx, err := instance2.Transfer(auth, toAddress, amt)
if err != nil {
return fmt.Errorf("\U0001F6AB contract failed: %s", err.Error())
}
fmt.Printf("\U0001F381 Transferred %s to %s \U0001F381... with tx:\n%s\n", util.FormatERC20Balance(amt), toAddress.String()[:12], tx.Hash().Hex())
return nil
}
func Approve(_spender common.Address, amt *big.Int, ctx context.Context) error {
auth, err := prepareTransfer(amt, ctx)
if err != nil {
return err
}
instance2 := ctx.Value(zapCommon.TokenTransactorContractContextKey).(*token.ZapTokenBSCTransactor)
tx, err := instance2.Approve(auth, _spender, amt)
if err != nil {
return err
}
fmt.Printf("Approved \U00002705 %s to %s \U00002705... with tx:\n%s\n", util.FormatERC20Balance(amt), _spender.String()[:12], tx.Hash().Hex())
return nil
}
func Balance(ctx context.Context, addr common.Address) error {
client := ctx.Value(zapCommon.ClientContextKey).(rpc.ETHClient)
ethBalance, err := client.BalanceAt(context.Background(), addr, nil)
if err != nil {
return fmt.Errorf("\U0001F6AB problem getting balance: %+v \U0001F6AB", err)
}
instance := ctx.Value(zapCommon.MasterContractContextKey).(*zap.ZapMaster)
zapBalance, err := instance.BalanceOf(nil, addr)
if err != nil {
log.Fatal(err)
return err
}
fmt.Printf("Lets \U0001F440 how much \U0001F4B0\U0001F4B0\U0001F4B0 %s has...\n", addr.String())
fmt.Printf("%10s BNB\n", util.FormatERC20Balance(ethBalance))
fmt.Printf("%10s ZAP\n", util.FormatERC20Balance(zapBalance))
return nil
}