forked from ajkagy/xls20-bridge-master
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Xrpl.cs
314 lines (292 loc) · 12 KB
/
Xrpl.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
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
using Ripple.Core.Types;
using RippleDotNet;
using RippleDotNet.Model.Account;
using RippleDotNet.Model.Transaction;
using RippleDotNet.Model;
using RippleDotNet.Requests.Account;
using RippleDotNet.Requests.Transaction;
using RippleDotNet.Responses.Transaction.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TransactionType = RippleDotNet.Model.TransactionType;
using RippleDotNet.Model.Transaction.TransactionTypes;
using Newtonsoft.Json;
namespace XLS_20_Bridge_MasterProcess
{
public class Xrpl
{
private static database db { get; set; }
private static Settings config { get; set; }
private static dynamic validatorServer { get; set; }
public Xrpl(database _db, Settings _config, dynamic _validatorServer)
{
config = _config;
db = _db;
validatorServer = _validatorServer;
}
public async Task SendNFTOfferTransactions()
{
List<BridgeNFT> list = db.GetRecordsByStatus("Validator Agreement Offer");
IRippleClient client = new RippleClient(config._xrplRPC);
foreach (BridgeNFT nft in list)
{
try
{
client.Connect();
SubmitBlobRequest request = new SubmitBlobRequest();
request.TransactionBlob = nft.validatorMeta[0].mintOfferSignedCombined;
Submit result = new Submit(); ;
try
{
result = await client.SubmitTransactionBlob(request);
}
catch (Exception ex)
{
db.UpdateTransactionHashOffer("Error", nft.contractAddress, nft.originOwner, nft.tokenId, 0, ex.Message);
continue;
}
//Sleep for ledger Close
Thread.Sleep(10000);
if (result.EngineResult == "tesSUCCESS" || result.EngineResult == "terQUEUED")
{
bool isValid = await isValidTxnBool(client, result.Transaction.Hash);
if (isValid)
{
db.UpdateTransactionHashOffer("OfferCompleted", nft.contractAddress, nft.originOwner, nft.tokenId, result);
}
else
{
db.UpdateTransactionHashOffer("Error", nft.contractAddress, nft.originOwner, nft.tokenId, result);
}
}
else
{
db.UpdateTransactionHashOffer("Error", nft.contractAddress, nft.originOwner, nft.tokenId, result);
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
try
{
client.Disconnect();
}
catch (Exception) { }
}
}
}
public async Task SendNFTTransactions()
{
List<BridgeNFT> list = db.GetRecordsByStatus("Validator Agreement");
foreach (BridgeNFT nft in list)
{
IRippleClient client = new RippleClient(config._xrplRPC);
try
{
client.Connect();
SubmitBlobRequest request = new SubmitBlobRequest();
request.TransactionBlob = nft.validatorMeta[0].mintSignCombined;
Submit result = new Submit();
try
{
result = await client.SubmitTransactionBlob(request);
}
catch (Exception ex)
{
db.UpdateTransactionHash("NFT Mint Failure", nft.contractAddress, nft.originOwner, nft.tokenId, 0, ex.Message);
continue;
}
//Sleep for ledger Close
Thread.Sleep(10000);
if (result.EngineResult == "tesSUCCESS" || result.EngineResult == "terQUEUED")
{
Tuple<bool, string> t = await isValidTxn(client, result.Transaction.Hash);
if (t.Item1)
{
db.UpdateTransactionHash("OfferCreate", nft.contractAddress, nft.originOwner, nft.tokenId, 1, result);
CreateOffer payload = new CreateOffer();
payload.type = "Request";
payload.command = "CreateOffer";
payload.validator = "0";
payload.contractAddress = nft.contractAddress;
payload.originOwner = nft.originOwner;
payload.tokenId = nft.tokenId;
payload.xrplAddress = nft.xrplAddress;
payload.txnHash = result.Transaction.Hash;
payload.tokenUri = nft.tokenUri;
var createOfferRequest = System.Text.Json.JsonSerializer.Serialize(payload);
validatorServer.MulticastText(createOfferRequest);
}
else
{
db.UpdateTransactionHash("Error", nft.contractAddress, nft.originOwner, nft.tokenId, 0, result);
}
}
else
{
db.UpdateTransactionHash("Error", nft.contractAddress, nft.originOwner, nft.tokenId, 0, result);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
try
{
client.Disconnect();
}
catch (Exception) { }
}
}
}
public async Task AddXRPLTransactions()
{
try
{
object marker = null;
int count = 0;
IRippleClient client = new RippleClient(config._xrplRPC);
try
{
client.Connect();
do
{
TransactionReturnObj returnObj = await ReturnTransactions(client, marker, config._xrplIssuer);
marker = returnObj.marker;
if (returnObj.transactions != null)
{
count = count + returnObj.transactions.Transactions.Count;
}
foreach (TransactionSummary tx in returnObj.transactions.Transactions)
{
if (tx.Validated && (tx.Transaction.TransactionType == TransactionType.NFTokenMint || tx.Transaction.TransactionType == TransactionType.NFTokenCreateOffer) && tx.Transaction.Memos != null)
{
string address = tx.Transaction.Account;
if (tx.Transaction.Memos.Count > 0)
{
foreach (Memo txnMemo in tx.Transaction.Memos)
{
try
{
if (txnMemo.Memo2.MemoDataAsText != "")
{
MemoFragment memo = JsonConvert.DeserializeObject<MemoFragment>(txnMemo.Memo2.MemoDataAsText.Replace(""", "\""));
switch (tx.Transaction.TransactionType)
{
case TransactionType.NFTokenMint:
db.UpdateMinted(memo, tx.Transaction.Hash);
break;
case TransactionType.NFTokenCreateOffer:
db.UpdateOffered(memo, tx.Transaction.Hash);
break;
default:
break;
}
}
}
catch (Exception) { }
}
}
}
}
//Throttle
Thread.Sleep(5 * 1000);
} while (marker != null);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
try
{
client.Disconnect();
}
catch (Exception) { }
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private async Task<Tuple<bool, string>> isValidTxn(IRippleClient client, string txnHash)
{
try
{
ITransactionResponseCommon response = await client.Transaction(txnHash);
if (response.Validated != null)
{
if (response.Validated.Value)
{
return Tuple.Create(true, response.Memos[0].Memo2.MemoDataAsText);
}
else
return Tuple.Create(false, "");
}
else
return Tuple.Create(false, "");
}
catch (Exception)
{
return Tuple.Create(false, "");
}
}
private async Task<bool> isValidTxnBool(IRippleClient client, string txnHash)
{
try
{
ITransactionResponseCommon response = await client.Transaction(txnHash);
if (response.Validated != null)
{
if (response.Validated.Value)
{
return true;
}
else
return false;
}
else
return false;
}
catch (Exception)
{
return false;
}
}
private async Task<TransactionReturnObj> ReturnTransactions(IRippleClient client, object marker, string xrplAddress)
{
TransactionReturnObj returnObj = new TransactionReturnObj();
AccountTransactionsRequest req = new AccountTransactionsRequest(xrplAddress);
if (marker != null)
{
req.Marker = marker;
}
AccountTransactions transactions = await client.AccountTransactions(req);
returnObj.transactions = transactions;
returnObj.marker = transactions.Marker;
return returnObj;
}
}
public struct TransactionReturnObj
{
public AccountTransactions transactions { get; set; }
public object marker { get; set; }
}
public struct MemoFragment
{
public string contractAddress { get; set; }
public string originOwner { get; set; }
public string tokenId { get; set; }
}
}