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

Add governance and lending pools features #66

Merged
merged 2 commits into from
Jul 31, 2024
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
53 changes: 53 additions & 0 deletions examples/folks_lending/add_liquidity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from algosdk.v2client.algod import AlgodClient

from tinyman.folks_lending.constants import (
TESTNET_FOLKS_POOL_MANAGER_APP_ID,
TESTNET_FOLKS_WRAPPER_LENDING_POOL_APP_ID)
from tinyman.folks_lending.transactions import \
prepare_add_liquidity_transaction_group
from tinyman.folks_lending.utils import get_lending_pools
from tinyman.v2.client import TinymanV2TestnetClient
from tinyman.v2.constants import TESTNET_VALIDATOR_APP_ID_V2

algod = AlgodClient("", "https://testnet-api.algonode.network")
account_sk, account_address = ('YOUR PRIVATE KEY HERE', 'YOUR ADDRESS HERE')
client = TinymanV2TestnetClient(algod_client=algod, user_address=account_address)

asset_1_id = 67395862 # USDC
asset_2_id = 0 # Algo

# Get f_asset ids

folks_pools = get_lending_pools(algod, TESTNET_FOLKS_POOL_MANAGER_APP_ID)
temp = dict()
for folks_pool in folks_pools:
temp[folks_pool['asset_id']] = folks_pool
folks_pools = temp

f_asset_1_id = folks_pools[asset_1_id]['f_asset_id']
f_asset_2_id = folks_pools[asset_2_id]['f_asset_id']

pool = client.fetch_pool(f_asset_1_id, f_asset_2_id, fetch=True)

# Add liquidity

txn_group = prepare_add_liquidity_transaction_group(
sender=account_address,
suggested_params=algod.suggested_params(),
wrapper_app_id=TESTNET_FOLKS_WRAPPER_LENDING_POOL_APP_ID,
tinyman_amm_app_id=TESTNET_VALIDATOR_APP_ID_V2,
lending_app_1_id=folks_pools[asset_1_id]['pool_app_id'],
lending_app_2_id=folks_pools[asset_2_id]['pool_app_id'],
lending_manager_app_id=TESTNET_FOLKS_POOL_MANAGER_APP_ID,
tinyman_pool_address=pool.address,
asset_1_id=asset_1_id,
asset_2_id=asset_2_id,
f_asset_1_id=f_asset_1_id,
f_asset_2_id=f_asset_2_id,
liquidity_token_id=pool.pool_token_asset.id,
asset_1_amount=10000,
asset_2_amount=10000
)

txn_group.sign_with_private_key(account_address, account_sk)
txn_group.submit(algod, True)
97 changes: 97 additions & 0 deletions examples/folks_lending/create_new_pool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from algosdk import transaction
from algosdk.v2client.algod import AlgodClient

from tinyman.folks_lending.constants import (
TESTNET_FOLKS_POOL_MANAGER_APP_ID,
TESTNET_FOLKS_WRAPPER_LENDING_POOL_APP_ID)
from tinyman.folks_lending.transactions import \
prepare_asset_optin_transaction_group
from tinyman.folks_lending.utils import get_lending_pools
from tinyman.utils import TransactionGroup
from tinyman.v2.client import TinymanV2TestnetClient

algod = AlgodClient("", "https://testnet-api.algonode.network")
account_sk, account_address = ('YOUR PRIVATE KEY HERE', 'YOUR ADDRESS HERE')
client = TinymanV2TestnetClient(algod_client=algod, user_address=account_address)

asset_1_id = 67396528 # goBTC
asset_2_id = 0 # Algo

# Get f_asset ids

folks_pools = get_lending_pools(algod, TESTNET_FOLKS_POOL_MANAGER_APP_ID)
temp = dict()
for folks_pool in folks_pools:
temp[folks_pool['asset_id']] = folks_pool
folks_pools = temp

f_asset_1_id = folks_pools[asset_1_id]['f_asset_id']
f_asset_2_id = folks_pools[asset_2_id]['f_asset_id']

pool = client.fetch_pool(f_asset_1_id, f_asset_2_id)

# Opt-in to assets

txns = [
transaction.AssetOptInTxn(
sender=account_address,
sp=algod.suggested_params(),
index=asset_1_id
),
transaction.AssetOptInTxn(
sender=account_address,
sp=algod.suggested_params(),
index=f_asset_1_id
),
transaction.AssetOptInTxn(
sender=account_address,
sp=algod.suggested_params(),
index=f_asset_2_id
)
]

if asset_2_id != 0:
txns.append(
transaction.AssetOptInTxn(
sender=account_address,
sp=algod.suggested_params(),
index=asset_2_id
)
)
txn_group = TransactionGroup(txns)
txn_group.sign_with_private_key(account_address, account_sk)
txn_group.submit(algod, True)

