Skip to content
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

Tinyman V2 #48

Merged
merged 82 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
9bac951
Initial work on staking related functionality
fergalwalsh Dec 20, 2021
ef310a0
Staking commitment example
fergalwalsh Dec 20, 2021
09bf7ba
Updates and examples
fergalwalsh Dec 20, 2021
6492c89
Fix balance parsing from log
fergalwalsh Feb 10, 2022
a8adc6b
Update prepare_commit_transaction
fergalwalsh Feb 10, 2022
f0a1826
Useful utils
fergalwalsh Feb 15, 2022
491e4c3
Update staking functionality
fergalwalsh Feb 15, 2022
028b383
add .idea to gitignore
gokselcoban Feb 15, 2022
758e731
add reward payment transaction parser
gokselcoban Feb 16, 2022
1796892
improve naming of reward payment parser
gokselcoban Feb 16, 2022
f19111c
Merge pull request #1 from Hipo/staking-paymet-transaction-parser
gokselcoban Feb 16, 2022
76e8cbb
Updates for reward notes
fergalwalsh Feb 16, 2022
491fcba
Fix arg name
fergalwalsh Feb 16, 2022
1fbd6c7
fix reward payment parser
gokselcoban Feb 16, 2022
40e8db8
Updates for staking
fergalwalsh Mar 9, 2022
e801e66
Merge branch 'staking' of github.com:Hipo/tinyman-py-sdk into staking
fergalwalsh Mar 9, 2022
9790a23
Update staking commitment transaction to fit Ledger limits
fergalwalsh Mar 11, 2022
5c4318d
add flake8 rules
gokselcoban Apr 1, 2022
e34817a
fix code formatting
gokselcoban Apr 1, 2022
833fb68
update reward payment note format
gokselcoban Apr 1, 2022
9b04b6e
add get_reward_metadata_from_note function
gokselcoban Apr 1, 2022
466eb94
add DATE_FORMAT constant for staking
gokselcoban Apr 1, 2022
80a9eb6
fix reward payment parser
gokselcoban Apr 4, 2022
bb38839
handle invalid reward payment note
gokselcoban Apr 5, 2022
64705e8
fix reward payment metadata
gokselcoban Apr 7, 2022
27e02de
Merge pull request #2 from Hipo/staking-on-demand-payment
gokselcoban Apr 25, 2022
0522607
add asa reward support to staking
gokselcoban Apr 28, 2022
f567d5e
Add round to commitment dict
fergalwalsh May 19, 2022
f026994
add prepare_end_program_transaction
fergalwalsh May 19, 2022
ffd2d13
Updated asc.json
fergalwalsh May 19, 2022
a376e7c
Merge pull request #3 from Hipo/staking-asa-support
gokselcoban May 19, 2022
3eddc2a
update staking commitment transaction group
gokselcoban Jun 2, 2022
b503546
Merge pull request #4 from Hipo/staking-commitment-required-asset-sup…
gokselcoban Jul 18, 2022
68f4fad
Merge branch 'main' of github.com:tinymanorg/tinyman-py-sdk into staking
gokselcoban Sep 26, 2022
b68c378
fix formatting errors
gokselcoban Sep 26, 2022
e26844c
add black to pre-commit hooks
gokselcoban Sep 26, 2022
f8b3e1a
remove deprecated wait_for_confirmation
gokselcoban Sep 26, 2022
c7f4b29
v2 wip
gokselcoban Sep 29, 2022
599ba66
improve add liquidity functions
gokselcoban Oct 3, 2022
6591a6d
add basic examples to readme
gokselcoban Oct 3, 2022
1c31127
improve markdown format
gokselcoban Oct 3, 2022
b8ce1c4
add flash loan & swap functions
gokselcoban Oct 5, 2022
1de874d
allow to initialize a pool with state dict
gokselcoban Oct 5, 2022
4275173
add fee and management functions
gokselcoban Oct 6, 2022
90ce5a0
add constants and asc.json
gokselcoban Oct 7, 2022
c90c317
add basic tests
gokselcoban Oct 7, 2022
b146461
use calculate_price_impact function for V1 calculations
gokselcoban Oct 12, 2022
064a810
remove redundant and late int conversions
gokselcoban Oct 12, 2022
3c2f4b0
drop python 3.7 support
gokselcoban Oct 12, 2022
566a210
setup github actions
gokselcoban Oct 12, 2022
5bd93ac
fix type annotations
gokselcoban Oct 12, 2022
a6834bf
remove pre-commit actions
gokselcoban Oct 12, 2022
4396677
update claim extra app call
gokselcoban Oct 12, 2022
572f120
fix typo
gokselcoban Oct 21, 2022
a47c5d4
fix typo
gokselcoban Oct 25, 2022
4176cc9
fix setup tools
gokselcoban Nov 3, 2022
c17550a
add __eq__ comparison method for Asset
gokselcoban Nov 3, 2022
a15acbb
fix fixed-input swap
gokselcoban Nov 3, 2022
c494a7c
handle swap cases with insufficient reserves
gokselcoban Nov 4, 2022
4a73cbd
add deprecation warning for sign_with_logicisg
gokselcoban Nov 15, 2022
5f3b0d3
minor improvements
gokselcoban Nov 15, 2022
4478f88
update exception names
gokselcoban Nov 15, 2022
c954e4f
fix the code format
gokselcoban Nov 15, 2022
05f48ac
bump version
gokselcoban Nov 16, 2022
9cc0546
update misleading fetch_pool parameters
gokselcoban Nov 22, 2022
d9b5c25
don't generate InternalSwapQuote if the output amount is zero
gokselcoban Dec 6, 2022
b8252de
update misleading asset parameters
gokselcoban Dec 6, 2022
713b529
Fix typo
omerfarukabaci Dec 13, 2022
d441b33
update testnet v2 app id
gokselcoban Dec 14, 2022
c1dee89
Merge pull request #2 from Hipo/fix-internal-swap-quotes
gokselcoban Dec 15, 2022
2e54703
update PYPI explanation
gokselcoban Dec 15, 2022
446f5e7
Merge branch 'main' of github.com:Hipo/private-tinyman-py-sdk into v2…
gokselcoban Dec 15, 2022
9f49941
don't allow opting into ALGO
gokselcoban Dec 15, 2022
70a2cb8
remove asc.json files
gokselcoban Dec 15, 2022
3949033
minor fixes
gokselcoban Dec 15, 2022
898469c
bump version
gokselcoban Dec 15, 2022
da0d141
add changelog
gokselcoban Dec 15, 2022
9b23653
fix formatting errors
gokselcoban Dec 15, 2022
7f5e303
remove redundant functions
gokselcoban Dec 15, 2022
e97601c
fix the exception name
gokselcoban Dec 15, 2022
709d661
prefer submit function of the client
gokselcoban Dec 15, 2022
ddf1e01
Merge branch 'main' of github.com:tinymanorg/tinyman-py-sdk into v2-u…
gokselcoban Dec 20, 2022
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
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
ignore = E501,F403,F405,E126,E121,W503,E203
34 changes: 34 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Lint & Tests

