Skip to content

Commit

Permalink
Update demo app to support latest version (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
franciszekjob authored Apr 12, 2024
1 parent a26019a commit 5aed9f9
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 32 deletions.
16 changes: 8 additions & 8 deletions Examples/StarknetDemo/StarknetDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
1D63E19E296DC6390080CA19 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D63E19D296DC6390080CA19 /* ContentView.swift */; };
1D63E1A0296DC63B0080CA19 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1D63E19F296DC63B0080CA19 /* Assets.xcassets */; };
1D63E1A3296DC63B0080CA19 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1D63E1A2296DC63B0080CA19 /* Preview Assets.xcassets */; };
1D63E1AB296DC6780080CA19 /* Starknet in Frameworks */ = {isa = PBXBuildFile; productRef = 1D63E1AA296DC6780080CA19 /* Starknet */; };
AB4C536C2BBC816900B0B6FE /* Starknet in Frameworks */ = {isa = PBXBuildFile; productRef = AB4C536B2BBC816900B0B6FE /* Starknet */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -30,7 +30,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
1D63E1AB296DC6780080CA19 /* Starknet in Frameworks */,
AB4C536C2BBC816900B0B6FE /* Starknet in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -91,7 +91,7 @@
);
name = StarknetDemo;
packageProductDependencies = (
1D63E1AA296DC6780080CA19 /* Starknet */,
AB4C536B2BBC816900B0B6FE /* Starknet */,
);
productName = StarknetDemo;
productReference = 1D63E198296DC6390080CA19 /* StarknetDemo.app */;
Expand Down Expand Up @@ -122,7 +122,7 @@
);
mainGroup = 1D63E18F296DC6390080CA19;
packageReferences = (
1D63E1A9296DC6780080CA19 /* XCRemoteSwiftPackageReference "starknet.swift" */,
AB4C536A2BBC816900B0B6FE /* XCRemoteSwiftPackageReference "starknet" */,
);
productRefGroup = 1D63E199296DC6390080CA19 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -357,9 +357,9 @@
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
1D63E1A9296DC6780080CA19 /* XCRemoteSwiftPackageReference "starknet.swift" */ = {
AB4C536A2BBC816900B0B6FE /* XCRemoteSwiftPackageReference "starknet" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/software-mansion/starknet.swift.git";
repositoryURL = "https://github.com/software-mansion/starknet.swift";
requirement = {
branch = main;
kind = branch;
Expand All @@ -368,9 +368,9 @@
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
1D63E1AA296DC6780080CA19 /* Starknet */ = {
AB4C536B2BBC816900B0B6FE /* Starknet */ = {
isa = XCSwiftPackageProductDependency;
package = 1D63E1A9296DC6780080CA19 /* XCRemoteSwiftPackageReference "starknet.swift" */;
package = AB4C536A2BBC816900B0B6FE /* XCRemoteSwiftPackageReference "starknet" */;
productName = Starknet;
};
/* End XCSwiftPackageProductDependency section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"location" : "https://github.com/software-mansion/starknet.swift.git",
"state" : {
"branch" : "main",
"revision" : "839cf285a2d6aa7dc6ca48ea84f97aecb1edd16e"
"revision" : "cefaf984c591ce9331ac8171a3e1f337482e2d00"
}
}
],
Expand Down
25 changes: 15 additions & 10 deletions Examples/StarknetDemo/StarknetDemo/AccountsStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ let rpcEndpoint = "http://127.0.0.1:5050/rpc"
// !!! Important !!!
// These private keys are for demo only. Never publish a privateKey of your wallet, nor
// store it in any repository as plain text.
let account1PrivateKey: Felt = "0xe3e70682c2094cac629f6fbed82c07cd"
let account2PrivateKey: Felt = "0xf728b4fa42485e3a0a5d2f346baa9455"
let account1PrivateKey: Felt = "0x71d7bb07b9a64f6f78ac4c816aff4da9"
let account2PrivateKey: Felt = "0xe1406455b7d66b1690803be066cbe5e"

// Addresses of accounts associated with above private keys.
let account1Address: Felt = "0x7e00d496e324876bbc8531f2d9a82bf154d1a04a50218ee74cdd372f75a551a"
let account2Address: Felt = "0x69b49c2cc8b16e80e86bfc5b0614a59aa8c9b601569c7b80dde04d3f3151b79"
let account1Address: Felt = "0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691"
let account2Address: Felt = "0x78662e7352d062084b0010068b99288486c2d8b914f6e2a55ce945f8792c8b1"

class AccountsStore: ObservableObject {
let accounts: [StarknetAccountProtocol]
Expand All @@ -57,7 +57,10 @@ class AccountsStore: ObservableObject {
}

init() {
// Create starknet provider that will be used to communicate with the given starknet node.
// Normally we should use provider.getChainId()
// for example purpose we can simply hardcode it as .sepolia
let chainId = StarknetChainId.sepolia

self.provider = StarknetProvider(url: rpcEndpoint)!

// Create a signer that will be used to sign starknet transactions with provided private key.
Expand All @@ -68,7 +71,9 @@ class AccountsStore: ObservableObject {
let account1 = StarknetAccount(
address: account1Address,
signer: account1Signer,
provider: provider
provider: provider,
chainId: chainId,
cairoVersion: .one
)

// And do the same for the second account.
Expand All @@ -77,7 +82,9 @@ class AccountsStore: ObservableObject {
let account2 = StarknetAccount(
address: account2Address,
signer: account2Signer,
provider: provider
provider: provider,
chainId: chainId,
cairoVersion: .one
)

self.accounts = [account1, account2]
Expand All @@ -86,7 +93,6 @@ class AccountsStore: ObservableObject {

func fetchBalance() async {
let accountIndex = currentAccountIndex

// Prepare a read call to be sent to starknet
let call = StarknetCall(
contractAddress: erc20ContractAddress,
Expand All @@ -101,7 +107,6 @@ class AccountsStore: ObservableObject {
// This erc20 contract uses uint256 instead of felt for balances, which is stored
// as two felts - lower and upper 128 bits of the uint256.
let balanceValue = result[0].value + result[1].value << 128

DispatchQueue.main.async {
self.accountBalances[accountIndex] = balanceValue
}
Expand Down Expand Up @@ -135,7 +140,7 @@ class AccountsStore: ObservableObject {
let senderAccount = accounts[currentAccountIndex]

Task {
let _ = try await senderAccount.execute(call: call)
let _ = try await senderAccount.executeV3(call: call)

try await Task.sleep(nanoseconds: UInt64(Double(NSEC_PER_SEC)))

Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ You can find the documentation of the project [here](https://docs.swmansion.com/
## Demo app
In the `Examples` folder you can find a demo ios application.

Before running it, make sure to run starknet devnet with given configuration:
Before running it, make sure to run compatible version of [`starknet-devnet`](https://github.com/0xSpaceShard/starknet-devnet-rs) with a given configuration:
```
starknet-devnet --port 5050 --seed 0
starknet-devnet --port 5050 --seed 0`
```
and run the demo app on ios simulator, to be able to access the local devnet instance.

To install starknet devnet, please run:
```
cargo install \
--locked \
--git https://github.com/0xSpaceShard/starknet-devnet-rs.git \
--rev fa1238e
```
Run the demo app on ios simulator, to be able to access the local devnet instance.

## Development

Expand Down
4 changes: 2 additions & 2 deletions Sources/Starknet/Data/Execution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private func starknetCallsToExecuteCalldataCairo0(calls: [StarknetCall]) -> [Fel
var wholeCalldata: [Felt] = []
var callArray: [Felt] = []

calls.forEach { call in
for call in calls {
callArray.append(call.contractAddress)
callArray.append(call.entrypoint)
callArray.append(Felt(wholeCalldata.count)!)
Expand All @@ -152,7 +152,7 @@ private func starknetCallsToExecuteCalldataCairo1(calls: [StarknetCall]) -> [Fel

callArray.append(Felt(calls.count)!)

calls.forEach { call in
for call in calls {
callArray.append(call.contractAddress)
callArray.append(call.entrypoint)
callArray.append(Felt(call.calldata.count)!)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Starknet/Data/TypedData/StarknetTypedData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public struct StarknetTypedData: Codable, Equatable, Hashable {
try params.forEach { param in
let extractedTypes = try extractTypes(from: param).map { $0.strippingPointer() }

extractedTypes.forEach { extractedType in
for extractedType in extractedTypes {
if allTypes.keys.contains(extractedType), !dependencies.contains(extractedType) {
dependencies.append(extractedType)
toVisit.append(extractedType)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Starknet/Network/HttpNetworkProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class HttpNetworkProvider {
var request = URLRequest(url: config.url, cachePolicy: .reloadIgnoringLocalCacheData)
request.httpMethod = config.method

config.params.forEach { header, value in
for (header, value) in config.params {
request.addValue(value, forHTTPHeaderField: header)
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/StarknetTests/Data/MerkleTreeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ final class MerkleTreeTest: XCTestCase {
XCTAssertEqual(tree!.branches.count, expectedBranchCount)
}

[StarknetHashMethod.pedersen, .poseidon].forEach { hashMethod in
for hashMethod in [StarknetHashMethod.pedersen, .poseidon] {
testBuildFrom1(hashMethod)
testBuildFrom2(hashMethod)
testBuildFrom4(hashMethod)
Expand Down
4 changes: 2 additions & 2 deletions Tests/StarknetTests/Data/MessageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ final class MessageTests: XCTestCase {
"\"payload\":[\"0x2bf223f583a5940873cd804ef3333a8a9306e878b5d4a7d00881f1616894d4d\",\"0x16345785d8a0000\",\"0x0\"]",
]

pairs.forEach {
XCTAssertTrue(encodedString.localizedStandardContains($0))
for pair in pairs {
XCTAssertTrue(encodedString.localizedStandardContains(pair))
}
}
}
2 changes: 1 addition & 1 deletion Tests/StarknetTests/Data/SelectorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class SelectorTests: XCTestCase {
("__l1_default__", "0x0"),
]

cases.forEach { name, hex in
for (name, hex) in cases {
XCTAssertEqual(starknetSelector(from: name), Felt(fromHex: hex))
}
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/StarknetTests/Data/TransactionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ final class TransactionTests: XCTestCase {
"\"version\":\"0x1\"",
]

pairs.forEach {
XCTAssertTrue(encodedString.localizedStandardContains($0))
for pair in pairs {
XCTAssertTrue(encodedString.localizedStandardContains(pair))
}
}

Expand Down

0 comments on commit 5aed9f9

Please sign in to comment.