Skip to content

Commit

Permalink
Merge pull request #53 from artemii235/etomic
Browse files Browse the repository at this point in the history
Add "decimals" field for ERC20 tokens without decimals function.
  • Loading branch information
artemii235 committed May 28, 2018
2 parents 0f020ec + 18873c4 commit 282f88c
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 49 deletions.
2 changes: 1 addition & 1 deletion etomic_build/coins

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions iguana/exchanges/LP_coins.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ struct iguana_info *LP_coinfind(char *symbol)
struct iguana_info *LP_coincreate(cJSON *item)
{
struct iguana_info cdata,*coin=0; int32_t isPoS,longestchain = 1; uint16_t port; uint64_t txfee; double estimatedrate; uint8_t pubtype,p2shtype,wiftype; char *name=0,*symbol,*assetname=0;

if ( (symbol= jstr(item,"coin")) != 0 && symbol[0] != 0 && strlen(symbol) < 16 && LP_coinfind(symbol) == 0 && (port= juint(item,"rpcport")) != 0 )
{
isPoS = jint(item,"isPoS");
Expand All @@ -514,6 +515,8 @@ struct iguana_info *LP_coincreate(cJSON *item)
}
else if ( (name= jstr(item,"name")) == 0 )
name = symbol;

cdata.decimals = juint(item,"decimals");
if ( LP_coininit(&cdata,symbol,name,assetname==0?"":assetname,isPoS,port,pubtype,p2shtype,wiftype,txfee,estimatedrate,longestchain,juint(item,"wiftaddr"),juint(item,"taddr"),LP_busport(port),jstr(item,"confpath")) < 0 )
{
coin = LP_coinadd(&cdata);
Expand Down
55 changes: 45 additions & 10 deletions iguana/exchanges/LP_etomic.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ char *LP_etomicalice_send_fee(struct basilisk_swap *swap)
if (strcmp(swap->I.alicestr,"ETH") == 0 ) {
return(sendEth(dexaddr, amount, secretKey, 1, 0, 0, 1));
} else {
return(sendErc20(swap->I.alicetomic, dexaddr, amount, secretKey, 1, 0, 0, 1));
struct iguana_info *alicecoin = LP_coinfind(swap->I.alicestr);

return(sendErc20(swap->I.alicetomic, dexaddr, amount, secretKey, 1, 0, 0, 1, alicecoin->decimals));
}
}

Expand Down Expand Up @@ -74,13 +76,15 @@ uint8_t LP_etomic_verify_alice_fee(struct basilisk_swap *swap)
}
return(1);
} else {
struct iguana_info *alicecoin = LP_coinfind(swap->I.alicestr);

if (strcmp(data.to, swap->I.alicetomic) != 0) {
printf("Alice ERC20 fee %s token address %s is not equal to expected %s\n", swap->otherfee.I.ethTxid, data.to, swap->I.alicetomic);
return(0);
}
char weiAmount[70];
satoshisToWei(weiAmount, swap->otherfee.I.amount);
return(verifyAliceErc20FeeData(swap->I.alicetomic, dexaddr, weiAmount, data.input));
return(verifyAliceErc20FeeData(swap->I.alicetomic, dexaddr, weiAmount, data.input, alicecoin->decimals));
}
}

Expand All @@ -107,20 +111,23 @@ char *LP_etomicalice_send_payment(struct basilisk_swap *swap)
}
else
{
struct iguana_info *alicecoin = LP_coinfind(swap->I.alicestr);

memset(&input20,0,sizeof(input20));
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.I.actualtxid.bytes, 32);
strcpy(input20.tokenAddress, swap->I.alicetomic);
satoshisToWei(input20.amount, swap->I.alicesatoshis);
input20.decimals = alicecoin->decimals;

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.etomicdest, ETOMIC_ALICECONTRACT, swap->I.alicetomic);
uint64_t allowance = getErc20Allowance(swap->I.etomicdest, ETOMIC_ALICECONTRACT, swap->I.alicetomic, alicecoin->decimals);
if (allowance < swap->I.alicesatoshis) {
printf("Alice token allowance is too low, setting new allowance\n");
ApproveErc20Input approveErc20Input;
Expand Down Expand Up @@ -173,13 +180,16 @@ uint8_t LP_etomic_verify_alice_payment(struct basilisk_swap *swap, char *txId)

return(verifyAliceEthPaymentData(input, data.input));
} else {
struct iguana_info *alicecoin = LP_coinfind(swap->I.alicestr);

memset(&input20,0,sizeof(input20));
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.I.actualtxid.bytes, 32);
strcpy(input20.tokenAddress, swap->I.alicetomic);
satoshisToWei(input20.amount, swap->I.alicesatoshis);
input20.decimals = alicecoin->decimals;

