Browse Source

Add methods to get ETH and ERC20 balance.

etomic
Artem Pikulin 7 years ago
parent
commit
b1ea433d0f
  1. 35
      iguana/exchanges/etomicswap/bob.c
  2. 50
      iguana/exchanges/etomicswap/etomiccurl.c
  3. 2
      iguana/exchanges/etomicswap/etomiccurl.h
  4. 23
      iguana/exchanges/etomicswap/etomiclib.cpp
  5. 4
      iguana/exchanges/etomicswap/etomiclib.h

35
iguana/exchanges/etomicswap/bob.c

@ -4,8 +4,8 @@
#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <inttypes.h>
#include "etomiclib.h"
#include <cjson/cJSON.h>
char* bobContractAddress = "0x9387Fd3a016bB0205e4e131Dde886B9d2BC000A2";
char* aliceAddress = "0x485d2cc2d13a9e12E4b53D606DB1c8adc884fB8a";
@ -23,9 +23,11 @@ int main(int argc, char** argv)
BOB_ERC20_PAYMENT,
BOB_CLAIMS_PAYMENT,
ALICE_CLAIMS_PAYMENT,
BOB_APPROVES_ERC20
BOB_APPROVES_ERC20,
BOB_ETH_BALANCE,
BOB_ERC20_BALANCE,
};
if (argc < 3) {
if (argc < 2) {
return 1;
}
int action = atoi(argv[1]);
@ -46,6 +48,8 @@ int main(int argc, char** argv)
};
result = bobSendsEthDeposit(input, txData);
printf("%s\n", result);
free(result);
break;
case BOB_ERC20_DEPOSIT:
txData.amount = "0";
@ -62,6 +66,8 @@ int main(int argc, char** argv)
};
result = bobSendsErc20Deposit(input1, txData);
printf("%s\n", result);
free(result);
break;
case BOB_CLAIMS_DEPOSIT:
txData.amount = "0";
@ -79,6 +85,8 @@ int main(int argc, char** argv)
};
result = bobRefundsDeposit(input2, txData);
printf("%s\n", result);
free(result);
break;
case ALICE_CLAIMS_DEPOSIT:
txData.amount = "0";
@ -96,6 +104,8 @@ int main(int argc, char** argv)
};
result = aliceClaimsBobDeposit(input3, txData);
printf("%s\n", result);
free(result);
break;
case BOB_ETH_PAYMENT:
txData.amount = "1000000000000000000";
@ -110,6 +120,8 @@ int main(int argc, char** argv)
};
result = bobSendsEthPayment(input4, txData);
printf("%s\n", result);
free(result);
break;
case BOB_ERC20_PAYMENT:
txData.amount = "0";
@ -126,6 +138,8 @@ int main(int argc, char** argv)
};
result = bobSendsErc20Payment(input5, txData);
printf("%s\n", result);
free(result);
break;
case BOB_CLAIMS_PAYMENT:
txData.amount = "0";
@ -143,6 +157,8 @@ int main(int argc, char** argv)
};
result = bobReclaimsBobPayment(input6, txData);
printf("%s\n", result);
free(result);
break;
case ALICE_CLAIMS_PAYMENT:
txData.amount = "0";
@ -160,18 +176,27 @@ int main(int argc, char** argv)
};
result = aliceSpendsBobPayment(input7, txData);
printf("%s\n", result);
free(result);
break;
case BOB_APPROVES_ERC20:
result = approveErc20(
"10000000000000000000",
"0xA7EF3f65714AE266414C9E58bB4bAa4E6FB82B41",
getenv("BOB_PK")
);
printf("%s\n", result);
free(result);
break;
case BOB_ETH_BALANCE:
printf("%" PRIu64 "\n", getEthBalance(bobAddress));
break;
case BOB_ERC20_BALANCE:
printf("%" PRIu64 "\n", getErc20Balance(bobAddress, tokenAddress));
break;
default:
return 1;
}
printf("%s\n", result);
free(result);
return 0;
}

50
iguana/exchanges/etomicswap/etomiccurl.c

