-
Notifications
You must be signed in to change notification settings - Fork 20
338 create sol price provider #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
5984c19
cherrypick part of changes
613a469
create indexer.py
c0d0e6c
remove solana_receipts_update.py
3f6b5c4
Cherry pick files from old branch
0790298
add requirement
340c854
fix refactoring issues
7449b38
Fix inspection issues
b3dacfa
fix last issue
a50cd47
Merge branch '336_indexer_refactoring' into 337_сreate_base_airdroppe…
2b8f879
Merge remote-tracking branch 'origin/develop' into 337_сreate_base_ai…
f51f2ed
simplify tests
6678924
add test
5d454b7
Merge remote-tracking branch 'origin/develop' into 337_сreate_base_ai…
add136a
add price provider
9a4be44
fix PriceProvider, add test
07aaca8
Add tests. Check worn on all nets
7a46c12
refactoring
2fc1424
integrate price_provider into airdropper
d157d67
integrate price provider
8a6abfd
use new faucet method
3d4dec9
add new parameter to airdropper main
5c29832
Test discriptions for airdropper
6a4efdc
Comments for price provider tests
14aeeed
remove unnecessary comment
bd35791
Merge remote-tracking branch 'origin/develop' into 338_create_sol_pri…
ff1f557
Merge remote-tracking branch 'origin/develop' into 338_create_sol_pri…
c28ca8e
fix error
8c68755
Merge remote-tracking branch 'origin/develop' into 338_create_sol_pri…
5325895
Merge remote-tracking branch 'origin/develop' into 338_create_sol_pri…
a1cbad2
fix airdropper run
4ed6d78
remove duplicated code
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| from solana.rpc.api import Client | ||
| from solana.publickey import PublicKey | ||
| import base64 | ||
| import base58 | ||
| import struct | ||
| from datetime import datetime | ||
| from logging import Logger | ||
|
|
||
| logger = Logger(__name__) | ||
|
|
||
| field_info = { | ||
| 'expo': { 'pos': 20, 'len': 4, 'format': '<i' }, | ||
| 'agg.price': { 'pos': 208, 'len': 8, 'format': '<q' }, | ||
| 'agg.conf': { 'pos': 216, 'len': 8, 'format': '<Q' }, | ||
| 'agg.status': { 'pos': 224, 'len': 4, 'format': '<I' }, | ||
| } | ||
|
|
||
|
|
||
| def _unpack_field(raw_data: bytes, field_name: str): | ||
| field = field_info.get(field_name, None) | ||
| if field is None: | ||
| raise Exception(f'Unknown field name: {field_name}') | ||
|
|
||
| start_idx = field['pos'] | ||
| stop_idx = field['pos'] + field['len'] | ||
| return struct.unpack(field['format'], raw_data[start_idx:stop_idx])[0] | ||
|
|
||
|
|
||
| mainnet_solana = "https://api.mainnet-beta.solana.com" | ||
| devnet_solana = "https://api.devnet.solana.com" | ||
| testnet_solana = "https://api.testnet.solana.com" | ||
|
|
||
|
|
||
| # See available price accounts at https://pyth.network/developers/accounts/ | ||
| mainnet_price_accounts = { 'SOL/USD': 'H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG' } | ||
| devnet_price_accounts = { 'SOL/USD': "J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix" } | ||
| testnet_price_accounts = { 'SOL/USD': "7VJsBtJzgTftYzEeooSDYyjKXvYRWJHdwvbwfBvTg9K" } | ||
|
|
||
|
|
||
| PRICE_STATUS_UNKNOWN = 0 | ||
| PRICE_STATUS_TRADING = 1 | ||
|
|
||
|
|
||
| class PriceProvider: | ||
| def __init__(self, solana_url: str, default_upd_int: int, price_accounts=None): | ||
| self.client = Client(solana_url) | ||
| self.default_upd_int = default_upd_int | ||
| self.prices = {} | ||
| if price_accounts is not None: | ||
| self.price_accounts = price_accounts | ||
| else: | ||
| self.price_accounts = {} | ||
|
|
||
|
|
||
| def _get_current_time(self): | ||
| return datetime.now().timestamp() | ||
|
|
||
|
|
||
| def _read_price(self, pairname): | ||
| acc_id = self.price_accounts.get(pairname, None) | ||
| if acc_id is None: | ||
| logger.warning(f'No account found for pair {pairname}') | ||
| return None | ||
|
|
||
| response = self.client.get_account_info(PublicKey(acc_id)) | ||
| result = response.get('result', None) | ||
| if result is None: | ||
| logger.warning(f'Failed to read account data for account {acc_id}') | ||
| return None | ||
|
|
||
| value = result.get('value', None) | ||
| if value is None: | ||
| logger.warning(f'Failed to read account data for account {acc_id}') | ||
| return None | ||
|
|
||
| data = value.get('data', None) | ||
| if not isinstance(data, list) or len(data) != 2: | ||
| logger.warning(f'Failed to read account data for account {acc_id}') | ||
| return None | ||
|
|
||
| encoding = data[1] | ||
| if encoding == 'base58': | ||
| data = base58.b58decode(data[0]) | ||
| elif encoding == 'base64': | ||
| data = base64.b64decode(data[0]) | ||
| else: | ||
| logger.warning(f'Unknown encoding {encoding}') | ||
| return None | ||
|
|
||
| status = _unpack_field(data, 'agg.status') | ||
| if status != PRICE_STATUS_TRADING: # not Trading | ||
| logger.warning(f'Price status is {status}') | ||
| return None | ||
|
|
||
| expo = _unpack_field(data, 'expo') | ||
| price = _unpack_field(data, 'agg.price') | ||
| return price * pow(10, expo) | ||
|
|
||
|
|
||
| def get_price(self, pairname): | ||
| price_data = self.prices.get(pairname, None) | ||
| current_time = self._get_current_time() | ||
|
|
||
| if price_data == None or current_time - price_data['last_update'] >= self.default_upd_int: | ||
| current_price = self._read_price(pairname) | ||
| if current_price is not None: | ||
| self.prices[pairname] = { 'price': current_price, 'last_update': current_time } | ||
| return current_price | ||
|
|
||
| if price_data is not None: | ||
| return price_data['price'] | ||
| else: | ||
| return None | ||
| # price_data is not None and current_time - price_data['last_update'] < self.default_upd_int | ||
| return price_data['price'] | ||
|
|
||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like a duplicate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed