Skip to content

Commit

Permalink
More parity changes
Browse files Browse the repository at this point in the history
  • Loading branch information
qtbeee committed Jan 11, 2020
1 parent 07f2e51 commit 9968261
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 61 deletions.
27 changes: 19 additions & 8 deletions Sources/Hedera/TransactionId.swift
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import SwiftProtobuf
import Foundation
import NIO

public struct TransactionId {
let accountId: AccountId
let transactionValidStart: Date
public let accountId: AccountId
public let validStart: Date

public init(account id: AccountId) {
accountId = id

// Allow the transaction to be accepted as long as the
// server is not more than 10 seconds behind us
transactionValidStart = Date(timeIntervalSinceNow: -10)
validStart = Date(timeIntervalSinceNow: -10)
}

public init(account id: AccountId, validStart: Date) {
accountId = id
transactionValidStart = validStart
self.validStart = validStart
}
}

extension TransactionId: CustomStringConvertible, CustomDebugStringConvertible {
public var description: String {
"\(accountId)@\(transactionValidStart.wholeSecondsSince1970).\(transactionValidStart.nanosSinceSecondSince1970)"
"\(accountId)@\(validStart.wholeSecondsSince1970).\(validStart.nanosSinceSecondSince1970)"
}

public var debugDescription: String {
Expand All @@ -38,7 +39,7 @@ extension TransactionId: LosslessStringConvertible {
guard let start = Date(String(atParts[atParts.startIndex.advanced(by: 1)])) else { return nil }

accountId = AccountId(id)
transactionValidStart = start
validStart = start
}
}

Expand All @@ -48,7 +49,7 @@ extension TransactionId: ProtoConvertible {
func toProto() -> Proto {
var proto = Proto()
proto.accountID = accountId.toProto()
proto.transactionValidStart = transactionValidStart.toProto()
proto.transactionValidStart = validStart.toProto()

return proto
}
Expand All @@ -57,6 +58,16 @@ extension TransactionId: ProtoConvertible {
guard proto.hasTransactionValidStart && proto.hasAccountID else { return nil }

accountId = AccountId(proto.accountID)
transactionValidStart = Date(proto.transactionValidStart)
validStart = Date(proto.transactionValidStart)
}
}

extension TransactionId {
public func getReceipt(client: Client) -> EventLoopFuture<TransactionReceipt> {
TransactionReceiptQuery().setTransactionId(self).execute(client: client)
}

public func getRecord(client: Client) -> EventLoopFuture<TransactionRecord> {
TransactionRecordQuery().setTransactionId(self).execute(client: client)
}
}
38 changes: 29 additions & 9 deletions Sources/Hedera/TransactionReceipt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,37 @@ import SwiftProtobuf
import Foundation

public final class TransactionReceipt {
let inner: Proto_TransactionReceipt

public let status: UInt32
public let accountId: AccountId?
public let fileId: FileId?
public let contractId: ContractId?
public let exchangeRateSet: ExchangeRateSet?

init(_ proto: Proto_TransactionReceipt) {
self.status = UInt32(proto.status.rawValue)
self.accountId = proto.hasAccountID ? AccountId(proto.accountID) : nil
self.fileId = proto.hasFileID ? FileId(proto.fileID) : nil
self.contractId = proto.hasContractID ? ContractId(proto.contractID) : nil
self.exchangeRateSet = proto.hasExchangeRate ? ExchangeRateSet(proto.exchangeRate) : nil
inner = proto

status = UInt32(proto.status.rawValue)
}

public var accountId: AccountId {
guard inner.hasAccountID else {
fatalError("Receipt does not contain an AccountId")
}

return AccountId(inner.accountID)
}

public var fileId: FileId {
guard inner.hasFileID else {
fatalError("Receipt does not contain a FileId")
}

return FileId(inner.fileID)
}

public var contractId: ContractId {
guard inner.hasContractID else {
fatalError("Receipt does not contain a ContractId")
}

return ContractId(inner.contractID)
}
}
34 changes: 17 additions & 17 deletions Sources/Hedera/TransactionRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import Sodium
import Foundation

public struct TransactionRecord {
let receipt: TransactionReceipt?
let transactionHash: Data
let consensusTimestamp: Date
let transactionId: TransactionId
let memo: String
let transactionFee: UInt64
public let transactionId: TransactionId
public let transactionHash: Bytes
public let transactionFee: UInt64
public let consensusTimestamp: Date
public let transactionMemo: String?
public let receipt: TransactionReceipt
public let transfers: [Transfer]
// let contractResult: ContractResultType?
let transferList: [AccountAmount]

init(_ proto: Proto_TransactionRecord) {
self.receipt = proto.hasReceipt ? TransactionReceipt(proto.receipt) : nil
self.transactionHash = proto.transactionHash
self.consensusTimestamp = Date(proto.consensusTimestamp)
self.transactionId = TransactionId(proto.transactionID)!
self.memo = proto.memo
self.transactionFee = UInt64(proto.transactionFee)
transactionId = TransactionId(proto.transactionID)!
transactionHash = Bytes(proto.transactionHash)
transactionFee = UInt64(proto.transactionFee)
consensusTimestamp = Date(proto.consensusTimestamp)
transactionMemo = proto.memo.isEmpty ? nil : proto.memo
receipt = TransactionReceipt(proto.receipt)
transfers = proto.transferList.accountAmounts.map { Transfer($0) }
// self.contractResult = ContractResultType(proto.body)
self.transferList = proto.transferList.accountAmounts.map { AccountAmount($0) }
}
}

public struct AccountAmount {
let accountId: AccountId
let amount: UInt64
public struct Transfer {
public let accountId: AccountId
public let amount: UInt64

init(_ proto: Proto_AccountAmount) {
accountId = AccountId(proto.accountID)
Expand Down
1 change: 1 addition & 0 deletions Sources/Hedera/TransactionRecordQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public final class TransactionRecordQuery: QueryBuilder<TransactionRecord> {
body.transactionGetRecord = Proto_TransactionGetRecordQuery()
}

@discardableResult
public func setTransactionId(_ id: TransactionId) -> Self {
body.transactionGetRecord.transactionID = id.toProto()

Expand Down
8 changes: 4 additions & 4 deletions Sources/Hedera/account/AccountInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import Foundation
public struct AccountInfo {
let accountId: AccountId
let contractAccountId: String?
let deleted: Bool
let isDeleted: Bool
let proxyAccountId: AccountId?
let proxyReceived: UInt64
let key: PublicKey
let balance: UInt64
let generateSendRecordThreshold: UInt64
let generateReceiveRecordThreshold: UInt64
let receiverSigRequired: Bool
let isReceiverSigRequired: Bool
let expirationTime: Date
let autoRenewPeriod: TimeInterval

Expand All @@ -24,14 +24,14 @@ public struct AccountInfo {

accountId = AccountId(accountInfo.accountID)
contractAccountId = accountInfo.contractAccountID.isEmpty ? nil : accountInfo.contractAccountID
deleted = accountInfo.deleted
isDeleted = accountInfo.deleted
self.proxyAccountId = proxyAccountId
proxyReceived = UInt64(accountInfo.proxyReceived)
key = PublicKey.fromProto(accountInfo.key)!
balance = accountInfo.balance
generateSendRecordThreshold = accountInfo.generateSendRecordThreshold
generateReceiveRecordThreshold = accountInfo.generateReceiveRecordThreshold
receiverSigRequired = accountInfo.receiverSigRequired
isReceiverSigRequired = accountInfo.receiverSigRequired
expirationTime = Date(accountInfo.expirationTime)
autoRenewPeriod = TimeInterval(accountInfo.autoRenewPeriod)!
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Hedera/contract/ContractExecuteTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ContractExecuteTransaction: TransactionBuilder {
///
/// The function must be payable to use this method
@discardableResult
public func setAmount(_ amount: UInt64) -> Self {
public func setPayableAmount(_ amount: UInt64) -> Self {
body.contractCall.amount = Int64(amount)

return self
Expand Down
8 changes: 4 additions & 4 deletions Sources/Hedera/contract/ContractInfoQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ public struct ContractInfo {
let contractId: ContractId
let accountId: AccountId
let contractAccountId: String
let adminKey: Ed25519PublicKey?
let adminKey: PublicKey?
let expirationTime: Date
let autoRenewPeriod: TimeInterval
let storage: UInt64
let memo: String
let contractMemo: String

init(_ contractInfo: Proto_ContractGetInfoResponse.ContractInfo) {
contractId = ContractId(contractInfo.contractID)
accountId = AccountId(contractInfo.accountID)
contractAccountId = contractInfo.contractAccountID
adminKey = Ed25519PublicKey(contractInfo.adminKey)
adminKey = PublicKey.fromProto(contractInfo.adminKey)
expirationTime = Date(contractInfo.expirationTime)
autoRenewPeriod = TimeInterval(contractInfo.autoRenewPeriod)!
storage = UInt64(contractInfo.storage)
memo = contractInfo.memo
contractMemo = contractInfo.memo
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public class ContractGetRecordsQuery: QueryBuilder<[TransactionRecord]> {
public class ContractRecordsQuery: QueryBuilder<[TransactionRecord]> {
public override init() {
super.init()

Expand Down
16 changes: 3 additions & 13 deletions Sources/Hedera/file/FileContentsQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@ import SwiftProtobuf
import Foundation
import Sodium

public struct FileContents {
let fileId: FileId
let contents: Data

init(_ contents: Proto_FileGetContentsResponse.FileContents) {
fileId = FileId(contents.fileID)
self.contents = contents.contents
}
}

public class FileContentsQuery: QueryBuilder<FileContents> {
public class FileContentsQuery: QueryBuilder<Bytes> {
public override init() {
super.init()

Expand All @@ -29,11 +19,11 @@ public class FileContentsQuery: QueryBuilder<FileContents> {
callback(&body.fileGetContents.header)
}

override func mapResponse(_ response: Proto_Response) -> FileContents {
override func mapResponse(_ response: Proto_Response) -> Bytes {
guard case .fileGetContents(let response) = response.response else {
fatalError("unreachable: response is not fileGetContents")
}

return FileContents(response.fileContents)
return Bytes(response.fileContents.contents)
}
}
8 changes: 4 additions & 4 deletions Sources/Hedera/file/FileInfoQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ public struct FileInfo {
let fileId: FileId
let size: UInt64
let expirationTime: Date
let deleted: Bool
let keys: KeyList
let isDeleted: Bool
let keys: [PublicKey]

init(_ info: Proto_FileGetInfoResponse.FileInfo) {
fileId = FileId(info.fileID)
size = UInt64(info.size)
expirationTime = Date(info.expirationTime)
deleted = info.deleted
keys = KeyList(info.keys)!
isDeleted = info.deleted
keys = KeyList(info.keys)!.keys
}
}

Expand Down

0 comments on commit 9968261

Please sign in to comment.