Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

[Aster Finance Futures API V3 文档](./aster-finance-futures-api-v3_CN.md)

# Aster Finance Futures API V3 DEMO

[Aster Finance Futures API V3 DEMO](./v3-demo/tx.py)

# API Documentation for Aster Finance Spot

[Aster Finance Spot API Document](./aster-finance-spot-api.md)
Expand Down
10 changes: 5 additions & 5 deletions aster-finance-futures-api-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,14 @@ my_dict = {'symbol': 'SANDUSDT', 'positionSide': 'BOTH', 'type': 'LIMIT', 'side'
my_dict['user'] = user
my_dict['signer'] = signer
my_dict['signature'] = '0x'+signed_message.signature.hex()
url ='http://127.0.0.1:9081/fapi/v3/order'
url ='https://fapi.asterdex.com/fapi/v3/order'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'PythonApp/1.0'
}
res = requests.post(url,data=my_dict,headers=headers)
print(url)
#curl -X POST 'http://127.0.0.1:9081/fapi/v3/order' -d 'symbol=SANDUSDT&positionSide=BOTH&type=LIMIT&side=BUY&timeInForce=GTC&quantity=190&price=0.28694&recvWindow=50000&timestamp=1749545309665&nonce=1748310859508867&user=0x63DD5aCC6b1aa0f563956C0e534DD30B6dcF7C4e&signer=0x21cF8Ae13Bb72632562c6Fff438652Ba1a151bb0&signature=0x0337dd720a21543b80ff861cd3c26646b75b3a6a4b5d45805d4c1d6ad6fc33e65f0722778dd97525466560c69fbddbe6874eb4ed6f5fa7e576e486d9b5da67f31b'
#curl -X POST 'https://fapi.asterdex.com/fapi/v3/order' -d 'symbol=SANDUSDT&positionSide=BOTH&type=LIMIT&side=BUY&timeInForce=GTC&quantity=190&price=0.28694&recvWindow=50000&timestamp=1749545309665&nonce=1748310859508867&user=0x63DD5aCC6b1aa0f563956C0e534DD30B6dcF7C4e&signer=0x21cF8Ae13Bb72632562c6Fff438652Ba1a151bb0&signature=0x0337dd720a21543b80ff861cd3c26646b75b3a6a4b5d45805d4c1d6ad6fc33e65f0722778dd97525466560c69fbddbe6874eb4ed6f5fa7e576e486d9b5da67f31b'
```

## Example of POST /fapi/v3/order
Expand Down Expand Up @@ -467,11 +467,11 @@ from eth_account import Account
my_dict['signer'] = signer
my_dict['signature'] = '0x'+signed_message.signature.hex()

url ='http://127.0.0.1:9081/fapi/v3/order'
url ='https://fapi.asterdex.com/fapi/v3/order'

res = requests.get(url, params=my_dict)
print(url)
#curl -X GET 'http://127.0.0.1:9081/fapi/v3/order?symbol=SANDUSDT&side=BUY&type=LIMIT&orderId=2194215&recvWindow=50000&timestamp=1749545309665&nonce=1748310859508867&user=0x63DD5aCC6b1aa0f563956C0e534DD30B6dcF7C4e&signer=0x21cF8Ae13Bb72632562c6Fff438652Ba1a151bb0&signature=0x4f5e36e91f0d4cf5b29b6559ebc2c808d3c808ebb13b2bcaaa478b98fb4195642c7473f0d1aa101359aaf278126af1a53bcb482fb05003bfb6bdc03de03c63151b'
#curl -X GET 'https://fapi.asterdex.com/fapi/v3/order?symbol=SANDUSDT&side=BUY&type=LIMIT&orderId=2194215&recvWindow=50000&timestamp=1749545309665&nonce=1748310859508867&user=0x63DD5aCC6b1aa0f563956C0e534DD30B6dcF7C4e&signer=0x21cF8Ae13Bb72632562c6Fff438652Ba1a151bb0&signature=0x4f5e36e91f0d4cf5b29b6559ebc2c808d3c808ebb13b2bcaaa478b98fb4195642c7473f0d1aa101359aaf278126af1a53bcb482fb05003bfb6bdc03de03c63151b'
```

