This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
DefaultXpringClient.swift
251 lines (214 loc) · 10 KB
/
DefaultXpringClient.swift
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
import Foundation
import SwiftGRPC
/// An interface into the Xpring Platform.
public class DefaultXpringClient {
/// A margin to pad the current ledger sequence with when submitting transactions.
private let maxLedgerVersionOffset: UInt32 = 10
/// A network client that will make and receive requests.
private let networkClient: NetworkClient
/// Initialize a new XpringClient.
///
/// - Parameter grpcURL: A url for a remote gRPC service which will handle network requests.
public convenience init(grpcURL: String) {
let networkClient = Org_Xrpl_Rpc_V1_XRPLedgerAPIServiceServiceClient(address: grpcURL, secure: false)
self.init(networkClient: networkClient)
}
/// Initialize a new XpringClient.
///
/// - Parameter networkClient: A network client which will make requests.
internal init(networkClient: NetworkClient) {
self.networkClient = networkClient
}
/// Retrieve the current fee to submit a transaction to the XRP Ledger.
///
/// - Throws: An error if there was a problem communicating with the XRP Ledger.
/// - Returns: A `Org_Xrpl_Rpc_V1_XRPDropsAmount` for submitting a transaction to the ledger.
private func getFee() throws -> Org_Xrpl_Rpc_V1_XRPDropsAmount {
let getFeeResponse = try getRawFee()
return getFeeResponse.fee.minimumFee
}
/// Retrieve a raw `GetFeeResponse` from the XRP Ledger.
///
/// - Throws: An error if there was a problem communicating with the XRP Ledger.
/// - Returns: A `Org_Xrpl_Rpc_V1_XRPDropsAmount` for submitting a transaction to the ledger.
private func getRawFee() throws -> Org_Xrpl_Rpc_V1_GetFeeResponse {
let getFeeRequest = Org_Xrpl_Rpc_V1_GetFeeRequest()
return try networkClient.getFee(getFeeRequest)
}
/// Retrieve Account Info from the XRP Ledger.
///
/// - Parameter classicAddress: The classic address to retrieve info for.
/// - Throws: An error if there was a problem communicating with the XRP Ledger.
/// - Returns: A `Org_Xrpl_Rpc_V1_AccountInfo` for submitting a transaction to the ledger.
private func getAccountInfo(for classicAddress: Address) throws -> Org_Xrpl_Rpc_V1_GetAccountInfoResponse {
let accountInfoRequest = Org_Xrpl_Rpc_V1_GetAccountInfoRequest.with {
$0.account = Org_Xrpl_Rpc_V1_AccountAddress.with {
$0.address = classicAddress
}
}
return try networkClient.getAccountInfo(accountInfoRequest)
}
}
extension DefaultXpringClient: XpringClientDecorator {
/// Get the balance for the given address.
///
/// - Parameter address: The X-Address to retrieve the balance for.
/// - Throws: An error if there was a problem communicating with the XRP Ledger or the inputs were invalid.
/// - Returns: An unsigned integer containing the balance of the address in drops.
public func getBalance(for address: Address) throws -> UInt64 {
guard
let classicAddressComponents = Utils.decode(xAddress: address)
else {
throw XRPLedgerError.invalidInputs("Please use the X-Address format. See: https://xrpaddress.info/.")
}
let accountInfoResponse = try self.getAccountInfo(for: classicAddressComponents.classicAddress)
return accountInfoResponse.accountData.balance.value.xrpAmount.drops
}
/// Retrieve the transaction status for a given transaction hash.
///
/// - Parameter transactionHash: The hash of the transaction.
/// - Throws: An error if there was a problem communicating with the XRP Ledger.
/// - Returns: The status of the given transaction.
public func getTransactionStatus(for transactionHash: TransactionHash) throws -> TransactionStatus {
let transactionStatus = try getRawTransactionStatus(for: transactionHash)
// Only full payment transactions can be bucketed.
guard transactionStatus.isFullPayment else {
return .unknown
}
// Return pending if the transaction is not validated.
guard transactionStatus.validated else {
return .pending
}
return transactionStatus.transactionStatusCode.starts(with: "tes") ? .succeeded : .failed
}
/// Send XRP to a recipient on the XRP Ledger.
///
/// - Parameters:
/// - amount: An unsigned integer representing the amount of XRP to send.
/// - destinationAddress: The X-Address which will receive the XRP.
/// - sourceWallet: The wallet sending the XRP.
/// - Throws: An error if there was a problem communicating with the XRP Ledger or the inputs were invalid.
/// - Returns: A transaction hash for the submitted transaction.
public func send(_ amount: UInt64, to destinationAddress: Address, from sourceWallet: Wallet) throws -> TransactionHash {
guard
let destinationClassicAddressComponents = Utils.decode(xAddress: destinationAddress),
let sourceClassicAddressComponents = Utils.decode(xAddress: sourceWallet.address)
else {
throw XRPLedgerError.invalidInputs("Please use the X-Address format. See: https://xrpaddress.info/.")
}
let accountInfo = try getAccountInfo(for: sourceClassicAddressComponents.classicAddress)
let fee = try getFee()
let lastValidatedLedgerSequence = try getLatestValidatedLedgerSequence()
var payment = Org_Xrpl_Rpc_V1_Payment.with {
$0.destination = Org_Xrpl_Rpc_V1_Destination.with {
$0.value = Org_Xrpl_Rpc_V1_AccountAddress.with {
$0.address = destinationClassicAddressComponents.classicAddress
}
}
$0.amount = Org_Xrpl_Rpc_V1_Amount.with {
$0.value = Org_Xrpl_Rpc_V1_CurrencyAmount.with {
$0.xrpAmount = Org_Xrpl_Rpc_V1_XRPDropsAmount.with {
$0.drops = amount
}
}
}
}
if let destinationTag = destinationClassicAddressComponents.tag {
payment.destinationTag = Org_Xrpl_Rpc_V1_DestinationTag.with {
$0.value = destinationTag
}
}
let signingPublicKeyBytes = try sourceWallet.publicKey.toBytes()
let transaction = Org_Xrpl_Rpc_V1_Transaction.with {
$0.account = Org_Xrpl_Rpc_V1_Account.with {
$0.value = Org_Xrpl_Rpc_V1_AccountAddress.with {
$0.address = sourceClassicAddressComponents.classicAddress
}
}
$0.fee = fee
$0.sequence = accountInfo.accountData.sequence
$0.payment = payment
$0.lastLedgerSequence = Org_Xrpl_Rpc_V1_LastLedgerSequence.with {
$0.value = lastValidatedLedgerSequence + maxLedgerVersionOffset
}
$0.signingPublicKey = Org_Xrpl_Rpc_V1_SigningPublicKey.with {
$0.value = Data(signingPublicKeyBytes)
}
}
guard let signedTransaction = Signer.sign(transaction, with: sourceWallet) else {
throw XRPLedgerError.signingError
}
let submitTransactionRequest = Org_Xrpl_Rpc_V1_SubmitTransactionRequest.with {
$0.signedTransaction = Data(signedTransaction)
}
let submitTransactionResponse = try networkClient.submitTransaction(submitTransactionRequest)
return [UInt8](submitTransactionResponse.hash).toHex()
}
/// Retrieve the latest validated ledger sequence on the XRP Ledger.
///
/// - Throws: An error if there was a problem communicating with the XRP Ledger.
/// - Returns: The index of the latest validated ledger.
public func getLatestValidatedLedgerSequence() throws -> UInt32 {
// The fee API response contains the last ledger sequence and a limited subset of RPCs were implemented in gRPC.
let getFeeResponse = try getRawFee()
return getFeeResponse.ledgerCurrentIndex
}
/// Retrieve the raw transaction status for the given transaction hash.
///
/// - Parameter transactionHash: The hash of the transaction.
/// - Throws: An error if there was a problem communicating with the XRP Ledger.
/// - Returns: The status of the given transaction.
public func getRawTransactionStatus(for transactionHash: TransactionHash) throws -> RawTransactionStatus {
let transactionHashBytes = try transactionHash.toBytes()
let transactionHashData = Data(transactionHashBytes)
let request = Org_Xrpl_Rpc_V1_GetTransactionRequest.with {
$0.hash = transactionHashData
}
let getTransactionResponse = try self.networkClient.getTransaction(request)
return RawTransactionStatus(getTransactionResponse: getTransactionResponse)
}
/// Check if an address exists on the XRP Ledger
///
/// - Parameter address: The address to check the existence of.
/// - Throws: An error if there was a problem communicating with the XRP Ledger.
/// - Returns: A boolean if the account is on the blockchain.
public func accountExists(for address: Address) throws -> Bool {
guard
let _ = Utils.decode(xAddress: address)
else {
throw XRPLedgerError.invalidInputs("Please use the X-Address format. See: https://xrpaddress.info/.")
}
do {
try _ = self.getBalance(for: address)
return true
} catch RPCError.callError(let callResult) {
if callResult.statusCode == StatusCode.notFound {
return false
}
throw RPCError.callError(callResult) // otherwise, an RPCError with unexpected statusCode, re-throw
} catch {
throw error // any other type of Error, re-throw
}
}
/// Retrieve the transaction history for an address.
///
/// - Parameter address: The address to retrieve transaction history for.
/// - Throws: An error if there was a problem communicating with the XRP Ledger.
/// - Returns: An array of transactions for the account.
public func getTransactionHistory(for address: Address) throws -> [Transaction] {
guard
let classicAddressComponents = Utils.decode(xAddress: address)
else {
throw XRPLedgerError.invalidInputs("Please use the X-Address format. See: https://xrpaddress.info/.")
}
let request = Org_Xrpl_Rpc_V1_GetAccountTransactionHistoryRequest.with {
$0.account = Org_Xrpl_Rpc_V1_AccountAddress.with {
$0.address = classicAddressComponents.classicAddress
}
}
let transactionHistory = try self.networkClient.getAccountTransactionHistory(request)
let rawTransactions = transactionHistory.transactions
// TODO(keefertaylor): Map fields from protocol buffers to Transaction objects here.
return rawTransactions.map { _ in Transaction() }
}
}