Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
378 lines (357 loc) · 10.5 KB

File metadata and controls

378 lines (357 loc) · 10.5 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
'use strict'
var util = require('./lib/util');
var Submit = require('./submit');
const FloatOperation = require('./lib/floatOperation');
var Decimal = require('decimal.js');
var chainsqlLibUtils = require('chainsql-lib').ChainsqlLibUtil;
const { intersection } = require('lodash');
class Ripple extends Submit {
constructor(ChainsqlAPI) {
super().ChainsqlAPI = ChainsqlAPI;
this.instructions = ChainsqlAPI.instructions;
}
submit(cb) {
let cbResult = util.parseCb(cb);
if (cbResult.isFunction) {
super.submit(cbResult.expectOpt).then(result => {
cb(null, result);
}).catch(error => {
cb(error, null);
});
} else {
return new Promise((resolve, reject) => {
super.submit(cbResult.expectOpt).then(result => {
resolve(result);
}).catch(error => {
reject(error);
});
});
}
}
}
Ripple.prototype.signTx = function () {
return this.ChainsqlAPI.api.sign(this.txJSON, this.ChainsqlAPI.connect.secret);
};
Ripple.prototype.prepareJson = function () {
let txJson = this.txJSON;
let instructions = this.instructions;
let transactionType = this.txType;
if (transactionType == "Payment") {
let issuer = txJson.source.maxAmount.counterparty;
let bIssuer = false;
if (issuer != "" && issuer != undefined && issuer != null) {
bIssuer = true;
}
let self = this;
return new Promise(function (resolve, reject) {
self.ChainsqlAPI.getTransferFee(issuer)
.then(function (data) {
if (bIssuer && data) {
let value = txJson.source.maxAmount.value;
let fee = 0;
if (data.min &&
data.max &&
data.min == data.max) {
//Fixed fee
fee = parseFloat(data.min);
} else if (data.rate) {
//Only TransferRate or with TranferFeeMin < TransferFeeMax
fee = FloatOperation.accMul(parseFloat(value), new Decimal(data.rate).sub(new Decimal(1)).toNumber());
if (data.min) {
fee = Math.max(fee, parseFloat(data.min));
}
if (data.max) {
fee = Math.min(fee, parseFloat(data.max));
}
} else if (data.min || data.max) {
//Not valid if set alone or not equal
throw new Error('Exception:transfer fee not valid');
}
if (fee > 0) {
txJson.source.maxAmount.value = (FloatOperation.accAdd(parseFloat(value), fee)).toString();
}
}
self.ChainsqlAPI.api.preparePayment(self.ChainsqlAPI.connect.address, txJson, instructions)
.then(function (data) {
resolve(data);
})
.catch(function (data) {
reject(data);
});
})
.catch(function (data) {
reject(data);
})
});
}
else if (transactionType == "AccountSet") {
return this.ChainsqlAPI.api.prepareSettings(this.ChainsqlAPI.connect.address, txJson, instructions);
}
else if (transactionType == "TrustSet") {
return this.ChainsqlAPI.api.prepareTrustline(this.ChainsqlAPI.connect.address, txJson, instructions);
}
else if (transactionType == "EscrowCreate") {
return this.ChainsqlAPI.api.prepareEscrowCreation(this.ChainsqlAPI.connect.address, txJson, instructions);
}
else if (transactionType == "EscrowFinish") {
return this.ChainsqlAPI.api.prepareEscrowExecution(this.ChainsqlAPI.connect.address, txJson, instructions);
}
else if (transactionType == "EscrowCancel") {
return this.ChainsqlAPI.api.prepareEscrowCancellation(this.ChainsqlAPI.connect.address, txJson, instructions);
}
else if (transactionType === "PayToContract") {
return chainsqlLibUtils.prepareTransaction(txJson, this.ChainsqlAPI.api, {});
}
else if (transactionType === "Authorize") {
return chainsqlLibUtils.prepareTransaction(txJson, this.ChainsqlAPI.api, {});
}
}
Ripple.prototype.preparePayment = function (account, amount, memos) {
var _amount = { value: 0 }
var type = typeof(amount);
if (type == "number" || type == "string") { _amount.value = amount; }
else if (type == "object") { _amount = amount; }
else {
throw new Error('error amount, amount must be object type or number type')
}
if("0x" === account.substring(0,2)) {
account = util.encodeChainsqlAddr(account.slice(2));
}
//
let payment = {
source: {
address: this.ChainsqlAPI.connect.address,
maxAmount: {
value: _amount.value.toString(),
currency: 'ZXC'
}
},
destination: {
address: account,
amount: {
value: _amount.value.toString(),
currency: 'ZXC'
}
},
memos: memos
};
if (util.isMeaningful(_amount.currency)) {
payment.source.maxAmount.currency = _amount.currency;
payment.destination.amount.currency = _amount.currency;
}
if (util.isMeaningful(_amount.issuer)) {
payment.source.maxAmount.counterparty = _amount.issuer;
payment.destination.amount.counterparty = _amount.issuer;
}
//
this.txType = "Payment";
this.txJSON = payment;
return this;
}
Ripple.prototype.accountSet = function (opt) {
var self = this;
var setting = {};
//judge validity
if (!opt || JSON.stringify(opt) == '{}') {
throw new Error('opt cannot be empty');
}
if (opt.hasOwnProperty("enableRippling")) {
opt.enableRippling ? setting.defaultChainsql = true : setting.defaultChainsql = false;
}
// //SetFlag or ClearFlag
// if (opt.hasOwnProperty('setFlag')) {
// let json = "{\"" + opt.setFlag + "\": true}";
// setting = JSON.parse(json);
// }
// else if (opt.hasOwnProperty("clearFlag")) {
// let json = "{\"" + opt.clearFlag + "\": false}";
// setting = JSON.parse(json);
// }
//TransferFee
if (opt.hasOwnProperty('min') && opt.hasOwnProperty('max')) {
if (opt.min > opt.max && opt.max > 0) {
throw new Error('min cannot be greater than max');
}
if (opt.min < 0 || opt.max < 0) {
throw new Error('opt.min or opt.max cannot be less than 0')
}
if (opt.min == opt.max) {
if (opt.min > 0) {
if(opt.rate && opt.rate != 1.0 && opt.rate != 0){
throw new Error('Cannot set transferRate if set fixed fee');
}
} else if(!opt.rate) {
setting.transferRate = 1.0;
}
} else if (!opt.rate) {
throw new Error('Must set transferRate if min < max');
}
setting.transferFeeMin = opt.min.toString();
setting.transferFeeMax = opt.max.toString();
} else if (opt.hasOwnProperty('min')) {
setting.transferFeeMin = opt.min.toString();
setting.transferFeeMax = '0';
if (!opt.rate) {
throw new Error('Must set transferRate if set min or max alone');
}
} else if (opt.hasOwnProperty('max')) {
setting.transferFeeMax = opt.max.toString();
setting.transferFeeMin = '0';
if (!opt.rate) {
throw new Error('Must set transferRate if set min or max alone');
}
}
if (opt.hasOwnProperty('rate')) {
// console.log(typeof(opt.rate))
if (typeof (opt.rate) != 'number' || opt.rate < 1.0 || opt.rate > 2.0) {
throw new Error('opt.rate must be a number >= 1.0 && <= 2.0')
}
setting.transferRate = opt.rate;
}
//
this.txType = "AccountSet";
this.txJSON = setting;
return this;
}
Ripple.prototype.whitelistSet = function (whitelists, flag) {
var self = this;
var setting = {};
setting.whitelists = whitelists;
setting.setFlag = flag;
this.txType = "AccountSet";
this.txJSON = setting;
return this;
}
Ripple.prototype.getTransferFee = function (issuerAddr) {
var self = this;
//
return new Promise(function (resolve, reject) {
if (util.isMeaningless(issuerAddr)) {
resolve({});
}
else {
self.ChainsqlAPI.api.getAccountInfo(issuerAddr)
.then(function (data) {
if (data.transferFeeMin || data.transferFeeMax || data.transferRate) {
var opt = {};
if (data.transferFeeMin) {
opt.min = parseFloat(data.transferFeeMin);
}
if (data.transferFeeMax) {
opt.max = parseFloat(data.transferFeeMax);
}
if (data.transferRate) {
opt.rate = data.transferRate;
}
resolve(opt);
} else {
resolve({});
}
})
.catch(function (err) {
reject(err);
})
}
});
}
Ripple.prototype.trustSet = function (amount) {
let trustline = {
currency: amount.currency,
counterparty: amount.issuer,
limit: amount.value.toString()
};
//
this.txType = "TrustSet";
this.txJSON = trustline;
return this;
}
Ripple.prototype.accountAuthorize = function (nFlag, bSet, account) {
var self = this;
var setting = {};
setting.Account = this.ChainsqlAPI.connect.address;
if(bSet)
setting.SetFlag = nFlag;
else
setting.ClearFlag = nFlag;
setting.TransactionType = "Authorize";
setting.Destination = account;
this.txType = "Authorize";
this.txJSON = setting;
return this;
}
Ripple.prototype.escrowCreate = function (sDestAddr, amount, opt) {
var _amount = { value: 0 }
var type = typeof(amount);
if (type == "number" || type == "string") { _amount.value = amount; }
else if (type == "object") { _amount = amount; }
else {
throw new Error('error amount, amount must be object type or number type')
}
if(util.isMeaningless(opt.dateFormatTMFinish) && util.isMeaningless(opt.dateFormatTMCancel)) {
throw new Error('temBAD_EXPIRATION, Malformed: Bad expiration');
}
//
const escrowCreation = {
destination: sDestAddr,
amount: {
value: _amount.value.toString(),
currency: 'ZXC'
}
};
if(util.isMeaningful(opt.dateFormatTMFinish)){
let dateFinish = new Date(opt.dateFormatTMFinish);
let tmExec = dateFinish.toISOString();
escrowCreation.allowExecuteAfter = tmExec;
}
if(util.isMeaningful(opt.dateFormatTMCancel)){
let dateCancel = new Date(opt.dateFormatTMCancel);
let tmCancel = dateCancel.toISOString();
escrowCreation.allowCancelAfter = tmCancel;
}
if (util.isMeaningful(_amount.currency)) {
escrowCreation.amount.currency = _amount.currency;
}
if (util.isMeaningful(_amount.issuer)) {
escrowCreation.amount.counterparty = _amount.issuer;
}
//
this.txType = "EscrowCreate";
this.txJSON = escrowCreation;
return this;
}
Ripple.prototype.escrowExecute = function (sOwnerAddr, nCreateEscrowSeq) {
const escrowExecution = {
owner: sOwnerAddr,
escrowSequence: nCreateEscrowSeq
};
//
this.txType = "EscrowFinish";
this.txJSON = escrowExecution;
return this;
}
Ripple.prototype.escrowCancel = function (sOwnerAddr, nCreateEscrowSeq) {
const escrowCancellation = {
owner: sOwnerAddr,
escrowSequence: nCreateEscrowSeq
};
//
this.txType = "EscrowCancel";
this.txJSON = escrowCancellation;
return this;
}
Ripple.prototype.payToContract = function (contractAddr, value, gas) {
let contractValue = (value*1000000).toString();
var contractTx = {
TransactionType : "Contract",
ContractOpType : 2,
Account : this.ChainsqlAPI.connect.address,
ContractAddress : contractAddr,
ContractData : "",
ContractValue : contractValue,
Gas : gas
};
this.txType = "PayToContract";
this.txJSON = contractTx;
return this;
}
module.exports = Ripple;
Morty Proxy This is a proxified and sanitized view of the page, visit original site.