Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HIP-904: Allow autoMaxTokenAssociation to accept -1 & TokenReject #360

Merged
merged 10 commits into from
Jul 26, 2024
5 changes: 2 additions & 3 deletions Sources/Hedera/Account/AccountBalance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import Foundation
import HederaProtobufs

private struct TokenBalance {
private struct TokenBalance: Sendable {
fileprivate let id: TokenId
fileprivate let balance: UInt64
fileprivate let decimals: UInt32
Expand Down Expand Up @@ -67,11 +67,10 @@ public struct AccountBalance: Sendable {
/// Token balances for the referenced account.
///
/// This access is *`O(n)`*.
@available(*, deprecated, message: "use a mirror query")
public var tokenBalances: [TokenId: UInt64] { tokenBalancesInner }

// hack to work around deprecated warning
private var tokenDecimalsInner: [TokenId: UInt32] {
public var tokenDecimalsInner: [TokenId: UInt32] {
Dictionary(uniqueKeysWithValues: tokensInner.map { ($0.id, $0.decimals) })
}

Expand Down
10 changes: 5 additions & 5 deletions Sources/Hedera/Account/AccountCreateTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class AccountCreateTransaction: Transaction {
autoRenewPeriod: Duration? = .days(90),
autoRenewAccountId: AccountId? = nil,
accountMemo: String = "",
maxAutomaticTokenAssociations: UInt32 = 0,
maxAutomaticTokenAssociations: Int32 = 0,
alias: EvmAddress? = nil,
stakedAccountId: AccountId? = nil,
stakedNodeId: UInt64? = nil,
Expand All @@ -59,7 +59,7 @@ public final class AccountCreateTransaction: Transaction {
self.receiverSignatureRequired = data.receiverSigRequired
self.autoRenewPeriod = data.hasAutoRenewPeriod ? .fromProtobuf(data.autoRenewPeriod) : nil
self.accountMemo = data.memo
self.maxAutomaticTokenAssociations = UInt32(data.maxAutomaticTokenAssociations)
self.maxAutomaticTokenAssociations = data.maxAutomaticTokenAssociations

if let id = data.stakedID {
switch id {
Expand Down Expand Up @@ -175,15 +175,15 @@ public final class AccountCreateTransaction: Transaction {
}

/// The maximum number of tokens that an Account can be implicitly associated with.
public var maxAutomaticTokenAssociations: UInt32 {
public var maxAutomaticTokenAssociations: Int32 {
willSet {
ensureNotFrozen()
}
}

/// Sets the maximum number of tokens that an Account can be implicitly associated with.
@discardableResult
public func maxAutomaticTokenAssociations(_ maxAutomaticTokenAssociations: UInt32) -> Self {
public func maxAutomaticTokenAssociations(_ maxAutomaticTokenAssociations: Int32) -> Self {
self.maxAutomaticTokenAssociations = maxAutomaticTokenAssociations

return self
Expand Down Expand Up @@ -288,7 +288,7 @@ extension AccountCreateTransaction: ToProtobuf {
autoRenewPeriod?.toProtobufInto(&proto.autoRenewPeriod)
// autoRenewAccountId?.toProtobufInto(&proto.autoRenewAccount)
proto.memo = accountMemo
proto.maxAutomaticTokenAssociations = Int32(maxAutomaticTokenAssociations)
proto.maxAutomaticTokenAssociations = maxAutomaticTokenAssociations

if let alias = alias {
proto.alias = alias.data
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 @@ -44,7 +44,7 @@ public struct AccountInfo: Sendable {
autoRenewPeriod: Duration?,
accountMemo: String,
ownedNfts: UInt64,
maxAutomaticTokenAssociations: UInt32,
maxAutomaticTokenAssociations: Int32,
aliasKey: PublicKey?,
ethereumNonce: UInt64,
ledgerId: LedgerId,
Expand Down Expand Up @@ -140,7 +140,7 @@ public struct AccountInfo: Sendable {
public let ownedNfts: UInt64

/// The maximum number of tokens that an Account can be implicitly associated with.
public let maxAutomaticTokenAssociations: UInt32
public let maxAutomaticTokenAssociations: Int32

/// The public key which aliases to this account.
public let aliasKey: PublicKey?
Expand Down Expand Up @@ -193,7 +193,7 @@ extension AccountInfo: TryProtobufCodable {
autoRenewPeriod: .fromProtobuf(autoRenewPeriod),
accountMemo: proto.memo,
ownedNfts: UInt64(proto.ownedNfts),
maxAutomaticTokenAssociations: UInt32(proto.maxAutomaticTokenAssociations),
maxAutomaticTokenAssociations: proto.maxAutomaticTokenAssociations,
aliasKey: try .fromAliasBytes(proto.alias),
ethereumNonce: UInt64(proto.ethereumNonce),
ledgerId: .fromBytes(proto.ledgerID),
Expand All @@ -217,7 +217,7 @@ extension AccountInfo: TryProtobufCodable {
autoRenewPeriod?.toProtobufInto(&proto.autoRenewPeriod)
proto.memo = accountMemo
proto.ownedNfts = Int64(ownedNfts)
proto.maxAutomaticTokenAssociations = Int32(maxAutomaticTokenAssociations)
proto.maxAutomaticTokenAssociations = maxAutomaticTokenAssociations
proto.alias = aliasKey?.toProtobufBytes() ?? Data()
proto.ethereumNonce = Int64(ethereumNonce)
proto.ledgerID = ledgerId.bytes
Expand Down
8 changes: 4 additions & 4 deletions Sources/Hedera/Account/AccountUpdateTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public final class AccountUpdateTransaction: Transaction {
self.accountMemo = data.hasMemo ? data.memo.value : nil
self.maxAutomaticTokenAssociations =
data.hasMaxAutomaticTokenAssociations
? UInt32(data.maxAutomaticTokenAssociations.value) : nil
? data.maxAutomaticTokenAssociations.value : nil
self.stakedAccountId = stakedAccountId
self.stakedNodeId = stakedNodeId
self.declineStakingReward = data.hasDeclineReward ? data.declineReward.value : nil
Expand Down Expand Up @@ -227,15 +227,15 @@ public final class AccountUpdateTransaction: Transaction {
}

/// The maximum number of tokens that an Account can be implicitly associated with.
public var maxAutomaticTokenAssociations: UInt32? {
public var maxAutomaticTokenAssociations: Int32? {
willSet {
ensureNotFrozen()
}
}

/// Sets the maximum number of tokens that an Account can be implicitly associated with.
@discardableResult
public func maxAutomaticTokenAssociations(_ maxAutomaticTokenAssociations: UInt32) -> Self {
public func maxAutomaticTokenAssociations(_ maxAutomaticTokenAssociations: Int32) -> Self {
self.maxAutomaticTokenAssociations = maxAutomaticTokenAssociations

return self
Expand Down Expand Up @@ -350,7 +350,7 @@ extension AccountUpdateTransaction: ToProtobuf {

if let maxAutomaticTokenAssociations = maxAutomaticTokenAssociations {
proto.maxAutomaticTokenAssociations = Google_Protobuf_Int32Value(
Int32(maxAutomaticTokenAssociations))
maxAutomaticTokenAssociations)
}

if let stakedAccountId = stakedAccountId {
Expand Down
6 changes: 6 additions & 0 deletions Sources/Hedera/AnyTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ internal enum ServicesTransactionDataList {
case tokenMint([Proto_TokenMintTransactionBody])
case tokenPause([Proto_TokenPauseTransactionBody])
case tokenRevokeKyc([Proto_TokenRevokeKycTransactionBody])
case tokenReject([Proto_TokenRejectTransactionBody])
case tokenUnfreeze([Proto_TokenUnfreezeAccountTransactionBody])
case tokenUnpause([Proto_TokenUnpauseTransactionBody])
case tokenUpdate([Proto_TokenUpdateTransactionBody])
Expand Down Expand Up @@ -235,6 +236,10 @@ internal enum ServicesTransactionDataList {
array.append(data)
self = .tokenUpdateNfts(array)

case (.tokenReject(var array), .tokenReject(let data)):
array.append(data)
self = .tokenReject(array)

default:
throw HError.fromProtobuf("mismatched transaction types")
}
Expand Down Expand Up @@ -302,6 +307,7 @@ extension ServicesTransactionDataList: TryFromProtobuf {
case .nodeDelete: throw HError.fromProtobuf("Unsupported transaction `NodeDeleteTransaction`")
case .nodeCreate: throw HError.fromProtobuf("Unsupported transaction `NodeCreateTransaction`")
case .nodeUpdate: throw HError.fromProtobuf("Unsupported transaction `NodeUpdateTransaction`")
case .tokenReject(let data): value = .tokenReject([data])
}

for transaction in iter {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Hedera/Contract/ContractCreateFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public final class ContractCreateFlow {
constructorParameters: Data = Data(),
gas: UInt64 = 0,
initialBalance: Hbar = .zero,
maxAutomaticTokenAssociations: UInt32 = 0,
maxAutomaticTokenAssociations: Int32 = 0,
declineStakingReward: Bool = false,
adminKey: Key? = nil,
autoRenewAccountId: AccountId? = nil,
Expand All @@ -79,7 +79,7 @@ public final class ContractCreateFlow {
fileprivate var constructorParameters: Data
fileprivate var gas: UInt64
fileprivate var initialBalance: Hbar
fileprivate var maxAutomaticTokenAssociations: UInt32
fileprivate var maxAutomaticTokenAssociations: Int32
fileprivate var declineStakingReward: Bool
fileprivate var adminKey: Key?
// fileprivate var proxyAccountId: AccountId?
Expand Down Expand Up @@ -220,7 +220,7 @@ public final class ContractCreateFlow {
}

/// The maximum number of tokens that the contract can be automatically associated with.
public var maxAutomaticTokenAssociations: UInt32 {
public var maxAutomaticTokenAssociations: Int32 {
get { contractCreateData.maxAutomaticTokenAssociations }
set(value) { contractCreateData.maxAutomaticTokenAssociations = value }
}
Expand All @@ -229,7 +229,7 @@ public final class ContractCreateFlow {
///
/// - Returns: `self`
@discardableResult
public func maxAutomaticTokenAssociations(_ maxAutomaticTokenAssociations: UInt32) -> Self {
public func maxAutomaticTokenAssociations(_ maxAutomaticTokenAssociations: Int32) -> Self {
self.maxAutomaticTokenAssociations = maxAutomaticTokenAssociations

return self
Expand Down
8 changes: 4 additions & 4 deletions Sources/Hedera/Contract/ContractCreateTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public final class ContractCreateTransaction: Transaction {
autoRenewPeriod: Duration? = .days(90),
constructorParameters: Data? = nil,
contractMemo: String = "",
maxAutomaticTokenAssociations: UInt32 = 0,
maxAutomaticTokenAssociations: Int32 = 0,
autoRenewAccountId: AccountId? = nil,
stakedAccountId: AccountId? = nil,
stakedNodeId: UInt64? = nil,
Expand Down Expand Up @@ -117,7 +117,7 @@ public final class ContractCreateTransaction: Transaction {
self.autoRenewPeriod = .fromProtobuf(data.autoRenewPeriod)
self.constructorParameters = !data.constructorParameters.isEmpty ? data.constructorParameters : nil
self.contractMemo = data.memo
self.maxAutomaticTokenAssociations = UInt32(data.maxAutomaticTokenAssociations)
self.maxAutomaticTokenAssociations = data.maxAutomaticTokenAssociations
self.autoRenewAccountId = data.hasAutoRenewAccountID ? try .fromProtobuf(data.autoRenewAccountID) : nil
self.stakedAccountId = stakedAccountId
self.stakedNodeId = stakedNodeId
Expand Down Expand Up @@ -285,15 +285,15 @@ public final class ContractCreateTransaction: Transaction {
}

/// The maximum number of tokens that this contract can be automatically associated with.
public var maxAutomaticTokenAssociations: UInt32 {
public var maxAutomaticTokenAssociations: Int32 {
willSet {
ensureNotFrozen()
}
}

/// Sets the maximum number of tokens that this contract can be automatically associated with.
@discardableResult
public func maxAutomaticTokenAssociations(_ maxAutomaticTokenAssociations: UInt32) -> Self {
public func maxAutomaticTokenAssociations(_ maxAutomaticTokenAssociations: Int32) -> Self {
self.maxAutomaticTokenAssociations = maxAutomaticTokenAssociations

return self
Expand Down
6 changes: 3 additions & 3 deletions Sources/Hedera/Contract/ContractInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public struct ContractInfo {
public let autoRenewAccountId: AccountId?

/// The maximum number of tokens that a contract can be implicitly associated with.
public let maxAutomaticTokenAssociations: UInt32
public let maxAutomaticTokenAssociations: Int32

/// Ledger ID for the network the response was returned from.
public let ledgerId: LedgerId
Expand Down Expand Up @@ -103,7 +103,7 @@ extension ContractInfo: TryProtobufCodable {
balance: .fromTinybars(Int64(proto.balance)),
isDeleted: proto.deleted,
autoRenewAccountId: try .fromProtobuf(autoRenewAccountId),
maxAutomaticTokenAssociations: UInt32(proto.maxAutomaticTokenAssociations),
maxAutomaticTokenAssociations: proto.maxAutomaticTokenAssociations,
ledgerId: .fromBytes(proto.ledgerID),
stakingInfo: try .fromProtobuf(proto.stakingInfo)
)
Expand All @@ -122,7 +122,7 @@ extension ContractInfo: TryProtobufCodable {
proto.balance = UInt64(balance.toTinybars())
proto.deleted = isDeleted
autoRenewAccountId?.toProtobufInto(&proto.autoRenewAccountID)
proto.maxAutomaticTokenAssociations = Int32(maxAutomaticTokenAssociations)
proto.maxAutomaticTokenAssociations = maxAutomaticTokenAssociations
proto.ledgerID = ledgerId.bytes
proto.stakingInfo = stakingInfo.toProtobuf()
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Hedera/Contract/ContractUpdateTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class ContractUpdateTransaction: Transaction {
adminKey: Key? = nil,
autoRenewPeriod: Duration? = nil,
contractMemo: String? = nil,
maxAutomaticTokenAssociations: UInt32? = nil,
maxAutomaticTokenAssociations: Int32? = nil,
autoRenewAccountId: AccountId? = nil,
proxyAccountId: AccountId? = nil,
stakedAccountId: AccountId? = nil,
Expand Down Expand Up @@ -87,7 +87,7 @@ public final class ContractUpdateTransaction: Transaction {
self.autoRenewPeriod = data.hasAutoRenewPeriod ? .fromProtobuf(data.autoRenewPeriod) : nil
self.contractMemo = memo
self.maxAutomaticTokenAssociations =
data.hasMaxAutomaticTokenAssociations ? UInt32(data.maxAutomaticTokenAssociations.value) : nil
data.hasMaxAutomaticTokenAssociations ? data.maxAutomaticTokenAssociations.value : nil
self.autoRenewAccountId = data.hasAutoRenewAccountID ? try .fromProtobuf(data.autoRenewAccountID) : nil
self.proxyAccountId = data.hasProxyAccountID ? try .fromProtobuf(data.proxyAccountID) : nil
self.stakedAccountId = stakedAccountId
Expand Down Expand Up @@ -180,15 +180,15 @@ public final class ContractUpdateTransaction: Transaction {
}

/// The maximum number of tokens that this contract can be automatically associated with.
public var maxAutomaticTokenAssociations: UInt32? {
public var maxAutomaticTokenAssociations: Int32? {
willSet {
ensureNotFrozen()
}
}

/// Sets the maximum number of tokens that this contract can be automatically associated with.
@discardableResult
public func maxAutomaticTokenAssociations(_ maxAutomaticTokenAssociations: UInt32?) -> Self {
public func maxAutomaticTokenAssociations(_ maxAutomaticTokenAssociations: Int32?) -> Self {
self.maxAutomaticTokenAssociations = maxAutomaticTokenAssociations

return self
Expand Down
8 changes: 4 additions & 4 deletions Sources/Hedera/FeeSchedule/RequestType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ public enum RequestType {
/// Delete a node
case nodeDelete

/// Get the info for a node
case nodeGetInfo
/// Reject a Token
case tokenReject

// this literally can't be smaller.
// swiftlint:disable:next function_body_length
Expand Down Expand Up @@ -341,7 +341,7 @@ public enum RequestType {
case .nodeCreate: self = .nodeCreate
case .nodeDelete: self = .nodeDelete
case .nodeUpdate: self = .nodeUpdate
case .nodeGetInfo: self = .nodeGetInfo
case .tokenReject: self = .tokenReject

case .UNRECOGNIZED(let code):
throw HError.fromProtobuf("unrecognized RequestType: `\(code)`")
Expand Down Expand Up @@ -427,7 +427,7 @@ public enum RequestType {
case .nodeCreate: return .nodeCreate
case .nodeUpdate: return .nodeUpdate
case .nodeDelete: return .nodeDelete
case .nodeGetInfo: return .nodeGetInfo
case .tokenReject: return .tokenReject
}
}
}
1 change: 1 addition & 0 deletions Sources/Hedera/Schedule/ScheduleInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public struct ScheduleInfo: Sendable {
case .nodeCreate(let data): proto.data = .nodeCreate(data)
case .nodeUpdate(let data): proto.data = .nodeUpdate(data)
case .nodeDelete(let data): proto.data = .nodeDelete(data)
case .tokenReject(let data): proto.data = .tokenReject(data)
case nil: break
}

Expand Down
Loading