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);
}
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());
}
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.");
}
}
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);
}
Aggregations