@@ -2,7 +2,6 @@ import BigInt
22import Foundation
33@testable import Starknet
44
5- @available ( macOS 15 . 0 , * )
65protocol 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 , * )
3836extension 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 , * )
128125func 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 " )
0 commit comments