-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathpath_convert.go
321 lines (300 loc) · 9.07 KB
/
path_convert.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
// Copyright © 2018 Immutability, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/shopspring/decimal"
coingecko "github.com/superoo7/go-gecko/v3"
)
const (
// WEI is "wei"
WEI string = "wei"
// KWEI is "ada", "kwei", "kilowei", "femtoether"
KWEI string = "kwei"
// MWEI is "babbage", "mwei", "megawei", "picoether"
MWEI string = "mwei"
// GWEI is "shannon", "gwei", "gigawei", "nanoether", "nano"
GWEI string = "gwei"
// MICRO is "szazbo", "micro", "microether"
MICRO string = "micro"
// MILLI is "finney", "milli", "milliether"
MILLI string = "milli"
// ETH is "ether", "eth"
ETH string = "ether"
// KILO is "einstein", "kilo", "kiloether", "kether", "grand"
KILO string = "kilo"
// MEGA is "mega", "megaether", "mether"
MEGA string = "mega"
// GIGA is giga", "gigaether", "gether"
GIGA string = "giga"
// TERA is "tera", "teraether", "tether"
TERA string = "tera"
// USD is US Dollar
USD string = "usd"
// AED is United Arab Emirates Dirham
AED string = "aed"
// ARS is Argentine Peso
ARS string = "ars"
)
func convertPaths(b *PluginBackend) []*framework.Path {
return []*framework.Path{
{
Pattern: "convert",
HelpSynopsis: "Convert any Ethereum unit to another.",
HelpDescription: `
Convert any Ethereum unit to another.
`,
Fields: map[string]*framework.FieldSchema{
"unit_from": {
Type: framework.TypeString,
Description: "The Ethereum unit to convert from.",
},
"unit_to": {
Type: framework.TypeString,
Description: "The Ethereum unit to convert to.",
},
"amount": {
Type: framework.TypeString,
Description: "Amount to convert.",
},
},
ExistenceCheck: pathExistenceCheck,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.CreateOperation: b.pathConvertWrite,
logical.UpdateOperation: b.pathConvertWrite,
},
},
{
Pattern: "test",
HelpSynopsis: "Test file upload.",
HelpDescription: `
Test file upload.
`,
Fields: map[string]*framework.FieldSchema{
"file": {
Type: framework.TypeString,
Description: "The file data.",
},
},
ExistenceCheck: pathExistenceCheck,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.CreateOperation: b.pathTest,
logical.UpdateOperation: b.pathTest,
},
},
}
}
// ValidUnit returns a normalized metric unit or an error
func ValidUnit(unit string) (string, error) {
switch strings.ToLower(unit) {
case "wei":
return WEI, nil
case "ada", "kwei", "kilowei", "femtoether":
return KWEI, nil
case "babbage", "mwei", "megawei", "picoether":
return MWEI, nil
case "shannon", "gwei", "gigawei", "nanoether", "nano":
return GWEI, nil
case "szazbo", "micro", "microether":
return MICRO, nil
case "finney", "milli", "milliether":
return MILLI, nil
case "ether", "eth":
return ETH, nil
case "einstein", "kilo", "kiloether", "kether", "grand":
return KILO, nil
case "mega", "megaether", "mether":
return MEGA, nil
case "giga", "gigaether", "gether":
return GIGA, nil
case "tera", "teraether", "tether":
return TERA, nil
case "usd", "USD":
return USD, nil
}
return "", fmt.Errorf("Unknown unit %s", unit)
}
// ToWeiMultiplier returns the multipler to convert a unit to wei
func ToWeiMultiplier(normalizeUnit string) decimal.Decimal {
var multiplier decimal.Decimal
switch normalizeUnit {
case WEI:
multiplier, _ = decimal.NewFromString("1")
case KWEI:
multiplier, _ = decimal.NewFromString("1000")
case MWEI:
multiplier, _ = decimal.NewFromString("1000000")
case GWEI:
multiplier, _ = decimal.NewFromString("1000000000")
case MICRO:
multiplier, _ = decimal.NewFromString("1000000000000")
case MILLI:
multiplier, _ = decimal.NewFromString("1000000000000000")
case ETH:
multiplier, _ = decimal.NewFromString("1000000000000000000")
case KILO:
multiplier, _ = decimal.NewFromString("1000000000000000000000")
case MEGA:
multiplier, _ = decimal.NewFromString("1000000000000000000000000")
case GIGA:
multiplier, _ = decimal.NewFromString("1000000000000000000000000000")
case TERA:
multiplier, _ = decimal.NewFromString("1000000000000000000000000000000")
}
return multiplier
}
// FromWeiMultiplier returns the multipler to convert a unit to wei
func FromWeiMultiplier(normalizeUnit string) decimal.Decimal {
var multiplier decimal.Decimal
switch normalizeUnit {
case WEI:
multiplier, _ = decimal.NewFromString("1")
case KWEI:
multiplier, _ = decimal.NewFromString("0.001")
case MWEI:
multiplier, _ = decimal.NewFromString("0.000001")
case GWEI:
multiplier, _ = decimal.NewFromString("0.000000001")
case MICRO:
multiplier, _ = decimal.NewFromString("0.000000000001")
case MILLI:
multiplier, _ = decimal.NewFromString("0.000000000000001")
case ETH:
multiplier, _ = decimal.NewFromString("0.000000000000000001")
case KILO:
multiplier, _ = decimal.NewFromString("0.000000000000000000001")
case MEGA:
multiplier, _ = decimal.NewFromString("0.000000000000000000000001")
case GIGA:
multiplier, _ = decimal.NewFromString("0.000000000000000000000000001")
case TERA:
multiplier, _ = decimal.NewFromString("0.000000000000000000000000000001")
}
return multiplier
}
// ConvertToWei will convert any valid ethereum unit to wei
func ConvertToWei(normalizeUnit string, amount decimal.Decimal) decimal.Decimal {
var result decimal.Decimal
result = amount.Mul(ToWeiMultiplier(normalizeUnit))
return result
}
// ConvertFromWei will convert any valid ethereum unit to wei
func ConvertFromWei(normalizeUnit string, amount decimal.Decimal) decimal.Decimal {
var result decimal.Decimal
result = amount.Mul(FromWeiMultiplier(normalizeUnit))
return result
}
// ConvertToUSD uses Coinmarketcap to estimate value of ETH in USD
func ConvertToUSD(amountInWei string) (decimal.Decimal, error) {
zero, _ := decimal.NewFromString("0")
httpClient := &http.Client{
Timeout: time.Second * 10,
}
client := coingecko.NewClient(httpClient)
singlePrice, err := client.SimpleSinglePrice("ethereum", "usd")
if err != nil {
log.Fatal(err)
}
balanceInWei, err := decimal.NewFromString(amountInWei)
if err != nil {
return zero, err
}
price := decimal.NewFromFloat32(singlePrice.MarketPrice)
balanceInETH := ConvertFromWei(ETH, balanceInWei)
exchangeValue := price.Mul(balanceInETH)
return exchangeValue, nil
}
func (b *PluginBackend) pathConvertWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
_, err := b.configured(ctx, req)
if err != nil {
return nil, err
}
usdFrom := false
usdTo := false
exchangeValue, _ := decimal.NewFromString("0")
amountFromInUnits, _ := decimal.NewFromString("0")
oneETH, _ := decimal.NewFromString("1")
unitFrom, err := ValidUnit(data.Get("unit_from").(string))
if err != nil {
return nil, err
}
amountFrom := data.Get("amount").(string)
amount, err := decimal.NewFromString(amountFrom)
if err != nil || amount.IsNegative() {
return nil, fmt.Errorf("amount is either not a number or is negative")
}
unitTo, err := ValidUnit(data.Get("unit_to").(string))
if err != nil {
return nil, err
}
if unitFrom == unitTo {
return nil, fmt.Errorf("Conversion from %s to %s makes no sense", unitFrom, unitTo)
}
if unitFrom == USD || unitTo == USD {
oneETHInWei := ConvertToWei(ETH, oneETH)
exchangeValue, err = ConvertToUSD(oneETHInWei.String())
if err != nil {
return nil, err
}
}
if unitFrom == USD {
usdFrom = true
unitFrom = ETH
amount = amount.Div(exchangeValue)
if err != nil {
return nil, err
}
}
amountFromInWei := ConvertToWei(unitFrom, amount)
if unitTo == USD {
usdTo = true
ethInWei := ConvertFromWei(ETH, amountFromInWei)
amountFromInUnits = ethInWei.Mul(exchangeValue)
} else {
amountFromInUnits = ConvertFromWei(unitTo, amountFromInWei)
}
if usdFrom {
unitFrom = data.Get("unit_from").(string)
amount, _ = decimal.NewFromString(data.Get("amount").(string))
} else if usdTo {
unitTo = data.Get("unit_to").(string)
}
return &logical.Response{
Data: map[string]interface{}{
"unit_from": unitFrom,
"amount_from": amount,
"unit_to": unitTo,
"amount_to": amountFromInUnits.String(),
},
}, nil
}
func (b *PluginBackend) pathTest(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
_, err := b.configured(ctx, req)
if err != nil {
return nil, err
}
fileData := data.Get("file").(string)
return &logical.Response{
Data: map[string]interface{}{
"file_data": fileData,
},
}, nil
}