## python script
Expand All @@ -497,7 +497,7 @@ from web3 import Web3
user = '0x63DD5aCC6b1aa0f563956C0e534DD30B6dcF7C4e'
signer='0x21cF8Ae13Bb72632562c6Fff438652Ba1a151bb0'
priKey = "0x4fd0a42218f3eae43a6ce26d22544e986139a01e5b34a62db53757ffca81bae1"
host = 'http://127.0.0.1:9081'
host = 'https://fapi.asterdex.com'
placeOrder = {'url': '/fapi/v3/order', 'method': 'POST',
'params':{'symbol': 'SANDUSDT', 'positionSide': 'BOTH', 'type': 'LIMIT', 'side': 'BUY',
'timeInForce': 'GTC', 'quantity': "30", 'price': 0.325,'reduceOnly': True}}
Expand Down
10 changes: 5 additions & 5 deletions aster-finance-futures-api-v3_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,14 @@ long microsecond = now.getEpochSecond() * 1000000 + now.getNano() / 1000;
my_dict['user'] = user
my_dict['signer'] = signer
my_dict['signature'] = '0x'+signed_message.signature.hex()
url ='http://127.0.0.1:9081/fapi/v3/order'
url ='https://fapi.asterdex.com/fapi/v3/order'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'PythonApp/1.0'
}
res = requests.post(url,data=my_dict,headers=headers)
print(url)
#curl -X POST 'http://127.0.0.1:9081/fapi/v3/order' -d 'symbol=SANDUSDT&positionSide=BOTH&type=LIMIT&side=BUY&timeInForce=GTC&quantity=190&price=0.28694&recvWindow=50000&timestamp=1749545309665&nonce=1748310859508867&user=0x63DD5aCC6b1aa0f563956C0e534DD30B6dcF7C4e&signer=0x21cF8Ae13Bb72632562c6Fff438652Ba1a151bb0&signature=0x0337dd720a21543b80ff861cd3c26646b75b3a6a4b5d45805d4c1d6ad6fc33e65f0722778dd97525466560c69fbddbe6874eb4ed6f5fa7e576e486d9b5da67f31b'
#curl -X POST 'https://fapi.asterdex.com/fapi/v3/order' -d 'symbol=SANDUSDT&positionSide=BOTH&type=LIMIT&side=BUY&timeInForce=GTC&quantity=190&price=0.28694&recvWindow=50000&timestamp=1749545309665&nonce=1748310859508867&user=0x63DD5aCC6b1aa0f563956C0e534DD30B6dcF7C4e&signer=0x21cF8Ae13Bb72632562c6Fff438652Ba1a151bb0&signature=0x0337dd720a21543b80ff861cd3c26646b75b3a6a4b5d45805d4c1d6ad6fc33e65f0722778dd97525466560c69fbddbe6874eb4ed6f5fa7e576e486d9b5da67f31b'

```

Expand Down Expand Up @@ -439,11 +439,11 @@ my_dict = {'symbol':'SANDUSDT','side':"SELL","type":'LIMIT','orderId':2194215}
my_dict['signer'] = signer
my_dict['signature'] = '0x'+signed_message.signature.hex()

url ='http://127.0.0.1:9081/fapi/v3/order'
url ='https://fapi.asterdex.com/fapi/v3/order'

res = requests.get(url, params=my_dict)
print(url)
#curl -X GET 'http://127.0.0.1:9081/fapi/v3/order?symbol=SANDUSDT&side=BUY&type=LIMIT&orderId=2194215&recvWindow=50000&timestamp=1749545309665&nonce=1748310859508867&user=0x63DD5aCC6b1aa0f563956C0e534DD30B6dcF7C4e&signer=0x21cF8Ae13Bb72632562c6Fff438652Ba1a151bb0&signature=0x4f5e36e91f0d4cf5b29b6559ebc2c808d3c808ebb13b2bcaaa478b98fb4195642c7473f0d1aa101359aaf278126af1a53bcb482fb05003bfb6bdc03de03c63151b'
#curl -X GET 'https://fapi.asterdex.com/fapi/v3/order?symbol=SANDUSDT&side=BUY&type=LIMIT&orderId=2194215&recvWindow=50000&timestamp=1749545309665&nonce=1748310859508867&user=0x63DD5aCC6b1aa0f563956C0e534DD30B6dcF7C4e&signer=0x21cF8Ae13Bb72632562c6Fff438652Ba1a151bb0&signature=0x4f5e36e91f0d4cf5b29b6559ebc2c808d3c808ebb13b2bcaaa478b98fb4195642c7473f0d1aa101359aaf278126af1a53bcb482fb05003bfb6bdc03de03c63151b'

```
## 完整python脚本示例
Expand All @@ -467,7 +467,7 @@ from web3 import Web3
user = '0x63DD5aCC6b1aa0f563956C0e534DD30B6dcF7C4e'
signer='0x21cF8Ae13Bb72632562c6Fff438652Ba1a151bb0'
priKey = "0x4fd0a42218f3eae43a6ce26d22544e986139a01e5b34a62db53757ffca81bae1"
host = 'http://127.0.0.1:9081'
host = 'https://fapi.asterdex.com'
placeOrder = {'url': '/fapi/v3/order', 'method': 'POST',
'params':{'symbol': 'SANDUSDT', 'positionSide': 'BOTH', 'type': 'LIMIT', 'side': 'BUY',
'timeInForce': 'GTC', 'quantity': "30", 'price': 0.325,'reduceOnly': True}}
Expand Down
4 changes: 4 additions & 0 deletions v3-demo/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eth-account~=0.13.7
eth-abi~=5.2.0
web3~=7.11.0
requests~=2.32.3
99 changes: 99 additions & 0 deletions v3-demo/tx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