# Bootstrap pool.

txn_group = pool.prepare_bootstrap_transactions(
user_address=account_address,
suggested_params=algod.suggested_params(),
)
txn_group.sign_with_private_key(account_address, account_sk)
txn_group.submit(algod, True)

# Opt-in to pool token.

pool = client.fetch_pool(f_asset_1_id, f_asset_2_id, fetch=True)

txn_group = TransactionGroup([
transaction.AssetOptInTxn(
sender=account_address,
sp=algod.suggested_params(),
index=pool.pool_token_asset.id
)
])
txn_group.sign_with_private_key(account_address, account_sk)
txn_group.submit(algod, True)

# Send an asset_optin appcall.

txn_group = prepare_asset_optin_transaction_group(
sender=account_address,
suggested_params=algod.suggested_params(),
wrapper_app_id=TESTNET_FOLKS_WRAPPER_LENDING_POOL_APP_ID,
assets_to_optin=[asset_1_id, asset_2_id, f_asset_1_id, f_asset_2_id, pool.pool_token_asset.id]
)
txn_group.sign_with_private_key(account_address, account_sk)
txn_group.submit(algod, True)
52 changes: 52 additions & 0 deletions examples/folks_lending/remove_liquidity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from algosdk.v2client.algod import AlgodClient

from tinyman.folks_lending.constants import (
TESTNET_FOLKS_POOL_MANAGER_APP_ID,
TESTNET_FOLKS_WRAPPER_LENDING_POOL_APP_ID)
from tinyman.folks_lending.transactions import \
prepare_remove_liquidity_transaction_group
from tinyman.folks_lending.utils import get_lending_pools
from tinyman.v2.client import TinymanV2TestnetClient
from tinyman.v2.constants import TESTNET_VALIDATOR_APP_ID_V2

algod = AlgodClient("", "https://testnet-api.algonode.network")
account_sk, account_address = ('YOUR PRIVATE KEY HERE', 'YOUR ADDRESS HERE')
client = TinymanV2TestnetClient(algod_client=algod, user_address=account_address)

asset_1_id = 67395862 # USDC
asset_2_id = 0 # Algo

# Get f_asset ids

folks_pools = get_lending_pools(algod, TESTNET_FOLKS_POOL_MANAGER_APP_ID)
temp = dict()
for folks_pool in folks_pools:
temp[folks_pool['asset_id']] = folks_pool
folks_pools = temp

f_asset_1_id = folks_pools[asset_1_id]['f_asset_id']
f_asset_2_id = folks_pools[asset_2_id]['f_asset_id']

pool = client.fetch_pool(f_asset_1_id, f_asset_2_id, fetch=True)

# Remove liquidity

txn_group = prepare_remove_liquidity_transaction_group(
sender=account_address,
suggested_params=algod.suggested_params(),
wrapper_app_id=TESTNET_FOLKS_WRAPPER_LENDING_POOL_APP_ID,
tinyman_amm_app_id=TESTNET_VALIDATOR_APP_ID_V2,
lending_app_1_id=folks_pools[asset_1_id]['pool_app_id'],
lending_app_2_id=folks_pools[asset_2_id]['pool_app_id'],
lending_manager_app_id=TESTNET_FOLKS_POOL_MANAGER_APP_ID,
tinyman_pool_address=pool.address,
asset_1_id=asset_1_id,
asset_2_id=asset_2_id,
f_asset_1_id=f_asset_1_id,
f_asset_2_id=f_asset_2_id,
liquidity_token_id=pool.pool_token_asset.id,
liquidity_token_amount=1_000_000
)

txn_group.sign_with_private_key(account_address, account_sk)
txn_group.submit(algod, True)
27 changes: 27 additions & 0 deletions examples/governance/00_opt_in_to_tiny.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from examples.v2.utils import get_algod
from tinyman.governance.client import TinymanGovernanceTestnetClient

from tinyman.governance.constants import TESTNET_TINY_ASSET_ID

# 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",
}

algod = get_algod()

# Client
governance_client = TinymanGovernanceTestnetClient(
algod_client=algod,
user_address=account["address"]
)

if not governance_client.asset_is_opted_in(TESTNET_TINY_ASSET_ID):
txn_group = governance_client.prepare_asset_optin_transactions(TESTNET_TINY_ASSET_ID)
txn_group.sign_with_private_key(address=account["address"], private_key=account["private_key"])
result = txn_group.submit(algod, wait=True)
print("TXN:", result)

print("Get some TINY token.")
46 changes: 46 additions & 0 deletions examples/governance/01_create_lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import time

