Skip to content

james19870606/TronWallet

Repository files navigation

TronWallet

TronWallet is an Android toolbelt for interaction with the Tron network.

language [jitpack]

For more specific usage, please refer to the demo

JitPack.io

I strongly recommend https://jitpack.io

repositories {
    ...
    maven { url 'https://jitpack.io' }
}
dependencies {
    implementation 'com.github.james19870606:TronWallet:1.1.1'
}
Setup TronWeb
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  {

}
Create Random
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)
Create Account
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)
Import Account From Mnemonic
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)
Import Account From PrivateKey
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)
Send TRX
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)
Send TRC20
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)
Fee Estimate When Send TRX
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)
Fee Estimate When Send TRC20
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

License

TronWeb is released under the MIT license. See LICENSE for details.