@@ -56,10 +56,14 @@ internal final class MirrorNodeService {
5656 }
5757
5858 internal func getContractNum( _ evmAddress: String ) async throws -> UInt64 {
59- let accountInfoResponse = try await self . mirrorNodeGateway. getContractInfo ( evmAddress)
59+ let contractInfoResponse = try await self . mirrorNodeGateway. getContractInfo ( evmAddress)
6060
61- guard let contractId = accountInfoResponse [ " contract_id " ] else {
62- fatalError ( " Error while processing getContractNum mirror node query " )
61+ guard let contractId = contractInfoResponse [ " contract_id " ] else {
62+ throw NSError (
63+ domain: " InvalidResponseError " , code: - 1 ,
64+ userInfo: [
65+ NSLocalizedDescriptionKey: " Error while processing getContractNum mirror node query "
66+ ] )
6367 }
6468
6569 let contractIdNum = ContractId ( String ( describing: contractId) ) ? . num
@@ -71,27 +75,43 @@ internal final class MirrorNodeService {
7175 let accountTokensResponse = try await self . mirrorNodeGateway. getAccountTokens ( evmAddress)
7276
7377 guard let tokens = accountTokensResponse [ " tokens " ] else {
74- fatalError ( " Error while processing getTokenBalancesForAccount mirror node query " )
78+ throw NSError (
79+ domain: " InvalidResponseError " , code: - 1 ,
80+ userInfo: [
81+ NSLocalizedDescriptionKey: " Error while processing getAccountTokens mirror node query "
82+ ] )
7583 }
7684
7785 var tokenBalances : [ Proto_TokenBalance ] = [ ]
7886
79- guard let tokensList = tokens as? [ [ String : Any ] ] else {
87+ guard let tokensList: [ [ String : Any ] ] = tokens as? [ [ String : Any ] ] else {
8088 throw NSError (
8189 domain: " InvalidResponseError " , code: - 1 ,
8290 userInfo: [
8391 NSLocalizedDescriptionKey: " Error while processing getTokenBalancesForAccount mirror node query "
8492 ] )
8593 }
8694 tokensList. forEach { token in
87- let tokenId = TokenId ( String ( describing: token [ " token_id " ] ) ) ? . toProtobuf ( )
88- let balance = UInt64 ( String ( describing: token [ " balance " ] ) )
89- let decimals = UInt32 ( String ( describing: token [ " decimals " ] ) )
95+ var tokenId : String = " "
96+ var balance : UInt64 = 0
97+ var decimals : UInt32 = 0
98+
99+ if let id = token [ " token_id " ] as? String {
100+ tokenId = id
101+ }
102+
103+ if let hbar = token [ " balance " ] as? String {
104+ balance = UInt64 ( hbar) !
105+ }
106+
107+ if let dec = token [ " decimals " ] as? String {
108+ decimals = UInt32 ( dec) !
109+ }
90110
91111 let tokenBalanceProto = Proto_TokenBalance . with { proto in
92- proto. tokenID = tokenId!
93- proto. balance = balance!
94- proto. decimals = decimals!
112+ proto. tokenID = TokenId ( tokenId) ! . toProtobuf ( )
113+ proto. balance = balance
114+ proto. decimals = decimals
95115 }
96116
97117 tokenBalances. append ( tokenBalanceProto)
@@ -114,28 +134,54 @@ internal final class MirrorNodeService {
114134
115135 var tokenBalances : [ Proto_TokenRelationship ] = [ ]
116136
117- if let tokensList = tokens as? [ [ String : Any ] ] {
118- try tokensList. forEach { token in
119- let tokenId = TokenId ( String ( describing: token [ " token_id " ] ) ) ? . toProtobuf ( )
120- let balance = UInt64 ( String ( describing: token [ " balance " ] ) )
121- let decimals = UInt32 ( String ( describing: token [ " decimals " ] ) )
122- let kycStatus = String ( describing: token [ " kyc_status " ] )
123- let freezeStatus = String ( describing: token [ " freeze_status " ] )
124- let automaticAssociation = Bool ( String ( describing: token [ " automatic_assocation " ] ) )
125-
126- let tokenRelationshipsProto = try Proto_TokenRelationship . with { proto in
127- proto. tokenID = tokenId!
128- proto. balance = balance!
129- proto. decimals = decimals!
130- proto. kycStatus = try getTokenKycStatusFromString ( kycStatus)
131- proto. freezeStatus = try getTokenFreezeStatusFromString ( freezeStatus)
132- proto. automaticAssociation = automaticAssociation!
133- }
134-
135- tokenBalances. append ( tokenRelationshipsProto)
137+ guard let tokensList: [ [ String : Any ] ] = tokens as? [ [ String : Any ] ] else {
138+ throw NSError (
139+ domain: " InvalidResponseError " , code: - 1 ,
140+ userInfo: [
141+ NSLocalizedDescriptionKey: " Error while processing getTokenBalancesForAccount mirror node query "
142+ ] )
143+ }
144+ try tokensList. forEach { token in
145+ var tokenId : String = " "
146+ var balance : UInt64 = 0
147+ var decimals : UInt32 = 0
148+ var kycStatus : String = " "
149+ var freezeStatus : String = " "
150+ var automaticAssociation : Bool = false
151+ if let id = token [ " token_id " ] as? String {
152+ tokenId = id
153+ }
154+
155+ if let hbar = token [ " balance " ] as? String {
156+ balance = UInt64 ( hbar) !
157+ }
158+
159+ if let dec = token [ " decimals " ] as? String {
160+ decimals = UInt32 ( dec) !
161+ }
162+
163+ if let kyc = token [ " kyc_status " ] as? String {
164+ kycStatus = kyc
165+ }
166+
167+ if let freeze = token [ " freeze_status " ] as? String {
168+ freezeStatus = freeze
169+ }
170+
171+ if let auto = token [ " automatic_association " ] as? String {
172+ automaticAssociation = Bool ( auto) !
173+ }
174+
175+ let tokenRelationshipsProto = try Proto_TokenRelationship . with { proto in
176+ proto. tokenID = TokenId ( tokenId) !. toProtobuf ( )
177+ proto. balance = balance
178+ proto. decimals = decimals
179+ proto. kycStatus = try getTokenKycStatusFromString ( kycStatus)
180+ proto. freezeStatus = try getTokenFreezeStatusFromString ( freezeStatus)
181+ proto. automaticAssociation = automaticAssociation
136182 }
137- } else {
138- fatalError ( " Error while processing getTokenRelationshipsForAccount mirror node query " )
183+
184+ tokenBalances . append ( tokenRelationshipsProto )
139185 }
140186
141187 return tokenBalances
0 commit comments