return(verifyAliceErc20PaymentData(input20, data.input));
}
Expand Down Expand Up @@ -223,6 +233,9 @@ char *LP_etomicalice_reclaims_payment(struct LP_swap_remember *swap)
}
uint8arrayToHex(input.bobSecret, invertedSecret.bytes, 32);

struct iguana_info *alice_coin = LP_coinfind(swap->alicecoin);
input.decimals = alice_coin->decimals;

strcpy(txData.from, swap->etomicdest);
strcpy(txData.to, ETOMIC_ALICECONTRACT);
strcpy(txData.amount, "0");
Expand Down Expand Up @@ -268,6 +281,8 @@ char *LP_etomicbob_spends_alice_payment(struct LP_swap_remember *swap)
}
uint8arrayToHex(input.aliceSecret, invertedSecret.bytes, 32);
uint8arrayToHex(input.bobHash, swap->secretBn, 20);
struct iguana_info *alice_coin = LP_coinfind(swap->alicecoin);
input.decimals = alice_coin->decimals;

strcpy(txData.from, swap->etomicsrc);
strcpy(txData.to, ETOMIC_ALICECONTRACT);
Expand Down Expand Up @@ -296,19 +311,22 @@ char *LP_etomicbob_sends_deposit(struct basilisk_swap *swap)
uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32);
return bobSendsEthDeposit(input, txData);
} else {
struct iguana_info *bobcoin = LP_coinfind(swap->I.bobstr);

uint8arrayToHex(input20.depositId, swap->bobdeposit.I.actualtxid.bytes, 32);
strcpy(input20.aliceAddress, swap->I.etomicdest);
uint8arrayToHex(input20.bobHash, swap->I.secretBn, 20);
satoshisToWei(input20.amount, swap->bobdeposit.I.amount);
strcpy(input20.tokenAddress, swap->I.bobtomic);
input20.lockTime = swap->bobdeposit.I.locktime;
input20.decimals = bobcoin->decimals;

strcpy(txData.from, swap->I.etomicsrc);
strcpy(txData.to, ETOMIC_BOBCONTRACT);
strcpy(txData.amount, "0");
uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32);

uint64_t allowance = getErc20Allowance(swap->I.etomicsrc, ETOMIC_BOBCONTRACT, swap->I.bobtomic);
uint64_t allowance = getErc20Allowance(swap->I.etomicsrc, ETOMIC_BOBCONTRACT, swap->I.bobtomic, bobcoin->decimals);
if (allowance < swap->bobdeposit.I.amount) {
printf("Bob token allowance is too low, setting new allowance\n");
ApproveErc20Input approveErc20Input;
Expand Down Expand Up @@ -362,12 +380,15 @@ uint8_t LP_etomic_verify_bob_deposit(struct basilisk_swap *swap, char *txId)

return verifyBobEthDepositData(input, data.input);
} else {
struct iguana_info *bobcoin = LP_coinfind(swap->I.bobstr);

uint8arrayToHex(input20.depositId, swap->bobdeposit.I.actualtxid.bytes, 32);
strcpy(input20.aliceAddress, swap->I.etomicdest);
uint8arrayToHex(input20.bobHash, swap->I.secretBn, 20);
satoshisToWei(input20.amount, swap->bobdeposit.I.amount);
strcpy(input20.tokenAddress, swap->I.bobtomic);
input20.lockTime = swap->bobdeposit.I.locktime;
input20.decimals = bobcoin->decimals;

return verifyBobErc20DepositData(input20, data.input);
}
Expand All @@ -384,9 +405,10 @@ char *LP_etomicbob_refunds_deposit(struct LP_swap_remember *swap)
memset(&txData,0,sizeof(txData));
memset(&input,0,sizeof(input));

