-
Notifications
You must be signed in to change notification settings - Fork 2
/
Converters.cs
105 lines (93 loc) · 2.63 KB
/
Converters.cs
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
using YamlDotNet.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Linq;
namespace akash_dep
{
public static class Converters
{
public static String YAMLtoJSON(String yml)
{
try
{
var r = new StringReader(yml);
var des = new Deserializer();
var yaml = des.Deserialize(r);
JsonSerializer ser = new JsonSerializer();
var w = new StringWriter();
ser.Serialize(w, yaml);
return w.ToString();
}
catch
{
return null;
}
}
public static JToken YAMLtoJSONToken(String yml)
{
String js_raw = YAMLtoJSON(yml);
if (js_raw == null) return null;
return STRtoJS(js_raw);
}
public static JToken STRtoJS(String js)
{
try
{
return JToken.Parse(js);
}
catch
{
return null;
}
}
public static double UAKTtoAKT(long uakt)
{
return uakt / Math.Pow(10, 6);
}
public static long AKTtoUAKT(double akt)
{
return (long)Math.Round(akt * Math.Pow(10, 6));
}
public static long UAKTJSget(JToken js)
{
try
{
String type = js["denom"].ToString();
if (type != "uakt")
{
Console.WriteLine("invalid denom " + type + " in " + js.ToString());
return 0;
}
long denom = js["amount"].ToObject<long>();
return denom;
}
catch
{
Console.WriteLine("invalid json for balance conv " + js.ToString());
return 0;
}
}
public static double UAKTJStoAKT(JToken js)
{
try
{
long amt = UAKTJSget(js);
return UAKTtoAKT(amt);
}
catch
{
Console.WriteLine("invalid json for balance uconv " + js.ToString());
return 0;
}
}
private static Random random = new Random();
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
}
}