Skip to content

Commit

Permalink
Merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
DelevoXDG committed Oct 11, 2023
2 parents 8ef9eb4 + 180a85d commit 9f33624
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 14 deletions.
18 changes: 9 additions & 9 deletions Sources/Starknet/Data/Transaction/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public struct StarknetInvokeTransactionV0: StarknetTransaction {
self.maxFee = try container.decode(Felt.self, forKey: .maxFee)
self.hash = try container.decodeIfPresent(Felt.self, forKey: .hash)

try verifyTransactionType(container: container, codingKeysType: Self.CodingKeys)
try verifyTransactionType(container: container, codingKeysType: Self.CodingKeys.self)
try verifyTransactionVersion(container: container, codingKeysType: CodingKeys.self)
}
}
Expand Down Expand Up @@ -268,8 +268,8 @@ public struct StarknetDeclareTransactionV0: StarknetTransaction {
self.senderAddress = try container.decode(Felt.self, forKey: .senderAddress)
self.hash = try container.decodeIfPresent(Felt.self, forKey: .hash)

try verifyTransactionType(container: container, codingKeysType: Self.CodingKeys)
try verifyTransactionVersion(container: container, codingKeysType: Self.CodingKeys)
try verifyTransactionType(container: container, codingKeysType: Self.CodingKeys.self)
try verifyTransactionVersion(container: container, codingKeysType: Self.CodingKeys.self)
}
}

Expand Down Expand Up @@ -319,8 +319,8 @@ public struct StarknetDeclareTransactionV1: StarknetTransaction {
self.senderAddress = try container.decode(Felt.self, forKey: .senderAddress)
self.hash = try container.decodeIfPresent(Felt.self, forKey: .hash)

try verifyTransactionType(container: container, codingKeysType: Self.CodingKeys)
try verifyTransactionVersion(container: container, codingKeysType: Self.CodingKeys)
try verifyTransactionType(container: container, codingKeysType: Self.CodingKeys.self)
try verifyTransactionVersion(container: container, codingKeysType: Self.CodingKeys.self)
}
}

Expand Down Expand Up @@ -375,8 +375,8 @@ public struct StarknetDeclareTransactionV2: StarknetTransaction {
self.senderAddress = try container.decode(Felt.self, forKey: .senderAddress)
self.hash = try container.decodeIfPresent(Felt.self, forKey: .hash)

try verifyTransactionType(container: container, codingKeysType: Self.CodingKeys)
try verifyTransactionVersion(container: container, codingKeysType: Self.CodingKeys)
try verifyTransactionType(container: container, codingKeysType: Self.CodingKeys.self)
try verifyTransactionVersion(container: container, codingKeysType: Self.CodingKeys.self)
}
}

Expand Down Expand Up @@ -416,8 +416,8 @@ public struct StarknetDeployTransaction: StarknetTransaction {
self.classHash = try container.decode(Felt.self, forKey: .classHash)
self.hash = try container.decodeIfPresent(Felt.self, forKey: .hash)

try verifyTransactionType(container: container, codingKeysType: Self.CodingKeys)
try verifyTransactionVersion(container: container, codingKeysType: Self.CodingKeys)
try verifyTransactionType(container: container, codingKeysType: Self.CodingKeys.self)
try verifyTransactionVersion(container: container, codingKeysType: Self.CodingKeys.self)
}
}

Expand Down
6 changes: 5 additions & 1 deletion Sources/Starknet/Data/Transaction/TransactionStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ public enum StarknetTransactionFinalityStatus: String, Codable {
}
}

