Skip to content

Commit

Permalink
Add grpc protos
Browse files Browse the repository at this point in the history
  • Loading branch information
qtbeee committed Sep 30, 2019
1 parent 10a8ada commit beb008d
Show file tree
Hide file tree
Showing 52 changed files with 3,251 additions and 1,510 deletions.
45 changes: 31 additions & 14 deletions Sources/Hedera/Client.swift
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
import Sodium
import SwiftGRPC

public typealias Node = (accountId: AccountId, address: String)

let defaultMaxTransactionFee: UInt64 = 100_000_000

public struct Client {
let operatorId: AccountId
let operatorSigner: (Bytes) -> Bytes
var operatorId: AccountId?
var operatorSigner: ((Bytes) -> Bytes)?

let nodes: [AccountId: Node]
var nodes: [AccountId: Node]
var node: AccountId?

var channels: [AccountId: Channel] = [:]

/// The default maximum fee for a transaction.
/// This can be overridden for an individual transaction with `.setTransactionFee()`.
let maxTransactionFee = defaultMaxTransactionFee
var maxTransactionFee = defaultMaxTransactionFee

// TODO: once queries are implemented
// /// The maximum payment that can be automatically attached to a query.
// /// If this is not set, payments will not be made automatically for queries.
// /// This can be overridden for an individual query with `.setPaymentDefault()`.
// var maxQueryPayment: UInt64?

}

public struct ClientBuilder {
var operatorId: AccountId?
var operatorSigner: ((Bytes) -> Bytes)?
var node: AccountId?

public init() {}
public init(node id: AccountId, address url: String) {
nodes = [ id: Node(accountId: id, address: url) ]
}

public mutating func setOperator(id: AccountId, secret: Ed25519PrivateKey) -> Self {
operatorId = id
Expand All @@ -40,12 +38,31 @@ public struct ClientBuilder {
public mutating func setOperator(id: AccountId, signer: @escaping (Bytes) -> Bytes) -> Self {
operatorId = id
operatorSigner = signer

return self
}

public mutating func setNode(_ id: AccountId) -> Self {
node = id
return self
}
}

public mutating func addNode(id: AccountId, address url: String) -> Self {
nodes[id] = Node(accountId: id, address: url)
return self
}

func pickNode() -> Node {
nodes.randomElement()!.value
}

mutating func channelFor(node: Node) -> Channel {
// TODO: what if the node is not on the client?
if let channel = channels[node.accountId] {
return channel
} else {
channels[node.accountId] = Channel(address: node.address)
return channels[node.accountId]!
}
}
}
Loading

0 comments on commit beb008d

Please sign in to comment.