#Python 3.9.6
# eth-account~=0.13.7
# eth-abi~=5.2.0
# web3~=7.11.0
# requests~=2.32.3
# pip install -r requirements.txt

import json
import math
import time
import requests

from eth_abi import encode
from eth_account import Account
from eth_account.messages import encode_defunct
from web3 import Web3

user = '0x63DD5aCC6b1aa0f563956C0e534DD30B6dcF7C4e'
signer='0x21cF8Ae13Bb72632562c6Fff438652Ba1a151bb0'
priKey = "0x4fd0a42218f3eae43a6ce26d22544e986139a01e5b34a62db53757ffca81bae1"

host = 'https://fapi.asterdex.com'

placeOrder = {'url': '/fapi/v3/order', 'method': 'POST',
'params':{'symbol': 'SANDUSDT', 'positionSide': 'BOTH', 'type': 'LIMIT', 'side': 'BUY',
'timeInForce': 'GTC', 'quantity': "30", 'price': 0.325,'reduceOnly': True}}
getOrder = {'url':'/fapi/v3/order','method':'GET','params':{'symbol':'SANDUSDT','side':"BUY","type":'LIMIT','orderId':2194215}}

def call(api):
nonce = math.trunc(time.time() * 1000000)
my_dict = api['params']
send(api['url'],api['method'],sign(my_dict,nonce))

def sign(my_dict,nonce):
my_dict = {key: value for key, value in my_dict.items() if value is not None}
my_dict['recvWindow'] = 50000
my_dict['timestamp'] = int(round(time.time()*1000))
msg = trim_param(my_dict,nonce)
signable_msg = encode_defunct(hexstr=msg)
signed_message = Account.sign_message(signable_message=signable_msg, private_key=priKey)
my_dict['nonce'] = nonce
my_dict['user'] = user
my_dict['signer'] = signer
my_dict['signature'] = '0x'+signed_message.signature.hex()

print(my_dict['signature'])
return my_dict

def trim_param(my_dict,nonce) -> str:
_trim_dict(my_dict)
json_str = json.dumps(my_dict, sort_keys=True).replace(' ', '').replace('\'','\"')
print(json_str)
encoded = encode(['string', 'address', 'address', 'uint256'], [json_str, user, signer, nonce])
print(encoded.hex())
keccak_hex =Web3.keccak(encoded).hex()
print(keccak_hex)
return keccak_hex

def _trim_dict(my_dict) :
for key in my_dict:
value = my_dict[key]
if isinstance(value, list):
new_value = []
for item in value:
if isinstance(item, dict):
new_value.append(json.dumps(_trim_dict(item)))
else:
new_value.append(str(item))
my_dict[key] = json.dumps(new_value)
continue
if isinstance(value, dict):
my_dict[key] = json.dumps(_trim_dict(value))
continue
my_dict[key] = str(value)

return my_dict

def send(url, method, my_dict):
url = host + url
print(url)
print(my_dict)
if method == 'POST':
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'PythonApp/1.0'
}
res = requests.post(url, data=my_dict, headers=headers)
print(res.text)
if method == 'GET':
res = requests.get(url, params=my_dict)
print(res.text)
if method == 'DELETE':
res = requests.delete(url, data=my_dict)
print(res.text)

if __name__ == '__main__':
# call(placeOrder)
call(getOrder)