@ -86,6 +86,7 @@ char* sendRawTx(char* rawTx)
char* tmp = cJSON_GetObjectItem(json, "result")->valuestring;
char* txId = (char*)malloc(strlen(tmp) + 1);
strcpy(txId, tmp);
cJSON_Delete(json);
free(requestResult);
return txId;
}
@ -110,3 +111,52 @@ int getNonce(char* address)
free(requestResult);
return nonce;
}
char* getEthBalanceRequest(char* address)
{
char* string;
cJSON *request = cJSON_CreateObject();
cJSON *params = cJSON_CreateArray();
cJSON_AddItemToObject(request, "jsonrpc", cJSON_CreateString("2.0"));
cJSON_AddItemToObject(request, "method", cJSON_CreateString("eth_getBalance"));
cJSON_AddItemToArray(params, cJSON_CreateString(address));
cJSON_AddItemToArray(params, cJSON_CreateString("latest"));
cJSON_AddItemToObject(request, "params", params);
cJSON_AddItemToObject(request, "id", cJSON_CreateNumber(2));
string = cJSON_PrintUnformatted(request);
char* requestResult = sendRequest(string);
cJSON_Delete(request);
cJSON *json = cJSON_Parse(requestResult);
char* tmp = cJSON_GetObjectItem(json, "result")->valuestring;
char* balance = (char*)malloc(strlen(tmp) + 1);
strcpy(balance, tmp);
cJSON_Delete(json);
free(requestResult);
return balance;
}
char* ethCall(char* to, const char* data)
{
char* string;
cJSON *request = cJSON_CreateObject();
cJSON *params = cJSON_CreateArray();
cJSON *txObject = cJSON_CreateObject();
cJSON_AddItemToObject(request, "jsonrpc", cJSON_CreateString("2.0"));
cJSON_AddItemToObject(request, "method", cJSON_CreateString("eth_call"));
cJSON_AddStringToObject(txObject, "to", to);
cJSON_AddStringToObject(txObject, "data", data);
cJSON_AddItemToArray(params, txObject);
cJSON_AddItemToArray(params, cJSON_CreateString("latest"));
cJSON_AddItemToObject(request, "params", params);
cJSON_AddItemToObject(request, "id", cJSON_CreateNumber(2));
string = cJSON_PrintUnformatted(request);
char* requestResult = sendRequest(string);
cJSON_Delete(request);
cJSON *json = cJSON_Parse(requestResult);
char* tmp = cJSON_GetObjectItem(json, "result")->valuestring;
char* balance = (char*)malloc(strlen(tmp) + 1);
strcpy(balance, tmp);
cJSON_Delete(json);
free(requestResult);
return balance;
}

2
iguana/exchanges/etomicswap/etomiccurl.h

@ -3,7 +3,9 @@ extern "C"{
#endif
char* sendRawTx(char* rawTx);
char* ethCall(char* to, const char* data);
int getNonce(char* address);
char* getEthBalanceRequest(char* address);
#ifdef __cplusplus
}

23
iguana/exchanges/etomicswap/etomiclib.cpp

@ -332,3 +332,26 @@ char* getPubKeyFromPriv(char* privKey)
ss << "0x" << publicKey;
return stringStreamToChar(ss);
}
uint64_t getEthBalance(char* address)
{
char* hexBalance = getEthBalanceRequest(address);
// convert wei to satoshi
u256 balance = jsToU256(hexBalance) / exp10<10>();
free(hexBalance);
return static_cast<uint64_t>(balance);
}
uint64_t getErc20Balance(char* address, char* tokenAddress)
{
std::stringstream ss;
ss << "0x70a08231"
<< "000000000000000000000000"
<< toHex(jsToAddress(address));
std::stringstream& resultStream = *(new std::stringstream);
char* hexBalance = ethCall(tokenAddress, ss.str().c_str());
// convert wei to satoshi
u256 balance = jsToU256(hexBalance) / exp10<10>();
free(hexBalance);
return static_cast<uint64_t>(balance);
}

4
iguana/exchanges/etomicswap/etomiclib.h

@ -1,6 +1,8 @@
//
// Created by artem on 24.01.18.
//
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -125,6 +127,8 @@ char* aliceSpendsBobPayment(AliceSpendsBobPaymentInput input, BasicTxData txData
char* privKey2Addr(char* privKey);
char* pubKey2Addr(char* pubKey);
char* getPubKeyFromPriv(char* privKey);
uint64_t getEthBalance(char* address);
uint64_t getErc20Balance(char* address, char* tokenAddress);
// Your prototype or Definition
#ifdef __cplusplus
}

Loading…
Cancel
Save