/
declare_tax.js
238 lines (134 loc) · 6.39 KB
/
declare_tax.js
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
//================ Resilience.me-client v0.1 ================
//====== scans an accounts incoming transactions =======
//====== for all currencies in wallet.currency =======
//====== and declares tax for the latest transactions =======
//#CPLEIF
//================ Create wallet ================
//this is just an example-wallet.
//actual wallet should be encoded with ripple-account secret key.
//taxRate is written in decimal, 0.02 means 2%.
var wallet = [
{
'currency':'RES',
'taxRate': 0.02
},
{
'currency':'USD',
'taxRate': 0.004
},
{
'currency':'EUR',
'taxRate': 0.01
},
];
//================ create a string with wallet.currency ================
//this is used to filter taxation data by the currencies that the user wants to tax
var IOUs = "";
for (var j = 0; j < wallet.length; j++) {
IOUs = IOUs + wallet[j].currency + " ";
}
//================ define parameters for the Ripple API ================
var params = {
'account': "rLaKjMvLbrAJwnH4VpawQ6ot9epZqJmbfQ",
'ledger_index_min': -1,
'ledger_index_max': -1,
'limit': 50
};
//================ and some other variables ================
//note: the last transaction that the user connected to resilience.me
//is used to filter out all transactions that the user has recieved since
var transaction_id_ping = "D5BAA90BCB77E2A3C66CD0F79A4A32079ABAF270B5C92369537EA1C1C204D7D1";
//the transaction_id_ping variable should be requested from the resilience.me-server
//I haven´t coded that yet.
var taxRate;
var tax_amount;
var TAX_BLOB = [];
var output = {};
var TOTAL_AMOUNT = [];
var IOU;
//================ Connect to the Ripple API ================
var ripple = require('ripple-lib');
var remote = new ripple.Remote({
trace: false,
trusted: true,
local_signing: true,
servers: [{
host: 's1.ripple.com'
, port: 443
, secure: true
}]
});
remote.connect(function() {
//================ first we get an accounts transaction history via the Ripple API ================
remote.request_account_tx(params)
.on('success', function(data) {
//================ now we filter data from the Ripple API.
//================ we want to scan all the transactions we requested from Ripple
//================ and filter out incoming transactions in
//================ the currencies that the user wants to tax
//================ we loop through all the transctions
//================ and filter out the ones we want from the user
for (var i=0; i < data.transactions.length; i++) {
var tx = data.transactions[i].tx;
//================ control check
//the first service-requirement of resilience.me is that
//the user needs to declare_tax for all the
//currencies they have connected to our service.
//if the loop reaches the transaction_id of the last previous transaction that the user has connected
//then we have all the data we need, and the Ripple API loop stops.
if (tx.hash === transaction_id_ping) { break }
//=============== /end of control check
//=============== we filter out account_tx data
if (tx.TransactionType === 'Payment' && IOUs.indexOf(tx.Amount.currency) > -1 && tx.Destination === params.account) {
//================ we need to add data from the users Resilience.me-Wallet to the Ripple data
//================ we can use a loop to look up taxRate-values for each currency
//================ and add taxRate to every [i] transaction that is processed
for (var k = 0; k < wallet.length; k++) {
if (tx.Amount.currency === wallet[k].currency) {
taxRate = wallet[k].taxRate;
}
}
//================ now that we have taxRate added to the transaction, we also calculate the tax_amount
tax_amount = taxRate * tx.Amount.value;
//================ now we have all the data from the Ripple API and from the Resilience.me-Wallet
//================ we store that data in the variable TAX_BLOB
var TAX_DATA = {};
TAX_DATA.transaction_id = tx.hash;
TAX_DATA.account = tx.Account;
TAX_DATA.amount = tx.Amount.value;
TAX_DATA.currency = tx.Amount.currency;
TAX_DATA.issuer = tx.Amount.issuer;
TAX_DATA.destination = tx.Destination;
TAX_DATA.taxRate = taxRate;
TAX_DATA.tax_amount = tax_amount;
//================ all transactions that should be sent to resilience.me-server
//================ are added to the TAX_BLOB that we declared at the top of the script
TAX_BLOB.push(TAX_DATA);
//================ we also sum up TOTAL_AMOUNT per currency (for use in script below)
if (TOTAL_AMOUNT[TAX_DATA.currency] === undefined) {
TOTAL_AMOUNT[TAX_DATA.currency] = Number(TAX_DATA.amount);
}
else {
TOTAL_AMOUNT[TAX_DATA.currency] += Number(TAX_DATA.amount);
}
}//end of if() filter-per-taxRate script
}//end of var i loop
//======== we now have all the data we need in TAX_BLOB
//======== this data should be sent to the resilience.me-server
output = JSON.stringify(TAX_BLOB, null, 2);
//============send to resilience.me-server (script coming soon)
//the TAX_BLOB JSON output should be sent to the resilience.me server.
//I´ll code that soon. Now I just console.log it:
console.log(TAX_BLOB);
//============ swarm-redistribution (script coming soon)
//the resilience.me-server returns unsigned lists of payments
//one list for each currency that was declared
//and the script signs them if they don´t exceed total amount taxed * tax_rate
//I haven´t coded this yet. I have some example-code below, to get you going.
//for some reason, TOTAL_AMOUNT.IOU doesn´t work, but TOTAL_AMOUNT.RES does, and IOU[0] = RES
for (var d = 0; d < wallet.length; d++) {
var IOU = wallet[d].currency;
console.log(TOTAL_AMOUNT.IOU * wallet[d].taxRate);
}
}).request();//end of Ripple API account_tx request
});//end of remote.connect()