Skip to content

Commit aee6cec

Browse files
Remove usage of UInt128 from prefundAccount (#232)
* Remove usage of `UInt128` from `prefundAccount` * Add comment * Update comment * Update `amount` type to `BigUInt` * Use notation with power * Fix typo
1 parent aa9857b commit aee6cec

File tree

6 files changed

+39
-14
lines changed

6 files changed

+39
-14
lines changed

Tests/StarknetTests/Accounts/AccountTest.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import XCTest
22

33
@testable import Starknet
44

5-
@available(macOS 15.0, *)
65
final class AccountTests: XCTestCase {
76
static var devnetClient: DevnetClientProtocol!
87

Tests/StarknetTests/Data/ExecutionTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import XCTest
22

33
@testable import Starknet
44

5-
@available(macOS 15.0, *)
65
final class ExecutionTests: XCTestCase {
76
static var devnetClient: DevnetClientProtocol!
87

Tests/StarknetTests/Devnet/DevnetClientTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
@testable import Starknet
22
import XCTest
33

4-
@available(macOS 15.0, *)
54
final class DevnetClientTests: XCTestCase {
65
var client: DevnetClientProtocol!
76

Tests/StarknetTests/Providers/ProviderTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import XCTest
22

33
@testable import Starknet
44

5-
@available(macOS 15.0, *)
65
final class ProviderTests: XCTestCase {
76
static var devnetClient: DevnetClientProtocol!
87

Tests/StarknetTests/Utils/DevnetClient/DevnetClient.swift

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import BigInt
22
import Foundation
33
@testable import Starknet
44

5-
@available(macOS 15.0, *)
65
protocol DevnetClientProtocol {
76
var rpcUrl: String { get }
87
var mintUrl: String { get }
@@ -19,7 +18,7 @@ protocol DevnetClientProtocol {
1918

2019
func isRunning() -> Bool
2120

22-
func prefundAccount(address: Felt, amount: UInt128, unit: StarknetPriceUnit) async throws
21+
func prefundAccount(address: Felt, amount: BigUInt, unit: StarknetPriceUnit) async throws
2322
func createDeployAccount(name: String, classHash: Felt, salt: Felt?) async throws -> DeployAccountResult
2423
func createAccount(name: String, classHash: Felt, salt: Felt?, type: String) async throws -> CreateAccountResult
2524
func deployAccount(name: String, classHash: Felt, prefund: Bool) async throws -> DeployAccountResult
@@ -34,9 +33,8 @@ protocol DevnetClientProtocol {
3433
func isTransactionSuccessful(transactionHash: Felt) async throws -> Bool
3534
}
3635

37-
@available(macOS 15.0, *)
3836
extension DevnetClientProtocol {
39-
func prefundAccount(address: Felt, amount: UInt128 = 10_000_000_000_000_000_000_000_000, unit: StarknetPriceUnit = .fri) async throws {
37+
func prefundAccount(address: Felt, amount: BigUInt = BigUInt(10).power(23), unit: StarknetPriceUnit = .fri) async throws {
4038
try await prefundAccount(address: address, amount: amount, unit: unit)
4139
}
4240

@@ -124,7 +122,6 @@ extension DevnetClientProtocol {
124122

125123
// Due to DevnetClient being albe to run only on a macos, this
126124
// factory method will throw, when ran on any other platform.
127-
@available(macOS 15.0, *)
128125
func makeDevnetClient() -> DevnetClientProtocol {
129126
#if os(macOS)
130127
return DevnetClient()
@@ -135,7 +132,6 @@ func makeDevnetClient() -> DevnetClientProtocol {
135132

136133
#if os(macOS)
137134

138-
@available(macOS 15.0, *)
139135
class DevnetClient: DevnetClientProtocol {
140136
private var devnetProcess: Process?
141137

@@ -297,15 +293,33 @@ func makeDevnetClient() -> DevnetClientProtocol {
297293
self.devnetProcess = nil
298294
}
299295

300-
public func prefundAccount(address: Felt, amount: UInt128, unit: StarknetPriceUnit) async throws {
296+
public func prefundAccount(address: Felt, amount: BigUInt, unit: StarknetPriceUnit) async throws {
301297
try guardDevnetIsRunning()
302298

303299
let url = URL(string: mintUrl)!
304300
var request = URLRequest(url: url)
305301
request.httpMethod = "POST"
306302

307303
let payload = PrefundPayload(address: address, amount: amount, unit: unit)
308-
request.httpBody = try JSONEncoder().encode(payload)
304+
305+
// TODO(#209): Once we can use UInt128, we can simply set
306+
// body as `request.httpBody = try JSONEncoder().encode(payload)`
307+
// Following adjustment is needed to remove quotes from the amount field
308+
// in the JSON body, because ATM we can't use UInt128 in the payload.
309+
310+
let data = try JSONEncoder().encode(payload)
311+
312+
guard var json = String(data: data, encoding: .utf8) else {
313+
throw DevnetClientError.prefundError
314+
}
315+
316+
json = json.replacingOccurrences(
317+
of: #"("amount"\s*:\s*)"([^"]*)""#,
318+
with: "$1$2",
319+
options: .regularExpression
320+
)
321+
322+
request.httpBody = Data(json.utf8)
309323

310324
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
311325
request.addValue("application/json", forHTTPHeaderField: "Accept")

Tests/StarknetTests/Utils/DevnetClient/DevnetClientModels.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,26 @@ struct InvokeContractResult {
7575
let transactionHash: Felt
7676
}
7777

78-
@available(macOS 15.0, *)
78+
// TODO(#209): Once we can use UInt128, we should change type of `amount` to UInt128
79+
// and remove coding keys and `encode` method (they won't be needed).
80+
7981
struct PrefundPayload: Codable {
8082
let address: Felt
81-
let amount: UInt128
83+
let amount: BigUInt
8284
let unit: StarknetPriceUnit
85+
86+
enum CodingKeys: String, CodingKey {
87+
case address
88+
case amount
89+
case unit
90+
}
91+
92+
public func encode(to encoder: Encoder) throws {
93+
var container = encoder.container(keyedBy: CodingKeys.self)
94+
try container.encode(address, forKey: .address)
95+
try container.encode(amount.description, forKey: .amount)
96+
try container.encode(unit, forKey: .unit)
97+
}
8398
}
8499

85100
// Simplified receipt that is intended to support any JSON-RPC version starting 0.3,

0 commit comments

Comments
 (0)