Skip to content

Commit

Permalink
Add gift cards endpoints and tests'
Browse files Browse the repository at this point in the history
  • Loading branch information
pcriadoperez committed Nov 19, 2024
1 parent bfc2a0a commit dfba408
Show file tree
Hide file tree
Showing 8 changed files with 476 additions and 115 deletions.
68 changes: 59 additions & 9 deletions binance/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,30 +131,30 @@ async def _request_api(
**kwargs,
):
uri = self._create_api_uri(path, signed, version)
force_params = kwargs.pop('force_params', False)
force_params = kwargs.pop("force_params", False)
return await self._request(method, uri, signed, force_params, **kwargs)

async def _request_futures_api(
self, method, path, signed=False, version=1, **kwargs
) -> Dict:
version = self._get_version(version, **kwargs)
uri = self._create_futures_api_uri(path, version=version)
force_params = kwargs.pop('force_params', False)
force_params = kwargs.pop("force_params", False)
return await self._request(method, uri, signed, force_params, **kwargs)

async def _request_futures_data_api(
self, method, path, signed=False, **kwargs
) -> Dict:
uri = self._create_futures_data_api_uri(path)
force_params = kwargs.pop('force_params', False)
force_params = kwargs.pop("force_params", False)
return await self._request(method, uri, signed, force_params, **kwargs)

async def _request_futures_coin_api(
self, method, path, signed=False, version=1, **kwargs
) -> Dict:
version = self._get_version(version, **kwargs)
uri = self._create_futures_coin_api_url(path, version=version)
force_params = kwargs.pop('force_params', False)
force_params = kwargs.pop("force_params", False)
return await self._request(method, uri, signed, force_params, **kwargs)

