Ethereumj

通過 ethereumj 發送代幣。如何為事務創建數據塊?

  • April 6, 2018

我正在嘗試在測試網中發送 Plasma 代幣,但不幸的是誤解了我應該如何正確創建交易。

我嘗試像這樣創建 tx:

String privateKeyString = "xxx";
BigInteger bigInteger = new BigInteger(privateKeyString, 16);
ECKey ecKey = ECKey.fromPrivate(bigInteger);

String plasmaContrtactAddress = "0x95D7321EdCe519419ba1DbC60A89bAfbF55EAC0D";

Transaction ethTx = new Transaction(
    nonce,
    gasPrice,
    gasLimit,
    plasmaContrtactAddress,
    value,
    data);

ethTx.sign(ecKey)

但是我應該如何創建字節

$$ $$數據正確嗎?這是等離子的 json abi 介面:

[{"constant":true,"inputs":[],"name":"message","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"ViewerStatus","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"ViewerValue","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"tokenRateEther","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"Login","type":"string"}],"name":"registrationFromLogin","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"MainAccount","type":"address"},{"name":"ActiveAliasAccount","type":"bool"},{"name":"ActiveMainAccount","type":"bool"},{"name":"PromilleDeposit","type":"uint256"},{"name":"DeleteMainAccount","type":"bool"}],"name":"setupAccount","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"NewPIN","type":"uint256"}],"name":"setupAccountPIN","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"deleteLogin","type":"string"},{"name":"newLogin","type":"string"}],"name":"setupLoginVipAccount","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"MainAccount","type":"address"}],"name":"setupAliasAccount","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"Account","type":"address"},{"name":"Freezen","type":"bool"}],"name":"frozenSubAccount","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"Account","type":"address"}],"name":"buyVipStatusPay","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[],"name":"buyTokenPay","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"toTransferETHER","type":"address"},{"name":"microToken","type":"uint256"},{"name":"SellDeposit","type":"bool"}],"name":"sellToken","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"login","type":"string"},{"name":"password","type":"string"}],"name":"depositaryPay","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"Account","type":"address"},{"name":"microToken","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"Login","type":"string"},{"name":"PIN","type":"uint256"}],"name":"transferFromLoginPay","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"Login","type":"string"}],"name":"transferToLoginPay","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"microToken","type":"uint256"}],"name":"returnDepositToBalance","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"SubAccount","type":"address"},{"name":"PIN","type":"uint256"}],"name":"transferProtectPay","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"PIN","type":"uint256"}],"name":"unblockTransferProtectPay","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"SubAccount","type":"address"},{"name":"pinSubAccount","type":"uint256"},{"name":"promilleRefund","type":"uint256"}],"name":"transferRefundSubAccountPay","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"MainAccount","type":"address"},{"name":"pinMainAccount","type":"uint256"},{"name":"SubAccount","type":"address"},{"name":"pinSubAccount","type":"uint256"},{"name":"promilleReferee","type":"uint256"}],"name":"refereeTransferProtect","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"VipAccount","type":"bool"},{"name":"ActiveAliasAccount","type":"bool"},{"name":"ActiveMainAccount","type":"bool"},{"name":"MainAccount","type":"address"},{"name":"MyReferralAccount","type":"address"}],"name":"MyAccountStatus","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"Deposit","type":"bool"},{"name":"PromilleDeposit","type":"bool"},{"name":"ZoneToken","type":"bool"}],"name":"MyAccountValue","outputs":[{"name":"value","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"myMessage","type":"string"}],"name":"advertisingPay","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"burnToken","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"microToken","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"Message","type":"string"}],"name":"Advertising","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"microToken","type":"uint256"}],"name":"Burn","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"PriceVipAccountMicroEther","type":"uint256"},{"indexed":false,"name":"TransferTokensFeeInPromille","type":"uint256"},{"indexed":false,"name":"BonusForZone1InPromille","type":"uint256"},{"indexed":false,"name":"BonusForZone2InPromille","type":"uint256"},{"indexed":false,"name":"BonusForZone3InPromille","type":"uint256"},{"indexed":false,"name":"BonusForZone4InPromille","type":"uint256"},{"indexed":false,"name":"BonusForZone5InPromille","type":"uint256"}],"name":"OptionsContract","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"microETHER","type":"uint256"}],"name":"TokenSellingRate","type":"event"}]

以及收貨人的地址:

0xEA8e872b51d76D690E4779F9AC403cf18B8460Fe

我如何使用 ethereumJ 庫或手動來實現它,無論如何?

更新:

首先 - 感謝cleanunicorn提供以下答案。

除了上面的程式碼 - 我的字節版本

$$ $$用於傳輸代幣的數據:

public byte[] createTransferData(String addressTo, @NonNull String value) {

   String functionSig = "0xa9059cbb"; // transfer
   byte[] functionBytes = ByteUtil.hexStringToBytes(functionSig);

   byte[] addressSrc = Hex.decode(Numeric.cleanHexPrefix(addressTo));
   byte[] emptyBytesAddress = new byte[32 - addressSrc.length];
   addressSrc = ByteUtil.merge(emptyBytesAddress, addressSrc);

   BigInteger bigInteger = new BigInteger(value);
   byte[] valueSrc = ByteUtil.hexStringToBytes(bigInteger.toString(16));

   // here we should calculate bytes' count of our hex value
   // and if we are out of bounds of the count/32 - we should provide 32 bytes extra
   int additionalEmptyBytes = 32 - (valueSrc.length - (valueSrc.length / 32) * 32);
   if (additionalEmptyBytes != 0) {
       valueSrc = ByteUtil.merge(new byte[additionalEmptyBytes], valueSrc);
   }

   return ByteUtil.merge(functionBytes, addressSrc, valueSrc);
}

這是ByteUtils類。

如果有人知道如何通過 ethereumJ 或更快地實現它 - 請提供您的答案。

我沒有經驗,ethereumJ但為了發送代幣,您需要向代幣智能合約發送交易。

您似乎正在嘗試與Plasma token契約進行互動。

data參數需要有3個組件

  • 您正在呼叫的函式的簽名

它的簽名transfer(address,uint256)轉換為

0xa9059cbb

  • 令牌的目的地

您的目的地是0xEA8e872b51d76D690E4779F9AC403cf18B8460Fe,這需要填充 32 字節

000000000000000000000000ea8e872b51d76d690e4779f9ac403cf18b8460fe

  • 發送了多少令牌

假設您正在嘗試發送 1000 個令牌,以十六進製表示,這意味著0x3e8,再次填充 32 個字節

00000000000000000000000000000000000000000000000000000000000003e8


把它們放在一起你的data參數應該是這樣的

0xa9059cbb000000000000000000000000ea8e872b51d76d690e4779f9ac403cf18b8460fe00000000000000000000000000000000000000000000000000000000000003e8

有一個易於使用的工具可以幫助您手動解決這個問題

https://jimkberry.github.io/eth-contractor/

還有另一個工具可以幫助您找出您提供的任何 ABI 或功能的簽名

https://github.com/cleanunicorn/abi2signature

引用自:https://ethereum.stackexchange.com/questions/44770