Search in sources :

Example 6 with ArgTxCall

use of org.aion.api.server.types.ArgTxCall in project aion by aionnetwork.

the class ApiWeb3Aion method eth_sendTransaction.

public RpcMsg eth_sendTransaction(Object _params) {
    JSONObject _tx;
    if (_params instanceof JSONArray) {
        _tx = ((JSONArray) _params).getJSONObject(0);
    } else if (_params instanceof JSONObject) {
        _tx = ((JSONObject) _params).getJSONObject("transaction");
    } else {
        return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid parameters");
    }
    ArgTxCall txParams = ArgTxCall.fromJSON(_tx, getRecommendedNrgPrice());
    ApiTxResponse response = sendTransaction(txParams);
    return processTxResponse(response);
}
Also used : ArgTxCall(org.aion.api.server.types.ArgTxCall) JSONObject(org.json.JSONObject) ApiTxResponse(org.aion.api.server.ApiTxResponse) JSONArray(org.json.JSONArray)

Example 7 with ArgTxCall

use of org.aion.api.server.types.ArgTxCall in project aion by aionnetwork.

the class ApiWeb3Aion method eth_estimateGas.

public RpcMsg eth_estimateGas(Object _params) {
    JSONObject _tx;
    if (_params instanceof JSONArray) {
        _tx = ((JSONArray) _params).getJSONObject(0);
    } else if (_params instanceof JSONObject) {
        _tx = ((JSONObject) _params).getJSONObject("transaction");
    } else {
        return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid parameters");
    }
    ArgTxCall txParams = ArgTxCall.fromJSONforCall(_tx, getRecommendedNrgPrice());
    NumericalValue estimate = new NumericalValue(estimateNrg(txParams));
    return new RpcMsg(estimate.toHexString());
}
Also used : ArgTxCall(org.aion.api.server.types.ArgTxCall) JSONObject(org.json.JSONObject) NumericalValue(org.aion.api.server.types.NumericalValue) JSONArray(org.json.JSONArray)

Example 8 with ArgTxCall

use of org.aion.api.server.types.ArgTxCall in project aion by aionnetwork.

the class ApiWeb3Aion method eth_signTransaction.

/**
 * Sign a transaction. This account needs to be unlocked.
 *
 * @param _params
 * @return
 */
public RpcMsg eth_signTransaction(Object _params) {
    JSONObject _tx;
    // Address to sign with
    String _address = null;
    if (_params instanceof JSONArray) {
        _tx = ((JSONArray) _params).getJSONObject(0);
        if (((JSONArray) _params).length() > 1)
            _address = ((JSONArray) _params).get(1) + "";
    } else if (_params instanceof JSONObject) {
        _tx = ((JSONObject) _params).getJSONObject("transaction");
        _address = ((JSONObject) _params).get("address") + "";
    } else {
        return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid parameters");
    }
    ArgTxCall txParams = ArgTxCall.fromJSON(_tx, getRecommendedNrgPrice());
    if (txParams == null)
        return new RpcMsg(null, RpcError.INVALID_PARAMS, "Please check your transaction object.");
    AionTransaction tx = signTransaction(txParams, _address);
    if (tx != null) {
        JSONObject obj = new JSONObject();
        obj.put("raw", StringUtils.toJsonHex(tx.getEncoded()));
        JSONObject txObj = new JSONObject();
        txObj.put("nonce", StringUtils.toJsonHex(tx.getNonce()));
        txObj.put("gasPrice", StringUtils.toJsonHex(tx.getEnergyPrice()));
        txObj.put("nrgPrice", StringUtils.toJsonHex(tx.getEnergyPrice()));
        txObj.put("gas", StringUtils.toJsonHex(tx.getEnergyLimit()));
        txObj.put("nrg", StringUtils.toJsonHex(tx.getEnergyLimit()));
        txObj.put("to", StringUtils.toJsonHex(tx.getDestinationAddress() == null ? EMPTY_BYTE_ARRAY : tx.getDestinationAddress().toByteArray()));
        txObj.put("value", StringUtils.toJsonHex(tx.getValue()));
        txObj.put("input", StringUtils.toJsonHex(tx.getData()));
        txObj.put("hash", StringUtils.toJsonHex(tx.getTransactionHash()));
        obj.put("tx", txObj);
        return new RpcMsg(obj);
    } else {
        if (LOG.isDebugEnabled())
            LOG.debug("Transaction signing failed");
        return new RpcMsg(null, RpcError.INTERNAL_ERROR, "Error in signing the transaction.");
    }
}
Also used : ArgTxCall(org.aion.api.server.types.ArgTxCall) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) AionTransaction(org.aion.base.AionTransaction) Hex.toHexString(org.aion.util.conversions.Hex.toHexString)

Example 9 with ArgTxCall

use of org.aion.api.server.types.ArgTxCall in project aion by aionnetwork.

the class ApiAionTest method testSendTransaction.

@Test
public void testSendTransaction() {
    byte[] msg = "test message".getBytes();
    // null params returns INVALID_TX
    ArgTxCall txcall = null;
    assertEquals(api.sendTransaction(txcall).getType(), TxResponse.INVALID_TX);
    // null from and empty from return INVALID_FROM
    txcall = new ArgTxCall(null, AddressUtils.ZERO_ADDRESS, msg, BigInteger.ONE, BigInteger.ONE, 100000, 100000, null);
    assertEquals(api.sendTransaction(txcall).getType(), TxResponse.INVALID_FROM);
    txcall = new ArgTxCall(null, AddressUtils.ZERO_ADDRESS, msg, BigInteger.ONE, BigInteger.ONE, 100000, 100000, null);
    assertEquals(api.sendTransaction(txcall).getType(), TxResponse.INVALID_FROM);
    // locked account should throw INVALID_ACCOUNT
    AionAddress addr = AddressUtils.wrapAddress(Keystore.create("testPwd"));
    txcall = new ArgTxCall(addr, AddressUtils.ZERO_ADDRESS, msg, repo.getNonce(addr), BigInteger.ONE, 100000, 100000, null);
    assertEquals(api.sendTransaction(txcall).getType(), TxResponse.INVALID_ACCOUNT);
}
Also used : ArgTxCall(org.aion.api.server.types.ArgTxCall) AionAddress(org.aion.types.AionAddress) Test(org.junit.Test)

Aggregations

ArgTxCall (org.aion.api.server.types.ArgTxCall)9 AionAddress (org.aion.types.AionAddress)6 JSONArray (org.json.JSONArray)5 AionTransaction (org.aion.base.AionTransaction)4 JSONObject (org.json.JSONObject)4 Test (org.junit.Test)4 ApiTxResponse (org.aion.api.server.ApiTxResponse)2 AionTxReceipt (org.aion.base.AionTxReceipt)2 Block (org.aion.zero.impl.types.Block)2 MiningBlock (org.aion.zero.impl.types.MiningBlock)2 StakingBlock (org.aion.zero.impl.types.StakingBlock)2 ByteString (com.google.protobuf.ByteString)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 BigInteger (java.math.BigInteger)1 ByteBuffer (java.nio.ByteBuffer)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1