Browse Source

Got both Bob and Alice ERC20 working.

patch-3
Artem Pikulin 7 years ago
parent
commit
06eb88dd7a
  1. 30
      iguana/exchanges/LP_etomic.c
  2. 3
      iguana/exchanges/LP_nativeDEX.c
  3. 6
      iguana/exchanges/LP_transaction.c
  4. 6
      iguana/exchanges/etomicswap/bob.c
  5. 12
      iguana/exchanges/etomicswap/etomiclib.cpp
  6. 4
      iguana/exchanges/etomicswap/etomiclib.h

30
iguana/exchanges/LP_etomic.c

@ -73,28 +73,29 @@ char *LP_etomicalice_send_payment(struct basilisk_swap *swap)
else
{
memset(&input20,0,sizeof(input20));
strcpy(input20.bobAddress, swap->I.etomicdest);
strcpy(input20.bobAddress, swap->I.etomicsrc);
uint8arrayToHex(input20.bobHash, swap->I.secretBn, 20);
uint8arrayToHex(input20.aliceHash, swap->I.secretAm, 20);
uint8arrayToHex(input20.dealId, swap->alicepayment.utxotxid.bytes, 32);
uint8arrayToHex(input20.dealId, swap->alicepayment.I.actualtxid.bytes, 32);
strcpy(input20.tokenAddress, swap->I.alicetomic);
satoshisToWei(input20.amount, swap->I.alicesatoshis);
strcpy(txData.from, swap->I.etomicsrc);
strcpy(txData.from, swap->I.etomicdest);
strcpy(txData.to, ETOMIC_ALICECONTRACT);
strcpy(txData.amount, "0");
uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32);
uint64_t allowance = getErc20Allowance(swap->I.etomicsrc, ETOMIC_ALICECONTRACT, swap->I.alicetomic);
uint64_t allowance = getErc20Allowance(swap->I.etomicdest, ETOMIC_ALICECONTRACT, swap->I.alicetomic);
if (allowance < swap->I.alicesatoshis) {
printf("Alice token allowance is too low, setting new allowance\n");
ApproveErc20Input approveErc20Input;
strcpy(approveErc20Input.tokenAddress, swap->I.alicetomic);
strcpy(approveErc20Input.owner, swap->I.etomicsrc);
strcpy(approveErc20Input.owner, swap->I.etomicdest);
strcpy(approveErc20Input.spender, ETOMIC_ALICECONTRACT);
// hard code for now
strcpy(approveErc20Input.amount, "20000000000000000000");
char *tokenBalance = getErc20BalanceHexWei(swap->I.etomicdest, swap->I.alicetomic);
strcpy(approveErc20Input.amount, tokenBalance);
free(tokenBalance);
strcpy(approveErc20Input.secret, txData.secretKey);
char *allowTxId = approveErc20(approveErc20Input);
@ -134,14 +135,14 @@ uint8_t LP_etomic_verify_alice_payment(struct basilisk_swap *swap, char *txId)
else
{
memset(&input20,0,sizeof(input20));
strcpy(input20.bobAddress, swap->I.etomicdest);
strcpy(input20.bobAddress, swap->I.etomicsrc);
uint8arrayToHex(input20.bobHash, swap->I.secretBn, 20);
uint8arrayToHex(input20.aliceHash, swap->I.secretAm, 20);
uint8arrayToHex(input20.dealId, swap->alicepayment.utxotxid.bytes, 32);
strcpy(input20.tokenAddress, swap->I.alicetomic);
satoshisToWei(input20.amount, swap->I.alicesatoshis);
strcpy(txData.from, swap->I.etomicsrc);
strcpy(txData.from, swap->I.etomicdest);
strcpy(txData.to, ETOMIC_ALICECONTRACT);
strcpy(txData.amount, "0");
uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32);
@ -260,8 +261,9 @@ char *LP_etomicbob_sends_deposit(struct basilisk_swap *swap)
strcpy(approveErc20Input.owner, swap->I.etomicsrc);
strcpy(approveErc20Input.spender, ETOMIC_BOBCONTRACT);
// hard code for now
strcpy(approveErc20Input.amount, "20000000000000000000");
char *tokenBalance = getErc20BalanceHexWei(swap->I.etomicsrc, swap->I.bobtomic);
strcpy(approveErc20Input.amount, tokenBalance);
free(tokenBalance);
strcpy(approveErc20Input.secret, txData.secretKey);
char *allowTxId = approveErc20(approveErc20Input);
@ -387,8 +389,9 @@ char *LP_etomicbob_sends_payment(struct basilisk_swap *swap)
strcpy(approveErc20Input.owner, swap->I.etomicsrc);
strcpy(approveErc20Input.spender, ETOMIC_BOBCONTRACT);
// hard code for now
strcpy(approveErc20Input.amount, "20000000000000000000");
char *tokenBalance = getErc20BalanceHexWei(swap->I.etomicsrc, swap->I.bobtomic);
strcpy(approveErc20Input.amount, tokenBalance);
free(tokenBalance);
strcpy(approveErc20Input.secret, txData.secretKey);
char *allowTxId = approveErc20(approveErc20Input);
@ -506,6 +509,7 @@ char *LP_etomicalice_spends_bob_payment(struct LP_swap_remember *swap)
strcpy(txData.to, ETOMIC_BOBCONTRACT);
strcpy(txData.amount, "0");
uint8arrayToHex(txData.secretKey, privkey.bytes, 32);
printf("priv key: %s", txData.secretKey);
return aliceSpendsBobPayment(input, txData);
}