async def _request_futures_coin_data_api(
Expand All @@ -163,12 +163,12 @@ async def _request_futures_coin_data_api(
version = self._get_version(version, **kwargs)
uri = self._create_futures_coin_data_api_url(path, version=version)

force_params = kwargs.pop('force_params', False)
force_params = kwargs.pop("force_params", False)
return await self._request(method, uri, signed, force_params, **kwargs)

async def _request_options_api(self, method, path, signed=False, **kwargs) -> Dict:
uri = self._create_options_api_uri(path)
force_params = kwargs.pop('force_params', True)
force_params = kwargs.pop("force_params", True)

return await self._request(method, uri, signed, force_params, **kwargs)

Expand All @@ -178,7 +178,7 @@ async def _request_margin_api(
version = self._get_version(version, **kwargs)
uri = self._create_margin_api_uri(path, version)

force_params = kwargs.pop('force_params', False)
force_params = kwargs.pop("force_params", False)
return await self._request(method, uri, signed, force_params, **kwargs)

async def _request_papi_api(
Expand All @@ -187,7 +187,7 @@ async def _request_papi_api(
version = self._get_version(version, **kwargs)
uri = self._create_papi_api_uri(path, version)

force_params = kwargs.pop('force_params', False)
force_params = kwargs.pop("force_params", False)
return await self._request(method, uri, signed, force_params, **kwargs)

async def _request_website(self, method, path, signed=False, **kwargs) -> Dict:
Expand Down Expand Up @@ -1797,7 +1797,9 @@ async def futures_place_batch_order(self, **params):
query_string = urlencode(params).replace("%40", "@").replace("%27", "%22")
params["batchOrders"] = query_string[12:]

return await self._request_futures_api("post", "batchOrders", True, data=params, force_params=True)
return await self._request_futures_api(
"post", "batchOrders", True, data=params, force_params=True
)

async def futures_get_order(self, **params):
return await self._request_futures_api("get", "order", True, data=params)
Expand Down Expand Up @@ -3610,3 +3612,51 @@ async def ws_futures_account_status(self, **params):
https://developers.binance.com/docs/derivatives/usds-margined-futures/account/websocket-api/Account-Information
"""
return await self._ws_futures_api_request("account.status", True, params)

####################################################
# Gift Card API Endpoints
####################################################

async def gift_card_fetch_token_limit(self, **params):
return await self._request_margin_api(
"get", "giftcard/buyCode/token-limit", signed=True, data=params
)

gift_card_fetch_token_limit.__doc__ = Client.gift_card_fetch_token_limit.__doc__

async def gift_card_fetch_rsa_public_key(self, **params):
return await self._request_margin_api(
"get", "giftcard/cryptography/rsa-public-key", signed=True, data=params
)

gift_card_fetch_rsa_public_key.__doc__ = (
Client.gift_card_fetch_rsa_public_key.__doc__
)

async def gift_card_verify(self, **params):
return await self._request_margin_api(
"get", "giftcard/verify", signed=True, data=params
)

gift_card_verify.__doc__ = Client.gift_card_verify.__doc__

async def gift_card_redeem(self, **params):
return await self._request_margin_api(
"post", "giftcard/redeemCode", signed=True, data=params
)

gift_card_redeem.__doc__ = Client.gift_card_redeem.__doc__

async def gift_card_create(self, **params):
return await self._request_margin_api(
"post", "giftcard/createCode", signed=True, data=params
)

gift_card_create.__doc__ = Client.gift_card_create.__doc__

async def gift_card_create_dual_token(self, **params):
return await self._request_margin_api(
"post", "giftcard/buyCode", signed=True, data=params
)

gift_card_create_dual_token.__doc__ = Client.gift_card_create_dual_token.__doc__
197 changes: 194 additions & 3 deletions binance/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def _request_futures_api(
) -> Dict:
version = self._get_version(version, **kwargs)
uri = self._create_futures_api_uri(path, version)
force_params = kwargs.pop('force_params', False)
force_params = kwargs.pop("force_params", False)

return self._request(method, uri, signed, force_params, **kwargs)

def _request_futures_data_api(self, method, path, signed=False, **kwargs) -> Dict:
Expand Down Expand Up @@ -7441,7 +7441,9 @@ def futures_place_batch_order(self, **params):
query_string = urlencode(params)
query_string = query_string.replace("%27", "%22")
params["batchOrders"] = query_string[12:]
return self._request_futures_api("post", "batchOrders", True, data=params, force_params=True)
return self._request_futures_api(
"post", "batchOrders", True, data=params, force_params=True
)

def futures_get_order(self, **params):
"""Check an order's status.
Expand Down Expand Up @@ -10525,3 +10527,192 @@ def ws_futures_account_status(self, **params):
https://developers.binance.com/docs/derivatives/usds-margined-futures/account/websocket-api/Account-Information
"""
return self._ws_futures_api_request_sync("account.status", True, params)

###############################################
### Gift card api
###############################################
def gift_card_fetch_token_limit(self, **params):
"""Verify which tokens are available for you to create Stablecoin-Denominated gift cards
https://developers.binance.com/docs/gift_card/market-data/Fetch-Token-Limit
:param baseToken: The token you want to pay, example: BUSD
:type baseToken: str
:return: api response
.. code-block:: python
{
"code": "000000",
"message": "success",
"data": [
{
"coin": "BNB",
"fromMin": "0.01",
"fromMax": "1"
}
],
"success": true
}
"""
return self._request_margin_api(
"get", "giftcard/buyCode/token-limit", signed=True, data=params
)

def gift_card_fetch_rsa_public_key(self, **params):
"""This API is for fetching the RSA Public Key. This RSA Public key will be used to encrypt the card code.
Important Note:
The RSA Public key fetched is valid only for the current day.
https://developers.binance.com/docs/gift_card/market-data/Fetch-RSA-Public-Key
:param recvWindow: The receive window for the request in milliseconds (optional)
:type recvWindow: int
:return: api response
.. code-block:: python
{
"code": "000000",
"message": "success",
"data": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCXBBVKLAc1GQ5FsIFFqOHrPTox5noBONIKr+IAedTR9FkVxq6e65updEbfdhRNkMOeYIO2i0UylrjGC0X8YSoIszmrVHeV0l06Zh1oJuZos1+7N+WLuz9JvlPaawof3GUakTxYWWCa9+8KIbLKsoKMdfS96VT+8iOXO3quMGKUmQIDAQAB",
"success": true
}
"""
return self._request_margin_api(
"get", "giftcard/cryptography/rsa-public-key", signed=True, data=params
)

def gift_card_verify(self, **params):
"""This API is for verifying whether the Binance Gift Card is valid or not by entering Gift Card Number.
Important Note:
If you enter the wrong Gift Card Number 5 times within an hour, you will no longer be able
to verify any Gift Card Number for that hour.
https://developers.binance.com/docs/gift_card/market-data/Verify-Binance-Gift-Card-by-Gift-Card-Number
:param referenceNo: Enter the Gift Card Number
:type referenceNo: str
:return: api response
.. code-block:: python
{
"code": "000000",
"message": "success",
"data": {
"valid": true,
"token": "BNB", # coin
"amount": "0.00000001" # amount
},
"success": true
}
"""
return self._request_margin_api(
"get", "giftcard/verify", signed=True, data=params
)

def gift_card_redeem(self, **params):
"""This API is for redeeming a Binance Gift Card. Once redeemed, the coins will be deposited in your funding wallet.
Important Note:
If you enter the wrong redemption code 5 times within 24 hours, you will no longer be able to
redeem any Binance Gift Cards that day.
Code Format Options:
- Plaintext
- Encrypted (Recommended for better security)
For encrypted format:
1. Fetch RSA public key from the RSA public key endpoint
2. Encrypt the code using algorithm: RSA/ECB/OAEPWithSHA-256AndMGF1Padding
https://developers.binance.com/docs/gift_card/market-data/Redeem-a-Binance-Gift-Card
:param code: Redemption code of Binance Gift Card to be redeemed, supports both Plaintext & Encrypted code
:type code: str
:param externalUid: External unique ID representing a user on the partner platform.
Helps identify redemption behavior and control risks/limits.
Max 400 characters. (optional)
:type externalUid: str
:param recvWindow: The receive window for the request in milliseconds (optional)
:type recvWindow: int
:return: api response
.. code-block:: python
{
"code": "000000",
"message": "success",
"data": {
"referenceNo": "0033002328060227",
"identityNo": "10317392647411060736",
"token": "BNB",
"amount": "0.00000001"
},
"success": true
}
"""
return self._request_margin_api(
"post", "giftcard/redeemCode", signed=True, data=params
)

def gift_card_create(self, **params):
"""
This API is for creating a Binance Gift Card.
To get started with, please make sure:
- You have a Binance account
- You have passed KYB
- You have a sufficient balance(Gift Card amount and fee amount) in your Binance funding wallet
- You need Enable Withdrawals for the API Key which requests this endpoint.
https://developers.binance.com/docs/gift_card/market-data
:param token: The token type contained in the Binance Gift Card
:type token: str
:param amount: The amount of the token contained in the Binance Gift Card
:type amount: float
:return: api response
.. code-block:: python
{
"code": "000000",
"message": "success",
"data": {
"referenceNo": "0033002144060553",
"code": "6H9EKF5ECCWFBHGE",
"expiredTime": 1727417154000
},
"success": true
}
"""
return self._request_margin_api(
"post", "giftcard/createCode", signed=True, data=params
)

def gift_card_create_dual_token(self, **params):
"""This API is for creating a dual-token ( stablecoin-denominated) Binance Gift Card. You may create a gift card using USDT as baseToken, that is redeemable to another designated token (faceToken). For example, you can create a fixed-value BTC gift card and pay with 100 USDT plus 1 USDT fee. This gift card can keep the value fixed at 100 USDT before redemption, and will be redeemable to BTC equivalent to 100 USDT upon redemption.
Once successfully created, the amount of baseToken (e.g. USDT) in the fixed-value gift card along with the fee would be deducted from your funding wallet.
To get started with, please make sure:
- You have a Binance account
- You have passed KYB
- You have a sufficient balance(Gift Card amount and fee amount) in your Binance funding wallet
- You need Enable Withdrawals for the API Key which requests this endpoint.
https://developers.binance.com/docs/gift_card/market-data/Create-a-dual-token-gift-card
:param baseToken: The token you want to pay, example: BUSD
:type baseToken: str
:param faceToken: The token you want to buy, example: BNB. If faceToken = baseToken, it's the same as createCode endpoint.
:type faceToken: str
:param discount: Stablecoin-denominated card discount percentage, Example: 1 for 1% discount. Scale should be less than 6.
:type discount: float
:return: api response
.. code-block:: python
{
"code": "000000",
"message": "success",
"data": {
"referenceNo": "0033002144060553",
"code": "6H9EKF5ECCWFBHGE",
"expiredTime": 1727417154000
},
"success": true
}
"""
return self._request_margin_api(
"post", "giftcard/buyCode", signed=True, data=params
)
Loading

0 comments on commit dfba408

Please sign in to comment.