Skip to content

Commit

Permalink
fix tests and candidTuple0
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantinos Gaitanis committed Oct 8, 2024
1 parent b3ff647 commit ba72e04
Show file tree
Hide file tree
Showing 20 changed files with 211 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Sources/Candid/Encoding/CandidDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private class CandidUnkeyedDecodingContainer: UnkeyedDecodingContainer {
var count: Int? { inputVector.count }
var isAtEnd: Bool { currentIndex == count }
private let inputVector: [CandidValue]
private (set) var currentIndex: Int = 0
private(set) var currentIndex: Int = 0
private let rootCodingPath: [CodingKey]

init(_ input: CandidValue, _ codingPath: [CodingKey]) throws {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Candid/Encoding/CandidEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public enum CandidEncoderError: Error {
private class CandidValueEncoder: Encoder {
let userInfo: [CodingUserInfoKey : Any] = [:]
let codingPath: [CodingKey]
private (set) var encodingValue: CandidEncodingValue!
private(set) var encodingValue: CandidEncodingValue!
var candidValue: CandidValue { encodingValue.candidValue }

init(_ codingPath: [CodingKey] = []) {
Expand Down Expand Up @@ -254,7 +254,7 @@ private protocol CandidEncodingValue: AnyObject {
}

private class CandidSingleEncodingValue: CandidEncodingValue {
private (set) var candidValue: CandidValue!
private(set) var candidValue: CandidValue!

func set(_ value: CandidValue) { candidValue = value }
func set(_ value: Bool) { candidValue = .bool(value) }
Expand Down
2 changes: 1 addition & 1 deletion Sources/Candid/Parser/CandidParserStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private extension CandidParserToken {
private struct CommentSkippingIterator: IteratorProtocol {
private let array: [CandidParsedRange]
private var iterator: [CandidParsedRange].Iterator
private (set) var currentStringIndex: String.Index?
private(set) var currentStringIndex: String.Index?

init(_ array: [CandidParsedRange]) {
self.array = array
Expand Down
4 changes: 2 additions & 2 deletions Sources/Candid/Parser/CandidTypeParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ private extension CandidTypeParser {
}

private class ParsingContext {
private (set) var namedTypes: [CandidNamedType] = []
private (set) var service: CandidInterfaceDefinition.ServiceDefinition?
private(set) var namedTypes: [CandidNamedType] = []
private(set) var service: CandidInterfaceDefinition.ServiceDefinition?

subscript (_ name: String) -> CandidType? {
return namedTypes[name]
Expand Down
2 changes: 1 addition & 1 deletion Sources/Candid/Parser/CandidValueParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ private extension CandidValueParser {
} else {
exponent = 0
}
let decimal = Decimal(sign: significandL.sign == .plus ? .plus : .minus, exponent: exponent, significand: significand)
let decimal = Decimal(sign: .plus, exponent: exponent, significand: significand)
return .float64(Double(truncating: decimal as NSNumber))
}
return nil
Expand Down
8 changes: 2 additions & 6 deletions Sources/Candid/Utils/OrderIndependentHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private struct OIHasher: PotentCodables.EncodesToData {
let valueHash = try hash(value)
return keyHash + valueHash
}
.sorted()
.sorted(by: compareData)
.reduce(Data(), +)

default:
Expand Down Expand Up @@ -109,11 +109,7 @@ private struct OIHasher: PotentCodables.EncodesToData {
return Leb128.encodeUnsigned(bigInt.magnitude)
}


}

extension Data: Comparable {
public static func < (lhs: Data, rhs: Data) -> Bool {
private func compareData(_ lhs: Data, _ rhs: Data) -> Bool {
guard lhs.count == rhs.count else {
return lhs.count < rhs.count
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/CodeGenerator/Generator/CandidCodeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,7 @@ fileprivate extension CandidType {

func swiftType() -> String {
switch self {
case .null, .reserved, .empty:
return "" // ???
case .null, .reserved, .empty: return "CandidTuple0"
case .bool: return "Bool"
case .natural: return "BigUInt"
case .integer: return "BigInt"
Expand All @@ -348,6 +347,9 @@ fileprivate extension CandidType {
guard keyedTypes.isTuple else {
fatalError("all non-tuple records should be named by now")
}
guard keyedTypes.count > 0 else {
return "CandidTuple0"
}
return "CandidTuple\(keyedTypes.count)<\(keyedTypes.map { $0.type.swiftType() }.joined(separator: ", "))>"
case .variant:
fatalError("all variants should be named by now")
Expand Down
6 changes: 3 additions & 3 deletions Sources/CodeGenerator/Generator/CodeGenerationContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import Foundation
import Candid

class CodeGenerationContext {
private (set) var namedTypes: [CandidNamedType] = []
private (set) var service: CodeGeneratorCandidService?
private (set) var candidValueSimplifiedType: CandidType?
private(set) var namedTypes: [CandidNamedType] = []
private(set) var service: CodeGeneratorCandidService?
private(set) var candidValueSimplifiedType: CandidType?

init(from interface: CandidInterfaceDefinition) throws {
let swiftSanitizedInterface = replacingReservedKeywords(interface)
Expand Down
2 changes: 1 addition & 1 deletion Sources/CodeGenerator/Generator/IndentedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

class IndentedString {
private (set) var output: String = ""
private(set) var output: String = ""
private var indent: Int = 0

init() {}
Expand Down
2 changes: 1 addition & 1 deletion Sources/DAB/Token/Generated/Index.did.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Foundation
import IcpKit
import BigInt

enum Index {
enum Index {
/// type Block = Value;
typealias Block = Value

Expand Down
26 changes: 12 additions & 14 deletions Sources/DAB/Token/Generated/NNS_SNS_W.did.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import IcpKit
import BigInt

enum NNS_SNS_W {
struct EmptyRecord: Codable {}

/// type GetWasmRequest = record {
/// hash : blob;
/// };
Expand Down Expand Up @@ -665,9 +663,9 @@ enum NNS_SNS_W {
}

/// get_allowed_principals : (record {}) -> (GetAllowedPrincipalsResponse) query;
func get_allowed_principals(sender: ICPSigningPrincipal? = nil) async throws -> GetAllowedPrincipalsResponse {
let caller = ICPQueryNoArgs<GetAllowedPrincipalsResponse>(canister, "get_allowed_principals")
let response = try await caller.callMethod(client, sender: sender)
func get_allowed_principals(_ arg0: CandidTuple0, sender: ICPSigningPrincipal? = nil) async throws -> GetAllowedPrincipalsResponse {
let caller = ICPQuery<CandidTuple0, GetAllowedPrincipalsResponse>(canister, "get_allowed_principals")
let response = try await caller.callMethod(arg0, client, sender: sender)
return response
}

Expand All @@ -681,9 +679,9 @@ enum NNS_SNS_W {
}

/// get_latest_sns_version_pretty : (null) -> (vec record { text; text }) query;
func get_latest_sns_version_pretty(sender: ICPSigningPrincipal? = nil) async throws -> [CandidTuple2<String, String>] {
let caller = ICPQueryNoArgs<[CandidTuple2<String, String>]>(canister, "get_latest_sns_version_pretty")
let response = try await caller.callMethod(client, sender: sender)
func get_latest_sns_version_pretty(_ arg0: CandidTuple0, sender: ICPSigningPrincipal? = nil) async throws -> [CandidTuple2<String, String>] {
let caller = ICPQuery<CandidTuple0, [CandidTuple2<String, String>]>(canister, "get_latest_sns_version_pretty")
let response = try await caller.callMethod(arg0, client, sender: sender)
return response
}

Expand All @@ -706,9 +704,9 @@ enum NNS_SNS_W {
}

/// get_sns_subnet_ids : (record {}) -> (GetSnsSubnetIdsResponse) query;
func get_sns_subnet_ids(sender: ICPSigningPrincipal? = nil) async throws -> GetSnsSubnetIdsResponse {
let caller = ICPQueryNoArgs<GetSnsSubnetIdsResponse>(canister, "get_sns_subnet_ids")
let response = try await caller.callMethod(client, sender: sender)
func get_sns_subnet_ids(_ arg0: CandidTuple0, sender: ICPSigningPrincipal? = nil) async throws -> GetSnsSubnetIdsResponse {
let caller = ICPQuery<CandidTuple0, GetSnsSubnetIdsResponse>(canister, "get_sns_subnet_ids")
let response = try await caller.callMethod(arg0, client, sender: sender)
return response
}

Expand Down Expand Up @@ -738,9 +736,9 @@ enum NNS_SNS_W {
}

/// list_deployed_snses : (record {}) -> (ListDeployedSnsesResponse) query;
func list_deployed_snses(sender: ICPSigningPrincipal? = nil) async throws -> ListDeployedSnsesResponse {
let caller = ICPQuery<EmptyRecord, ListDeployedSnsesResponse>(canister, "list_deployed_snses")
let response = try await caller.callMethod(EmptyRecord(), client, sender: sender)
func list_deployed_snses(_ arg0: CandidTuple0, sender: ICPSigningPrincipal? = nil) async throws -> ListDeployedSnsesResponse {
let caller = ICPQuery<CandidTuple0, ListDeployedSnsesResponse>(canister, "list_deployed_snses")
let response = try await caller.callMethod(arg0, client, sender: sender)
return response
}

Expand Down
132 changes: 132 additions & 0 deletions Sources/DAB/Token/Generated/icrc1_oracle.did.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//
// This file was generated using IcpKit CandidCodeGenerator
// For more information visit https://github.com/kosta-bity/IcpKit
//

import Foundation
import IcpKit
import BigInt

enum icrc1_oracle {
/// type Category = variant {
/// Sns;
/// Known;
/// Spam;
/// ChainFusionTestnet;
/// ChainFusion;
/// Community;
/// Native;
/// };
enum Category: Codable {
case Sns
case Spam
case Native
case Known
case ChainFusionTestnet
case ChainFusion
case Community

enum CodingKeys: String, CandidCodingKey {
case Sns
case Spam
case Native
case Known
case ChainFusionTestnet
case ChainFusion
case Community
}
}

/// type Conf = record {
/// controllers : opt vec principal;
/// im_canister : opt principal;
/// };
struct Conf: Codable {
let controllers: [ICPPrincipal]?
let im_canister: ICPPrincipal?
}

/// type ICRC1 = record {
/// logo : opt text;
/// name : text;
/// ledger : text;
/// category : Category;
/// index : opt text;
/// symbol : text;
/// decimals : nat8;
/// fee : nat;
/// };
struct ICRC1: Codable {
let fee: BigUInt
let decimals: UInt8
let logo: String?
let name: String
let ledger: String
let category: Category
let index: String?
let symbol: String
}

/// type ICRC1Request = record {
/// logo : opt text;
/// name : text;
/// ledger : text;
/// index : opt text;
/// symbol : text;
/// decimals : nat8;
/// fee : nat;
/// };
struct ICRC1Request: Codable {
let fee: BigUInt
let decimals: UInt8
let logo: String?
let name: String
let ledger: String
let index: String?
let symbol: String
}


/// service : (opt Conf) -> {
/// get_all_icrc1_canisters : () -> (vec ICRC1) query;
/// replace_icrc1_canisters : (vec ICRC1) -> ();
/// store_new_icrc1_canisters : (vec ICRC1) -> ();
/// store_icrc1_canister : (ICRC1Request) -> ();
/// sync_controllers: () -> (vec text);
/// }
class Service: ICPService {
/// get_all_icrc1_canisters : () -> (vec ICRC1) query;
func get_all_icrc1_canisters(sender: ICPSigningPrincipal? = nil) async throws -> [ICRC1] {
let caller = ICPQueryNoArgs<[ICRC1]>(canister, "get_all_icrc1_canisters")
let response = try await caller.callMethod(client, sender: sender)
return response
}

/// replace_icrc1_canisters : (vec ICRC1) -> ();
func replace_icrc1_canisters(_ arg0: [ICRC1], sender: ICPSigningPrincipal? = nil) async throws {
let caller = ICPCallNoResult<[ICRC1]>(canister, "replace_icrc1_canisters")
let _ = try await caller.callMethod(arg0, client, sender: sender)
}

/// store_new_icrc1_canisters : (vec ICRC1) -> ();
func store_new_icrc1_canisters(_ arg0: [ICRC1], sender: ICPSigningPrincipal? = nil) async throws {
let caller = ICPCallNoResult<[ICRC1]>(canister, "store_new_icrc1_canisters")
let _ = try await caller.callMethod(arg0, client, sender: sender)
}

/// store_icrc1_canister : (ICRC1Request) -> ();
func store_icrc1_canister(_ arg0: ICRC1Request, sender: ICPSigningPrincipal? = nil) async throws {
let caller = ICPCallNoResult<ICRC1Request>(canister, "store_icrc1_canister")
let _ = try await caller.callMethod(arg0, client, sender: sender)
}

/// sync_controllers: () -> (vec text);
func sync_controllers(sender: ICPSigningPrincipal? = nil) async throws -> [String] {
let caller = ICPCallNoArgs<[String]>(canister, "sync_controllers")
let response = try await caller.callMethod(client, sender: sender)
return response
}

}

}
2 changes: 1 addition & 1 deletion Sources/DAB/Token/Index/ICPTransactionProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ICPTransactionProvider {

private func deployedSnses() async throws -> [NNS_SNS_W.DeployedSns] {
if let cachedSnses = cachedSnses { return cachedSnses }
cachedSnses = try await service.list_deployed_snses().instances
cachedSnses = try await service.list_deployed_snses(.init()).instances
return cachedSnses!
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/IcpKit/Cryptography/ICPBlock.Transaction+Hash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ private extension ICPBlock.Transaction.Operation {
3: [0: CBOR(fee)],
]
]
case .approve(let from, let allowance, _, let fee, _, let spender):
return nil
default: return nil
// TODO: Can not find any docs for this...
// case .approve(let from, let allowance, _, let fee, _, let spender):
// return [
// 3: [
// 0: CBOR(from.hex),
Expand Down
21 changes: 20 additions & 1 deletion Sources/IcpKit/Models/CandidTuple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@

import Foundation

public struct CandidTuple0 {
public init() {}
}

public struct CandidTuple1<T0> {
public let _0: T0
public var tuple: (T0) { (_0) }

public init(_ _0: T0) {
self._0 = _0
}

enum CodingKeys: Int, CodingKey {
case _0
}
}

public struct CandidTuple2<T0, T1> {
public let _0: T0
public let _1: T1
Expand Down Expand Up @@ -147,7 +164,9 @@ public struct CandidTuple7<T0, T1, T2, T3, T4, T5, T6> {
case _6
}
}

extension CandidTuple0: Codable {}
extension CandidTuple1: Encodable where T0: Encodable {}
extension CandidTuple1: Decodable where T0: Decodable {}
extension CandidTuple2: Encodable where T0: Encodable, T1: Encodable {}
extension CandidTuple2: Decodable where T0: Decodable, T1: Decodable {}
extension CandidTuple3: Encodable where T0: Encodable, T1: Encodable, T2: Encodable {}
Expand Down
1 change: 1 addition & 0 deletions Tests/CodeGeneratorTests/DidFiles/TestImports.did
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Record = record {
b: nat;
c: record { bool; text };
};
type EmptyRecord = record {};
type UnnamedType0 = record { vec opt int8; nat8 };
type RepeatedRecord = record { vec opt int8; nat8 };
type Variant = variant {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ enum TestImports {
/// type AData = blob;
typealias AData = Data

typealias EmptyRecord = CandidTuple0

/// type Function00 = func () -> ();
typealias Function00 = ICPCallNoArgsNoResult

Expand Down
Loading

0 comments on commit ba72e04

Please sign in to comment.