Skip to content

Commit

Permalink
Merge pull request #67 from artemii235/etomic
Browse files Browse the repository at this point in the history
#57 #59 #62. "version" RPC. Check ETH/ERC20 payment status before spend. eth_withdraw "broadcast".
  • Loading branch information
artemii235 authored Jun 11, 2018
2 parents 4a0028e + adb2364 commit 9cd2565
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 20 deletions.
8 changes: 4 additions & 4 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ cmake --build . --target marketmaker-testnet'''
sh '''docker-compose build
docker-compose up -d
./start_BEER_OTHER_trade.sh ETH
timeout 600 grep -q "SWAP completed" <(docker-compose logs -f clientnode)
timeout 600 grep -q "SWAP completed" <(docker-compose logs -f seednode)
timeout 600 grep -q "SWAP completed" <(COMPOSE_HTTP_TIMEOUT=600 docker-compose logs -f clientnode)
timeout 600 grep -q "SWAP completed" <(COMPOSE_HTTP_TIMEOUT=600 docker-compose logs -f seednode)
docker-compose down'''
}
}
Expand All @@ -32,8 +32,8 @@ docker-compose down'''
sh '''docker-compose build
docker-compose up -d
./start_BEER_OTHER_trade_inverted.sh ETH
timeout 600 grep -q "SWAP completed" <(docker-compose logs -f clientnode)
timeout 600 grep -q "SWAP completed" <(docker-compose logs -f seednode)
timeout 600 grep -q "SWAP completed" <(COMPOSE_HTTP_TIMEOUT=600 docker-compose logs -f clientnode)
timeout 600 grep -q "SWAP completed" <(COMPOSE_HTTP_TIMEOUT=600 docker-compose logs -f seednode)
docker-compose down'''
}
}
Expand Down
7 changes: 7 additions & 0 deletions iguana/exchanges/LP_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ cancel(uuid)\n\
buy(base, rel, price, relvolume, timeout=10, duration=3600, nonce)\n\
sell(base, rel, price, basevolume, timeout=10, duration=3600, nonce)\n\
withdraw(coin, outputs[], broadcast=0)\n\
eth_withdraw(coin, to, amount, gas, gas_price, broadcast=0)\n\
txblast(coin, utxotxid, utxovout, utxovalue, txfee, passphrase, outputs[], broadcast=0)\n\
sendrawtransaction(coin, signedtx)\n\
swapstatus(pending=0, fast=0)\n\
Expand Down Expand Up @@ -176,7 +177,13 @@ getfee(coin)\n\
sleep(seconds=60)\n\
listtransactions(coin, address, count=10, skip=0)\n\
jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
version\n\
\"}"));
if ( strcmp(method,"version") == 0 ) {
retjson = cJSON_CreateObject();
jaddstr(retjson,"result",MM_VERSION);
return(jprint(retjson,1));
}

if ( (base= jstr(argjson,"base")) == 0 )
base = "";
Expand Down
30 changes: 30 additions & 0 deletions iguana/exchanges/LP_etomic.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
// Created by artem on 24.01.18.
//
#include "LP_etomic.h"
#define ALICE_PAYMENT_SENT 1
#define BOB_DEPOSIT_SENT 1
#define BOB_PAYMENT_SENT 1

int32_t LP_etomic_wait_for_confirmation(char *txId)
{
Expand Down Expand Up @@ -218,6 +221,10 @@ char *LP_etomicalice_reclaims_payment(struct LP_swap_remember *swap)
privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr);

uint8arrayToHex(input.dealId, swap->txids[BASILISK_ALICEPAYMENT].bytes, 32);
if (alicePaymentStatus(input.dealId + 2) != ALICE_PAYMENT_SENT) {
printf("Alice payment smart contract status check failed, can't spend\n");
return NULL;
}
satoshisToWei(input.amount, swap->destamount);

if (swap->alicetomic[0] != 0) {
Expand Down Expand Up @@ -266,6 +273,11 @@ char *LP_etomicbob_spends_alice_payment(struct LP_swap_remember *swap)
privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr);

uint8arrayToHex(input.dealId, swap->txids[BASILISK_ALICEPAYMENT].bytes, 32);
if (alicePaymentStatus(input.dealId + 2) != ALICE_PAYMENT_SENT) {
printf("Alice payment smart contract status check failed, can't spend\n");
return NULL;
}

satoshisToWei(input.amount, swap->destamount);

if (swap->alicetomic[0] != 0) {
Expand Down Expand Up @@ -417,6 +429,11 @@ char *LP_etomicbob_refunds_deposit(struct LP_swap_remember *swap)
return NULL;
}
uint8arrayToHex(input.depositId, swap->txids[BASILISK_BOBDEPOSIT].bytes, 32);
if (bobDepositStatus(input.depositId + 2) != BOB_DEPOSIT_SENT) {
printf("Bob deposit smart contract status check failed, can't claim\n");
return NULL;
}

