Skip to content

Commit 1637bab

Browse files
committed
fix: TokenBalance and TokenRelationship query response mapping
Signed-off-by: Ricky Saechao <ricky@launchbadge.com>
1 parent 6c20f01 commit 1637bab

File tree

7 files changed

+76
-43
lines changed

7 files changed

+76
-43
lines changed

Sources/Hedera/Account/AccountBalance.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import Foundation
2222
import HederaProtobufs
2323

24-
private struct TokenBalance {
24+
internal struct TokenBalance {
2525
fileprivate let id: TokenId
2626
fileprivate let balance: UInt64
2727
fileprivate let decimals: UInt32
@@ -59,6 +59,16 @@ public struct AccountBalance: Sendable {
5959

6060
private let tokensInner: [TokenBalance]
6161

62+
internal init(
63+
accountId: AccountId,
64+
hbars: Hbar,
65+
tokensInner: [TokenBalance]
66+
) {
67+
self.accountId = accountId
68+
self.hbars = hbars
69+
self.tokensInner = tokensInner
70+
}
71+
6272
// hack to work around deprecated warning
6373
private var tokenBalancesInner: [TokenId: UInt64] {
6474
Dictionary(uniqueKeysWithValues: tokensInner.map { ($0.id, $0.balance) })

Sources/Hedera/Account/AccountBalanceQuery.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,25 @@ public final class AccountBalanceQuery: Query<AccountBalance> {
8888
internal override func makeQueryResponse(_ context: Context, _ response: Proto_Response.OneOf_Response) async throws
8989
-> Response
9090
{
91+
let mirrorNodeGateway = try MirrorNodeGateway.forNetwork(context.mirrorNetworkNodes, context.ledgerId)
92+
let mirrorNodeService = MirrorNodeService.init(mirrorNodeGateway)
93+
9194
guard case .cryptogetAccountBalance(let proto) = response else {
9295
throw HError.fromProtobuf("unexpected \(response) received, expected `cryptogetAccountBalance`")
9396
}
9497

95-
return try .fromProtobuf(proto)
98+
let accountId = try AccountId.fromProtobuf(proto.accountID)
99+
let tokenBalanceProto = try await mirrorNodeService.getTokenBalancesForAccount(String(accountId.num))
100+
101+
// var tokenBalances: [TokenId: UInt64] = [:]
102+
103+
// for balance in tokenBalanceProto {
104+
// tokenBalances[.fromProtobuf(balance.tokenID)] = balance.balance
105+
// }
106+
107+
return AccountBalance(
108+
accountId: accountId, hbars: .fromTinybars(Int64(proto.balance)),
109+
tokensInner: .fromProtobuf(tokenBalanceProto))
96110
}
97111

98112
public override func validateChecksums(on ledgerId: LedgerId) throws {

Sources/Hedera/Account/AccountId.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ public struct AccountId: Sendable, EntityId, ValidateChecksums {
135135
public func toBytes() -> Data {
136136
toProtobufBytes()
137137
}
138+
139+
public func populateAccountIdNum(_ client: Client) async throws -> Self {
140+
let mirrorNodeGateway = try MirrorNodeGateway.forClient(client)
141+
let mirrorNodeService = MirrorNodeService(mirrorNodeGateway)
142+
143+
let accountNumFromMirror = try await mirrorNodeService.getAccountNum(self.evmAddress!.toString())
144+
145+
return Self(shard: shard, realm: realm, num: accountNumFromMirror)
146+
}
138147
}
139148

140149
extension AccountId: TryProtobufCodable {

Sources/Hedera/Account/AccountInfoQuery.swift

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,9 @@ public final class AccountInfoQuery: Query<AccountInfo> {
6969
throw HError.fromProtobuf("unexpected \(response) received, expected `cryptoGetInfo`")
7070
}
7171

72-
let accountInfoProto = proto.accountInfo
73-
let accountId = try AccountId.fromProtobuf(accountInfoProto.accountID)
72+
let accountInfo = try AccountInfo.fromProtobuf(proto.accountInfo)
7473
let tokenRelationshipsProto = try await mirrorNodeService.getTokenRelationshipsForAccount(
75-
String(describing: accountId.num))
74+
String(describing: accountInfo.accountId.num))
7675

7776
var tokenRelationships: [TokenId: TokenRelationship] = [:]
7877

@@ -81,26 +80,26 @@ public final class AccountInfoQuery: Query<AccountInfo> {
8180
}
8281

8382
return AccountInfo(
84-
accountId: try AccountId.fromProtobuf(accountInfoProto.accountID),
85-
contractAccountId: accountInfoProto.contractAccountID,
86-
isDeleted: accountInfoProto.deleted,
87-
proxyAccountId: try .fromProtobuf(accountInfoProto.proxyAccountID),
88-
proxyReceived: Hbar.fromTinybars(accountInfoProto.proxyReceived),
89-
key: try .fromProtobuf(accountInfoProto.key),
90-
balance: .fromTinybars(Int64(accountInfoProto.balance)),
91-
sendRecordThreshold: Hbar.fromTinybars(Int64(accountInfoProto.generateSendRecordThreshold)),
92-
receiveRecordThreshold: Hbar.fromTinybars(Int64(accountInfoProto.generateReceiveRecordThreshold)),
93-
isReceiverSignatureRequired: accountInfoProto.receiverSigRequired,
94-
expirationTime: .fromProtobuf(accountInfoProto.expirationTime),
95-
autoRenewPeriod: .fromProtobuf(accountInfoProto.autoRenewPeriod),
96-
accountMemo: accountInfoProto.memo,
97-
ownedNfts: UInt64(accountInfoProto.ownedNfts),
98-
maxAutomaticTokenAssociations: UInt32(accountInfoProto.maxAutomaticTokenAssociations),
99-
aliasKey: try .fromAliasBytes(accountInfoProto.alias),
100-
ethereumNonce: UInt64(accountInfoProto.ethereumNonce),
83+
accountId: accountInfo.accountId,
84+
contractAccountId: accountInfo.contractAccountId,
85+
isDeleted: accountInfo.isDeleted,
86+
proxyAccountId: accountInfo.proxyAccountId,
87+
proxyReceived: accountInfo.proxyReceived,
88+
key: accountInfo.key,
89+
balance: accountInfo.balance,
90+
sendRecordThreshold: accountInfo.sendRecordThreshold,
91+
receiveRecordThreshold: accountInfo.receiveRecordThreshold,
92+
isReceiverSignatureRequired: accountInfo.isReceiverSignatureRequired,
93+
expirationTime: accountInfo.expirationTime,
94+
autoRenewPeriod: accountInfo.autoRenewPeriod,
95+
accountMemo: accountInfo.accountMemo,
96+
ownedNfts: accountInfo.ownedNfts,
97+
maxAutomaticTokenAssociations: accountInfo.maxAutomaticTokenAssociations,
98+
aliasKey: accountInfo.aliasKey,
99+
ethereumNonce: accountInfo.ethereumNonce,
101100
tokenRelationships: tokenRelationships,
102-
ledgerId: .fromBytes(accountInfoProto.ledgerID),
103-
staking: try .fromProtobuf(accountInfoProto.stakingInfo)
101+
ledgerId: accountInfo.ledgerId,
102+
staking: accountInfo.staking
104103
)
105104
}
106105

Sources/Hedera/Contract/ContractInfo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ extension ContractInfo: TryProtobufCodable {
104104

105105
self.init(
106106
contractId: try .fromProtobuf(proto.contractID),
107-
accountId: try .fromProtobuf(proto.accountID),
107+
accountId: try AccountId.fromProtobuf(proto.accountID),
108108
contractAccountId: proto.contractAccountID,
109109
adminKey: try .fromProtobuf(adminKey),
110110
expirationTime: .fromProtobuf(expirationTime),

Sources/Hedera/Contract/ContractInfoQuery.swift

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ public final class ContractInfoQuery: Query<ContractInfo> {
6565
guard case .contractGetInfo(let proto) = response else {
6666
throw HError.fromProtobuf("unexpected \(response) received, expected `contractGetInfo`")
6767
}
68-
let contractInfoProto = proto.contractInfo
69-
let contractId = try ContractId.fromProtobuf(contractInfoProto.contractID)
68+
69+
let contractInfo = try ContractInfo.fromProtobuf(proto.contractInfo)
7070
let tokenRelationshipsProto = try await mirrorNodeService.getTokenRelationshipsForAccount(
71-
String(describing: contractId.num))
71+
String(describing: contractInfo.contractId.num))
7272

7373
var tokenRelationships: [TokenId: TokenRelationship] = [:]
7474

@@ -77,21 +77,22 @@ public final class ContractInfoQuery: Query<ContractInfo> {
7777
}
7878

7979
return ContractInfo(
80-
contractId: try ContractId.fromProtobuf(contractInfoProto.contractID),
81-
accountId: try AccountId.fromProtobuf(contractInfoProto.accountID),
82-
contractAccountId: contractInfoProto.contractAccountID,
83-
adminKey: try .fromProtobuf(contractInfoProto.adminKey),
84-
expirationTime: .fromProtobuf(contractInfoProto.expirationTime),
85-
autoRenewPeriod: .fromProtobuf(contractInfoProto.autoRenewPeriod),
86-
storage: UInt64(contractInfoProto.storage),
87-
contractMemo: contractInfoProto.memo,
88-
balance: .fromTinybars(Int64(contractInfoProto.balance)),
89-
isDeleted: contractInfoProto.deleted,
90-
autoRenewAccountId: try .fromProtobuf(contractInfoProto.autoRenewAccountID),
91-
maxAutomaticTokenAssociations: UInt32(contractInfoProto.maxAutomaticTokenAssociations),
80+
contractId: contractInfo.contractId,
81+
accountId: contractInfo.accountId,
82+
contractAccountId: contractInfo.contractAccountId,
83+
adminKey: contractInfo.adminKey,
84+
expirationTime: contractInfo.expirationTime,
85+
autoRenewPeriod: contractInfo.autoRenewPeriod,
86+
storage: contractInfo.storage,
87+
contractMemo: contractInfo.contractMemo,
88+
balance: contractInfo.balance,
89+
isDeleted: contractInfo.isDeleted,
90+
autoRenewAccountId: contractInfo.autoRenewAccountId,
91+
maxAutomaticTokenAssociations: contractInfo.maxAutomaticTokenAssociations,
9292
tokenRelationships: tokenRelationships,
93-
ledgerId: .fromBytes(contractInfoProto.ledgerID),
94-
stakingInfo: try .fromProtobuf(contractInfoProto.stakingInfo))
93+
ledgerId: contractInfo.ledgerId,
94+
stakingInfo: contractInfo.stakingInfo
95+
)
9596
}
9697

9798
internal override func validateChecksums(on ledgerId: LedgerId) throws {

Tests/HederaE2ETests/Contract/ContractInfo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Hedera
2222
import XCTest
2323

2424
internal final class ContractInfo: XCTestCase {
25-
internal func testQueryBoo() async throws {
25+
internal func testQuery() async throws {
2626
let testEnv = try TestEnvironment.nonFree
2727

2828
let contractId = try await ContractHelpers.makeContract(testEnv, operatorAdminKey: true)

0 commit comments

Comments
 (0)