Browse Source

#19 Add gas usage estimation on ERC20 transfer.

pass-iguana-arg
Artem Pikulin 7 years ago
parent
commit
bebf5d5050
  1. 22
      iguana/exchanges/etomicswap/etomiccurl.c
  2. 1
      iguana/exchanges/etomicswap/etomiccurl.h
  3. 10
      iguana/exchanges/etomicswap/etomiclib.cpp

22
iguana/exchanges/etomicswap/etomiccurl.c

@ -186,7 +186,7 @@ char* getEthBalanceRequest(char* address)
return balance;
}
char* ethCall(char* to, const char* data)
char *ethCall(char *to, const char *data)
{
cJSON *params = cJSON_CreateArray();
cJSON *txObject = cJSON_CreateObject();
@ -205,6 +205,26 @@ char* ethCall(char* to, const char* data)
return result;
}
uint64_t estimateGas(char *from, char *to, const char *data)
{
cJSON *params = cJSON_CreateArray();
cJSON *txObject = cJSON_CreateObject();
cJSON_AddStringToObject(txObject, "from", from);
cJSON_AddStringToObject(txObject, "to", to);
cJSON_AddStringToObject(txObject, "data", data);
cJSON_AddItemToArray(params, txObject);
cJSON_AddItemToArray(params, cJSON_CreateString("latest"));
cJSON *resultJson = sendRpcRequest("eth_estimateGas", params);
cJSON_Delete(params);
uint64_t result = 0;
if (resultJson != NULL && is_cJSON_String(resultJson) && resultJson->valuestring != NULL) {
result = (uint64_t)strtoul(resultJson->valuestring, NULL, 0);
result = (result / 100) * 120; // add 20% because real gas usage might differ from estimate
}
cJSON_Delete(resultJson);
return result;
}
EthTxReceipt getEthTxReceipt(char *txId)
{
EthTxReceipt result;

1
iguana/exchanges/etomicswap/etomiccurl.h

@ -39,6 +39,7 @@ typedef struct
char* sendRawTx(char* rawTx);
char* sendRawTxWaitConfirm(char* rawTx);
char* ethCall(char* to, const char* data);
uint64_t estimateGas(char *from, char *to, const char *data);
int64_t getNonce(char* address);
char* getEthBalanceRequest(char* address);
EthTxReceipt getEthTxReceipt(char *txId);

10
iguana/exchanges/etomicswap/etomiclib.cpp

@ -624,15 +624,21 @@ char *sendErc20(char *tokenAddress, char *to, char *amount, char *privKey, uint8
{
TransactionSkeleton tx;
char *from = privKey2Addr(privKey), *result;
std::stringstream ss = getErc20TransferData(tokenAddress, to, amount);
tx.from = jsToAddress(from);
tx.to = jsToAddress(tokenAddress);
tx.value = 0;
tx.gas = 60000;
uint64_t gas = estimateGas(from, tokenAddress, ss.str().c_str());
if (gas > 0) {
tx.gas = gas;
} else {
tx.gas = 150000;
}
tx.gasPrice = getGasPriceFromStation() * boost::multiprecision::pow(u256(10), 9);
tx.nonce = getNonce(from);
free(from);
std::stringstream ss = getErc20TransferData(tokenAddress, to, amount);
tx.data = jsToBytes(ss.str());
char *rawTx = signTx(tx, privKey);

Loading…
Cancel
Save