strcpy(input.aliceAddress, swap->etomicdest);

bits256 invertedSecret;
Expand Down Expand Up @@ -567,6 +584,10 @@ char *LP_etomicbob_reclaims_payment(struct LP_swap_remember *swap)
return NULL;
}
uint8arrayToHex(input.paymentId, swap->txids[BASILISK_BOBPAYMENT].bytes, 32);
if (bobPaymentStatus(input.paymentId + 2) != BOB_PAYMENT_SENT) {
printf("Bob payment smart contract status check failed, can't spend\n");
return NULL;
}
strcpy(input.aliceAddress, swap->etomicdest);
uint8arrayToHex(input.aliceHash, swap->secretAm, 20);

Expand Down Expand Up @@ -608,6 +629,10 @@ char *LP_etomicalice_spends_bob_payment(struct LP_swap_remember *swap)
privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr);

uint8arrayToHex(input.paymentId, swap->txids[BASILISK_BOBPAYMENT].bytes, 32);
if (bobPaymentStatus(input.paymentId + 2) != BOB_PAYMENT_SENT) {
printf("Bob payment smart contract status check failed, can't spend\n");
return NULL;
}
satoshisToWei(input.amount, swap->values[BASILISK_BOBPAYMENT]);

if (swap->bobtomic[0] != 0) {
Expand Down Expand Up @@ -656,6 +681,11 @@ char *LP_etomicalice_claims_bob_deposit(struct LP_swap_remember *swap)
privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr);

uint8arrayToHex(input.depositId, swap->txids[BASILISK_BOBDEPOSIT].bytes, 32);
if (bobDepositStatus(input.depositId + 2) != BOB_DEPOSIT_SENT) {
printf("Bob deposit smart contract status check failed, can't claim\n");
return NULL;
}

satoshisToWei(input.amount, swap->values[BASILISK_BOBDEPOSIT]);

if (swap->bobtomic[0] != 0) {
Expand Down
68 changes: 55 additions & 13 deletions iguana/exchanges/LP_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -2046,13 +2046,49 @@ char *LP_movecoinbases(char *symbol)

#ifndef NOTETOMIC

char *LP_eth_tx_fee(struct iguana_info *coin, char *dest_addr, char *amount, int64_t gas, int64_t gas_price)
{
bits256 privkey;
cJSON *retjson = cJSON_CreateObject();
int64_t actual_gas_price = 0, actual_gas = 0;
char privkey_str[70];

if (gas_price > 0) {
actual_gas_price = gas_price;
} else {
actual_gas_price = getGasPriceFromStation(0);
if (actual_gas_price == 0) {
return (clonestr("{\"error\":\"Couldn't get gas price from station!\"}"));
}
}
cJSON_AddNumberToObject(retjson, "gas_price", actual_gas_price);

if (gas > 0) {
actual_gas = gas;
} else if (strcmp(coin->symbol, "ETH") == 0) {
actual_gas = 21000;
} else {
privkey = LP_privkey(coin->symbol, coin->smartaddr, coin->taddr);
uint8arrayToHex(privkey_str, privkey.bytes, 32);
actual_gas = estimate_erc20_gas(coin->etomic, dest_addr, amount, privkey_str, coin->decimals);
if (actual_gas == 0) {
return (clonestr("{\"error\":\"Couldn't estimate erc20 transfer gas usage!\"}"));
}
}
cJSON_AddNumberToObject(retjson, "gas", actual_gas);

double_t eth_fee = (actual_gas_price * actual_gas) / 1000000000.0;
cJSON_AddNumberToObject(retjson, "eth_fee", eth_fee);
return(jprint(retjson,1));
}

char *LP_eth_withdraw(struct iguana_info *coin,cJSON *argjson)
{
cJSON *retjson = cJSON_CreateObject();
cJSON *gas_json = cJSON_GetObjectItem(argjson, "gas");
cJSON *gas_price_json = cJSON_GetObjectItem(argjson, "gas_price");
char *dest_addr, *tx_id, privkey_str[70], amount_str[100];
int64_t amount = 0, gas = 0, gas_price = 0;
int64_t amount = 0, gas = 0, gas_price = 0, broadcast = 0;
bits256 privkey;

dest_addr = jstr(argjson, "to");
Expand All @@ -2075,21 +2111,27 @@ char *LP_eth_withdraw(struct iguana_info *coin,cJSON *argjson)
return (clonestr("{\"error\":\"'gas_price' can't be lower than 1!\"}"));
}
}
privkey = LP_privkey(coin->symbol, coin->smartaddr, coin->taddr);
uint8arrayToHex(privkey_str, privkey.bytes, 32);