struct iguana_info *ecoin;
struct iguana_info *ecoin, *bobcoin;
bits256 privkey;
ecoin = LP_coinfind("ETOMIC");
bobcoin = LP_coinfind(swap->bobcoin);
privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr);

EthTxReceipt receipt = getEthTxReceipt(swap->bobDepositEthTx);
Expand All @@ -410,6 +432,7 @@ char *LP_etomicbob_refunds_deposit(struct LP_swap_remember *swap)
strcpy(input.tokenAddress, "0x0000000000000000000000000000000000000000");
}
satoshisToWei(input.amount, swap->values[BASILISK_BOBDEPOSIT]);
input.decimals = bobcoin->decimals;

strcpy(txData.from, swap->etomicsrc);
strcpy(txData.to, ETOMIC_BOBCONTRACT);
Expand Down Expand Up @@ -439,19 +462,22 @@ char *LP_etomicbob_sends_payment(struct basilisk_swap *swap)
uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32);
return bobSendsEthPayment(input, txData);
} else {
struct iguana_info *bobcoin = LP_coinfind(swap->I.bobstr);

uint8arrayToHex(input20.paymentId, swap->bobpayment.I.actualtxid.bytes, 32);
strcpy(input20.aliceAddress, swap->I.etomicdest);
uint8arrayToHex(input20.aliceHash, swap->I.secretAm, 20);
satoshisToWei(input20.amount, swap->bobpayment.I.amount);
strcpy(input20.tokenAddress, swap->I.bobtomic);
input20.lockTime = swap->bobpayment.I.locktime;
input20.decimals = bobcoin->decimals;

strcpy(txData.from, swap->I.etomicsrc);
strcpy(txData.to, ETOMIC_BOBCONTRACT);
strcpy(txData.amount, "0");
uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32);

uint64_t allowance = getErc20Allowance(swap->I.etomicsrc, ETOMIC_BOBCONTRACT, swap->I.bobtomic);
uint64_t allowance = getErc20Allowance(swap->I.etomicsrc, ETOMIC_BOBCONTRACT, swap->I.bobtomic, bobcoin->decimals);
if (allowance < swap->bobpayment.I.amount) {
printf("Bob token allowance is too low, setting new allowance\n");
ApproveErc20Input approveErc20Input;
Expand Down Expand Up @@ -504,12 +530,15 @@ uint8_t LP_etomic_verify_bob_payment(struct basilisk_swap *swap, char *txId)

return verifyBobEthPaymentData(input, data.input);
} else {
struct iguana_info *bobcoin = LP_coinfind(swap->I.bobstr);

uint8arrayToHex(input20.paymentId, swap->bobpayment.I.actualtxid.bytes, 32);
strcpy(input20.aliceAddress, swap->I.etomicdest);
uint8arrayToHex(input20.aliceHash, swap->I.secretAm, 20);
satoshisToWei(input20.amount, swap->bobpayment.I.amount);
strcpy(input20.tokenAddress, swap->I.bobtomic);
input20.lockTime = swap->bobpayment.I.locktime;
input20.decimals = bobcoin->decimals;

return verifyBobErc20PaymentData(input20, data.input);
}
Expand All @@ -526,9 +555,10 @@ char *LP_etomicbob_reclaims_payment(struct LP_swap_remember *swap)
memset(&txData,0,sizeof(txData));
memset(&input,0,sizeof(input));

struct iguana_info *ecoin;
struct iguana_info *ecoin, *bobcoin;
bits256 privkey;
ecoin = LP_coinfind("ETOMIC");
bobcoin = LP_coinfind(swap->bobcoin);
privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr);

