Search code examples
soliditysmartcontractstron

Tron Wallet address declaration format in Solidity


I'm trying to build a simple contract that just forwards any incoming transferred amount to a set wallet address. I managed to get this contract to work using the Remix IDE with ethereum addresses but I cannot port it to Tron now. In the TronIDE I cannot compile the line declaring the wallet address.

Here is the contract code I am using. Can anyone shed some light on what format the tron wallet address needs to be in. I tried converting it to hex (transferTo variable) and it didn't work, I tried using the base58 address and it didn't work.

Aparently it does compile fine in the TronIDE compiler with the ethereum address though, I even managed to deploy it to Shasta without any issues (except for it to forward the actual transferred amounts of course).

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract PaymentForwarder {

address constant transferTo = 414dbc78301522ae1529d01f4093ae3daad3f26827; // this throws a ParseError: Identifier-start is not allowed at end of a number.
address constant transferToAlternative = TH4EovGaTrmWxhJSmeMVKy5ZpnDGE3DgJ8; // this thows a DeclarationError: Undeclared identifier.
address constant workingDeclaration = 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2; // this works fine in the tron solidity compiler even though it's an ethereum address
    
event TransferReceived(address _from, uint _amount);

constructor() {

}

receive() payable external {

    address payable paya = payable(transferTo);

    paya.transfer(msg.value);

    emit TransferReceived(msg.sender, msg.value);
}

}

Can anyone shed some light on why this is and what format does the wallet address need to be in?

Thank you


Solution

  • If you already have the hexstring like transferTo (from quick glance), then you can simply add the hexstring with 0x in the beginning, and convert it to EVM address with the following function.

    /**
      * @dev convert uint256 (HexString add 0x at beginning) tron address to solidity address type
      * @param  tronAddress uint256 tronAddress, begin with 0x, followed by HexString
      * @return Solidity address type
      */
         
    function convertFromTronInt(uint256 tronAddress) public view returns(address){
          return address(tronAddress);
    }
    

    On the other hand, transferToAlternative is still in its original TRON address form, then you'll need to use tronweb TRON's official library and convert the address to its hexstring form with the following function:

    tronWeb.address.toHex("TH4EovGaTrmWxhJSmeMVKy5ZpnDGE3DgJ8")
    

    Then simply, continue to the step above to convert it fully to EVM address.

    Hope this helps!

    Source:

    Cheers~