TronWeb is an iOS toolbelt for interaction with the Tron network.
For more specific usage, please refer to the demo
Add this to your podfile and run pod install
to install:
pod 'TronWeb', '~> 1.1.8'
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.
Once you have your Swift package set up, adding TronWeb as a dependency is as easy as adding it to the dependencies value of your Package.swift.
dependencies: [
.package(url: "https://github.com/james19870606/TronWeb.git", .upToNextMajor(from: "1.1.8"))
]
import TronWeb
import TronWeb3
let tronWeb = TronWeb3()
let privateKey = ""
let TRONApiKey = ""
if tronWeb.isGenerateTronWebInstanceSuccess != true {
tronWeb.setup(privateKey: privateKey, node: chainType == .main ? TRONMainNet : TRONNileNet) { [weak self] setupResult,error in
guard let self = self else { return }
if setupResult {
//......
} else {
print(error)
}
}
} else {
//......
}
tronWeb.createRandom { [weak self] state, address, privateKey, publicKey, mnemonic, error in
guard let self = self else { return }
self.createRandomBtn.isEnabled = true
tipLabel.text = "create finished."
if state {
let text =
"address: " + address + "\n\n" +
"mnemonic: " + mnemonic + "\n\n" +
"privateKey: " + privateKey + "\n\n" +
"publicKey: " + publicKey
walletDetailTextView.text = text
} else {
walletDetailTextView.text = error
}
}
tronWeb.createAccount { [weak self] state, base58Address, hexAddress, privateKey, publicKey, error in
guard let self = self else { return }
self.createAccountBtn.isEnabled = true
tipLabel.text = "create finished."
if state {
let text =
"base58Address: " + base58Address + "\n\n" +
"hexAddress: " + hexAddress + "\n\n" +
"privateKey: " + privateKey + "\n\n" +
"publicKey: " + publicKey
walletDetailTextView.text = text
} else {
walletDetailTextView.text = error
}
}
tronWeb.importAccountFromMnemonic (mnemonic: mnemonic){ [weak self] state, address, privateKey, publicKey, error in
guard let self = self else { return }
self.importAccountFromMnemonicBtn.isEnabled = true
tipLabel.text = "import finished."
if state {
let text =
"address: " + address + "\n\n" +
"privateKey: " + privateKey + "\n\n" +
"publicKey: " + publicKey
walletDetailTextView.text = text
} else {
walletDetailTextView.text = error
}
}
tronWeb.importAccountFromPrivateKey (privateKey: privateKey){ [weak self] state, base58, hex, error in
guard let self = self else { return }
self.importAccountFromPrivateKeyBtn.isEnabled = true
tipLabel.text = "import finished."
if state {
let text =
"base58: " + base58 + "\n\n" +
"hex: " + hex
walletDetailTextView.text = text
} else {
walletDetailTextView.text = error
}
}
let remark = ""
let toAddress = ""
let amountText = "1" // This value is 0.000001
tronWeb.trxTransferWithRemark(remark: remark,
toAddress: toAddress,
amount: amountText){ [weak self] (state, txid,error) in
guard let self = self else { return }
print("state = \(state)")
print("txid = \(txid)")
if (state) {
self.hashLabel.text = txid
} else {
self.hashLabel.text = error
}
}
let remark = ""
let toAddress = ""
let amountText = "1" // This value is 0.000001
let trc20Address = ""
tronWeb.trc20TokenTransfer(toAddress: toAddress,
trc20ContractAddress: trc20Address, amount: amountText,
remark: remark,
feeLimit: "100000000") { [weak self] (state, txid,error) in
guard let self = self else { return }
print("state = \(state)")
print("txid = \(txid)")
if (state) {
self.hashLabel.text = txid
} else {
self.hashLabel.text = error
}
}
let toAddress = reviceAddressField.text,
let amountText = amountTextField.text else { return}
let remark = remarkTextView.text ?? ""
tronWeb.estimateTRXTransferFee(toAddress: toAddress, amount: amountText,note: remark){ (state,sendAccountResources,feeDic,error) in
if state {
} else {
}
}
let toAddress = ""
let amountText = amountTextField.text
let trc20Address = self.trc20AddressTextField.text
tronWeb.estimateEnergy(url:chainType == .main ? TRONMainNet : TRONNileNet, toAddress: toAddress, trc20ContractAddress: trc20Address, amount: amountText) { (state,feeDic,error) in
if state {
/*
feeDic = {
energyFee = 420;
"energy_used" = 4146;
feeLimit = "1.74132";
};
*/
} else {
}
}
更详细的使用方法,建议参考 demo
TronWeb is released under the MIT license. See LICENSE for details.