3
iguana/exchanges/LP_nativeDEX.c

@ -178,9 +178,6 @@ char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_
#include "LP_prices.c"
#include "LP_scan.c"
#include "LP_transaction.c"
#ifndef NOTETOMIC
#include "LP_etomic.c"
#endif
#include "LP_stats.c"
#include "LP_remember.c"
#include "LP_instantdex.c"

6
iguana/exchanges/LP_transaction.c

@ -2054,6 +2054,10 @@ void basilisk_alicepayment(struct basilisk_swap *swap,struct iguana_info *coin,s
basilisk_rawtx_gen(swap->ctx,"alicepayment",swap->I.started,swap->persistent_pubkey33,0,1,alicepayment,alicepayment->I.locktime,alicepayment->spendscript,alicepayment->I.spendlen,swap->I.Atxfee,1,0,swap->persistent_privkey,swap->changermd160,coinaddr);
}
#ifndef NOTETOMIC
#include "LP_etomic.c"
#endif
int32_t basilisk_alicetxs(int32_t pairsock,struct basilisk_swap *swap,uint8_t *data,int32_t maxlen)
{
char coinaddr[64],alicestr[65],alicetomic[128]; int32_t retval = -1; struct iguana_info *coin;
@ -2205,7 +2209,7 @@ int32_t LP_verify_bobdeposit(struct basilisk_swap *swap,uint8_t *data,int32_t da
printf(" <- aliceclaim\n");*/
//basilisk_txlog(swap,&swap->aliceclaim,swap->I.putduration+swap->I.callduration);
#ifndef NOTETOMIC
if (swap->bobdeposit.I.ethTxid[0] != 0) {
if (swap->bobdeposit.I.ethTxid[0] != 0 && LP_etomic_is_empty_tx_id(swap->bobdeposit.I.ethTxid) == 0) {
if (LP_etomic_wait_for_confirmation(swap->bobdeposit.I.ethTxid) < 0 || LP_etomic_verify_bob_deposit(swap, swap->bobdeposit.I.ethTxid) == 0) {
return(-1);
}

6
iguana/exchanges/etomicswap/bob.c

@ -11,7 +11,7 @@
char* bobContractAddress = "0x9387Fd3a016bB0205e4e131Dde886B9d2BC000A2";
char* aliceAddress = "0x485d2cc2d13a9e12E4b53D606DB1c8adc884fB8a";
char* bobAddress = "0xA7EF3f65714AE266414C9E58bB4bAa4E6FB82B41";
char* bobAddress = "0xbAB36286672fbdc7B250804bf6D14Be0dF69fa29";
char* tokenAddress = "0xc0eb7AeD740E1796992A08962c15661bDEB58003";
int main(int argc, char** argv)
@ -222,7 +222,7 @@ int main(int argc, char** argv)
case BOB_APPROVES_ERC20:
printf("approving erc20\n");
ApproveErc20Input input8;
strcpy(input8.amount, "20000000000000000000");
strcpy(input8.amount, "0");
strcpy(input8.spender, bobContractAddress);
strcpy(input8.owner, bobAddress);
strcpy(input8.tokenAddress, tokenAddress);
@ -239,7 +239,7 @@ int main(int argc, char** argv)
printf("%" PRIu64 "\n", getEthBalance(bobAddress));
break;
case BOB_ERC20_BALANCE:
printf("%" PRIu64 "\n", getErc20Balance(bobAddress, tokenAddress));
printf("%" PRIu64 "\n", getErc20BalanceSatoshi(bobAddress, tokenAddress));
break;
case TX_RECEIPT:
printf("getTxReceipt\n");

12
iguana/exchanges/etomicswap/etomiclib.cpp

@ -434,7 +434,7 @@ uint64_t getEthBalance(char* address)
return static_cast<uint64_t>(balance);
}
uint64_t getErc20Balance(char *address, char *tokenAddress)
uint64_t getErc20BalanceSatoshi(char *address, char *tokenAddress)
{
std::stringstream ss;
ss << "0x70a08231"
@ -448,6 +448,16 @@ uint64_t getErc20Balance(char *address, char *tokenAddress)
return static_cast<uint64_t>(balance);
}
char *getErc20BalanceHexWei(char *address, char *tokenAddress)
{
std::stringstream ss;
ss << "0x70a08231"
<< "000000000000000000000000"
<< toHex(jsToAddress(address));
char *hexBalance = ethCall(tokenAddress, ss.str().c_str());
return hexBalance;
}
uint64_t getErc20Allowance(char *owner, char *spender, char *tokenAddress)
{
std::stringstream ss;

4
iguana/exchanges/etomicswap/etomiclib.h

@ -168,8 +168,8 @@ char* getPubKeyFromPriv(char* privKey);
// returns satoshis, not wei!
uint64_t getEthBalance(char* address);
// returns satoshis, not wei!
uint64_t getErc20Balance(char* address, char tokenAddress[65]);
uint64_t getErc20BalanceSatoshi(char* address, char tokenAddress[65]);
char *getErc20BalanceHexWei(char* address, char tokenAddress[65]);
uint8_t getErc20Decimals(char *tokenAddress);

Loading…
Cancel
Save