Search in sources :

Example 1 with TransactionArguments

use of org.ethereum.core.TransactionArguments in project rskj by rsksmart.

the class TransactionArgumentsUtil method processArguments.

/**
 * transform the Web3.CallArguments in TransactionArguments that can be used in
 * the TransactionBuilder
 */
public static TransactionArguments processArguments(CallArguments argsParam, TransactionPool transactionPool, Account senderAccount, byte defaultChainId) {
    TransactionArguments argsRet = new TransactionArguments();
    argsRet.setFrom(argsParam.getFrom());
    argsRet.setTo(stringHexToByteArray(argsParam.getTo()));
    argsRet.setNonce(strHexOrStrNumberToBigInteger(argsParam.getNonce(), () -> transactionPool.getPendingState().getNonce(senderAccount.getAddress())));
    argsRet.setValue(strHexOrStrNumberToBigInteger(argsParam.getValue(), () -> BigInteger.ZERO));
    argsRet.setGasPrice(strHexOrStrNumberToBigInteger(argsParam.getGasPrice(), () -> BigInteger.ZERO));
    argsRet.setGasLimit(strHexOrStrNumberToBigInteger(argsParam.getGas(), () -> null));
    if (argsRet.getGasLimit() == null) {
        argsRet.setGasLimit(strHexOrStrNumberToBigInteger(argsParam.getGasLimit(), () -> DEFAULT_GAS_LIMIT));
    }
    if (argsParam.getData() != null && argsParam.getData().startsWith("0x")) {
        argsRet.setData(argsParam.getData().substring(2));
        // needs to change the parameter because some places expect the changed value after sendTransaction call
        argsParam.setData(argsRet.getData());
    }
    argsRet.setChainId(hexToChainId(argsParam.getChainId()));
    if (argsRet.getChainId() == 0) {
        argsRet.setChainId(defaultChainId);
    }
    return argsRet;
}
Also used : TransactionArguments(org.ethereum.core.TransactionArguments)

Example 2 with TransactionArguments

use of org.ethereum.core.TransactionArguments in project rskj by rsksmart.

the class PersonalModuleWalletEnabled method sendTransaction.

private String sendTransaction(CallArguments args, Account senderAccount) throws Exception {
    if (senderAccount == null) {
        throw new Exception("From address private key could not be found in this node");
    }
    TransactionArguments txArgs = TransactionArgumentsUtil.processArguments(args, transactionPool, senderAccount, config.getNetworkConstants().getChainId());
    Transaction tx = Transaction.builder().withTransactionArguments(txArgs).build();
    tx.sign(senderAccount.getEcKey().getPrivKeyBytes());
    eth.submitTransaction(tx);
    return tx.getHash().toJsonString();
}
Also used : Transaction(org.ethereum.core.Transaction) TransactionArguments(org.ethereum.core.TransactionArguments)

Example 3 with TransactionArguments

use of org.ethereum.core.TransactionArguments in project rskj by rsksmart.

the class EthModuleTransactionBase method sendTransaction.

@Override
public synchronized String sendTransaction(CallArguments args) {
    Account senderAccount = this.wallet.getAccount(new RskAddress(args.getFrom()));
    if (senderAccount == null) {
        throw RskJsonRpcRequestException.invalidParamError(TransactionArgumentsUtil.ERR_COULD_NOT_FIND_ACCOUNT + args.getFrom());
    }
    String txHash = null;
    try {
        synchronized (transactionPool) {
            TransactionArguments txArgs = TransactionArgumentsUtil.processArguments(args, transactionPool, senderAccount, constants.getChainId());
            Transaction tx = Transaction.builder().withTransactionArguments(txArgs).build();
            tx.sign(senderAccount.getEcKey().getPrivKeyBytes());
            if (!tx.acceptTransactionSignature(constants.getChainId())) {
                throw RskJsonRpcRequestException.invalidParamError(TransactionArgumentsUtil.ERR_INVALID_CHAIN_ID + args.getChainId());
            }
            TransactionPoolAddResult result = transactionGateway.receiveTransaction(tx.toImmutableTransaction());
            if (!result.transactionsWereAdded()) {
                throw RskJsonRpcRequestException.transactionError(result.getErrorMessage());
            }
            txHash = tx.getHash().toJsonString();
        }
        return txHash;
    } finally {
        LOGGER.debug("eth_sendTransaction({}): {}", args, txHash);
    }
}
Also used : Account(org.ethereum.core.Account) ImmutableTransaction(org.ethereum.core.ImmutableTransaction) Transaction(org.ethereum.core.Transaction) TransactionArguments(org.ethereum.core.TransactionArguments) RskAddress(co.rsk.core.RskAddress) TransactionPoolAddResult(org.ethereum.core.TransactionPoolAddResult)

Example 4 with TransactionArguments

use of org.ethereum.core.TransactionArguments in project rskj by rsksmart.

the class TransactionArgumentsUtilTest method processArguments.

@Test
public void processArguments() {
    Constants constants = Constants.regtest();
    Wallet wallet = new Wallet(new HashMapDB());
    RskAddress sender = wallet.addAccount();
    RskAddress receiver = wallet.addAccount();
    CallArguments args = TransactionFactoryHelper.createArguments(sender, receiver);
    TransactionArguments txArgs = TransactionArgumentsUtil.processArguments(args, null, wallet.getAccount(sender), constants.getChainId());
    assertEquals(txArgs.getValue(), BigInteger.valueOf(100000L));
    assertEquals(txArgs.getGasPrice(), BigInteger.valueOf(10000000000000L));
    assertEquals(txArgs.getGasLimit(), BigInteger.valueOf(30400L));
    assertEquals(txArgs.getChainId(), 33);
    assertEquals(txArgs.getNonce(), BigInteger.ONE);
    assertEquals(txArgs.getData(), null);
    assertArrayEquals(txArgs.getTo(), receiver.getBytes());
}
Also used : Wallet(co.rsk.core.Wallet) TransactionArguments(org.ethereum.core.TransactionArguments) RskAddress(co.rsk.core.RskAddress) CallArguments(org.ethereum.rpc.CallArguments) Constants(org.ethereum.config.Constants) HashMapDB(org.ethereum.datasource.HashMapDB) Test(org.junit.Test)

Aggregations

TransactionArguments (org.ethereum.core.TransactionArguments)4 RskAddress (co.rsk.core.RskAddress)2 Transaction (org.ethereum.core.Transaction)2 Wallet (co.rsk.core.Wallet)1 Constants (org.ethereum.config.Constants)1 Account (org.ethereum.core.Account)1 ImmutableTransaction (org.ethereum.core.ImmutableTransaction)1 TransactionPoolAddResult (org.ethereum.core.TransactionPoolAddResult)1 HashMapDB (org.ethereum.datasource.HashMapDB)1 CallArguments (org.ethereum.rpc.CallArguments)1 Test (org.junit.Test)1