Browse Source

#59 Check ETH/ERC20 payment/deposit status before trying to spend it.

pass-iguana-arg
Artem Pikulin 7 years ago
parent
commit
4e50178ca0
  1. 8
      Jenkinsfile
  2. 30
      iguana/exchanges/LP_etomic.c
  3. 2
      iguana/exchanges/etomicswap/etomiccurl.c
  4. 38
      iguana/exchanges/etomicswap/etomiclib.cpp
  5. 6
      iguana/exchanges/etomicswap/etomiclib.h

8
Jenkinsfile

@ -22,8 +22,8 @@ cmake --build . --target marketmaker-testnet'''
sh '''docker-compose build sh '''docker-compose build
docker-compose up -d docker-compose up -d
./start_BEER_OTHER_trade.sh ETH ./start_BEER_OTHER_trade.sh ETH
timeout 600 grep -q "SWAP completed" <(docker-compose logs -f clientnode) timeout 600 grep -q "SWAP completed" <(COMPOSE_HTTP_TIMEOUT=600 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 seednode)
docker-compose down''' docker-compose down'''
} }
} }
@ -32,8 +32,8 @@ docker-compose down'''
sh '''docker-compose build sh '''docker-compose build
docker-compose up -d docker-compose up -d
./start_BEER_OTHER_trade_inverted.sh ETH ./start_BEER_OTHER_trade_inverted.sh ETH
timeout 600 grep -q "SWAP completed" <(docker-compose logs -f clientnode) timeout 600 grep -q "SWAP completed" <(COMPOSE_HTTP_TIMEOUT=600 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 seednode)
docker-compose down''' docker-compose down'''
} }
} }

30
iguana/exchanges/LP_etomic.c

@ -22,6 +22,9 @@
// Created by artem on 24.01.18. // Created by artem on 24.01.18.
// //
#include "LP_etomic.h" #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) int32_t LP_etomic_wait_for_confirmation(char *txId)
{ {
@ -218,6 +221,10 @@ char *LP_etomicalice_reclaims_payment(struct LP_swap_remember *swap)
privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr); privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr);
uint8arrayToHex(input.dealId, swap->txids[BASILISK_ALICEPAYMENT].bytes, 32); 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); satoshisToWei(input.amount, swap->destamount);
if (swap->alicetomic[0] != 0) { if (swap->alicetomic[0] != 0) {
@ -266,6 +273,11 @@ char *LP_etomicbob_spends_alice_payment(struct LP_swap_remember *swap)
privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr); privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr);
uint8arrayToHex(input.dealId, swap->txids[BASILISK_ALICEPAYMENT].bytes, 32); 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); satoshisToWei(input.amount, swap->destamount);
if (swap->alicetomic[0] != 0) { if (swap->alicetomic[0] != 0) {
@ -417,6 +429,11 @@ char *LP_etomicbob_refunds_deposit(struct LP_swap_remember *swap)
return NULL; return NULL;
} }
uint8arrayToHex(input.depositId, swap->txids[BASILISK_BOBDEPOSIT].bytes, 32); 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); strcpy(input.aliceAddress, swap->etomicdest);
bits256 invertedSecret; bits256 invertedSecret;
@ -567,6 +584,10 @@ char *LP_etomicbob_reclaims_payment(struct LP_swap_remember *swap)
return NULL; return NULL;
} }
uint8arrayToHex(input.paymentId, swap->txids[BASILISK_BOBPAYMENT].bytes, 32); 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); strcpy(input.aliceAddress, swap->etomicdest);
uint8arrayToHex(input.aliceHash, swap->secretAm, 20); uint8arrayToHex(input.aliceHash, swap->secretAm, 20);
@ -608,6 +629,10 @@ char *LP_etomicalice_spends_bob_payment(struct LP_swap_remember *swap)
privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr); privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr);
uint8arrayToHex(input.paymentId, swap->txids[BASILISK_BOBPAYMENT].bytes, 32); 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]); satoshisToWei(input.amount, swap->values[BASILISK_BOBPAYMENT]);
if (swap->bobtomic[0] != 0) { if (swap->bobtomic[0] != 0) {
@ -656,6 +681,11 @@ char *LP_etomicalice_claims_bob_deposit(struct LP_swap_remember *swap)
privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr); privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr);
uint8arrayToHex(input.depositId, swap->txids[BASILISK_BOBDEPOSIT].bytes, 32); 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]); satoshisToWei(input.amount, swap->values[BASILISK_BOBDEPOSIT]);
if (swap->bobtomic[0] != 0) { if (swap->bobtomic[0] != 0) {

2
iguana/exchanges/etomicswap/etomiccurl.c

@ -196,7 +196,7 @@ char *ethCall(char *to, const char *data)
cJSON_AddItemToArray(params, cJSON_CreateString("latest")); cJSON_AddItemToArray(params, cJSON_CreateString("latest"));
cJSON *resultJson = sendRpcRequest("eth_call", params); cJSON *resultJson = sendRpcRequest("eth_call", params);
cJSON_Delete(params); cJSON_Delete(params);
char* result = NULL; char *result = NULL;
if (resultJson != NULL && is_cJSON_String(resultJson) && resultJson->valuestring != NULL) { if (resultJson != NULL && is_cJSON_String(resultJson) && resultJson->valuestring != NULL) {
result = (char *) malloc(strlen(resultJson->valuestring) + 1); result = (char *) malloc(strlen(resultJson->valuestring) + 1);
strcpy(result, resultJson->valuestring); strcpy(result, resultJson->valuestring);

38
iguana/exchanges/etomicswap/etomiclib.cpp

@ -758,7 +758,7 @@ char *sendErc20(
return result; 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); std::stringstream ss = getErc20TransferData(tokenAddress, to, amount, decimals);
if (strcmp(ss.str().c_str(), data) != 0) { if (strcmp(ss.str().c_str(), data) != 0) {
@ -767,3 +767,39 @@ uint8_t verifyAliceErc20FeeData(char* tokenAddress, char *to, char *amount, char
} }
return 1; 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;
}

6
iguana/exchanges/etomicswap/etomiclib.h

@ -200,7 +200,11 @@ char *sendErc20(
); );
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);
// Your prototype or Definition
uint8_t alicePaymentStatus(char *paymentId);
uint8_t bobDepositStatus(char *depositId);
uint8_t bobPaymentStatus(char *paymentId);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

Loading…
Cancel
Save