Repository Public
1// SP Wallet Core2// File: Sources/SPWalletCore/Domains/Swap/XSwapAPI.swift3// Description: Native cross-chain swaps through THORChain (services/api/src/xswap.ts): what is swappable, the preparation of a4// firm swap (one transfer of the source asset to the Asgard vault with the memo `=:ASSET:DEST:LIMIT`), its5// verification before any passkey, and the "look now" refresh. The swap is complete only when the wallet's own6// chain adapters saw the outbound transaction (`XSwapSection.isComplete`).7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10import Foundation1112public struct XSwapAPI: SwapOperationAPI {13 public let client: APIClient14 public init(_ client: APIClient) { self.client = client }1516 /** The pinned assets, which of them may be an input, the operators and the routers. */17 public func execution() async throws -> XSwapExecutionInfo { try await client.get("/xswap/execution") }1819 /**20 Prepare a firm THORChain swap and VERIFY it: the intent must send exactly the requested amount of the requested asset from21 the wallet's address to the vault it names, for the requested destination asset and network; the memo must deliver to the22 wallet's OWN address on the destination network with the approved limit; the guaranteed minimum must satisfy `minimumOutput`.23 The vault address is part of what the owner approves: show `intent.xswap.vault` on the review screen.24 */25 public func prepare(_ r: XSwapRequest, accounts: Accounts, minimumOutput: MinimumOutputRule = .asPrepared, idempotencyKey: String, now: Date = Date()) async throws -> PreparedOperation {26 if let q = r.quote { guard q.provider == "thorchain" else { throw ApprovalError.inconsistent("provider") } }27 let a: SwapVerification.Answer = try await SwapVerification.post(client, "/xswap/proposals", body: r, idempotencyKey: idempotencyKey)28 var e = OperationExpectation(kind: .xswap, network: r.fromNetwork, asset: r.fromAsset,29 amount: .exactly(try SwapVerification.requestedUnits(r.amount, decimals: a.proposal.decimals, kind: .xswap)))30 e.toNetwork = r.toNetwork; e.toAsset = r.toAsset; e.provider = "thorchain"; e.minimumOutput = SwapVerification.floor(minimumOutput, quote: r.quote)31 return try await SwapVerification.verifyOrWithdraw(client, a, expecting: e, accounts: accounts, now: now)32 }3334 /** Look now instead of waiting for the server's worker. Safe at any time, any number of times; it signs nothing. */35 public func refresh(_ proposalId: String) async throws -> Proposal { try await client.post("/xswap/proposals/\(APIClient.segment(proposalId))/refresh") }36}37