Skip to content

Commit

Permalink
IOS-7392 Refactor tron to use raw data (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
tureck1y authored Aug 13, 2024
1 parent 2e210a9 commit 0aa69ee
Show file tree
Hide file tree
Showing 13 changed files with 310 additions and 236 deletions.
8 changes: 8 additions & 0 deletions BlockchainSdk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,8 @@
DC87B9F02B8F4D9700C61C5A /* BitcoreResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC87B9ED2B8F4D9700C61C5A /* BitcoreResponse.swift */; };
DC87B9F12B8F4D9700C61C5A /* BitcoreTarget.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC87B9EE2B8F4D9700C61C5A /* BitcoreTarget.swift */; };
DC886FCD2AF5637D0098669C /* CommonTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC886FCC2AF5637D0098669C /* CommonTests.swift */; };
DCF5E0E52C6A361A00062F28 /* TronTransactionParams.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCF5E0E42C6A361A00062F28 /* TronTransactionParams.swift */; };
DCF5E0FB2C6A455300062F28 /* TronFunction.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCF5E0FA2C6A455300062F28 /* TronFunction.swift */; };
E93532CE292D0E86008FD979 /* BlockBookUtxoProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9941ACA2927B07300399DE3 /* BlockBookUtxoProvider.swift */; };
E9941AC92927846B00399DE3 /* BlockBookTarget.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9941AC82927846B00399DE3 /* BlockBookTarget.swift */; };
EF0DA78228523FAC0081092A /* StellarTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5DFCE549274BB88700C1B1A0 /* StellarTests.swift */; };
Expand Down Expand Up @@ -1613,6 +1615,8 @@
DC87B9EE2B8F4D9700C61C5A /* BitcoreTarget.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BitcoreTarget.swift; sourceTree = "<group>"; };
DC886FCC2AF5637D0098669C /* CommonTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommonTests.swift; sourceTree = "<group>"; };
DCE3379A28F2B7260029A6F5 /* BlockchainSdkExample.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.entitlements; path = BlockchainSdkExample.entitlements; sourceTree = "<group>"; };
DCF5E0E42C6A361A00062F28 /* TronTransactionParams.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TronTransactionParams.swift; sourceTree = "<group>"; };
DCF5E0FA2C6A455300062F28 /* TronFunction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TronFunction.swift; sourceTree = "<group>"; };
E9941AC82927846B00399DE3 /* BlockBookTarget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlockBookTarget.swift; sourceTree = "<group>"; };
E9941ACA2927B07300399DE3 /* BlockBookUtxoProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlockBookUtxoProvider.swift; sourceTree = "<group>"; };
EF1339B52AB4B2A600B78BA3 /* TransferERC20TokenMethod.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransferERC20TokenMethod.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3491,11 +3495,13 @@
DA9F76E827EC8AEB00F0665C /* TronAddressService.swift */,
B00B58C72BCEB9F4007475F7 /* TronAPIResolver.swift */,
EF3B19452AA85E620084AA1C /* TronExternalLinkProvider.swift */,
DCF5E0FA2C6A455300062F28 /* TronFunction.swift */,
DA9F76F027EC9C8300F0665C /* TronJsonRpcProvider.swift */,
DA9F76FD27ECA78800F0665C /* TronNetworkModels.swift */,
DA9F76EE27EC9BCD00F0665C /* TronNetworkService.swift */,
DA9F76F427EC9CB400F0665C /* TronTarget.swift */,
DA3081312817EC1A00DE41F1 /* TronTransactionBuilder.swift */,
DCF5E0E42C6A361A00062F28 /* TronTransactionParams.swift */,
2DFD3AA32BE560BD00FCA7DC /* TronUtils.swift */,
2DDE5B9C29C4F8D200A5B708 /* TronWalletAssembly.swift */,
DA9F76EB27EC9A2900F0665C /* TronWalletManager.swift */,
Expand Down Expand Up @@ -4994,6 +5000,7 @@
DA5ACCC02C05B7D1005892AC /* KoinosTransferEvent.swift in Sources */,
DA570CF927A82322007F34EB /* PolkadotBlockchainMeta.swift in Sources */,
5D14E4972397B87000C15FC8 /* WalletManager.swift in Sources */,
DCF5E0FB2C6A455300062F28 /* TronFunction.swift in Sources */,
B6F3AA6D2ADD92230059C99A /* Publisher+.swift in Sources */,
0AEFED102C3D5B0E00C0F400 /* ICPProviderTarget.swift in Sources */,
DC5E64E12B1650F400E81AA5 /* OP_CHECKSIG.swift in Sources */,
Expand Down Expand Up @@ -5076,6 +5083,7 @@
0AF90CDD2B87658600772F04 /* AuroraExternalLinkProvider.swift in Sources */,
0A0E225D2B9B366400075FF1 /* MoonbeamExternalLinkProvider.swift in Sources */,
B6F3AA722ADD925D0059C99A /* NEARTarget.swift in Sources */,
DCF5E0E52C6A361A00062F28 /* TronTransactionParams.swift in Sources */,
EFD717D02A2619C400E5430D /* Wallet+PublicKey.swift in Sources */,
B698247F2B71A8D300E1333D /* HederaNetworkResult.swift in Sources */,
5DFF005F2521F772005203FB /* DucatusWalletManager.swift in Sources */,
Expand Down
23 changes: 0 additions & 23 deletions BlockchainSdk/Blockchains/Tron/TronAddressService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,6 @@ import CryptoSwift
public struct TronAddressService {
private let prefix: UInt8 = 0x41
private let addressLength = 21

public init() {}

static func toByteForm(_ base58String: String) -> Data? {
guard let bytes = base58String.base58CheckDecodedBytes else {
return nil
}

return Data(bytes)
}

static func toHexForm(_ base58String: String, length: Int?) -> String? {
guard let data = toByteForm(base58String) else {
return nil
}

let hex = data.hexString.lowercased()
if let length = length {
return String(repeating: "0", count: length - hex.count) + hex
} else {
return hex
}
}
}

