From 780fe7123546fa9a5c39c50c151939a91c56c936 Mon Sep 17 00:00:00 2001 From: Artem Pikulin Date: Mon, 5 Mar 2018 20:57:27 +0700 Subject: [PATCH] Send Alice ETH and ERC20 fee. --- iguana/exchanges/LP_etomic.c | 49 ++++++++--------------- iguana/exchanges/etomicswap/bob.c | 8 ++++ iguana/exchanges/etomicswap/etomiclib.cpp | 43 ++++++++++++++++++++ iguana/exchanges/etomicswap/etomiclib.h | 11 +++-- 4 files changed, 75 insertions(+), 36 deletions(-) diff --git a/iguana/exchanges/LP_etomic.c b/iguana/exchanges/LP_etomic.c index 0ec3a85af..37688d9bb 100644 --- a/iguana/exchanges/LP_etomic.c +++ b/iguana/exchanges/LP_etomic.c @@ -49,6 +49,18 @@ int32_t LP_etomic_wait_for_confirmation(char *txId) return(receipt.confirmations); } +char *LP_etomicalice_send_fee(struct basilisk_swap *swap) +{ + char amount[100], secretKey[70]; + satoshisToWei(amount, swap->myfee.I.amount); + uint8arrayToHex(secretKey, swap->persistent_privkey.bytes, 32); + if (strcmp(swap->I.alicestr,"ETH") == 0 ) { + return(sendEth(ETH_FEE_ACCEPTOR, amount, secretKey)); + } else { + return(sendErc20(swap->I.alicetomic, ETH_FEE_ACCEPTOR, amount, secretKey)); + } +} + char *LP_etomicalice_send_payment(struct basilisk_swap *swap) { AliceSendsEthPaymentInput input; AliceSendsErc20PaymentInput input20; BasicTxData txData; @@ -113,10 +125,8 @@ uint8_t LP_etomic_verify_alice_payment(struct basilisk_swap *swap, char *txId) if (data.exists == 0 || strcmp(data.to, ETOMIC_ALICECONTRACT) != 0 || strcmp(data.from, swap->I.etomicdest) != 0) { return 0; } - AliceSendsEthPaymentInput input; AliceSendsErc20PaymentInput input20; BasicTxData txData; + AliceSendsEthPaymentInput input; AliceSendsErc20PaymentInput input20; - // set input and txData fields from the swap data structure - memset(&txData,0,sizeof(txData)); if ( strcmp(swap->I.alicestr,"ETH") == 0 ) { memset(&input,0,sizeof(input)); @@ -125,11 +135,6 @@ uint8_t LP_etomic_verify_alice_payment(struct basilisk_swap *swap, char *txId) uint8arrayToHex(input.aliceHash, swap->I.secretAm, 20); uint8arrayToHex(input.dealId, swap->alicepayment.I.actualtxid.bytes, 32); - strcpy(txData.from, swap->I.etomicdest); - strcpy(txData.to, ETOMIC_ALICECONTRACT); - satoshisToWei(txData.amount, swap->I.alicesatoshis); - uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32); - return(verifyAliceEthPaymentData(input, data.input)); } else @@ -142,10 +147,6 @@ uint8_t LP_etomic_verify_alice_payment(struct basilisk_swap *swap, char *txId) strcpy(input20.tokenAddress, swap->I.alicetomic); satoshisToWei(input20.amount, swap->I.alicesatoshis); - strcpy(txData.from, swap->I.etomicdest); - strcpy(txData.to, ETOMIC_ALICECONTRACT); - strcpy(txData.amount, "0"); - uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32); return(verifyAliceErc20PaymentData(input20, data.input)); } } @@ -283,8 +284,6 @@ uint8_t LP_etomic_verify_bob_deposit(struct basilisk_swap *swap, char *txId) } BobSendsEthDepositInput input; BobSendsErc20DepositInput input20; - BasicTxData txData; - memset(&txData,0,sizeof(txData)); memset(&input,0,sizeof(input)); memset(&input20,0,sizeof(input20)); if ( strcmp(swap->I.bobstr,"ETH") == 0 ) { @@ -292,10 +291,6 @@ uint8_t LP_etomic_verify_bob_deposit(struct basilisk_swap *swap, char *txId) strcpy(input.aliceAddress, swap->I.etomicdest); uint8arrayToHex(input.bobHash, swap->I.secretBn, 20); - strcpy(txData.from, swap->I.etomicsrc); - strcpy(txData.to, ETOMIC_BOBCONTRACT); - satoshisToWei(txData.amount, swap->bobdeposit.I.amount); - uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32); return verifyBobEthDepositData(input, data.input); } else { uint8arrayToHex(input20.depositId, swap->bobdeposit.I.actualtxid.bytes, 32); @@ -304,10 +299,6 @@ uint8_t LP_etomic_verify_bob_deposit(struct basilisk_swap *swap, char *txId) satoshisToWei(input20.amount, swap->bobdeposit.I.amount); strcpy(input20.tokenAddress, swap->I.bobtomic); - strcpy(txData.from, swap->I.etomicsrc); - strcpy(txData.to, ETOMIC_BOBCONTRACT); - strcpy(txData.amount, "0"); - uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32); return verifyBobErc20DepositData(input20, data.input); } } @@ -411,8 +402,6 @@ uint8_t LP_etomic_verify_bob_payment(struct basilisk_swap *swap, char *txId) } BobSendsEthPaymentInput input; BobSendsErc20PaymentInput input20; - BasicTxData txData; - memset(&txData,0,sizeof(txData)); memset(&input,0,sizeof(input)); memset(&input20,0,sizeof(input20)); @@ -421,10 +410,6 @@ uint8_t LP_etomic_verify_bob_payment(struct basilisk_swap *swap, char *txId) strcpy(input.aliceAddress, swap->I.etomicdest); uint8arrayToHex(input.aliceHash, swap->I.secretAm, 20); - strcpy(txData.from, swap->I.etomicsrc); - strcpy(txData.to, ETOMIC_BOBCONTRACT); - satoshisToWei(txData.amount, swap->bobpayment.I.amount); - uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32); return verifyBobEthPaymentData(input, data.input); } else { uint8arrayToHex(input20.paymentId, swap->bobpayment.I.actualtxid.bytes, 32); @@ -433,10 +418,6 @@ uint8_t LP_etomic_verify_bob_payment(struct basilisk_swap *swap, char *txId) satoshisToWei(input20.amount, swap->bobpayment.I.amount); strcpy(input20.tokenAddress, swap->I.bobtomic); - strcpy(txData.from, swap->I.etomicsrc); - strcpy(txData.to, ETOMIC_BOBCONTRACT); - strcpy(txData.amount, "0"); - uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32); return verifyBobErc20PaymentData(input20, data.input); } } @@ -555,9 +536,11 @@ char *sendEthTx(struct basilisk_swap *swap, struct basilisk_rawtx *rawtx) return LP_etomicbob_sends_deposit(swap); } else if (rawtx == &swap->bobpayment && swap->I.bobtomic[0] != 0) { return LP_etomicbob_sends_payment(swap); + } else if (swap->I.iambob == 0 && rawtx == &swap->myfee && swap->I.alicetomic[0] != 0) { + return LP_etomicalice_send_fee(swap); } else { char *result = malloc(67); - strcpy(result, "0x0000000000000000000000000000000000000000000000000000000000000000"); + strcpy(result, EMPTY_ETH_TX_ID); return result; } } diff --git a/iguana/exchanges/etomicswap/bob.c b/iguana/exchanges/etomicswap/bob.c index 1d8037ad5..662477848 100644 --- a/iguana/exchanges/etomicswap/bob.c +++ b/iguana/exchanges/etomicswap/bob.c @@ -278,5 +278,13 @@ int main(int argc, char** argv) uint64_t tokenAllowance = getErc20Allowance(bobAddress, bobContractAddress, tokenAddress); printf("allowance: %" PRIu64 "\n", tokenAllowance); + char *sendEthTx = sendEth(bobAddress, "100000000000000", getenv("BOB_PK")); + printf("sent ETH: %s\n", sendEthTx); + free(sendEthTx); + + char *sendErc20Tx = sendErc20(tokenAddress, bobAddress, "100000000000000", getenv("BOB_PK")); + printf("sent Erc20: %s\n", sendErc20Tx); + free(sendErc20Tx); + return 0; } diff --git a/iguana/exchanges/etomicswap/etomiclib.cpp b/iguana/exchanges/etomicswap/etomiclib.cpp index eec1eb1a7..425edd988 100644 --- a/iguana/exchanges/etomicswap/etomiclib.cpp +++ b/iguana/exchanges/etomicswap/etomiclib.cpp @@ -497,3 +497,46 @@ void satoshisToWei(char *dest, uint64_t input) sprintf(dest, "%" PRIu64, input); strcat(dest, "0000000000"); } + +char *sendEth(char *to, char *amount, char *privKey) +{ + TransactionSkeleton tx; + char *from = privKey2Addr(privKey); + tx.from = jsToAddress(from); + tx.to = jsToAddress(to); + tx.value = jsToU256(amount); + tx.gas = 21000; + tx.gasPrice = ETOMIC_GASMULT * exp10<9>(); + tx.nonce = getNonce(from); + free(from); + + char *rawTx = signTx(tx, privKey); + char *result = sendRawTx(rawTx); + free(rawTx); + return result; +} + +char *sendErc20(char *tokenAddress, char *to, char *amount, char *privKey) +{ + TransactionSkeleton tx; + char *from = privKey2Addr(privKey); + tx.from = jsToAddress(from); + tx.to = jsToAddress(tokenAddress); + tx.value = 0; + tx.gas = 60000; + tx.gasPrice = ETOMIC_GASMULT * exp10<9>(); + tx.nonce = getNonce(from); + free(from); + + std::stringstream ss; + ss << "0xa9059cbb" + << "000000000000000000000000" + << toHex(jsToAddress(to)) + << toHex(toBigEndian(jsToU256(amount))); + tx.data = jsToBytes(ss.str()); + + char *rawTx = signTx(tx, privKey); + char *result = sendRawTx(rawTx); + free(rawTx); + return result; +} diff --git a/iguana/exchanges/etomicswap/etomiclib.h b/iguana/exchanges/etomicswap/etomiclib.h index de23c53ff..495c63489 100644 --- a/iguana/exchanges/etomicswap/etomiclib.h +++ b/iguana/exchanges/etomicswap/etomiclib.h @@ -13,12 +13,14 @@ extern "C" { #define ETOMIC_BOBCONTRACT "0x9387fd3a016bb0205e4e131dde886b9d2bc000a2" #define ETOMIC_GASMULT 100 #else -#define ETOMIC_ALICECONTRACT "0x9bC5418CEdED51dB08467fc4b62F32C5D9EBdA55" -#define ETOMIC_BOBCONTRACT "0xB1Ad803ea4F57401639c123000C75F5B66E4D123" +#define ETOMIC_ALICECONTRACT "0x9bc5418ceded51db08467fc4b62f32c5d9ebda55" +#define ETOMIC_BOBCONTRACT "0xb1ad803ea4f57401639c123000c75f5b66e4d123" #define ETOMIC_GASMULT 4 #endif + #define EMPTY_ETH_TX_ID "0x0000000000000000000000000000000000000000000000000000000000000000" +#define ETH_FEE_ACCEPTOR "0x485d2cc2d13a9e12e4b53d606db1c8adc884fb8a" typedef struct { char from[65]; @@ -133,7 +135,7 @@ typedef struct { char secret[70]; } ApproveErc20Input; -char* approveErc20(ApproveErc20Input input); +char *approveErc20(ApproveErc20Input input); char* aliceSendsEthPayment(AliceSendsEthPaymentInput input, BasicTxData txData); uint8_t verifyAliceEthPaymentData(AliceSendsEthPaymentInput input, char *data); @@ -178,6 +180,9 @@ uint64_t getErc20Allowance(char *owner, char *spender, char *tokenAddress); void uint8arrayToHex(char *dest, uint8_t *input, int len); void satoshisToWei(char *dest, uint64_t input); + +char *sendEth(char *to, char *amount, char *privKey); +char *sendErc20(char *tokenAddress, char *to, char *amount, char *privKey); // Your prototype or Definition #ifdef __cplusplus }