on: [push, pull_request]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.9", "3.8"]
py-algorand-sdk-version: ["1.18", "1.17", "1.16", "1.15", "1.14", "1.13"]

steps:
- uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 black py-algorand-sdk==${{ matrix.py-algorand-sdk-version }}

- name: Run flake8
run: flake8 ${{ github.workspace }} --ignore=E501,F403,F405,E126,E121,W503,E203

- name: Run Black
run: black ${{ github.workspace }} --check

- name: Run Unit tests
run: python -m unittest
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.idea

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down Expand Up @@ -129,3 +130,7 @@ dmypy.json

# Pyre type checker
.pyre/

# Tutorials
account*.json
assets*.json
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
repos:
- repo: https://github.com/pycqa/flake8
rev: '3.9.2' # pick a git hash / tag to point to
hooks:
- id: flake8
args: ['--ignore=E501,F403,F405,E126,E121,W503,E203', '.']
exclude: ^(env|venv)

- repo: https://github.com/psf/black
rev: 22.8.0
hooks:
- id: black
args: ['.', '--check']
exclude: ^(env|venv)
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Change Log

## 2.0.0

### Added

* Added Tinyman V2 support (`tinyman.v2`).
* Added Staking support (`tinyman.staking`).
- It allows creating commitment transaction by `prepare_commit_transaction` and tracking commitments by `parse_commit_transaction`.
* Added `calculate_price_impact` function to `tinyman.utils`.
* Improved `TransactionGroup` class.
- Added `+` operator support for composability, it allows creating a new transaction group (`txn_group_1 + txn_group_2`).
- Added `id` property, it returns the transactions group id.
- Added `TransactionGroup.sign_with_logicsig` function and deprecated `TransactionGroup.sign_with_logicisg` because of the typo.

