Skip to content

Instantly share code, notes, and snippets.

@alex-georgiou
Created February 22, 2024 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-georgiou/4057047b8c9a83a772181dfceb57567a to your computer and use it in GitHub Desktop.
Save alex-georgiou/4057047b8c9a83a772181dfceb57567a to your computer and use it in GitHub Desktop.
Sample code for interfacing with the PHP-API and the WP-REST API of the Bitcoin and Altcoin Wallets Exchange extension.
<?php
/*
* Sample code for interfacing with the PHP-API of the Bitcoin and Altcoin Wallets Exchange extension.
*
* For more information refer to:
* - `wp-content/plugins/wallets-exchange/docs/developer.md`
* - The PHPdocumentor link to the Exchange extension's PHP-API below:
*
* @author Alex Georgiou <info@dashed-slug.net>
* @copyright 2024
* @license GPL-2.0-or-later
* @version 1.0.0
* @link https://wallets-phpdoc.dashed-slug.net/wallets-exchange/files/build-apis-php-api.html
*/
// SET THIS TO THE PATH OF YOUR WORDPRESS INSTALLATION
$wppath = '/var/www/wordpress';
$wppath = rtrim( $wppath, '/ \n\r\t\v\x00' );
if ( ! file_exists( "$wppath/wp-load.php" ) ) {
echo "Invalid WordPress dir!\n";
die;
}
require_once "$wppath/wp-load.php";
// echo "DB NAME: " . DB_NAME ."\n";
// echo "DB USER: " . DB_USER ."\n";
// echo "DB_HOST: " . DB_HOST ."\n";
// echo "ABSPATH: " . ABSPATH ."\n";
// exit;
// post ID of the wallets_market post corresponding to the market
$market_id = 123;
try {
$market_summary = apply_filters(
'wallets_api_market_summary',
null,
array(
'market_id' => $market_id,
)
);
printf(
"Market summary:\n%s\n\n",
json_encode( $market_summary, JSON_PRETTY_PRINT )
);
} catch ( \Exception $e ) {
printf( "ERROR: %s\n", $e->getMessage() );
die;
}
try {
$market_order_book = apply_filters(
'wallets_api_market_order_book',
null,
array(
'market_id' => $market_id,
)
);
printf(
"Market order book:\n%s\n\n",
json_encode( $market_order_book, JSON_PRETTY_PRINT )
);
} catch ( \Exception $e ) {
printf( "ERROR: %s\n", $e->getMessage() );
die;
}
try {
$market_trades = apply_filters(
'wallets_api_trades',
null,
array(
'user_id' => 1, // get trade history of this user
'market_id' => $market_id,
)
);
printf(
"Market trades:\n%s\n\n",
json_encode( $market_trades, JSON_PRETTY_PRINT )
);
} catch ( \Exception $e ) {
printf( "ERROR: %s\n", $e->getMessage() );
die;
}
// place a limit buy (bid) order by MARKET ID
try {
$bid_order_id = apply_filters(
'wallets_api_limit_order',
null,
array(
'user_id' => 1, // id of user who places the order
'market_id' => 49714, // the post_id of our market
'amount' => 1000, // units of quote currency
'rate' => 0.074, // exchange rate
'direction' => 'bid', // bid or ask
)
);
printf( "Placed bid order: %s\n", $bid_order_id );
} catch ( \Exception $e ) {
printf( "ERROR: %s\n", $e->getMessage() );
die;
}
// place a limit sell (ask) order by TICKER SYMBOLS
try {
$ask_order_id = apply_filters(
'wallets_api_limit_order',
null,
array(
'user_id' => 1,
'base_symbol' => 'EUR',
'quote_symbol' => 'DOGE',
'amount' => 1000,
'rate' => 0.08,
'direction' => 'ask',
)
);
printf( "Placed ask order: %s\n", $ask_order_id );
} catch ( \Exception $e ) {
printf( "ERROR: %s\n", $e->getMessage() );
die;
}
// cancel the bid order
try {
do_action(
'wallets_api_cancel_order',
array(
'user_id' => 1,
'order_id' => $bid_order_id,
)
);
printf( "Placed bid order: %s\n", $bid_order_id );
} catch ( \Exception $e ) {
printf( "ERROR: %s\n", $e->getMessage() );
die;
}
// cancel the ask order
try {
do_action(
'wallets_api_cancel_order',
array(
'user_id' => 1,
'order_id' => $ask_order_id,
)
);
printf( "Cancelled ask order: %s\n", $ask_order_id );
} catch ( \Exception $e ) {
printf( "ERROR: %s\n", $e->getMessage() );
die;
}
#!/bin/bash -x
# Sample code for interfacing with the WP-REST API of the Bitcoin and Altcoin Wallets Exchange extension.
# For more information refer to: wp-content/plugins/wallets-exchange/docs/developer.md
# To authenticate this script, go to your WordPress user profile,
# and create a new Application Password.
# Set your username, user_id and the application password below:
user_name='admin'
user_id=1
application_password='1Pso sUN0 Ns3J 5yas TY8q hVp0'
# Set the URL to your site below:
server=https://www.example.com
# get markets
echo "get info about all markets"
curl "$server/wp-json/dswalletsexchange/v1/markets/" | jq .
# Set the post ID to the wallets_market post representing your market
market_id=49714
# get the orderbook bids side
echo "get bids side of orderbook for the market"
curl "$server/wp-json/dswalletsexchange/v1/markets/$market_id/orderbook/bids" | jq .
#get the orderbook asks side
echo "get asks side of orderbook for the market"
curl "$server/wp-json/dswalletsexchange/v1/markets/$market_id/orderbook/asks" | jq .
# get open orders of current user
echo "get open orders of user 1"
curl --user "$user_name:$application_password" "$server/wp-json/dswalletsexchange/v1/users/$user_id/orders?open=1" | jq .
# place a limit buy
echo "place a limit buy order"
limit_buy_response=`curl \
-X POST \
--user "$user_name:$application_password" \
"$server/wp-json/dswalletsexchange/v1/users/1/orders" \
-d "market_id=$market_id&amount=1000&rate=0.074&side=bid"`
echo "Placed limit buy order: $limit_buy_response\n";
limit_buy_order_id=`echo $limit_buy_response | jq -r .order_id`
# cancel the limit buy
cancel_response=`curl \
-X DELETE \
--user "$user_name:$application_password" \
"$server/wp-json/dswalletsexchange/v1/users/1/orders/$limit_buy_order_id"`
echo "Response from cancelling order: $cancel_response\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment