Skip to content

Commit

Permalink
stake-pool-py: Create and deserialize stake pools (#2557)
Browse files Browse the repository at this point in the history
* stake-pool-py: Create and deserialize stake pools with CI

* Add ability to add / remove validators

* Add vote init instruction for cleaner tests

* Fixup CI

* Add deposit / withdraw sol

* Add update instructions

* Add increase / decrease stake

* Add deposit / withdraw stake
  • Loading branch information
joncinque authored Dec 9, 2021
1 parent e29bc53 commit a788886
Show file tree
Hide file tree
Showing 49 changed files with 2,963 additions and 30 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/fuzz-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ jobs:

- uses: actions/cache@v2
with:
path: |
~/.cache
path: ~/.cache/solana
key: solana-${{ env.SOLANA_VERSION }}
restore-keys: |
solana-
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/pull-request-binary-oracle-pair.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ jobs:

- uses: actions/cache@v2
with:
path: |
~/.cache
path: ~/.cache/solana
key: solana-${{ env.SOLANA_VERSION }}

- name: Install dependencies
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/pull-request-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ jobs:

- uses: actions/cache@v2
with:
path: |
~/.cache
path: ~/.cache/solana
key: solana-${{ env.SOLANA_VERSION }}

- name: Install dependencies
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/pull-request-feature-proposal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ jobs:

- uses: actions/cache@v2
with:
path: |
~/.cache
path: ~/.cache/solana
key: solana-${{ env.SOLANA_VERSION }}

- name: Install dependencies
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/pull-request-governance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ jobs:

- uses: actions/cache@v2
with:
path: |
~/.cache
path: ~/.cache/solana
key: solana-${{ env.SOLANA_VERSION }}

- name: Install dependencies
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/pull-request-libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ jobs:

- uses: actions/cache@v2
with:
path: |
~/.cache
path: ~/.cache/solana
key: solana-${{ env.SOLANA_VERSION }}

- name: Install dependencies
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/pull-request-memo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ jobs:

- uses: actions/cache@v2
with:
path: |
~/.cache
path: ~/.cache/solana
key: solana-${{ env.SOLANA_VERSION }}

- name: Install dependencies
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/pull-request-name-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ jobs:

- uses: actions/cache@v2
with:
path: |
~/.cache
path: ~/.cache/solana
key: solana-${{ env.SOLANA_VERSION }}

- name: Install dependencies
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/pull-request-record.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ jobs:

- uses: actions/cache@v2
with:
path: |
~/.cache
path: ~/.cache/solana
key: solana-${{ env.SOLANA_VERSION }}

- name: Install dependencies
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/pull-request-shared-memory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ jobs:

- uses: actions/cache@v2
with:
path: |
~/.cache
path: ~/.cache/solana
key: solana-${{ env.SOLANA_VERSION }}

- name: Install dependencies
Expand Down
34 changes: 32 additions & 2 deletions .github/workflows/pull-request-stake-pool.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ jobs:

- uses: actions/cache@v2
with:
path: |
~/.cache
path: ~/.cache/solana
key: solana-${{ env.SOLANA_VERSION }}

- name: Install dependencies
Expand All @@ -57,3 +56,34 @@ jobs:
- name: Build and test
run: ./ci/cargo-test-bpf.sh stake-pool

- name: Upload programs
uses: actions/upload-artifact@v2
with:
name: stake-pool-programs
path: "target/deploy/*.so"
if-no-files-found: error

py-test:
runs-on: ubuntu-latest
needs: cargo-test-bpf
steps:
- uses: actions/checkout@v2

- name: Setup Python version
uses: actions/setup-python@v2
with:
python-version: 3.8

- uses: actions/cache@v2
with:
path: ~/.cache/pip
key: pip-stake-pool-${{ hashFiles('stake-pool/py/requirements.txt') }}

- name: Download programs
uses: actions/download-artifact@v2
with:
name: stake-pool-programs
path: target/deploy

- run: ./ci/py-test-stake-pool.sh
3 changes: 1 addition & 2 deletions .github/workflows/pull-request-token-lending.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ jobs:

- uses: actions/cache@v2
with:
path: |
~/.cache
path: ~/.cache/solana
key: solana-${{ env.SOLANA_VERSION }}

- name: Install dependencies
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/pull-request-token-swap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ jobs:

- uses: actions/cache@v2
with:
path: |
~/.cache
path: ~/.cache/solana
key: solana-${{ env.SOLANA_VERSION }}

- name: Install dependencies
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/pull-request-token.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ jobs:

- uses: actions/cache@v2
with:
path: |
~/.cache
path: ~/.cache/solana
key: solana-${{ env.SOLANA_VERSION }}

- name: Install dependencies
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ jobs:

- uses: actions/cache@v2
with:
path: |
~/.cache
path: ~/.cache/solana
key: solana-${{ env.SOLANA_VERSION }}

- name: Install dependencies
Expand Down
21 changes: 21 additions & 0 deletions ci/py-test-stake-pool.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -ex
cd "$(dirname "$0")/.."
source ./ci/solana-version.sh install

cd stake-pool/py
python3 -m venv venv
source ./venv/bin/activate
pip3 install -r requirements.txt
check_dirs=(
"spl_token"
"stake"
"stake_pool"
"system"
"tests"
"vote"
)
flake8 "${check_dirs[@]}"
mypy "${check_dirs[@]}"
python3 -m pytest
2 changes: 2 additions & 0 deletions stake-pool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ Full documentation is available at https://spl.solana.com/stake-pool
The command-line interface tool is available in the `./cli` directory.

Javascript bindings are available in the `./js` directory.

Python bindings are available in the `./py` directory.
2 changes: 2 additions & 0 deletions stake-pool/py/.flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length=120
7 changes: 7 additions & 0 deletions stake-pool/py/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# python cache files
*__pycache__*
.pytest_cache
.mypy_cache

# venv
venv/
3 changes: 3 additions & 0 deletions stake-pool/py/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Stake-Pool Python Bindings

WIP Python bindings to interact with the stake pool program.
35 changes: 35 additions & 0 deletions stake-pool/py/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
anyio==3.3.4
attrs==21.2.0
base58==2.1.0
cachetools==4.2.4
certifi==2021.10.8
cffi==1.15.0
charset-normalizer==2.0.7
construct==2.10.67
flake8==4.0.1
h11==0.12.0
httpcore==0.13.7
httpx==0.20.0
idna==3.3
iniconfig==1.1.1
mccabe==0.6.1
mypy==0.910
mypy-extensions==0.4.3
packaging==21.2
pluggy==1.0.0
py==1.10.0
pycodestyle==2.8.0
pycparser==2.20
pyflakes==2.4.0
PyNaCl==1.4.0
pyparsing==2.4.7
pytest==6.2.5
pytest-asyncio==0.16.0
requests==2.26.0
rfc3986==1.5.0
six==1.16.0
sniffio==1.2.0
solana==0.18.1
toml==0.10.2
typing-extensions==3.10.0.2
urllib3==1.26.7
Empty file.
57 changes: 57 additions & 0 deletions stake-pool/py/spl_token/actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from solana.publickey import PublicKey
from solana.keypair import Keypair
from solana.rpc.async_api import AsyncClient
from solana.rpc.commitment import Confirmed
from solana.rpc.types import TxOpts
from solana.transaction import Transaction
import solana.system_program as sys

from spl.token.constants import TOKEN_PROGRAM_ID
from spl.token.async_client import AsyncToken
from spl.token._layouts import MINT_LAYOUT
import spl.token.instructions as spl_token


async def create_associated_token_account(
client: AsyncClient,
payer: Keypair,
owner: PublicKey,
mint: PublicKey
) -> PublicKey:
txn = Transaction()
create_txn = spl_token.create_associated_token_account(
payer=payer.public_key, owner=owner, mint=mint
)
txn.add(create_txn)
await client.send_transaction(txn, payer, opts=TxOpts(skip_confirmation=False, preflight_commitment=Confirmed))
return create_txn.keys[1].pubkey


async def create_mint(client: AsyncClient, payer: Keypair, mint: Keypair, mint_authority: PublicKey):
mint_balance = await AsyncToken.get_min_balance_rent_for_exempt_for_mint(client)
print(f"Creating pool token mint {mint.public_key}")
txn = Transaction()
txn.add(
sys.create_account(
sys.CreateAccountParams(
from_pubkey=payer.public_key,
new_account_pubkey=mint.public_key,
lamports=mint_balance,
space=MINT_LAYOUT.sizeof(),
program_id=TOKEN_PROGRAM_ID,
)
)
)
txn.add(
spl_token.initialize_mint(
spl_token.InitializeMintParams(
program_id=TOKEN_PROGRAM_ID,
mint=mint.public_key,
decimals=9,
mint_authority=mint_authority,
freeze_authority=None,
)
)
)
await client.send_transaction(
txn, payer, mint, opts=TxOpts(skip_confirmation=False, preflight_commitment=Confirmed))
Empty file added stake-pool/py/stake/__init__.py
Empty file.
Loading

0 comments on commit a788886

Please sign in to comment.