// MARK: - AddressProvider
Expand Down
20 changes: 20 additions & 0 deletions BlockchainSdk/Blockchains/Tron/TronFunction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// TronFunction.swift
// BlockchainSdk
//
// Created by Alexander Osokin on 12.08.2024.
// Copyright © 2024 Tangem AG. All rights reserved.
//

import Foundation
import CryptoSwift

enum TronFunction: String {
case transfer = "transfer(address,uint256)"
case approve = "approve(address,uint256)"
case balanceOf = "balanceOf(address)"

var prefix: Data {
Data(rawValue.bytes).sha3(.keccak256).prefix(4)
}
}
4 changes: 2 additions & 2 deletions BlockchainSdk/Blockchains/Tron/TronJsonRpcProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class TronJsonRpcProvider: HostProvider {
requestPublisher(for: .broadcastHex(data: data))
}

func tokenBalance(address: String, contractAddress: String) -> AnyPublisher<TronTriggerSmartContractResponse, Error> {
requestPublisher(for: .tokenBalance(address: address, contractAddress: contractAddress))
func tokenBalance(address: String, contractAddress: String, parameter: String) -> AnyPublisher<TronTriggerSmartContractResponse, Error> {
requestPublisher(for: .tokenBalance(address: address, contractAddress: contractAddress, parameter: parameter))
}

func contractEnergyUsage(sourceAddress: String, contractAddress: String, parameter: String) -> AnyPublisher<TronContractEnergyUsageResponse, Error> {
Expand Down
14 changes: 7 additions & 7 deletions BlockchainSdk/Blockchains/Tron/TronNetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ class TronNetworkService: MultiNetworkProvider {
}
}

func accountInfo(for address: String, tokens: [Token], transactionIDs: [String]) -> AnyPublisher<TronAccountInfo, Error> {
func accountInfo(for address: String, tokens: [Token], transactionIDs: [String], encodedAddress: String) -> AnyPublisher<TronAccountInfo, Error> {
Publishers.Zip3(
getAccount(for: address),
tokenBalances(address: address, tokens: tokens),
tokenBalances(address: address, tokens: tokens, parameter: encodedAddress),
confirmedTransactionIDs(ids: transactionIDs)
)
.map { [blockchain] (accountInfo, tokenBalances, confirmedTransactionIDs) in
Expand Down Expand Up @@ -136,8 +136,8 @@ class TronNetworkService: MultiNetworkProvider {
.eraseToAnyPublisher()
}
}
private func tokenBalances(address: String, tokens: [Token]) -> AnyPublisher<[Token: Decimal], Error> {

private func tokenBalances(address: String, tokens: [Token], parameter: String) -> AnyPublisher<[Token: Decimal], Error> {
tokens
.publisher
.setFailureType(to: Error.self)
Expand All @@ -146,7 +146,7 @@ class TronNetworkService: MultiNetworkProvider {
return .anyFail(error: WalletError.empty)
}
return self
.tokenBalance(address: address, token: token)
.tokenBalance(address: address, token: token, parameter: parameter)
.setFailureType(to: Error.self)
.eraseToAnyPublisher()
}
Expand All @@ -159,9 +159,9 @@ class TronNetworkService: MultiNetworkProvider {
.eraseToAnyPublisher()
}

private func tokenBalance(address: String, token: Token) -> AnyPublisher<(Token, Decimal), Never> {
private func tokenBalance(address: String, token: Token, parameter: String) -> AnyPublisher<(Token, Decimal), Never> {
providerPublisher {
$0.tokenBalance(address: address, contractAddress: token.contractAddress)
$0.tokenBalance(address: address, contractAddress: token.contractAddress, parameter: parameter)
.tryMap { response in
let bigUIntValue = try TronUtils().combineBigUIntValueAtBalance(response: response.constant_result)

Expand Down
12 changes: 5 additions & 7 deletions BlockchainSdk/Blockchains/Tron/TronTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct TronTarget: TargetType {
case getAccountResource(address: String)
case getNowBlock
case broadcastHex(data: Data)
case tokenBalance(address: String, contractAddress: String)
case tokenBalance(address: String, contractAddress: String, parameter: String)
case contractEnergyUsage(sourceAddress: String, contractAddress: String, parameter: String)
case getTransactionInfoById(transactionID: String)
}
Expand Down Expand Up @@ -68,22 +68,20 @@ struct TronTarget: TargetType {
case .broadcastHex(let data):
let request = TronBroadcastRequest(transaction: data.hexString.lowercased())
return .requestJSONEncodable(request)
case .tokenBalance(let address, let contractAddress):
let hexAddress = TronAddressService.toHexForm(address, length: 64) ?? ""

case .tokenBalance(let address, let contractAddress, let parameter):
let request = TronTriggerSmartContractRequest(
owner_address: address,
contract_address: contractAddress,
function_selector: "balanceOf(address)",
parameter: hexAddress,
function_selector: TronFunction.balanceOf.rawValue,
parameter: parameter,
visible: true
)
return .requestJSONEncodable(request)
case .contractEnergyUsage(let sourceAddress, let contractAddress, let parameter):
let request = TronTriggerSmartContractRequest(
owner_address: sourceAddress,
contract_address: contractAddress,
function_selector: "transfer(address,uint256)",
function_selector: TronFunction.transfer.rawValue,
parameter: parameter,
visible: true
)
Expand Down
Loading

0 comments on commit 0aa69ee

Please sign in to comment.