broadcast = jint(argjson, "broadcast");
satoshisToWei(amount_str, amount);
if (strcmp(coin->symbol, "ETH") == 0) {
tx_id = sendEth(dest_addr, amount_str, privkey_str, 0, gas, gas_price, 0);
if (broadcast == 1) {
privkey = LP_privkey(coin->symbol, coin->smartaddr, coin->taddr);
uint8arrayToHex(privkey_str, privkey.bytes, 32);
if (strcmp(coin->symbol, "ETH") == 0) {
tx_id = sendEth(dest_addr, amount_str, privkey_str, 0, gas, gas_price, 0);
} else {
tx_id = sendErc20(coin->etomic, dest_addr, amount_str, privkey_str, 0, gas, gas_price, 0, coin->decimals);
}
if (tx_id != NULL) {
jaddstr(retjson, "tx_id", tx_id);
free(tx_id);
} else {
jaddstr(retjson, "error", "Error sending transaction");
}
return (jprint(retjson, 1));
} else {
tx_id = sendErc20(coin->etomic, dest_addr, amount_str, privkey_str, 0, gas, gas_price, 0, coin->decimals);
return LP_eth_tx_fee(coin, dest_addr, amount_str, gas, gas_price);
}
if (tx_id != NULL) {
jaddstr(retjson, "tx_id", tx_id);
free(tx_id);
} else {
jaddstr(retjson, "error", "Error sending transaction");
}
return(jprint(retjson,1));
}

char *LP_eth_gas_price()
Expand Down
2 changes: 1 addition & 1 deletion iguana/exchanges/etomicswap/etomiccurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ char *ethCall(char *to, const char *data)
cJSON_AddItemToArray(params, cJSON_CreateString("latest"));
cJSON *resultJson = sendRpcRequest("eth_call", params);
cJSON_Delete(params);
char* result = NULL;
char *result = NULL;
if (resultJson != NULL && is_cJSON_String(resultJson) && resultJson->valuestring != NULL) {
result = (char *) malloc(strlen(resultJson->valuestring) + 1);
strcpy(result, resultJson->valuestring);
Expand Down
53 changes: 52 additions & 1 deletion iguana/exchanges/etomicswap/etomiclib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,21 @@ std::stringstream getErc20TransferData(char *tokenAddress, char *to, char *amoun
return ss;
}

uint64_t estimate_erc20_gas(
char *tokenAddress,
char *to,
char *amount,
char *privKey,
uint8_t decimals
)
{
std::stringstream ss = getErc20TransferData(tokenAddress, to, amount, decimals);
char *from = privKey2Addr(privKey);
uint64_t result = estimateGas(from, tokenAddress, ss.str().c_str());
free(from);
return result;
}

char *sendErc20(
char *tokenAddress,
char *to,
Expand Down Expand Up @@ -758,7 +773,7 @@ char *sendErc20(
return result;
}

uint8_t verifyAliceErc20FeeData(char* tokenAddress, char *to, char *amount, char *data, uint8_t decimals)
uint8_t verifyAliceErc20FeeData(char *tokenAddress, char *to, char *amount, char *data, uint8_t decimals)
{
std::stringstream ss = getErc20TransferData(tokenAddress, to, amount, decimals);
if (strcmp(ss.str().c_str(), data) != 0) {
Expand All @@ -767,3 +782,39 @@ uint8_t verifyAliceErc20FeeData(char* tokenAddress, char *to, char *amount, char
}
return 1;
}

uint8_t alicePaymentStatus(char *paymentId)
{
char buffer[100];
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, "0x81cd872a");
strcat(buffer, paymentId);
char *hexStatus = ethCall(ETOMIC_ALICECONTRACT, buffer);
auto status = (uint8_t) strtol(hexStatus + 66, NULL, 0);
free(hexStatus);
return status;
}

uint8_t bobDepositStatus(char *depositId)
{
char buffer[100];
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, "0x3d4dff7b");
strcat(buffer, depositId);
char *hexStatus = ethCall(ETOMIC_BOBCONTRACT, buffer);
auto status = (uint8_t) strtol(hexStatus + 130, NULL, 0);
free(hexStatus);
return status;
}

uint8_t bobPaymentStatus(char *paymentId)
{
char buffer[100];
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, "0x0716326d");
strcat(buffer, paymentId);
char *hexStatus = ethCall(ETOMIC_BOBCONTRACT, buffer);
auto status = (uint8_t) strtol(hexStatus + 130, NULL, 0);
free(hexStatus);
return status;
}
14 changes: 13 additions & 1 deletion iguana/exchanges/etomicswap/etomiclib.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,19 @@ char *sendErc20(
);

uint8_t verifyAliceErc20FeeData(char* tokenAddress, char *to, char *amount, char *data, uint8_t decimals);
// Your prototype or Definition

uint8_t alicePaymentStatus(char *paymentId);
uint8_t bobDepositStatus(char *depositId);
uint8_t bobPaymentStatus(char *paymentId);

uint64_t estimate_erc20_gas(
char *tokenAddress,
char *to,
char *amount,
char *privKey,
uint8_t decimals
);

#ifdef __cplusplus
}
#endif

0 comments on commit 9cd2565

Please sign in to comment.