-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomisego.go
174 lines (160 loc) · 3.71 KB
/
omisego.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
package main
import (
"github.com/onyxium-strategies/onyxium-strategy-worker/models"
// "flag"
"fmt"
omg "github.com/Alainy/OmiseGo-Go-SDK"
"github.com/joho/godotenv"
// log "github.com/sirupsen/logrus"
"net/url"
"os"
)
var (
baseTokenId string
// SeedLedger = flag.Bool("seed", false, "Seed the database with all available tokens for users.")
currencies = map[string]string{
"BTC": "Bitcoin",
"ETH": "Ethereum",
"NEO": "NEO",
"OMG": "Omisego",
"LTC": "Litecoin",
}
)
const subUnitToUnit = 100000000
func initOmisego() error {
err := seedTokens()
if err != nil {
return err
}
err = getBaseTokenId()
if err != nil {
return err
}
err = getPrimaryWalletAddress()
return err
}
func initOMGClient() (*omg.AdminAPI, error) {
// Get authentication and connection to the eWallet Admin API.
adminURL := &url.URL{
Scheme: "http",
Host: "localhost:4000",
Path: "/api/admin",
}
client, err := omg.NewClient(os.Getenv("accessKey"), os.Getenv("secretKey"), adminURL)
if err != nil {
return nil, err
}
OMGProvider := omg.AdminAPI{
Client: client,
}
return &OMGProvider, nil
}
func seedTokens() error {
tokenList, err := env.Ledger.TokenAll(omg.ListParams{})
if err != nil {
return err
}
for symbol, name := range currencies {
if !symbolInSlice(symbol, tokenList.Data) {
// Mint tokens for the master account
body := omg.TokenCreateParams{
Name: name,
Symbol: symbol,
Description: name,
SubunitToUnit: subUnitToUnit,
Amount: 2100000000000000,
}
_, err := env.Ledger.TokenCreate(body)
if err != nil {
return err
}
}
}
return nil
}
func getBaseTokenId() error {
if baseTokenId = os.Getenv("baseTokenId"); baseTokenId == "" {
tokenList, err := env.Ledger.TokenAll(omg.ListParams{})
if err != nil {
return err
}
for _, token := range tokenList.Data {
if token.Symbol == "BTC" {
baseTokenId = token.Id
myEnv, err := godotenv.Read()
if err != nil {
return err
}
myEnv["baseTokenId"] = token.Id
err = godotenv.Write(myEnv, "./.env")
return err
}
}
}
return nil
}
func getPrimaryWalletAddress() error {
body := omg.ListByIdParams{
os.Getenv("accountId"),
omg.ListParams{},
}
walletList, err := env.Ledger.AccountGetWallets(body)
if err != nil {
return err
}
for _, wallet := range walletList.Data {
if wallet.Identifier == "primary" {
myEnv, err := godotenv.Read()
if err != nil {
return err
}
myEnv["primaryWalletAddress"] = wallet.Address
err = godotenv.Write(myEnv, "./.env")
return err
}
}
return nil
}
func NewOMGUser(user *models.User) error {
// create user on ewallet
userCreateBody := omg.UserParams{
ProviderUserId: user.Id.Hex(),
Username: user.Email,
}
_, err := env.Ledger.UserCreate(userCreateBody)
if err != nil {
return err
}
// get primary wallet of user
getWalletBody := omg.ListByProviderUserIdParams{
user.Id.Hex(),
omg.ListParams{},
}
walletList, err := env.Ledger.UserGetWalletsByProviderUserId(getWalletBody)
if err != nil {
return err
}
for _, wallet := range walletList.Data {
if wallet.Identifier == "primary" {
// send initial funds of 10 BTC
transactionBody := omg.TransactionCreateParams{
IdempotencyToken: omg.NewIdempotencyToken(),
FromAddress: os.Getenv("primaryWalletAddress"),
ToAddress: wallet.Address,
TokenId: os.Getenv("baseTokenId"),
Amount: 10 * subUnitToUnit,
}
_, err = env.Ledger.TransactionCreate(transactionBody)
return err
}
}
return fmt.Errorf("Unable to send initial funds to new user.")
}
func symbolInSlice(a string, list []omg.Token) bool {
for _, b := range list {
if b.Symbol == a {
return true
}
}
return false
}