-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrpc.js
More file actions
173 lines (145 loc) · 4.74 KB
/
rpc.js
File metadata and controls
173 lines (145 loc) · 4.74 KB
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
// 2014 XDN Developers
// 2018 TurtleCoin Developers
/* Format to two decimal places */
function fromAtomic(num)
{
return (num / 100).toFixed(2);
}
function toAtomic(num)
{
return Math.round(num * 100);
}
function callRpc(method, params, callback)
{
var url = "http://" + $("#rpcHost").val() + ":" + $("#rpcPort").val() + "/json_rpc";
var request =
{
"params" : params,
"jsonrpc" : "2.0",
"id" : "test",
"method" : method,
"password" : config.rpcPassword
};
console.log('Sending RPC request to ' + url + ' with parameters: ' + JSON.stringify(params))
var resultNode = document.getElementById("rpc-result");
/* Clear any previous errors */
resultNode.innerHTML = "";
$.ajax(
{
url: url,
type: "POST",
cache: false,
data: JSON.stringify(request),
success: function(result)
{
callback({success: true, result: result});
},
error: function(jqXHR, textStatus, errorThrown)
{
console.log('Failed to contact walletd: jqXHR = ' + jqXHR +
', textStatus = ' + textStatus + ', errorThrown = ' +
errorThrown)
if (errorThrown != "")
{
resultNode.innerHTML = "Failed to contact walletd: " + errorThrown;
}
else
{
resultNode.innerHTML = "Failed to contact walletd: Is walletd open, with the cors header enabled?";
}
callback({success: false, result: errorThrown});
},
dataType: "json"
});
}
function sendTransaction(address, amount, fee)
{
var params =
{
"transfers" : [{address: address, amount: toAtomic(amount)}],
"fee" : toAtomic(fee),
"anonymity" : config.mixin
};
var returnValue = callRpc("sendTransaction", params, function(returnValue)
{
if (returnValue.success)
{
var resultNode = document.getElementById("rpc-result");
/* See if the RPC succeeded */
if (returnValue.result.hasOwnProperty("error"))
{
resultNode.innerHTML = "Failed to send transaction, error: "
+ returnValue.result.error.message;
}
else
{
resultNode.innerHTML = "Success sending transaction, transaction hash: "
+ returnValue.result.result.transactionHash;
}
}
});
}
function getBalance()
{
var returnValue = callRpc("getBalance", {}, function(returnValue)
{
if (returnValue.success)
{
var resultNode = document.getElementById("rpc-result");
if (returnValue.result.hasOwnProperty("error"))
{
resultNode.innerHTML = "Failed to get balance, error: "
+ returnValue.result.error.message;
}
else
{
/* eep! */
var json = returnValue.result.result;
resultNode.innerHTML = "Locked: "
+ fromAtomic(json.lockedAmount)
+ " TRTL"
+ "</br>Unlocked: "
+ fromAtomic(json.availableBalance)
+ " TRTL";
}
}
});
}
$(document).ready(function()
{
document.getElementById('rpcHost').value = config.host;
document.getElementById('rpcPort').value = config.port;
var resultNode = document.getElementById("rpc-result");
$('#getBalance').click(function()
{
console.log('getBalance() clicked...')
getBalance();
});
$('#sendTransaction').click(function()
{
console.log('sendTransaction() clicked...')
resultNode.innerHTML = "";
var address = $("#address").val();
var amount = $("#amount").val();
var fee = $("#fee").val();
if (address.length != config.addressLength)
{
resultNode.innerHTML = "Address is incorrect length! Should be "
+ config.addressLength + " characters.";
return;
}
if (amount < config.minAmount)
{
resultNode.innerHTML = "Amount is too small! Must be at least "
+ config.minAmount + " TRTL.";
return;
}
if (fee < config.minFee)
{
resultNode.innerHTML = "Fee is too small! Must be at least "
+ config.minFee + " TRTL.";
return;
}
sendTransaction(address, amount, fee);
});
});