Skip to content

Commit

Permalink
Merge pull request #73 from keefertaylor/conseil-contracts
Browse files Browse the repository at this point in the history
Add separate RPC for contract code
  • Loading branch information
keefertaylor authored Apr 22, 2019
2 parents 4874d49 + aa4cfab commit bc498e9
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 22 deletions.
18 changes: 16 additions & 2 deletions Extensions/PromiseKit/ConseilClient+Promises.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@ public extension ConseilClient {
///
/// - Parameters:
/// - account: The account to query.
/// - limit: The number of transactions to return, defaults to 100.
/// - limit: The number of accounts to return, defaults to 100.
/// - completion: A completion callback.
func originatedAccounts(
from account: String,
limit: Int = 100
) -> Promise<[[String: Any]]> {
let rpc = GetOriginatedAccounts(account: account, limit: limit)
let rpc = GetOriginatedAccountsRPC(account: account, limit: limit)
return send(rpc)
}

/// Retrieve originated contracts.
///
/// - Parameters:
/// - account: The account to query.
/// - limit: The number of contracts to return, defaults to 100.
/// - completion: A completion callback.
func originatedContracts(
from account: String,
limit: Int = 100
) -> Promise<[[String: Any]]> {
let rpc = GetOriginatedContractsRPC(account: account, limit: limit)
return send(rpc)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,17 @@ extension ConseilClientIntegrationTests {

wait(for: [expectation], timeout: .expectationTimeout)
}

func testGetOriginatedContracts_promises() {
let expectation = XCTestExpectation(description: "promise fulfilled")
conseilClient.originatedContracts(from: Wallet.contractOwningAddress).done { result in
XCTAssertNotNil(result)
XCTAssert(result.count > 1)
expectation.fulfill()
} .catch { _ in
XCTFail()
}

wait(for: [expectation], timeout: .expectationTimeout)
}
}
15 changes: 15 additions & 0 deletions IntegrationTests/TezosKit/ConseilClientIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,19 @@ class ConseilClientIntegrationTests: XCTestCase {
}
wait(for: [expectation], timeout: .expectationTimeout)
}

public func testConseilOriginatedContracts() {
let expectation = XCTestExpectation(description: "completion called")
conseilClient.originatedContracts(from: Wallet.contractOwningAddress) { result in
switch result {
case .success(let results):
XCTAssert(results.count > 1)
expectation.fulfill()
case .failure(let error):
print(error)
XCTFail()
}
}
wait(for: [expectation], timeout: .expectationTimeout)
}
}
3 changes: 3 additions & 0 deletions IntegrationTests/TezosKit/TezosNodeIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ extension Wallet {
public static let testWallet =
Wallet(mnemonic: "predict corn duty process brisk tomato shrimp virtual horror half rhythm cook")!
public static let originatedAddress = "KT1D5jmrBD7bDa3jCpgzo32FMYmRDdK2ihka"

// An address which has originated contracts on it.
public static let contractOwningAddress = "tz1RYq8wjcCbRZykY7XH15WPkzK7TWwPvJJt"
}

