-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: add publisher program to sync cli #44
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
eb23897
feat: add publisher program to sync cli
ali-bahjati 61a233d
fix: use seed properly and update docker image
ali-bahjati c4c6e46
refactor: remove extra comment
ali-bahjati 2866a35
fix: docker
ali-bahjati 52166be
fix: use str with trimming
ali-bahjati a1bd54f
fix: add instruction ids
ali-bahjati af0ee0b
fix: update formatting
ali-bahjati 347ccb7
fix: update init publisher instruction
ali-bahjati 108ee4e
fix: remove duplicate account_exists
ali-bahjati 416f353
chore: bump version
ali-bahjati 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 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 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 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 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,164 @@ | ||
from typing import Tuple | ||
|
||
from construct import Bytes, Int8ul, Struct | ||
from solana import system_program | ||
from solana.publickey import PublicKey | ||
from solana.system_program import SYS_PROGRAM_ID, CreateAccountWithSeedParams | ||
from solana.transaction import AccountMeta, TransactionInstruction | ||
|
||
|
||
def config_account_pubkey(program_key: PublicKey) -> PublicKey: | ||
[config_account, _] = PublicKey.find_program_address( | ||
[b"CONFIG"], | ||
program_key, | ||
) | ||
return config_account | ||
|
||
|
||
def publisher_config_account_pubkey( | ||
publisher_key: PublicKey, program_key: PublicKey | ||
) -> PublicKey: | ||
[publisher_config_account, _] = PublicKey.find_program_address( | ||
[b"PUBLISHER_CONFIG", bytes(publisher_key)], | ||
program_key, | ||
) | ||
return publisher_config_account | ||
|
||
|
||
def initialize_publisher_program( | ||
program_key: PublicKey, | ||
authority: PublicKey, | ||
) -> TransactionInstruction: | ||
""" | ||
Pyth publisher program initialize instruction with the given authority | ||
|
||
accounts: | ||
- payer account (signer, writable) - we pass the authority as the payer | ||
- config account (writable) | ||
- system program | ||
""" | ||
|
||
[config_account, bump] = PublicKey.find_program_address( | ||
[b"CONFIG"], | ||
program_key, | ||
) | ||
|
||
ix_data_layout = Struct( | ||
"instruction_id" / Int8ul, | ||
"bump" / Int8ul, | ||
"authority" / Bytes(32), | ||
) | ||
|
||
ix_data = ix_data_layout.build( | ||
dict( | ||
instruction_id=0, | ||
bump=bump, | ||
authority=bytes(authority), | ||
) | ||
) | ||
|
||
return TransactionInstruction( | ||
data=ix_data, | ||
keys=[ | ||
AccountMeta(pubkey=authority, is_signer=True, is_writable=True), | ||
AccountMeta(pubkey=config_account, is_signer=False, is_writable=True), | ||
AccountMeta(pubkey=SYS_PROGRAM_ID, is_signer=False, is_writable=False), | ||
], | ||
program_id=program_key, | ||
) | ||
|
||
|
||
def create_buffer_account( | ||
program_key: PublicKey, | ||
base_pubkey: PublicKey, | ||
publisher_pubkey: PublicKey, | ||
space: int, | ||
lamports: int, | ||
) -> Tuple[PublicKey, TransactionInstruction]: | ||
# Since the string representation of the PublicKey is 44 bytes long (base58 encoded) | ||
# and we use 32 bytes of it, the chances of collision are very low. | ||
# | ||
# The seed has a max length of 32 and although the publisher_pubkey is 32 bytes, | ||
# it is impossible to convert it to a string with a length of 32 that the | ||
# underlying library (solders) can handle. We don't know exactly why, but it | ||
# seems to be related to str -> &str conversion in pyo3 that solders uses to | ||
# interact with the Rust implementation of the logic. | ||
seed = str(publisher_pubkey)[:32] | ||
new_account_pubkey = PublicKey.create_with_seed( | ||
base_pubkey, | ||
seed, | ||
program_key, | ||
) | ||
|
||
return ( | ||
new_account_pubkey, | ||
system_program.create_account_with_seed( | ||
CreateAccountWithSeedParams( | ||
from_pubkey=base_pubkey, | ||
new_account_pubkey=new_account_pubkey, | ||
base_pubkey=base_pubkey, | ||
seed=seed, | ||
program_id=program_key, | ||
lamports=lamports, | ||
space=space, | ||
) | ||
), | ||
) | ||
|
||
|
||
def initialize_publisher_config( | ||
program_key: PublicKey, | ||
publisher_key: PublicKey, | ||
authority: PublicKey, | ||
buffer_account: PublicKey, | ||
) -> TransactionInstruction: | ||
""" | ||
Pyth publisher program initialize publisher config instruction with the given authority | ||
|
||
accounts: | ||
- authority account (signer, writable) | ||
- config account | ||
- publisher config account (writable) | ||
- buffer account (writable) | ||
- system program | ||
""" | ||
|
||
[config_account, config_bump] = PublicKey.find_program_address( | ||
[b"CONFIG"], | ||
program_key, | ||
) | ||
|
||
[publisher_config_account, publisher_config_bump] = PublicKey.find_program_address( | ||
[b"PUBLISHER_CONFIG", bytes(publisher_key)], | ||
program_key, | ||
) | ||
|
||
ix_data_layout = Struct( | ||
"instruction_id" / Int8ul, | ||
"config_bump" / Int8ul, | ||
"publisher_config_bump" / Int8ul, | ||
"publisher" / Bytes(32), | ||
) | ||
|
||
ix_data = ix_data_layout.build( | ||
dict( | ||
instruction_id=2, | ||
config_bump=config_bump, | ||
publisher_config_bump=publisher_config_bump, | ||
publisher=bytes(publisher_key), | ||
) | ||
) | ||
|
||
return TransactionInstruction( | ||
data=ix_data, | ||
keys=[ | ||
AccountMeta(pubkey=authority, is_signer=True, is_writable=True), | ||
AccountMeta(pubkey=config_account, is_signer=False, is_writable=True), | ||
ali-bahjati marked this conversation as resolved.
Show resolved
Hide resolved
|
||
AccountMeta( | ||
pubkey=publisher_config_account, is_signer=False, is_writable=False | ||
), | ||
AccountMeta(pubkey=buffer_account, is_signer=False, is_writable=True), | ||
AccountMeta(pubkey=SYS_PROGRAM_ID, is_signer=False, is_writable=False), | ||
], | ||
program_id=program_key, | ||
) |
This file contains 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
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.
I think if you use a formatter such as black/ruff it will combine these two import declarations.
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.
actually it seems that the formatter in pre-commit (black i assume) is breaking them apart. idk why but i can't join them.