Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protocol 22 Support #165

Merged
merged 9 commits into from
Oct 24, 2024
Merged
6 changes: 0 additions & 6 deletions stellarsdk/stellarsdk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -932,8 +932,6 @@
835F0CEA29770BB100793708 /* GetHealthResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 835F0CE829770BB100793708 /* GetHealthResponse.swift */; };
835F0CF229770C0800793708 /* SimulateTransactionResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 835F0CF129770C0800793708 /* SimulateTransactionResponse.swift */; };
835F0CF329770C0800793708 /* SimulateTransactionResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 835F0CF129770C0800793708 /* SimulateTransactionResponse.swift */; };
835F0CF829770C3F00793708 /* SimulateTransactionCost.swift in Sources */ = {isa = PBXBuildFile; fileRef = 835F0CF729770C3F00793708 /* SimulateTransactionCost.swift */; };
835F0CF929770C3F00793708 /* SimulateTransactionCost.swift in Sources */ = {isa = PBXBuildFile; fileRef = 835F0CF729770C3F00793708 /* SimulateTransactionCost.swift */; };
835F0CFB29770C6E00793708 /* Footprint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 835F0CFA29770C6E00793708 /* Footprint.swift */; };
835F0CFC29770C6E00793708 /* Footprint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 835F0CFA29770C6E00793708 /* Footprint.swift */; };
835F0CFE29770C8700793708 /* SendTransactionResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 835F0CFD29770C8700793708 /* SendTransactionResponse.swift */; };
Expand Down Expand Up @@ -1787,7 +1785,6 @@
835F0CE529770B8B00793708 /* SorobanRpcRequestError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SorobanRpcRequestError.swift; sourceTree = "<group>"; };
835F0CE829770BB100793708 /* GetHealthResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetHealthResponse.swift; sourceTree = "<group>"; };
835F0CF129770C0800793708 /* SimulateTransactionResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimulateTransactionResponse.swift; sourceTree = "<group>"; };
835F0CF729770C3F00793708 /* SimulateTransactionCost.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimulateTransactionCost.swift; sourceTree = "<group>"; };
835F0CFA29770C6E00793708 /* Footprint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Footprint.swift; sourceTree = "<group>"; };
835F0CFD29770C8700793708 /* SendTransactionResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendTransactionResponse.swift; sourceTree = "<group>"; };
835F0D0029770CA700793708 /* GetTransactionResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetTransactionResponse.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2862,7 +2859,6 @@
835F0CE529770B8B00793708 /* SorobanRpcRequestError.swift */,
83DDEEDD29E3EFC20052EE8D /* GetLatestLedgerResponse.swift */,
835F0CF129770C0800793708 /* SimulateTransactionResponse.swift */,
835F0CF729770C3F00793708 /* SimulateTransactionCost.swift */,
830DAB4B29AD59DE00ED4E66 /* SimulateTransactionResult.swift */,
835F0CFD29770C8700793708 /* SendTransactionResponse.swift */,
830DAB4229AD540200ED4E66 /* GetEventsResponse.swift */,
Expand Down Expand Up @@ -4273,7 +4269,6 @@
745DF4802052FF9B000D3660 /* CreatePassiveOfferOperationXDR.swift in Sources */,
745DF4812052FF9B000D3660 /* SetOptionsOperationXDR.swift in Sources */,
745DF4822052FF9B000D3660 /* AllowTrustOpAssetXDR.swift in Sources */,
835F0CF929770C3F00793708 /* SimulateTransactionCost.swift in Sources */,
745DF4832052FF9C000D3660 /* AllowTrustOperationXDR.swift in Sources */,
83DB2ACE2ACD6265008B04D6 /* InteractiveService.swift in Sources */,
745DF4842052FF9C000D3660 /* ChangeTrustOperationXDR.swift in Sources */,
Expand Down Expand Up @@ -4958,7 +4953,6 @@
830EFC5A20323AC000607C35 /* TransactionMetaXDR.swift in Sources */,
836971E42024C47300398A98 /* LinkResponse.swift in Sources */,
744FC6E8212D3CCC00ADFC88 /* String.swift in Sources */,
835F0CF829770C3F00793708 /* SimulateTransactionCost.swift in Sources */,
833CBFE72036F7C200C23CFE /* AccountMergeOperation.swift in Sources */,
83DB2ACD2ACD6265008B04D6 /* InteractiveService.swift in Sources */,
744602E4203B22CC00D807C1 /* Network.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ public class AssetResponse: NSObject, Decodable {

public var numClaimableBalances:Int

/// The number of units of credit issued.
public var amount:Decimal

/// The number of accounts that: 1) trust this asset and 2) where if the asset has the auth_required flag then the account is authorized to hold the asset.
public var numberOfAccounts:Int

public var balances:AssetBalances

public var claimableBalancesAmount:Decimal
Expand Down Expand Up @@ -62,10 +56,8 @@ public class AssetResponse: NSObject, Decodable {
case assetIssuer = "asset_issuer"
case accounts
case numClaimableBalances = "num_claimable_balances"
case amount
case balances
case claimableBalancesAmount = "claimable_balances_amount"
case numberOfAccounts = "num_accounts"
case flags
case pagingToken = "paging_token"
case numLiquidityPools = "num_liquidity_pools"
Expand All @@ -89,12 +81,9 @@ public class AssetResponse: NSObject, Decodable {
assetIssuer = try values.decodeIfPresent(String.self, forKey: .assetIssuer)
accounts = try values.decode(AssetAccounts.self, forKey: .accounts)
numClaimableBalances = try values.decode(Int.self, forKey: .numClaimableBalances)
let amountString = try values.decode(String.self, forKey: .amount) as String
amount = Decimal(string: amountString)!
balances = try values.decode(AssetBalances.self, forKey: .balances)
let claimableBalancesAmountString = try values.decode(String.self, forKey: .claimableBalancesAmount) as String
claimableBalancesAmount = Decimal(string: claimableBalancesAmountString)!
numberOfAccounts = try values.decode(Int.self, forKey: .numberOfAccounts)
flags = try values.decode(AccountFlagsResponse.self, forKey: .flags)
pagingToken = try values.decode(String.self, forKey: .pagingToken)
numLiquidityPools = try values.decode(Int.self, forKey: .numLiquidityPools)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ public class SubmitTransactionAsyncResponse: NSObject, Decodable {
private enum CodingKeys: String, CodingKey {
case txStatus = "tx_status"
case txHash = "hash"
case errorResultXdr = "errorResultXdr"
case errorResultXdrP21 = "errorResultXdr"
case errorResultXdr = "error_result_xdr"
}

public required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
txStatus = try values.decode(String.self, forKey: .txStatus)
txHash = try values.decode(String.self, forKey: .txHash)
errorResultXdr = try values.decodeIfPresent(String.self, forKey: .errorResultXdr)
if (errorResultXdr == nil) {
errorResultXdr = try values.decodeIfPresent(String.self, forKey: .errorResultXdrP21) // protocol version < 22
}
}
}
77 changes: 70 additions & 7 deletions stellarsdk/stellarsdk/responses/xdr/TransactionXDR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,43 @@ public struct CreateContractArgsXDR: XDRCodable {
}
}

public struct CreateContractV2ArgsXDR: XDRCodable {
public let contractIDPreimage: ContractIDPreimageXDR
public let executable: ContractExecutableXDR
public let constructorArgs: [SCValXDR]

public init(contractIDPreimage:ContractIDPreimageXDR, executable:ContractExecutableXDR, constructorArgs:[SCValXDR]) {
self.contractIDPreimage = contractIDPreimage
self.executable = executable
self.constructorArgs = constructorArgs
}

public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
contractIDPreimage = try container.decode(ContractIDPreimageXDR.self)
executable = try container.decode(ContractExecutableXDR.self)
constructorArgs = try decodeArray(type: SCValXDR.self, dec: decoder)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
try container.encode(contractIDPreimage)
try container.encode(executable)
try container.encode(constructorArgs)
}
}

public enum HostFunctionType: Int32 {
case invokeContract = 0
case createContract = 1
case uploadContractWasm = 2
case createContractV2 = 3
}

public enum HostFunctionXDR: XDRCodable {
case invokeContract(InvokeContractArgsXDR)
case createContract(CreateContractArgsXDR)
case createContractV2(CreateContractV2ArgsXDR)
case uploadContractWasm(Data)

public init(from decoder: Decoder) throws {
Expand All @@ -278,6 +306,9 @@ public enum HostFunctionXDR: XDRCodable {
case .uploadContractWasm:
let wasm = try container.decode(Data.self)
self = .uploadContractWasm(wasm)
case .createContractV2:
let createContract = try container.decode(CreateContractV2ArgsXDR.self)
self = .createContractV2(createContract)
case .none:
throw StellarSDKError.decodingError(message: "invaid HostFunctionXDR discriminant")
}
Expand All @@ -288,6 +319,7 @@ public enum HostFunctionXDR: XDRCodable {
case .invokeContract: return HostFunctionType.invokeContract.rawValue
case .createContract: return HostFunctionType.createContract.rawValue
case .uploadContractWasm: return HostFunctionType.uploadContractWasm.rawValue
case .createContractV2: return HostFunctionType.createContractV2.rawValue
}
}

Expand All @@ -304,6 +336,9 @@ public enum HostFunctionXDR: XDRCodable {
case .uploadContractWasm (let val):
try container.encode(val)
break
case .createContractV2(let val):
try container.encode(val)
break
}
}

Expand All @@ -324,6 +359,7 @@ public enum HostFunctionXDR: XDRCodable {
return nil
}
}

public var uploadContractWasm:Data? {
switch self {
case .uploadContractWasm(let val):
Expand All @@ -332,16 +368,27 @@ public enum HostFunctionXDR: XDRCodable {
return nil
}
}

public var createContractV2:CreateContractV2ArgsXDR? {
switch self {
case .createContractV2(let val):
return val
default:
return nil
}
}
}

public enum SorobanAuthorizedFunctionType: Int32 {
case contractFn = 0
case contractHostFn = 1
case createContractHostFn = 1
case createContractV2HostFn = 2
}

public enum SorobanAuthorizedFunctionXDR: XDRCodable {
case contractFn(InvokeContractArgsXDR)
case contractHostFn(CreateContractArgsXDR)
case createContractHostFn(CreateContractArgsXDR)
case createContractV2HostFn(CreateContractV2ArgsXDR)

public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
Expand All @@ -352,9 +399,12 @@ public enum SorobanAuthorizedFunctionXDR: XDRCodable {
case .contractFn:
let contractFn = try container.decode(InvokeContractArgsXDR.self)
self = .contractFn(contractFn)
case .contractHostFn:
case .createContractHostFn:
let contractHostFn = try container.decode(CreateContractArgsXDR.self)
self = .contractHostFn(contractHostFn)
self = .createContractHostFn(contractHostFn)
case .createContractV2HostFn:
let contractV2HostFn = try container.decode(CreateContractV2ArgsXDR.self)
self = .createContractV2HostFn(contractV2HostFn)
case .none:
throw StellarSDKError.decodingError(message: "invaid SorobanAuthorizedFunctionXDR discriminant")
}
Expand All @@ -363,7 +413,8 @@ public enum SorobanAuthorizedFunctionXDR: XDRCodable {
public func type() -> Int32 {
switch self {
case .contractFn: return SorobanAuthorizedFunctionType.contractFn.rawValue
case .contractHostFn: return SorobanAuthorizedFunctionType.contractHostFn.rawValue
case .createContractHostFn: return SorobanAuthorizedFunctionType.createContractHostFn.rawValue
case .createContractV2HostFn: return SorobanAuthorizedFunctionType.createContractV2HostFn.rawValue
}
}

Expand All @@ -374,7 +425,10 @@ public enum SorobanAuthorizedFunctionXDR: XDRCodable {
case .contractFn(let val):
try container.encode(val)
break
case .contractHostFn(let val):
case .createContractHostFn(let val):
try container.encode(val)
break
case .createContractV2HostFn(let val):
try container.encode(val)
break
}
Expand All @@ -391,7 +445,16 @@ public enum SorobanAuthorizedFunctionXDR: XDRCodable {

public var contractHostFn:CreateContractArgsXDR? {
switch self {
case .contractHostFn(let val):
case .createContractHostFn(let val):
return val
default:
return nil
}
}

public var contractV2HostFn:CreateContractV2ArgsXDR? {
switch self {
case .createContractV2HostFn(let val):
return val
default:
return nil
Expand Down
14 changes: 14 additions & 0 deletions stellarsdk/stellarsdk/sdk/InvokeHostFunctionOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ public class InvokeHostFunctionOperation:Operation {
return InvokeHostFunctionOperation(hostFunction: hostFunction, sourceAccountId: sourceAccountId)
}

/// only avaliable for protocol version >= 22
public static func forCreatingContractWithConstructor(wasmId:String, address: SCAddressXDR, constructorArguments:[SCValXDR] = [], salt:WrappedData32? = nil, sourceAccountId:String? = nil) throws -> InvokeHostFunctionOperation {
var saltToSet = salt
if saltToSet == nil {
saltToSet = try randomSalt()
}
let contractIdPreimageFormAddress = ContractIDPreimageFromAddressXDR(address: address, salt:saltToSet!)
let contractIDPreimage = ContractIDPreimageXDR.fromAddress(contractIdPreimageFormAddress)
let executable = ContractExecutableXDR.wasm(wasmId.wrappedData32FromHex())
let createContractV2Args = CreateContractV2ArgsXDR(contractIDPreimage: contractIDPreimage, executable: executable, constructorArgs: constructorArguments)
let hostFunction = HostFunctionXDR.createContractV2(createContractV2Args)
return InvokeHostFunctionOperation(hostFunction: hostFunction, sourceAccountId: sourceAccountId)
}

public static func forDeploySACWithSourceAccount(address: SCAddressXDR, salt:WrappedData32? = nil, sourceAccountId:String? = nil) throws -> InvokeHostFunctionOperation {
var saltToSet = salt
if saltToSet == nil {
Expand Down
5 changes: 4 additions & 1 deletion stellarsdk/stellarsdk/soroban/SorobanServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,10 @@ public class SorobanServer {
let response = await self.getLedgerEntries(base64EncodedKeys: [ledgerKeyBase64])
switch response {
case .success(let response):
let data = try? LedgerEntryDataXDR(fromBase64: response.entries[0].xdr)
var data:LedgerEntryDataXDR?
if (response.entries.count > 0) {
data = try? LedgerEntryDataXDR(fromBase64: response.entries.first!.xdr)
}
if let accountData = data?.account {
let account = Account(keyPair: KeyPair(publicKey: accountData.accountID), sequenceNumber: accountData.sequenceNumber);
return .success(response: account)
Expand Down
11 changes: 6 additions & 5 deletions stellarsdk/stellarsdk/soroban/responses/EventInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ public class EventInfo: NSObject, Decodable {
/// Unique identifier for this event.
public var id:String

/// Duplicate of id field, but in the standard place for pagination tokens.
public var pagingToken:String

/// If true the event was emitted during a successful contract call.
public var inSuccessfulContractCall:Bool

Expand All @@ -45,17 +42,21 @@ public class EventInfo: NSObject, Decodable {
/// The transaction which triggered this event.
public var txHash:String

/// for paging , only available for protocol version < 22
public var pagingToken:String?


private enum CodingKeys: String, CodingKey {
case type
case ledger
case ledgerClosedAt
case contractId
case id
case pagingToken
case inSuccessfulContractCall
case topic
case value
case txHash
case pagingToken
}

public required init(from decoder: Decoder) throws {
Expand All @@ -66,11 +67,11 @@ public class EventInfo: NSObject, Decodable {
contractId = try values.decode(String.self, forKey: .contractId)
id = try values.decode(String.self, forKey: .id)
inSuccessfulContractCall = try values.decode(Bool.self, forKey: .inSuccessfulContractCall)
pagingToken = try values.decode(String.self, forKey: .pagingToken)
topic = try values.decode([String].self, forKey: .topic)
value = try values.decode(String.self, forKey: .value)
valueXdr = try SCValXDR.fromXdr(base64: value)
txHash = try values.decode(String.self, forKey: .txHash)
pagingToken = try values.decodeIfPresent(String.self, forKey: .pagingToken) // protocol version < 22
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ public class GetEventsResponse: NSObject, Decodable {
/// The sequence number of the latest ledger known to Soroban RPC at the time it handled the request.
public var latestLedger:Int

/// For paging, only avaliable for protocol version >= 22
public var cursor:String?

private enum CodingKeys: String, CodingKey {
case events
case latestLedger
case cursor
}

public required init(from decoder: Decoder) throws {
Expand All @@ -28,5 +32,6 @@ public class GetEventsResponse: NSObject, Decodable {
events = []
}
latestLedger = try values.decode(Int.self, forKey: .latestLedger)
cursor = try values.decodeIfPresent(String.self, forKey: .cursor) // protocol version > 22
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class GetTransactionResponse: NSObject, Decodable {
/// (optional) A base64 encoded string of the raw TransactionMeta XDR struct for this transaction.
public var resultMetaXdr:String?

/// hex-encoded transaction hash string. Only available for protocol version >= 22
public var txHash:String?

/// (optional) Will be present on failed transactions.
public var error:TransactionStatusError?

Expand All @@ -65,6 +68,7 @@ public class GetTransactionResponse: NSObject, Decodable {
case envelopeXdr
case resultXdr
case resultMetaXdr
case txHash
case error
}

Expand All @@ -81,6 +85,7 @@ public class GetTransactionResponse: NSObject, Decodable {
envelopeXdr = try values.decodeIfPresent(String.self, forKey: .envelopeXdr)
resultXdr = try values.decodeIfPresent(String.self, forKey: .resultXdr)
resultMetaXdr = try values.decodeIfPresent(String.self, forKey: .resultMetaXdr)
txHash = try values.decodeIfPresent(String.self, forKey: .txHash) // protocol version >= 22
error = try values.decodeIfPresent(TransactionStatusError.self, forKey: .error)
}

Expand Down
Loading