Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
FairCoin
FairCoin FreeVision
Commits
66ae169f
Commit
66ae169f
authored
Mar 12, 2019
by
Gampe Sebastian
Browse files
add /cbid /cmid /cask to bot
parent
75615dda
Changes
3
Hide whitespace changes
Inline
Side-by-side
FREEVISION_TG_BOT.md
View file @
66ae169f
...
...
@@ -13,3 +13,13 @@
show complete conversion table:
/c 1 FAIR
use the bid price for calculation ( defaulf exchange price)
/c ...
/cbid ...
use price between bid and ask for calculation
/cmid ...
use ask price for calculation
/cask ...
functions/get_data.php
View file @
66ae169f
...
...
@@ -17,9 +17,18 @@ define('COINGECKO_CURRENCIES_URL','https://api.coingecko.com/api/v3/simple/suppo
define
(
'COINGECKO_FAIR_PAIRS'
,
'../data/coingecko_fair_pairs.json'
);
define
(
'COINGECKO_FAIR_PAIRS_URL'
,
'https://api.coingecko.com/api/v3/simple/price?ids=faircoin&vs_currencies='
);
define
(
'WALLET_TICKER_BID'
,
'../data/ticker'
);
define
(
'WALLET_TICKER_ASK'
,
'../data/ticker_ask'
);
define
(
'WALLET_TICKER_MID'
,
'../data/ticker_mid'
);
define
(
'DEFAULT_CURRENCY'
,
'eur'
);
define
(
'ASK_DEPTH_EUR'
,
1000
);
define
(
'BID_DEPTH_EUR'
,
1000
);
define
(
'SLOT_SIZE_EUR'
,
100
);
define
(
'ASK_DEPTH_SLOTS'
,
10
);
define
(
'BID_DEPTH_SLOTS'
,
10
);
define
(
'SLOT_SIZE_FAIR'
,
1000
);
define
(
'ASK_OFFSET_PERC'
,
1.05
);
define
(
'BID_OFFSET_PERC'
,
0.95
);
...
...
@@ -176,6 +185,12 @@ function get_data($upd,$curr){
return
json_encode
(
$JS
[
'FREEVISION'
]);
}
/*
###################################################
get orderbook of coinexchange
---------------------------------------------------
*/
// recalculate data
$ARR
=
[
COINEXCHANGE
,
COINGECKO_FAIR_PAIRS
];
$ARR_ID
=
[
'BTCFAIR'
,
'COINGECKO_FAIR_PAIRS'
];
...
...
@@ -187,36 +202,20 @@ function get_data($upd,$curr){
$JS
[
$ARR_ID
[
$key
]]
=
json_decode
(
$json
,
true
);
}
//
calculate price
s
//
exchange pair
s
$PAIR
=
$JS
[
'COINGECKO_FAIR_PAIRS'
][
'faircoin'
];
$btc_eur
=
$PAIR
[
'eur'
]
/
$PAIR
[
'btc'
];
$BTCFAIR
=
$JS
[
'BTCFAIR'
][
'result'
];
$ask_depth_btc
=
ASK_DEPTH_EUR
/
$btc_eur
;
$bid_depth_btc
=
BID_DEPTH_EUR
/
$btc_eur
;
// get bid price by market depth ( at https://www.coinexchange.io/market/FAIR/BTC )
$c
=
0
;
$lp
=
0
;
foreach
(
$BTCFAIR
[
'BuyOrders'
]
as
$order
){
if
(
$c
<
$bid_depth_btc
){
$c
+=
$order
[
'Price'
]
*
$order
[
'Quantity'
];
$lp
=
$order
[
'Price'
];
}
else
{
break
;
}
}
$bid_price_btc
=
$lp
;
// get ask price by market depth ( at https://www.coinexchange.io/market/FAIR/BTC )
$c
=
0
;
$lp
=
0
;
foreach
(
$BTCFAIR
[
'SellOrders'
]
as
$order
){
if
(
$c
<
$ask_depth_btc
){
$c
+=
$order
[
'Price'
]
*
$order
[
'Quantity'
];
$lp
=
$order
[
'Price'
];
}
else
{
break
;
}
}
$ask_price_btc
=
$lp
;
// coinexchange orderbook data
$COINEXCHANGE_FAIR_BTC
=
$JS
[
'BTCFAIR'
][
'result'
];
// timestamp of entry
$timestamp
=
time
();
// initialize currency ticker for wallets
$WALLET_TICKER
=
[];
$WALLET_TICKER_ASK
=
[];
$WALLET_TICKER_MID
=
[];
// iterate all available currency pairs
foreach
(
$PAIR
as
$currency
=>
$price
){
...
...
@@ -265,11 +264,61 @@ function get_data($upd,$curr){
}
}
$fv_bid_current
=
floatval
(
$FREEVISION
[
'fv_bid'
]
*
$FREEVISION
[
'config'
][
'FACTOR'
]
/
$factor
);
/* check market max depth bid */
$depth_fair
=
0
;
$depth_price_btc
=
0
;
foreach
(
$COINEXCHANGE_FAIR_BTC
[
'BuyOrders'
]
as
$order
){
if
(
$depth_fair
<
(
BID_DEPTH_SLOTS
*
SLOT_SIZE_FAIR
)
){
if
(
$FREEVISION
[
'fv_bid'
]
<=
$m
*
$order
[
'Price'
]
/
$factor
){
$depth_fair
+=
$order
[
'Quantity'
];
$depth_price_btc
=
$order
[
'Price'
];
}
else
{
if
(
$depth_price_btc
==
0
){
$depth_price_btc
=
$order
[
'Price'
];
}
$depth_fair
=
100
;
break
;
}
}
else
{
$depth_fair
=
BID_DEPTH_SLOTS
*
SLOT_SIZE_FAIR
;
//$depth_price_btc=$order['Price'];
break
;
}
}
$bid_depth_fair
=
intVal
(
$depth_fair
/
SLOT_SIZE_FAIR
)
*
SLOT_SIZE_FAIR
;
$bid_depth_slots
=
intVal
(
$depth_fair
/
SLOT_SIZE_FAIR
);
$bid_price_btc
=
$depth_price_btc
;
/* check market max depth ask */
$depth_fair
=
0
;
$depth_price_btc
=
0
;
foreach
(
$COINEXCHANGE_FAIR_BTC
[
'SellOrders'
]
as
$order
){
if
(
$depth_fair
<
(
ASK_DEPTH_SLOTS
*
SLOT_SIZE_FAIR
)
){
if
(
$FREEVISION
[
'fv_ask'
]
>=
$m
*
$order
[
'Price'
]
/
$factor
){
$depth_fair
+=
$order
[
'Quantity'
];
$depth_price_btc
=
$order
[
'Price'
];
}
else
{
if
(
$depth_price_btc
==
0
){
$depth_price_btc
=
$order
[
'Price'
];
}
break
;
}
}
else
{
$depth_fair
=
ASK_DEPTH_SLOTS
*
SLOT_SIZE_FAIR
;
//$depth_price_btc=$order['Price'];
break
;
}
}
$ask_depth_fair
=
intVal
(
$depth_fair
/
SLOT_SIZE_FAIR
)
*
SLOT_SIZE_FAIR
;
$ask_depth_slots
=
intVal
(
$depth_fair
/
SLOT_SIZE_FAIR
);
$ask_price_btc
=
$depth_price_btc
;
$fv_bid_bid
=
freevision_price_normalize
(
25
,
$m
*
$bid_price_btc
,
BID_OFFSET_PERC
,
'bid'
)
/
$factor
;
$fv_bid_ask
=
freevision_price_normalize
(
25
,
$m
*
$ask_price_btc
,
BID_OFFSET_PERC
,
'bid'
)
/
$factor
;
$fv_ask
=
freevision_price_normalize
(
5
,
$m
*
$ask_price_btc
,
ASK_OFFSET_PERC
,
'ask'
)
/
$factor
;
$fv_bid_current
=
floatval
(
$FREEVISION
[
'fv_bid'
]
*
$FREEVISION
[
'config'
][
'FACTOR'
]
/
$factor
);
/*
add hysteresis for freevision bid price
...
...
@@ -293,10 +342,17 @@ function get_data($upd,$curr){
$FREEVISION
[
'fm_ask'
]
=
number_format
(
$m
*
$ask_price_btc
/
$factor
,
4
,
'.'
,
''
);
$FREEVISION
[
'config'
][
'FACTOR'
]
=
$factor
;
$FREEVISION
[
'config'
][
'FACTOR_PREPOSITION'
]
=
FACTOR_PREPOSITION
[
$factor
];
$FREEVISION
[
'config'
][
'ASK_DEPTH'
]
=
number_format
(
$m
*
ASK_DEPTH_EUR
/
$btc_eur
/
$factor
,
0
,
'.'
,
''
);
$FREEVISION
[
'config'
][
'BID_DEPTH'
]
=
number_format
(
$m
*
BID_DEPTH_EUR
/
$btc_eur
/
$factor
,
0
,
'.'
,
''
);
$FREEVISION
[
'config'
][
'ASK_OFFSET_PERC'
]
=
ASK_OFFSET_PERC
;
$FREEVISION
[
'config'
][
'BID_DEPTH'
]
=
number_format
(
$fv_bid
*
$bid_depth_fair
,
0
,
'.'
,
''
);
$FREEVISION
[
'config'
][
'BID_DEPTH_SLOTS'
]
=
$bid_depth_slots
;
$FREEVISION
[
'config'
][
'ASK_DEPTH'
]
=
number_format
(
$fv_ask
*
$ask_depth_fair
,
0
,
'.'
,
''
);
$FREEVISION
[
'config'
][
'ASK_DEPTH_SLOTS'
]
=
$ask_depth_slots
;
$FREEVISION
[
'config'
][
'BID_OFFSET_PERC'
]
=
BID_OFFSET_PERC
;
$FREEVISION
[
'config'
][
'ASK_OFFSET_PERC'
]
=
ASK_OFFSET_PERC
;
// add freevision bid price to wallet ticker
$WALLET_TICKER_BID
[
strtoupper
(
$currency
)][
'last'
]
=
round
(
$FREEVISION
[
'fv_bid'
]
*
$factor
,
8
);
$WALLET_TICKER_ASK
[
strtoupper
(
$currency
)][
'last'
]
=
round
(
$FREEVISION
[
'fv_ask'
]
*
$factor
,
8
);
$WALLET_TICKER_MID
[
strtoupper
(
$currency
)][
'last'
]
=
round
(
(
$FREEVISION
[
'fv_bid'
]
+
$FREEVISION
[
'fv_ask'
]
)
/
2
*
$factor
,
8
);
// overwrite current prices with new prices
$fp
=
fopen
(
$fn_freevision
,
'w+'
);
...
...
@@ -337,6 +393,19 @@ function get_data($upd,$curr){
}
// update wallet ticker file (.json)
$fp
=
fopen
(
WALLET_TICKER_BID
,
'w+'
);
fwrite
(
$fp
,
json_encode
(
$WALLET_TICKER_BID
));
fclose
(
$fp
);
$fp
=
fopen
(
WALLET_TICKER_MID
,
'w+'
);
fwrite
(
$fp
,
json_encode
(
$WALLET_TICKER_MID
));
fclose
(
$fp
);
$fp
=
fopen
(
WALLET_TICKER_ASK
,
'w+'
);
fwrite
(
$fp
,
json_encode
(
$WALLET_TICKER_ASK
));
fclose
(
$fp
);
return
get_data
(
false
,
$curr
);
}
...
...
@@ -379,4 +448,7 @@ if( $_GET['test'] == 1 ){
}
echo
update_data
();
//### update telegram stats
include_once
(
'tg.php'
);
?>
functions/tg_webhook.php
0 → 100644
View file @
66ae169f
<?php
include_once
(
'tg_conf.php'
);
define
(
'BOT_API'
,
'https://api.telegram.org/bot'
.
BOT_TOKEN
.
'/'
);
function
apiRequestWebhook
(
$method
,
$parameters
)
{
if
(
!
is_string
(
$method
))
{
error_log
(
"Method name must be a string
\n
"
);
return
false
;
}
if
(
!
$parameters
)
{
$parameters
=
array
();
}
else
if
(
!
is_array
(
$parameters
))
{
error_log
(
"Parameters must be an array
\n
"
);
return
false
;
}
$parameters
[
"method"
]
=
$method
;
header
(
"Content-Type: application/json"
);
echo
json_encode
(
$parameters
);
return
true
;
}
function
exec_curl_request
(
$handle
)
{
$response
=
curl_exec
(
$handle
);
if
(
$response
===
false
)
{
$errno
=
curl_errno
(
$handle
);
$error
=
curl_error
(
$handle
);
error_log
(
"Curl returned error
$errno
:
$error
\n
"
);
curl_close
(
$handle
);
return
false
;
}
$http_code
=
intval
(
curl_getinfo
(
$handle
,
CURLINFO_HTTP_CODE
));
curl_close
(
$handle
);
if
(
$http_code
>=
500
)
{
// do not wat to DDOS server if something goes wrong
sleep
(
10
);
return
false
;
}
else
if
(
$http_code
!=
200
)
{
$response
=
json_decode
(
$response
,
true
);
error_log
(
"Request has failed with error
{
$response
[
'error_code'
]
}
:
{
$response
[
'description'
]
}
\n
"
);
if
(
$http_code
==
401
)
{
throw
new
Exception
(
'Invalid access token provided'
);
}
return
false
;
}
else
{
$response
=
json_decode
(
$response
,
true
);
if
(
isset
(
$response
[
'description'
]))
{
error_log
(
"Request was successful:
{
$response
[
'description'
]
}
\n
"
);
}
$response
=
$response
[
'result'
];
}
return
$response
;
}
function
apiRequest
(
$method
,
$parameters
)
{
if
(
!
is_string
(
$method
))
{
error_log
(
"Method name must be a string
\n
"
);
return
false
;
}
if
(
!
$parameters
)
{
$parameters
=
array
();
}
else
if
(
!
is_array
(
$parameters
))
{
error_log
(
"Parameters must be an array
\n
"
);
return
false
;
}
foreach
(
$parameters
as
$key
=>
&
$val
)
{
// encoding to JSON array parameters, for example reply_markup
if
(
!
is_numeric
(
$val
)
&&
!
is_string
(
$val
))
{
$val
=
json_encode
(
$val
);
}
}
$url
=
BOT_API
.
$method
.
'?'
.
http_build_query
(
$parameters
);
$handle
=
curl_init
(
$url
);
curl_setopt
(
$handle
,
CURLOPT_RETURNTRANSFER
,
true
);
curl_setopt
(
$handle
,
CURLOPT_CONNECTTIMEOUT
,
5
);
curl_setopt
(
$handle
,
CURLOPT_TIMEOUT
,
60
);
return
exec_curl_request
(
$handle
);
}
function
apiRequestJson
(
$method
,
$parameters
)
{
if
(
!
is_string
(
$method
))
{
error_log
(
"Method name must be a string
\n
"
);
return
false
;
}
if
(
!
$parameters
)
{
$parameters
=
array
();
}
else
if
(
!
is_array
(
$parameters
))
{
error_log
(
"Parameters must be an array
\n
"
);
return
false
;
}
$parameters
[
"method"
]
=
$method
;
$handle
=
curl_init
(
BOT_API
);
curl_setopt
(
$handle
,
CURLOPT_RETURNTRANSFER
,
true
);
curl_setopt
(
$handle
,
CURLOPT_CONNECTTIMEOUT
,
5
);
curl_setopt
(
$handle
,
CURLOPT_TIMEOUT
,
60
);
curl_setopt
(
$handle
,
CURLOPT_POST
,
true
);
curl_setopt
(
$handle
,
CURLOPT_POSTFIELDS
,
json_encode
(
$parameters
));
curl_setopt
(
$handle
,
CURLOPT_HTTPHEADER
,
array
(
"Content-Type: application/json"
));
return
exec_curl_request
(
$handle
);
}
function
processMessage
(
$message
)
{
// process incoming message
$message_id
=
$message
[
'message_id'
];
$chat_id
=
$message
[
'chat'
][
'id'
];
if
(
isset
(
$message
[
'text'
]))
{
// incoming text message
$text
=
$message
[
'text'
];
$CMD
=
preg_split
(
'/ /'
,
preg_replace
(
'/ /'
,
' '
,
$text
));
if
(
strpos
(
$text
,
"/c "
)
===
0
or
strpos
(
$text
,
"/cbid "
)
===
0
)
{
$reply
=
command_c
(
'ticker'
,
$CMD
[
1
],
$CMD
[
2
],
$CMD
[
3
],
$CMD
[
4
]);
if
(
$reply
)
apiRequestJson
(
"sendMessage"
,
array
(
'chat_id'
=>
$chat_id
,
"text"
=>
$reply
));
}
else
if
(
strpos
(
$text
,
"/cmid "
)
===
0
)
{
$reply
=
command_c
(
'ticker_mid'
,
$CMD
[
1
],
$CMD
[
2
],
$CMD
[
3
],
$CMD
[
4
]);
if
(
$reply
)
apiRequestJson
(
"sendMessage"
,
array
(
'chat_id'
=>
$chat_id
,
"text"
=>
$reply
));
}
else
if
(
strpos
(
$text
,
"/cask "
)
===
0
)
{
$reply
=
command_c
(
'ticker_ask'
,
$CMD
[
1
],
$CMD
[
2
],
$CMD
[
3
],
$CMD
[
4
]);
if
(
$reply
)
apiRequestJson
(
"sendMessage"
,
array
(
'chat_id'
=>
$chat_id
,
"text"
=>
$reply
));
}
else
if
(
strpos
(
$text
,
"/help"
)
===
0
||
strpos
(
$text
,
"/h"
)
===
0
)
{
$reply
=
''
;
$reply
=
'https://git.fairkom.net/FairCoin/faircoin-freevision/raw/master/FREEVISION_TG_BOT.md'
;
apiRequest
(
"sendMessage"
,
array
(
'chat_id'
=>
$chat_id
,
"text"
=>
$reply
));
}
else
{
//apiRequestWebhook("sendMessage", array('chat_id' => $chat_id, "reply_to_message_id" => $message_id, "text" => 'Cool'));
}
}
}
function
command_c
(
$fl
,
$amount
,
$in
,
$to
,
$out
){
if
(
is_numeric
(
$amount
)
){
if
(
!
empty
(
$in
)
){
$fn
=
'../data/'
.
$fl
;
if
(
file_exists
(
$fn
)){
$fp
=
fopen
(
$fn
,
'r'
);
$PRICE
=
json_decode
(
fread
(
$fp
,
filesize
(
$fn
)),
true
);
fclose
(
$fp
);
// check source currency prefix
$prefix
=
substr
(
$in
,
0
,
2
);
if
(
$prefix
==
'µ'
){
$currency
=
strtoupper
(
substr
(
$in
,
2
,
10
)
);
}
else
{
$prefix
=
substr
(
$in
,
0
,
1
);
$currency
=
strtoupper
(
substr
(
$in
,
1
,
10
)
);
}
if
(
(
!
empty
(
$PRICE
[
$currency
]
)
||
$currency
==
'FAIR'
)
&&
(
$prefix
==
'm'
||
$prefix
==
'µ'
)
){
switch
(
$prefix
){
case
'm'
:
$in_factor
=
1000
;
$in_prefix
=
$prefix
;
break
;
case
'µ'
:
$in_factor
=
1000000
;
$in_prefix
=
$prefix
;
break
;
}
$in_currency
=
$currency
;
}
else
{
$in_factor
=
1
;
$in_currency
=
strtoupper
(
$in
);
}
// check target currency prefix
if
(
!
empty
(
$out
)
){
$prefix
=
substr
(
$out
,
0
,
2
);
if
(
$prefix
==
'µ'
){
$currency
=
strtoupper
(
substr
(
$out
,
2
,
10
)
);
}
else
{
$prefix
=
substr
(
$out
,
0
,
1
);
$currency
=
strtoupper
(
substr
(
$out
,
1
,
10
)
);
}
if
(
(
!
empty
(
$PRICE
[
$currency
]
)
||
$currency
==
'FAIR'
)
&&
(
$prefix
==
'm'
||
$prefix
==
'µ'
)
){
switch
(
$prefix
){
case
'm'
:
$out_factor
=
1000
;
$out_prefix
=
$prefix
;
break
;
case
'µ'
:
$out_factor
=
1000000
;
$out_prefix
=
$prefix
;
break
;
}
$out_currency
=
$currency
;
}
else
{
$out_factor
=
1
;
$out_currency
=
strtoupper
(
$out
);
}
}
else
{
$out_factor
=
1
;
$out_currency
=
NULL
;
}
if
(
$in_currency
==
'FAIR'
&&
$to
==
'to'
&&
!
empty
(
$out_currency
)
)
{
if
(
!
empty
(
$PRICE
[
$out_currency
]
)
){
$price
=
$PRICE
[
$out_currency
][
'last'
]
/
$in_factor
*
$out_factor
;
$reply
=
$amount
.
' '
.
$in_prefix
.
$in_currency
.
' = '
.
round
(
$amount
*
$price
,
8
)
.
' '
.
$out_prefix
.
$out_currency
;
$reply
.
=
' ( '
.
$price
*
$out_factor
/
$in_factor
.
' '
.
$out_prefix
.
$out_currency
.
' / '
.
$in_prefix
.
'FAIR )'
;
}
else
if
(
$out_currency
==
'FAIR'
){
$price
=
1
/
$in_factor
*
$out_factor
;
$reply
=
$amount
.
' '
.
$in_prefix
.
$in_currency
.
' = '
.
round
(
$amount
*
$price
,
8
)
.
' '
.
$out_prefix
.
$out_currency
;
$reply
.
=
' ( '
.
$out_factor
/
$in_factor
.
' '
.
$out_prefix
.
$out_currency
.
' / '
.
$in_prefix
.
'FAIR )'
;
}
else
{
$reply
=
'currency not available!'
;
}
}
else
if
(
strtoupper
(
$in_currency
)
==
'FAIR'
)
{
$reply
=
''
;
foreach
(
$PRICE
as
$key
=>
$value
){
$price
=
$PRICE
[
$key
][
'last'
]
/
$in_factor
*
$out_factor
;
$reply
.
=
$amount
.
' '
.
$in_prefix
.
$in_currency
.
' = '
.
round
(
$amount
*
$price
,
8
)
.
' '
.
$out_prefix
.
$key
;
$reply
.
=
' ( '
.
$price
*
$out_factor
/
$in_factor
.
' '
.
$out_prefix
.
$key
.
' / '
.
$in_prefix
.
'FAIR )
'
;
}
}
else
{
$price
=
$PRICE
[
$in_currency
][
'last'
];
$reply
=
$amount
.
' '
.
$in_prefix
.
$in_currency
.
' = '
.
round
(
$amount
/
$price
*
$out_factor
/
$in_factor
,
2
)
.
' '
.
$out_prefix
.
'FAIR'
;
$reply
.
=
' ( '
.
$price
/
$out_factor
*
$in_factor
.
' '
.
$in_prefix
.
$in_currency
.
' / '
.
$out_prefix
.
'FAIR )'
;
}
return
$reply
;
}
}
}
}
$content
=
file_get_contents
(
"php://input"
);
$update
=
json_decode
(
$content
,
true
);
if
(
!
$update
)
{
// receive wrong update, must not happen
exit
;
}
if
(
isset
(
$update
[
"message"
]))
{
processMessage
(
$update
[
"message"
]);
}
?>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment