Split DefaultSmartWalletService into domain services#81
Open
tomas-martins-crossmint wants to merge 13 commits into
Open
Split DefaultSmartWalletService into domain services#81tomas-martins-crossmint wants to merge 13 commits into
tomas-martins-crossmint wants to merge 13 commits into
Conversation
Contributor
Prompt To Fix All With AIFix the following 2 code review issues. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 2
Sources/Wallet/Data/Services/DefaultTransferService.swift:6-10
The synthesized `Encodable` for `TransferBody` encodes `nil` optionals as `"signer": null`, whereas the original hand-rolled implementation used `encodeIfPresent`, which omits the key entirely. If the transfer API rejects an explicit `null` for `signer`, every call without a signer will fail after this refactor.
```suggestion
private struct TransferBody: Encodable {
let recipient: String
let amount: String
let signer: String?
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(recipient, forKey: .recipient)
try container.encode(amount, forKey: .amount)
try container.encodeIfPresent(signer, forKey: .signer)
}
private enum CodingKeys: String, CodingKey { case recipient, amount, signer }
}
```
### Issue 2 of 2
Sources/CrossmintService/JSONCoder.swift:26-35
`try?` silently discards the original `DecodingError`, so all failures surface only as the generic caller-supplied error. Logging the underlying error before throwing preserves debuggability without changing the public API.
```suggestion
func decodeOrThrow<T: Decodable, E: Error>(
_ type: T.Type,
from data: Data,
onFailure: @autoclosure () -> E
) throws(E) -> T {
do {
return try decode(type, from: data)
} catch {
print("[JSONCoder.decodeOrThrow] Failed to decode \(T.self): \(error)")
throw onFailure()
}
}
```
Reviews (1): Last reviewed commit: "Address afeight-review: extract Endpoint..." | Re-trigger Greptile |
Contributor
Prompt To Fix All With AIFix the following 2 code review issues. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 2
Sources/Wallet/Data/Services/DefaultTransferService.swift:6-10
The new `TransferBody` struct loses the `encodeIfPresent` behavior that the old inline `Body` had for the optional `signer` field. Swift's synthesised `Encodable` encodes `nil` optionals as `"signer": null` in JSON, whereas the original code used `encodeIfPresent` which omitted the key entirely. If the API treats a missing key differently from `null` — for example rejecting the request or routing it differently — this silent change will cause failures for every transfer call where `signer` is not provided.
```suggestion
private struct TransferBody: Encodable {
let recipient: String
let amount: String
let signer: String?
private enum CodingKeys: String, CodingKey { case recipient, amount, signer }
func encode(to encoder: Encoder) throws {
var c = encoder.container(keyedBy: CodingKeys.self)
try c.encode(recipient, forKey: .recipient)
try c.encode(amount, forKey: .amount)
try c.encodeIfPresent(signer, forKey: .signer)
}
}
```
### Issue 2 of 2
Sources/CrossmintService/JSONCoder.swift:26-35
`decodeOrThrow` silently discards the underlying decode error via `try?`, replacing it with the generic `onFailure` value. This makes it impossible to tell from crash reports or logs which field failed to decode and why. Consider logging or wrapping the original error before discarding it, or at minimum keeping the `CrossmintServiceError` available for internal diagnostics while still surfacing the domain-specific error to callers.
Reviews (1): Last reviewed commit: "Address afeight-review: extract Endpoint..." | Re-trigger Greptile |
1700579 to
fa239c8
Compare
…uct, fix line length violations
…efaultSmartWalletService becomes a thin facade
… SignatureRequestProtocol
…ation across services
Contributor
|
Reviews (2): Last reviewed commit: "extract executeTransactionRequest into T..." | Re-trigger Greptile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DefaultSmartWalletServicewas a single class implementing all wallet operations across every domain. It now delegates to six standalone services, each owning one domain, andSmartWalletServicebecomes a protocol composition.Changes
SmartWalletService— now a protocol composition ofWalletService,TransactionService,TransferService,BalanceService,NFTService, andSignatureService; previously a single flat protocol with all methodsDefaultSmartWalletService— thin facade holding one instance of each domain service and delegating every method; contains no logic of its ownDefaultWalletService,DefaultTransactionService,DefaultTransferService,DefaultBalanceService,DefaultNFTService,DefaultSignatureService— new standalone service structs, each responsible for one domain, grouped underSources/Wallet/Data/Services/<Domain>/AuthManagerProviding— internal protocol with a defaultauthHeadersimplementation, adopted by all six services to remove the duplicationTransactionRequestExecuting— internal protocol with a defaultexecuteTransactionRequestimplementation, adopted by the three services that dispatch on-chain transactionsCreateSignatureRequest.Body— replacesSignatureRequestProtocolexistential and the oldResponseTypeenum; each case carries its associated request value directlyTransferTokenRequest— replaces the six-parametertransferTokenmethod signature with a params structEndpoints.swift— centralises all URL path construction into staticEndpointfactory methods; previously built inline in each service method