TronWallet is an Android toolbelt for interaction with the Tron network.
For more specific usage, please refer to the demo
I strongly recommend https://jitpack.io
repositories {
...
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.james19870606:TronWallet:1.1.1'
}
val onCompleted = {result : Boolean,error : String ->
//......
}
val privateKey = ""
val node = if(position == 0) TRONMainNet else TRONNileNet
if (tronweb?.isGenerateTronWebInstanceSuccess == false) {
tronweb?.setup(true, privateKey, node = node,onCompleted = onCompleted)
} else {
}
val onCompleted = {state: Boolean, address: String, privateKey: String, publicKey: String, mnemonic: String, error: String ->
runOnUiThread {
val text = """
address: $address
mnemonic: $mnemonic
privateKey: $privateKey
publicKey: $publicKey
"""
walletDetail?.setText(if (state) text else error)
}
}
tronweb?.createRandom(onCompleted = onCompleted)
val onCompleted = { state: Boolean, hexAddress: String, base58Address: String, privateKey: String, publicKey: String, error: String ->
runOnUiThread {
val text = """
hexAddress: $hexAddress
base58Address: $base58Address
privateKey: $privateKey
publicKey: $publicKey
"""
walletDetail?.setText(if (state) text else error)
}
}
tronweb?.createAccount(onCompleted = onCompleted)
val mnemonic = mnemonicEditText?.getText().toString();
val onCompleted = { state: Boolean, address: String, privateKey: String, publicKey: String, error: String ->
runOnUiThread {
val text = """
address: $address
privateKey: $privateKey
publicKey: $publicKey
"""
walletDetail?.setText(if (state) text else error)
}
}
tronweb?.importAccountFromMnemonic(mnemonic, onCompleted = onCompleted)
val privateKey = privateKeyEditText?.getText().toString();
val onCompleted = { state: Boolean,base58: String, hex: String, error: String ->
runOnUiThread {
val text = """
base58: $base58
hex: $hex
"""
walletDetail?.setText(if (state) text else error)
}
}
tronweb?.importAccountFromPrivateKey(privateKey, onCompleted = onCompleted)
val onCompleted = {state : Boolean, txid: String ,error:String->
this.runOnUiThread {
if (state){
hashValue?.text = txid
} else {
hashValue?.text = error
}
}
}
tronweb?.trxTransferWithOutRemark(
toAddress ,
amount ,
onCompleted = onCompleted)
val onCompleted = {state : Boolean, txid: String,error:String ->
this.runOnUiThread {
if (state){
hashValue?.text = txid
} else {
hashValue?.text = error
}
}
}
tronweb?.trc20TokenTransfer(
toAddress,
trc20ContractAddress,
amount,
remark,
onCompleted = onCompleted)
val toAddress = receiveEditText?.text.toString()
val amount = amountEditText?.text.toString()
val note = "Test data"
val onCompleted = {state : Boolean, sendAccountResources:LinkedTreeMap<String, Any>,feeDic:LinkedTreeMap<String, Any>,error:String ->
this.runOnUiThread {
if (state){
val activationFee = feeDic["activationFee"] as Double
val noteFee = feeDic["noteFee"] as Double
val requiredBandwidth = feeDic["requiredBandwidth"] as Double
val totalFee = activationFee + noteFee + requiredBandwidth / 1000
hashValue?.text =
"Resource Consumed ${requiredBandwidth.toInt()} Bandwidth \nFee $totalFee TRX"
} else {
hashValue?.text = error
}
}
}
tronweb?.estimateTRXTransferFee(toAddress, note, amount, onCompleted)
val onCompleted = {state : Boolean, energyUsed:Double,energyFee:Double,error:String ->
this.runOnUiThread {
if (state){
val trxFee = (energyUsed * energyFee) / 1_000_000
hashValue?.text =
"Resource Consumed 339 Bandwidth $energyUsed Energy\nFee $trxFee TRX"
} else {
hashValue?.text = error
}
}
}
val url = if(position == 0) TRONMainNet else TRONNileNet
tronweb?.getFeeEstimate(
url,
toAddress,
trc20ContractAddress,
amount,
onCompleted = onCompleted)
更详细的使用方法,建议参考 demo
TronWeb is released under the MIT license. See LICENSE for details.