EthTxReceipt receipt = getEthTxReceipt(swap->bobPaymentEthTx);
Expand All @@ -546,6 +576,7 @@ char *LP_etomicbob_reclaims_payment(struct LP_swap_remember *swap)
strcpy(input.tokenAddress, "0x0000000000000000000000000000000000000000");
}
satoshisToWei(input.amount, swap->values[BASILISK_BOBPAYMENT]);
input.decimals = bobcoin->decimals;

strcpy(txData.from, swap->etomicsrc);
strcpy(txData.to, ETOMIC_BOBCONTRACT);
Expand All @@ -570,9 +601,10 @@ char *LP_etomicalice_spends_bob_payment(struct LP_swap_remember *swap)
printf("Bob payment %s receipt status failed, can't spend\n", swap->bobPaymentEthTx);
return NULL;
}
struct iguana_info *ecoin;
struct iguana_info *ecoin, *bobcoin;
bits256 privkey;
ecoin = LP_coinfind("ETOMIC");
bobcoin = LP_coinfind(swap->bobcoin);
privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr);

uint8arrayToHex(input.paymentId, swap->txids[BASILISK_BOBPAYMENT].bytes, 32);
Expand All @@ -591,6 +623,7 @@ char *LP_etomicalice_spends_bob_payment(struct LP_swap_remember *swap)
invertedSecret.bytes[i] = swap->privAm.bytes[31 - i];
}
uint8arrayToHex(input.aliceSecret, invertedSecret.bytes, 32);
input.decimals = bobcoin->decimals;

strcpy(txData.from, swap->etomicdest);
strcpy(txData.to, ETOMIC_BOBCONTRACT);
Expand All @@ -616,9 +649,10 @@ char *LP_etomicalice_claims_bob_deposit(struct LP_swap_remember *swap)
return NULL;
}

struct iguana_info *ecoin;
struct iguana_info *ecoin, *bobcoin;
bits256 privkey;
ecoin = LP_coinfind("ETOMIC");
bobcoin = LP_coinfind(swap->bobcoin);
privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr);

uint8arrayToHex(input.depositId, swap->txids[BASILISK_BOBDEPOSIT].bytes, 32);
Expand All @@ -632,6 +666,7 @@ char *LP_etomicalice_claims_bob_deposit(struct LP_swap_remember *swap)

strcpy(input.bobAddress, swap->etomicsrc);
uint8arrayToHex(input.bobHash, swap->secretBn, 20);
input.decimals = bobcoin->decimals;

strcpy(txData.from, swap->etomicdest);
strcpy(txData.to, ETOMIC_BOBCONTRACT);
Expand Down Expand Up @@ -718,6 +753,6 @@ uint64_t LP_etomic_get_balance(struct iguana_info *coin, char *coinaddr)
if (strcmp(coin->symbol, "ETH") == 0) {
return getEthBalance(coinaddr);
} else {
return getErc20BalanceSatoshi(coinaddr, coin->etomic);
return getErc20BalanceSatoshi(coinaddr, coin->etomic, coin->decimals);
}
}
2 changes: 1 addition & 1 deletion iguana/exchanges/LP_include.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ struct iguana_info
double price_kmd,force,perc,goal,goalperc,relvolume,rate;
void *electrum; void *ctx;
uint64_t maxamount,kmd_equiv,balanceA,balanceB,valuesumA,valuesumB;
uint8_t pubkey33[33],zcash;
uint8_t pubkey33[33],zcash,decimals;
int32_t privkeydepth;
bits256 cachedtxid,notarizationtxid; uint8_t *cachedtxiddata; int32_t cachedtxidlen;
bits256 cachedmerkle,notarizedhash; int32_t cachedmerkleheight;
Expand Down
2 changes: 1 addition & 1 deletion iguana/exchanges/LP_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -1987,7 +1987,7 @@ char *LP_eth_withdraw(struct iguana_info *coin,cJSON *argjson)
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);
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);
Expand Down
Loading

0 comments on commit 282f88c

Please sign in to comment.