public enum LegacyStarknetTransactionStatus: String, Codable {
public enum StarknetGatewayTransactionStatus: String, Codable {
case pending = "PENDING"
case rejected = "REJECTED"
case acceptedL1 = "ACCEPTED_ON_L1"
case acceptedL2 = "ACCEPTED_ON_L2"
case received = "RECEIVED"
case notReceived = "NOT_RECEIVED"
case reverted = "REVERTED"
case aborted = "ABORTED"

public var encodedValue: Felt {
Felt.fromShortString(self.rawValue.lowercased())!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ enum JsonRpcMethod: String, Encodable {
case getTransactionReceipt = "starknet_getTransactionReceipt"
case simulateTransactions = "starknet_simulateTransactions"
case estimateMessageFee = "starknet_estimateMessageFee"
case pathfinderGetTransactionStatus = "pathfinder_getTransactionStatus"
case specVersion = "starknet_specVersion"
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ struct GetTransactionReceiptPayload: Encodable {
}
}

struct GetTransactionStatusPayload: Encodable {
let transactionHash: Felt

enum CodingKeys: String, CodingKey {
case transactionHash = "transaction_hash"
}
}

struct SimulateTransactionsParams: Encodable {
let transactions: [any StarknetSequencerTransaction]
let blockId: StarknetBlockId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ public class StarknetProvider: StarknetProviderProtocol {
return result.transactionReceipt
}

public func pathfinderGetTransactionStatus(hash: Felt) async throws -> StarknetGatewayTransactionStatus {
#warning("This method is currently supported only by Pathfinder nodes. It will be replaced by getTransactionStatus in starknet.swift 0.7.0")
let params = GetTransactionStatusPayload(transactionHash: hash)

let result = try await makeRequest(method: .pathfinderGetTransactionStatus, params: params, receive: StarknetGatewayTransactionStatus.self)

return result
}

public func simulateTransactions(_ transactions: [any StarknetSequencerTransaction], at blockId: StarknetBlockId, simulationFlags: Set<StarknetSimulationFlag>) async throws -> [StarknetSimulatedTransaction] {
let params = SimulateTransactionsParams(transactions: transactions, blockId: blockId, simulationFlags: simulationFlags)

Expand Down
12 changes: 10 additions & 2 deletions Sources/Starknet/Providers/StarknetProviderProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,21 @@ public protocol StarknetProviderProtocol {
/// - Returns: Transaction found with provided blockId and index.
func getTransactionBy(blockId: StarknetBlockId, index: UInt64) async throws -> any StarknetTransaction

/// Get all event objects matching the conditions in the provided filter
/// Get transaction receipt of a submitted transaction
///
/// - Parameters:
/// - txHash : the hash of the requested transaction
/// - hash : the hash of the requested transaction
/// - Returns: receipt of a transaction identified by given hash
func getTransactionReceiptBy(hash: Felt) async throws -> StarknetTransactionReceiptProtocol

/// Get the gateway status of a submitted transaction.
/// This method is supported only by Pathfinder nodes.
///
/// - Parameters:
/// - hash: The hash of the requested transaction
/// - Returns: Transaction found with provided hash
func pathfinderGetTransactionStatus(hash: Felt) async throws -> StarknetGatewayTransactionStatus

/// Simulate running a given list of transactions, and generate the execution trace
///
/// - Parameters:
Expand Down
19 changes: 19 additions & 0 deletions Tests/StarknetTests/Crypto/TransactionStatusTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import XCTest

@testable import Starknet

final class TransactionStatusTests: XCTestCase {
func testGetTransactionStatusResponse() async throws {
let json = """
{"jsonrpc":"2.0","result":"REJECTED","id":0}
""".data(using: .utf8)!
let invalidJson = """
{"jsonrpc":"2.0","result":"INVALID_STATUS","id":0}
""".data(using: .utf8)!

let decoder = JSONDecoder()

XCTAssertNoThrow(try decoder.decode(JsonRpcResponse<StarknetGatewayTransactionStatus>.self, from: json))
XCTAssertThrowsError(try decoder.decode(JsonRpcResponse<StarknetGatewayTransactionStatus>.self, from: invalidJson))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct PrefundPayload: Codable {
// to avoid DevnetClient relying on StarknetTransactionReceipt.
// Only use it for checking whether a transaction was successful.
struct DevnetReceipt: Decodable {
let status: LegacyStarknetTransactionStatus?
let status: StarknetGatewayTransactionStatus?
let executionStatus: StarknetTransactionExecutionStatus?
let finalityStatus: StarknetTransactionFinalityStatus?

Expand All @@ -93,7 +93,7 @@ struct DevnetReceipt: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.status = try container.decodeIfPresent(LegacyStarknetTransactionStatus.self, forKey: .status)
self.status = try container.decodeIfPresent(StarknetGatewayTransactionStatus.self, forKey: .status)
self.executionStatus = try container.decodeIfPresent(StarknetTransactionExecutionStatus.self, forKey: .executionStatus)
self.finalityStatus = try container.decodeIfPresent(StarknetTransactionFinalityStatus.self, forKey: .finalityStatus)

Expand Down

0 comments on commit 9f33624

Please sign in to comment.