corr bet and add tx validation

added min margin
This commit is contained in:
dimxy
2019-05-18 00:28:14 +05:00
parent e9fd323fb5
commit b932d0fab5
2 changed files with 21 additions and 7 deletions

View File

@@ -42,7 +42,8 @@ extern CScript KOMODO_EARLYTXID_SCRIPTPUB;
//#define PRICES_POINTFACTOR (int64_t)10000 //#define PRICES_POINTFACTOR (int64_t)10000
#define PRICES_REVSHAREDUST 10000 #define PRICES_REVSHAREDUST 10000
#define PRICES_SUBREVSHAREFEE(amount) ((amount) * 199 / 200) #define PRICES_SUBREVSHAREFEE(amount) ((amount) * 199 / 200) // revshare fee percentage == 0.005
#define PRICES_MINAVAILFUNDFRACTION 0.1 // leveraged bet limit < fund fraction
bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn); bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn);

View File

@@ -243,7 +243,7 @@ static bool ValidateBetTx(struct CCcontract_info *cp, Eval *eval, const CTransac
if ( ASSETCHAINS_EARLYTXIDCONTRACT == EVAL_PRICES && KOMODO_EARLYTXID_SCRIPTPUB.size() == 0 ) if ( ASSETCHAINS_EARLYTXIDCONTRACT == EVAL_PRICES && KOMODO_EARLYTXID_SCRIPTPUB.size() == 0 )
GetKomodoEarlytxidScriptPub(); GetKomodoEarlytxidScriptPub();
if (bettx.vout.size() < 5 || bettx.vout.size() > 6) if (bettx.vout.size() < 6 || bettx.vout.size() > 7)
return eval->Invalid("incorrect vout number for bet tx"); return eval->Invalid("incorrect vout number for bet tx");
vscript_t opret; vscript_t opret;
@@ -322,9 +322,9 @@ static bool ValidateAddFundingTx(struct CCcontract_info *cp, Eval *eval, const C
if (addfundingtx.vout[2].scriptPubKey != KOMODO_EARLYTXID_SCRIPTPUB) if (addfundingtx.vout[2].scriptPubKey != KOMODO_EARLYTXID_SCRIPTPUB)
return eval->Invalid("the fee was paid to wrong address."); return eval->Invalid("the fee was paid to wrong address.");
int64_t betamount = addfundingtx.vout[2].nValue; int64_t betamount = addfundingtx.vout[1].nValue;
if (betamount != PRICES_SUBREVSHAREFEE(amount)) { if (betamount != PRICES_SUBREVSHAREFEE(amount)) {
return eval->Invalid("invalid position size in the opreturn"); return eval->Invalid("invalid bet position size in the opreturn");
} }
return true; return true;
@@ -602,6 +602,19 @@ int64_t AddPricesInputs(struct CCcontract_info *cp, CMutableTransaction &mtx, ch
return(totalinputs); return(totalinputs);
} }
// return min equity percentage depending on leverage value
// for lev=1 2%
// for lev>=100 10%
double prices_minmarginpercent(int16_t leverage)
{
int16_t absleverage = std::abs(leverage);
if (absleverage < 100)
return (absleverage * 0.080808 + 1.9191919) / 100.0;
else
return 0.01;
}
UniValue prices_rawtxresult(UniValue &result, std::string rawtx, int32_t broadcastflag) UniValue prices_rawtxresult(UniValue &result, std::string rawtx, int32_t broadcastflag)
{ {
CTransaction tx; CTransaction tx;
@@ -1606,7 +1619,7 @@ int32_t prices_scanchain(std::vector<OneBetData> &bets, int16_t leverage, std::v
endheight = height; endheight = height;
int64_t equity = totalposition + totalprofits; int64_t equity = totalposition + totalprofits;
if (equity < 0) if (equity < (double)totalposition * prices_minmarginpercent(leverage))
{ // we are in loss { // we are in loss
break; break;
} }
@@ -1833,7 +1846,7 @@ int32_t prices_getbetinfo(uint256 bettxid, BetInfo &betinfo)
betinfo.liquidationprice = betinfo.averageCostbasis - betinfo.averageCostbasis / betinfo.leverage; betinfo.liquidationprice = betinfo.averageCostbasis - betinfo.averageCostbasis / betinfo.leverage;
} }
if (betinfo.equity >= 0) if (betinfo.equity >= (double)totalposition * prices_minmarginpercent(betinfo.leverage))
betinfo.isRekt = false; betinfo.isRekt = false;
else else
{ {
@@ -2384,7 +2397,7 @@ static bool prices_isacceptableamount(const std::vector<uint16_t> &vecparsed, in
std::cerr << "prices_isacceptableamount() amount=" << amount << " leverage=" << leverage << " fundTotals.totalFund=" << fundTotals.totalFund << " fundTotals.totalEquity=" << fundTotals.totalEquity << std::endl; std::cerr << "prices_isacceptableamount() amount=" << amount << " leverage=" << leverage << " fundTotals.totalFund=" << fundTotals.totalFund << " fundTotals.totalEquity=" << fundTotals.totalEquity << std::endl;
// if not fit to matched = allow to bet for leveraged amount no more than 10% from free fund // if not fit to matched = allow to bet for leveraged amount no more than 10% from free fund
if (amount * leverage < (fundTotals.totalFund - fundTotals.totalEquity) * 0.1) if (amount * leverage < (fundTotals.totalFund - fundTotals.totalEquity) * PRICES_MINAVAILFUNDFRACTION)
return true; return true;
return false; return false;