extension URL {
Expand Down
12 changes: 4 additions & 8 deletions TezosKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
772F48A82224894E00DF0F9D /* TezosNodeIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 772F48A72224894E00DF0F9D /* TezosNodeIntegrationTests.swift */; };
772F48AA2224894E00DF0F9D /* TezosKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 77CE359621F7DC76006ADABA /* TezosKit.framework */; };
7741B95D226DF5860005B398 /* GetOriginatedContractsRPC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7741B95C226DF5860005B398 /* GetOriginatedContractsRPC.swift */; };
77633D532247EFE20011106A /* TezosNodeClientTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77633D522247EFE20011106A /* TezosNodeClientTests.swift */; };
7774780522221DE50010BA4D /* AbstractClientTest+Promises.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7774780422221DE50010BA4D /* AbstractClientTest+Promises.swift */; };
7774780B2222281B0010BA4D /* TezosKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 77CE359621F7DC76006ADABA /* TezosKit.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
Expand Down Expand Up @@ -44,10 +45,6 @@
77B69EBB224E5F0E00DB4319 /* GetReceivedTransactions.RPC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77B69EBA224E5F0E00DB4319 /* GetReceivedTransactions.RPC.swift */; };
77B69EBF224E92C900DB4319 /* ConseilClientTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77B69EBE224E92C900DB4319 /* ConseilClientTests.swift */; };
77B69EC1224F2A2800DB4319 /* GetOriginatedAccountsRPC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77B69EC0224F2A2800DB4319 /* GetOriginatedAccountsRPC.swift */; };
77B69ED1225348B400DB4319 /* ConseilClient+Promises.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77B69ED0225348B400DB4319 /* ConseilClient+Promises.swift */; };
77B69ED522534F2B00DB4319 /* ConseilClientIntegrationTests+Promises.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77B69ED422534F2B00DB4319 /* ConseilClientIntegrationTests+Promises.swift */; };
77B69ECB22510FE200DB4319 /* ConseilPlatformTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77B69ECA22510FE200DB4319 /* ConseilPlatformTest.swift */; };
77B69ECD2251104300DB4319 /* ConseilNetworkTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77B69ECC2251104300DB4319 /* ConseilNetworkTest.swift */; };
77B69EC322510C6500DB4319 /* ConseilEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77B69EC222510C6500DB4319 /* ConseilEntity.swift */; };
77B69EC522510D5400DB4319 /* ConseilEntityTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77B69EC422510D5400DB4319 /* ConseilEntityTest.swift */; };
77B69ECB22510FE200DB4319 /* ConseilPlatformTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77B69ECA22510FE200DB4319 /* ConseilPlatformTest.swift */; };
Expand Down Expand Up @@ -202,6 +199,7 @@
772F48A52224894E00DF0F9D /* IntegrationTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = IntegrationTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
772F48A72224894E00DF0F9D /* TezosNodeIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TezosNodeIntegrationTests.swift; sourceTree = "<group>"; };
772F48A92224894E00DF0F9D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7741B95C226DF5860005B398 /* GetOriginatedContractsRPC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetOriginatedContractsRPC.swift; sourceTree = "<group>"; };
77633D522247EFE20011106A /* TezosNodeClientTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TezosNodeClientTests.swift; sourceTree = "<group>"; };
7774780422221DE50010BA4D /* AbstractClientTest+Promises.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AbstractClientTest+Promises.swift"; sourceTree = "<group>"; };
7774780E222228E50010BA4D /* PromiseKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = PromiseKit.framework; path = Carthage/Build/iOS/PromiseKit.framework; sourceTree = "<group>"; };
Expand Down Expand Up @@ -234,10 +232,6 @@
77B69EBA224E5F0E00DB4319 /* GetReceivedTransactions.RPC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetReceivedTransactions.RPC.swift; sourceTree = "<group>"; };
77B69EBE224E92C900DB4319 /* ConseilClientTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConseilClientTests.swift; sourceTree = "<group>"; };
77B69EC0224F2A2800DB4319 /* GetOriginatedAccountsRPC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetOriginatedAccountsRPC.swift; sourceTree = "<group>"; };
77B69ED0225348B400DB4319 /* ConseilClient+Promises.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ConseilClient+Promises.swift"; sourceTree = "<group>"; };
77B69ED422534F2B00DB4319 /* ConseilClientIntegrationTests+Promises.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ConseilClientIntegrationTests+Promises.swift"; sourceTree = "<group>"; };
77B69ECA22510FE200DB4319 /* ConseilPlatformTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConseilPlatformTest.swift; sourceTree = "<group>"; };
77B69ECC2251104300DB4319 /* ConseilNetworkTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConseilNetworkTest.swift; sourceTree = "<group>"; };
77B69EC222510C6500DB4319 /* ConseilEntity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConseilEntity.swift; sourceTree = "<group>"; };
77B69EC422510D5400DB4319 /* ConseilEntityTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConseilEntityTest.swift; sourceTree = "<group>"; };
77B69ECA22510FE200DB4319 /* ConseilPlatformTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConseilPlatformTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -437,6 +431,7 @@
77B69EA8224C39AC00DB4319 /* ConseilNetwork.swift */,
77B69EAA224C3A5300DB4319 /* GetSentTransactionsRPC.swift */,
77B69EC0224F2A2800DB4319 /* GetOriginatedAccountsRPC.swift */,
7741B95C226DF5860005B398 /* GetOriginatedContractsRPC.swift */,
77B69EBA224E5F0E00DB4319 /* GetReceivedTransactions.RPC.swift */,
77B69EAE224C435B00DB4319 /* TransactionsResponseAdapter.swift */,
77B69EB8224E5C6500DB4319 /* ConseilQueryRPC.swift */,
Expand Down Expand Up @@ -920,6 +915,7 @@
77CE387A21FC7ED8006ADABA /* PreapplyOperationRPC.swift in Sources */,
77CE387121FC7ED8006ADABA /* RPC.swift in Sources */,
77CE386321FC7ED8006ADABA /* GetAddressCounterRPC.swift in Sources */,
7741B95D226DF5860005B398 /* GetOriginatedContractsRPC.swift in Sources */,
77DEB6E522373B0E00E4733D /* Result.swift in Sources */,
77CE386721FC7ED8006ADABA /* TezResponseAdapter.swift in Sources */,
77CE385C21FC7ED8006ADABA /* OperationFees.swift in Sources */,
Expand Down
19 changes: 17 additions & 2 deletions TezosKit/Conseil/ConseilClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,29 @@ public class ConseilClient: AbstractClient {
///
/// - Parameters:
/// - account: The account to query.
/// - limit: The number of transactions to return, defaults to 100.
/// - limit: The number of accounts to return, defaults to 100.
/// - completion: A completion callback.
public func originatedAccounts(
from account: String,
limit: Int = 100,
completion: @escaping (Result<[[String: Any]], TezosKitError>) -> Void
) {
let rpc = GetOriginatedAccounts(account: account, limit: limit)
let rpc = GetOriginatedAccountsRPC(account: account, limit: limit)
send(rpc, completion: completion)
}

/// Retrieve originated contracts.
///
/// - Parameters:
/// - account: The account to query.
/// - limit: The number of contracts to return, defaults to 100.
/// - completion: A completion callback.
public func originatedContracts(
from account: String,
limit: Int = 100,
completion: @escaping (Result<[[String: Any]], TezosKitError>) -> Void
) {
let rpc = GetOriginatedContractsRPC(account: account, limit: limit)
send(rpc, completion: completion)
}

Expand Down
2 changes: 1 addition & 1 deletion TezosKit/Conseil/GetOriginatedAccountsRPC.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Keefer Taylor, 2019.

/// An RPC which fetches originated accounts for an account.
public class GetOriginatedAccounts: ConseilQueryRPC<[[String: Any]]> {
public class GetOriginatedAccountsRPC: ConseilQueryRPC<[[String: Any]]> {
/// - Parameters:
/// - account: The account to query.
/// - limit: The number of items to return.
Expand Down
26 changes: 26 additions & 0 deletions TezosKit/Conseil/GetOriginatedContractsRPC.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright Keefer Taylor, 2019.

/// An RPC which fetches originated contracts for an account.
public class GetOriginatedContractsRPC: ConseilQueryRPC<[[String: Any]]> {
/// - Parameters:
/// - account: The account to query.
/// - limit: The number of items to return.
public init(account: String, limit: Int) {
let predicates: [ConseilPredicate] = [
ConseilQuery.Predicates.predicateWith(field: "manager", set: [account]),
ConseilQuery.Predicates.predicateWith(
field: "script",
operation: ConseilQuery.Predicates.Operation.isNull,
inverse: true
)
]
let orderBy: ConseilOrderBy = ConseilQuery.OrderBy.orderBy(field: "block_level")
let query: [String: Any] = ConseilQuery.query(predicates: predicates, orderBy: orderBy, limit: limit)

super.init(
query: query,
entity: .account,
responseAdapterClass: JSONArrayResponseAdapter.self
)
}
}
8 changes: 2 additions & 6 deletions TezosKit/RPC/GetAddressCodeRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

import Foundation

/**
* An RPC which will retrieve the code associated with a smart contract.
*/
/// An RPC which will retrieve the code associated with a smart contract.
public class GetAddressCodeRPC: RPC<ContractCode> {
/**
* - Parameter address: The address of the contract to load.
*/
/// - Parameter address: The address of the contract to load.
public init(address: String) {
let endpoint = "chains/main/blocks/head/context/contracts/\(address)/script"
super.init(endpoint: endpoint, responseAdapterClass: ContractCodeResponseAdapter.self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import Foundation

/**
* Parse a given response as code and storage for a contract.
*/
/// Parse a given response as code and storage for a contract.
public class ContractCodeResponseAdapter: AbstractResponseAdapter<ContractCode> {
public override class func parse(input: Data) -> ContractCode? {
guard let parsedDictionary = JSONDictionaryResponseAdapter.parse(input: input),
Expand Down

0 comments on commit bc498e9

Please sign in to comment.