from examples.v2.utils import get_algod
from tinyman.governance.client import TinymanGovernanceTestnetClient
from tinyman.governance.constants import WEEK
from tinyman.governance.vault.constants import MIN_LOCK_TIME

# 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",
}

algod = get_algod()

# Client
governance_client = TinymanGovernanceTestnetClient(
algod_client=algod,
user_address=account["address"]
)

account_state = governance_client.fetch_account_state()
print("Account State before TXN:", account_state)

end_timestamp_of_current_week = (int(time.time()) // WEEK + 1) * WEEK
lock_end_timestamp = end_timestamp_of_current_week + MIN_LOCK_TIME

# lock_end_timestamp = int(time.time()) + 100

txn_group = governance_client.prepare_create_lock_transactions(
locked_amount=10_000_000,
lock_end_time=lock_end_timestamp,
)
txn_group.sign_with_private_key(account["address"], account["private_key"])
txn_group.submit(algod, wait=True)

account_state = governance_client.fetch_account_state()
print("Account State after TXN:", account_state)

tiny_power = governance_client.get_tiny_power()
print("TINY POWER:", tiny_power)

total_tiny_power = governance_client.get_total_tiny_power()
print("Total TINY POWER:", total_tiny_power)
print(f"User TINY Power %{(tiny_power / total_tiny_power) * 100}")
39 changes: 39 additions & 0 deletions examples/governance/02_extend_lock_end_time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from examples.v2.utils import get_algod
from tinyman.governance.client import TinymanGovernanceTestnetClient

from tinyman.governance.constants import WEEK

# 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",
}

algod = get_algod()

# Client
governance_client = TinymanGovernanceTestnetClient(
algod_client=algod,
user_address=account["address"]
)

account_state = governance_client.fetch_account_state()
print("Account State before TXN:", account_state)

new_lock_end_time = account_state.lock_end_time + 4 * WEEK
txn_group = governance_client.prepare_extend_lock_end_time_transactions(
new_lock_end_time=new_lock_end_time,
)
txn_group.sign_with_private_key(account["address"], account["private_key"])
txn_group.submit(algod, wait=True)

account_state = governance_client.fetch_account_state()
print("Account State after TXN:", account_state)

tiny_power = governance_client.get_tiny_power()
print("TINY POWER:", tiny_power)

total_tiny_power = governance_client.get_total_tiny_power()
print("Total TINY POWER:", total_tiny_power)
print(f"User TINY Power %{(tiny_power / total_tiny_power) * 100}")
36 changes: 36 additions & 0 deletions examples/governance/03_increase_lock_amount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from examples.v2.utils import get_algod
from tinyman.governance.client import TinymanGovernanceTestnetClient

# 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",
}

algod = get_algod()

# Client
governance_client = TinymanGovernanceTestnetClient(
algod_client=algod,
user_address=account["address"]
)

account_state = governance_client.fetch_account_state()
print("Account State before TXN:", account_state)

txn_group = governance_client.prepare_increase_lock_amount_transactions(
locked_amount=4000000000,
)
txn_group.sign_with_private_key(account["address"], account["private_key"])
txn_group.submit(algod, wait=True)

account_state = governance_client.fetch_account_state()
print("Account State after TXN:", account_state)

tiny_power = governance_client.get_tiny_power()
print("TINY POWER:", tiny_power)

total_tiny_power = governance_client.get_total_tiny_power()
print("Total TINY POWER:", total_tiny_power)
print(f"User TINY Power %{(tiny_power / total_tiny_power) * 100}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from examples.v2.utils import get_algod
from tinyman.governance.client import TinymanGovernanceTestnetClient

from tinyman.governance.constants import WEEK

# 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",
}

algod = get_algod()

# Client
governance_client = TinymanGovernanceTestnetClient(
algod_client=algod,
user_address=account["address"]
)

account_state = governance_client.fetch_account_state()
print("Account State before TXN:", account_state)

txn_group = governance_client.prepare_increase_lock_amount_and_extend_lock_end_time_transactions(
locked_amount=5_000_000_000,
new_lock_end_time=account_state.lock_end_time + 4 * WEEK
)
txn_group.sign_with_private_key(account["address"], account["private_key"])
txn_group.submit(algod, wait=True)

account_state = governance_client.fetch_account_state()
print("Account State after TXN:", account_state)

tiny_power = governance_client.get_tiny_power()
print("TINY POWER:", tiny_power)

total_tiny_power = governance_client.get_total_tiny_power()
print("Total TINY POWER:", total_tiny_power)
print(f"User TINY Power %{(tiny_power / total_tiny_power) * 100}")
Loading
Loading