### Changed

* `get_program` (V1) is moved from `tinyman.utils` to `tinyman.v1.contracts`.
* `get_state_from_account_info` (V1) is moved from `tinyman.utils` to `tinyman.v1.utils`.

### Removed

* Deprecated `wait_for_confirmation` function is removed. `wait_for_confirmation` is added to [Algorand SDK](https://github.com/algorand/py-algorand-sdk).
* Drop Python 3.7 support.

156 changes: 148 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,150 @@ The SDK has been updated for Tinyman V1.1.


## Installation
tinyman-py-sdk is not yet released on PYPI. It can be installed directly from this repository with pip:
tinyman-py-sdk is not released on PYPI. It can be installed directly from this repository with pip:

`pip install git+https://github.com/tinymanorg/tinyman-py-sdk.git`

## V2

## Sneak Preview

```python
# examples/v2/sneak_preview.py

from examples.v2.utils import get_algod
from tinyman.v2.client import TinymanV2TestnetClient

algod = get_algod()
client = TinymanV2TestnetClient(algod_client=algod)

# Fetch our two assets of interest
USDC = client.fetch_asset(10458941)
ALGO = client.fetch_asset(0)

# Fetch the pool we will work with
pool = client.fetch_pool(USDC, ALGO)
print(f"Pool Info: {pool.info()}")

# Get a quote for a swap of 1 ALGO to USDC with 1% slippage tolerance
quote = pool.fetch_fixed_input_swap_quote(amount_in=ALGO(1_000_000), slippage=0.01)
print(quote)
print(f"USDC per ALGO: {quote.price}")
print(f"USDC per ALGO (worst case): {quote.price_with_slippage}")
```

## Tutorial

You can find a tutorial under the `examples/v2/tutorial` folder.

To run a step use `python <file_name>` such as `python 01_generate_account.py`.

#### Prerequisites
1. [Generating an account](examples/v2/01_generate_account.py)
2. [Creating assets](examples/v2/02_create_assets.by)

#### Steps

3. [Bootstrapping a pool](examples/v2/03_bootstrap_pool.py)
4. [Adding initial liquidity to the pool](examples/v2/04_add_initial_liquidity.py)
5. [Adding flexible (add two asset with a flexible rate) liquidity to the pool](examples/v2/05_add_flexible_liquidity.py)
6. [Adding single asset (add only one asset) liquidity to the pool](examples/v2/06_add_single_asset_liquidity.py)
7. [Removing liquidity to the pool](examples/v2/07_remove_liquidity.py)
8. [Removing single asset(receive single asset) liquidity to the pool](examples/v2/08_single_asset_remove_liquidity.py)
9. [Swapping fixed-input](examples/v2/09_fixed_input_swap.py)
10. [Swapping fixed-output](examples/v2/10_fixed_output_swap.py)

## Example Operations

### Bootstrap

```python
txn_group = pool.prepare_bootstrap_transactions()
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
txn_info = txn_group.submit(algod, wait=True)
```

### Add Liquidity

#### Initial Add Liquidity

```python
quote = pool.fetch_initial_add_liquidity_quote(
amount_a=<AssetAmount>,
amount_b=<AssetAmount>,
)
txn_group = pool.prepare_add_liquidity_transactions_from_quote(quote)
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
txn_info = txn_group.submit(algod, wait=True)
```

#### Flexible Add Liquidity

```python
quote = pool.fetch_flexible_add_liquidity_quote(
amount_a=<AssetAmount>,
amount_b=<AssetAmount>,
)
txn_group = pool.prepare_add_liquidity_transactions_from_quote(quote=quote)
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
txn_info = txn_group.submit(algod, wait=True)
```

#### Single Asset Add Liquidity

```python
quote = pool.fetch_single_asset_add_liquidity_quote(amount_a=<AssetAmount>)
txn_group = pool.prepare_add_liquidity_transactions_from_quote(quote=quote)
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
txn_info = txn_group.submit(algod, wait=True)
```

### Remove Liquidity

#### Remove Liquidity

```python
quote = pool.fetch_remove_liquidity_quote(
pool_token_asset_in=<AssetAmount>,
)
txn_group = pool.prepare_remove_liquidity_transactions_from_quote(quote=quote)
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
txn_info = txn_group.submit(algod, wait=True)
```

#### Single Asset Remove Liquidity

```python
quote = pool.fetch_single_asset_remove_liquidity_quote(
pool_token_asset_in=<AssetAmount>,
output_asset=<Asset>,
)
txn_group = pool.prepare_remove_liquidity_transactions_from_quote(quote=quote)
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
txn_info = txn_group.submit(algod, wait=True)
```

### Swap

#### Fixed Input Swap

```python
quote = pool.fetch_fixed_input_swap_quote(amount_in=<AssetAmount>)
txn_group = pool.prepare_swap_transactions_from_quote(quote=quote)
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
txn_info = txn_group.submit(algod, wait=True)
```

#### Fixed Output Swap

```python
quote = pool.fetch_fixed_output_swap_quote(amount_in=<AssetAmount>)
txn_group = pool.prepare_swap_transactions_from_quote(quote=quote)
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
txn_info = txn_group.submit(algod, wait=True)
```

## V1.1

## Sneak Preview

Expand Down Expand Up @@ -48,7 +188,7 @@ print(f'TINYUSDC per ALGO (worst case): {quote.price_with_slippage}')
## Examples

### Basic Swapping
[swapping1.py](examples/swapping1.py)
[swapping1.py](examples/v1/swapping1.py)
This example demonstrates basic functionality including:
* retrieving Pool details
* getting a swap quote
Expand All @@ -58,16 +198,16 @@ This example demonstrates basic functionality including:
* checking excess amounts
* preparing redeem transactions

[swapping1_less_convenience.py](examples/swapping1_less_convenience.py)
This example has exactly the same functionality as [swapping1.py](examples/swapping1.py) but is purposely more verbose, using less convenience functions.
[swapping1_less_convenience.py](examples/v1/swapping1_less_convenience.py)
This example has exactly the same functionality as [swapping1.py](examples/v1/swapping1.py) but is purposely more verbose, using less convenience functions.


### Basic Pooling
[pooling1.py](examples/pooling1.py)
[pooling1.py](examples/v1/pooling1.py)
This example demonstrates retrieving the current pool position/share for an address.

### Basic Add Liquidity (Minting)
[add_liquidity1.py](examples/add_liquidity1.py)
[add_liquidity1.py](examples/v1/add_liquidity1.py)
This example demonstrates add liquidity to an existing pool.

### Basic Burning
Expand Down Expand Up @@ -119,9 +259,9 @@ for i, txn in enumerate(transaction_group.transactions):
transaction_group.signed_transactions[i] = kmd.sign_transaction(handle, KMD_WALLET_PASSWORD, txn)
```

A User account LogicSig can also be used in a similar way or using the `sign_with_logicisg` convenience method:
A User account LogicSig can also be used in a similar way or using the `sign_with_logicsig` convenience method:
```python
transaction_group.sign_with_logicisg(logicsig)
transaction_group.sign_with_logicsig(logicsig)
```

### Submission
Expand Down
32 changes: 32 additions & 0 deletions asc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"repo": "https://github.com/tinymanorg/tinyman-staking",
"ref": "main",
"contracts": {
"staking_app": {
"type": "app",
"approval_program": {
"bytecode": "BSADAAEIJgcIZW5kX3RpbWUGYXNzZXRzBG1pbnMMdmVyaWZpY2F0aW9uCGJhbGFuY2UgAmlkD3Byb2dyYW1fY291bnRlcjEZIhJAAC8xGSMSQAAZMRmBAhJAAicxGYEEEkACITEZgQUSQAIfADYaAIAFc2V0dXASQAGZADYaAIAGY3JlYXRlEkAAijYaAIAGY29tbWl0EkAAfTYaAIAFY2xhaW0SQAEDNhoAgAZ1cGRhdGUSQAE8NhoAgA51cGRhdGVfcmV3YXJkcxJAAN82GgCADXVwZGF0ZV9hc3NldHMSQAD4NhoAgAtlbmRfcHJvZ3JhbRJAAPI2GgArEkAA9TYaAIALbG9nX2JhbGFuY2USQADvACNDIyhiMgcNRCMpYiJKJAtbNjAAEkAACiMISYEODERC/+tMSDUBIypiNAEkC1s1AjYaARdBAAg2GgEXNAIPRCI2MABwAERJFicETFCwNhoBFw9EgBN0aW55bWFuU3Rha2luZy92MTpiMQVRABMSRDEFVxMANQE0ASJbIycFYhJENAEkWzYwABJENAGBEFs2GgEXEkQjQyNDIoACcjE2GgFmIoACcjI2GgJmIoACcjM2GgNmIoACcjQ2GgRmIoACcjU2GgVmI0MiKTYaAWYiKjYaAmYjQyIoNhoBF2YjQyNDMgkxABJEIis2GgFmI0MiNjAAcABESUQWJwRMULAjQycGZCMINQEnBjQBZyInBTQBZiKAA3VybDYaAWYigA9yZXdhcmRfYXNzZXRfaWQ2GgIXZiKADXJld2FyZF9wZXJpb2Q2GgMXZiKACnN0YXJ0X3RpbWU2GgQXZiIoNhoFF2YiKTYaBmYiKjYaB2YjQyNDMgkxABJDMgkxABJDAA==",
"address": "KJ3W4IB66Q4ZITCNVABJXAV4I4HKWSZIJMD6BTFATQAWO5AKOV5VMZ6OFI",
"size": 658,
"variables": [],
"source": "https://github.com/tinymanorg/tinyman-staking/tree/main/contracts/staking.teal"
},
"clear_program": {
"bytecode": "BIEB",
"address": "P7GEWDXXW5IONRW6XRIRVPJCT2XXEQGOBGG65VJPBUOYZEJCBZWTPHS3VQ",
"size": 3,
"variables": [],
"source": "https://github.com/tinymanorg/tinyman-staking/tree/main/contracts/clear_state.teal"
},
"global_state_schema": {
"num_uints": 2,
"num_byte_slices": 2
},
"local_state_schema": {
"num_uints": 5,
"num_byte_slices": 11
},
"name": "staking_app"
}
}
}
Empty file added examples/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions examples/staking/commitments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import requests
from tinyman.staking import parse_commit_transaction
from tinyman.staking.constants import TESTNET_STAKING_APP_ID

app_id = TESTNET_STAKING_APP_ID
result = requests.get(
f"https://indexer.testnet.algoexplorerapi.io/v2/transactions?application-id={app_id}&latest=50"
).json()
for txn in result["transactions"]:
commit = parse_commit_transaction(txn, app_id)
if commit:
print(commit)
print()
40 changes: 40 additions & 0 deletions examples/staking/make_commitment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This sample is provided for demonstration purposes only.
# It is not intended for production use.
# This example does not constitute trading advice.

from tinyman.v1.client import TinymanTestnetClient

from tinyman.staking import prepare_commit_transaction

# Hardcoding account keys is not a great practice. This is for demonstration purposes only.
# See the README & Docs for alternative signing methods.
account = {
"address": "ALGORAND_ADDRESS_HERE",
"private_key": "base64_private_key_here", # Use algosdk.mnemonic.to_private_key(mnemonic) if necessary
}

client = TinymanTestnetClient(user_address=account["address"])

# Fetch our two assets of interest
TINYUSDC = client.fetch_asset(21582668)
ALGO = client.fetch_asset(0)

# Fetch the pool we will work with
pool = client.fetch_pool(TINYUSDC, ALGO)


sp = client.algod.suggested_params()

txn_group = prepare_commit_transaction(
app_id=client.staking_app_id,
program_id=1,
program_account="B4XVZ226UPFEIQBPIY6H454YA4B7HYXGEM7UDQR2RJP66HVLOARZTUTS6Q",
pool_asset_id=pool.liquidity_asset.id,
amount=600_000_000,
sender=account["address"],
suggested_params=sp,
)

txn_group.sign_with_private_key(account["address"], account["private_key"])
result = client.submit(txn_group, wait=True)
print(result)
Loading