diff --git a/pages/guides/fetch-network/cosmpy/use-cases/_meta.json b/pages/guides/fetch-network/cosmpy/use-cases/_meta.json index e3b637ec3..c365e20e1 100644 --- a/pages/guides/fetch-network/cosmpy/use-cases/_meta.json +++ b/pages/guides/fetch-network/cosmpy/use-cases/_meta.json @@ -1,6 +1,6 @@ { "stake-auto-compounder" : "Stake auto-compounder️", "stake-optimizer" : "Stake optimizer️", - "stake-optimizer" : "Stake optimizer️", - "stake-optimizer" : "Stake optimizer️" + "oracles" : "Oracles \uD83D\uDD2E", + "wallet-top-up" : "Wallet top-up \uD83D\uDCB5" } \ No newline at end of file diff --git a/pages/guides/fetch-network/cosmpy/use-cases/oracles.mdx b/pages/guides/fetch-network/cosmpy/use-cases/oracles.mdx new file mode 100644 index 000000000..032eed42f --- /dev/null +++ b/pages/guides/fetch-network/cosmpy/use-cases/oracles.mdx @@ -0,0 +1,63 @@ +# Oracles 🔮 + +## Introduction + +**Oracles** are entities that can update state variables in smart contracts and whose goal is usually to accurately estimate or predict some real world quantity or quantities. These quantities can then be used in the logic of other smart contracts. This guide shows how to write a CosmPy script that deploys and updates an oracle contract with a coin price, and another script that deploys a contract that queries this coin price. + +## Getting started + +### Aerial oracle + +1. First of all, create a Python script and name it: `touch aerial_oracle.py` +2. We need to download the binaries for both contracts, which can be done as follows: + + ``` + wget https://raw.githubusercontent.com/fetchai/agents-aea/develop/packages/fetchai/contracts/oracle/build/oracle.wasm + wget https://raw.githubusercontent.com/fetchai/agents-aea/develop/packages/fetchai/contracts/oracle_client/build/oracle_client.wasm + ``` + +3. We would then also require the following imports: + + ```py copy + from time import sleep + import requests + from cosmpy.aerial.client import LedgerClient, NetworkConfig + from cosmpy.aerial.contract import LedgerContract + from cosmpy.aerial.faucet import FaucetApi + from cosmpy.aerial.wallet import LocalWallet + from cosmpy.crypto.address import Address + ``` + +4. We then need to choose a data source for the coin price, the update interval, the decimal precision, and the decimal timeout for the oracle value: + + ```py copy + COIN_PRICE_URL = ( + "https://api.coingecko.com/api/v3/simple/price?ids=fetch-ai&vs_currencies=usd" + ) + UPDATE_INTERVAL_SECONDS = 10 + ORACLE_VALUE_DECIMALS = 5 + DEFAULT_TIMEOUT = 60.0 + ``` + +5. We then proceed and define a `_parse_commandline()` by first importing the argparse module, which is a standard Python module for parsing command-line arguments. + + ```py copy + def _parse_commandline(): + parser = argparse.ArgumentParser() + parser.add_argument( + "contract_path", help="The path to the oracle contract to upload" + ) + parser.add_argument( + "contract_address", + nargs="?", + type=Address, + help="The address of the oracle contract if already deployed", + ) + return parser.parse_args() + ``` + + This first creates an argument `parser` object. The `ArgumentParser` class provides a way to specify the arguments your script should accept and automatically generates help messages and error messages. We then use `add_argument()` to add a positional argument named `contract_path`. This argument is required and should be a path to the oracle contract that you want to upload. The help argument provides a description of what this argument does. We further add another positional argument named `contract_address`. This argument is optional (`nargs="?"` allows it to be omitted), and it should be of type `Address`. The `type` argument specifies the type of the argument. In this case, `Address` is a custom type or class used to represent addresses. The `help` argument provides a description of what this argument does. At the end, we parse the command-line arguments provided when the script is executed. It returns an object that contains the values of the parsed arguments. + + + + diff --git a/pages/guides/fetch-network/cosmpy/use-cases/stake-optimizer.md b/pages/guides/fetch-network/cosmpy/use-cases/stake-optimizer.mdx similarity index 96% rename from pages/guides/fetch-network/cosmpy/use-cases/stake-optimizer.md rename to pages/guides/fetch-network/cosmpy/use-cases/stake-optimizer.mdx index a1f70e8d6..041648e5b 100644 --- a/pages/guides/fetch-network/cosmpy/use-cases/stake-optimizer.md +++ b/pages/guides/fetch-network/cosmpy/use-cases/stake-optimizer.mdx @@ -1,4 +1,4 @@ -# Stake optimizer️ +# Stake optimizer ## Getting started @@ -17,8 +17,8 @@ from cosmpy.aerial.config import NetworkConfig ledger = LedgerClient(NetworkConfig.fetchai_stable_testnet()) ``` -## Set and Query Variables -### Staking Variables +## Set and query variables +### Staking variables First, we need to define the desired amount and the total period that we would like to stake in: `initial_stake` and `total_period` variables. Here we will stake 50 TESTFET for 60000 minutes. For this guide, we will work with minutes as a time unit: @@ -27,7 +27,7 @@ initial_stake = 50000000000000000000 total_period = 60000 ``` -### Validator Selection and Variables +### Validator selection and variables We are now ready to select a validator to delegate our tokens. We can do this by analyzing which one has the lowest commission and a reasonable amount of stake delegated compared to the total stake. @@ -152,20 +152,23 @@ minute_reward = anual_reward/360/24/60 rate = minute_reward/initial_stake ``` -### Calculate Optimal Compounding Period +### Calculate optimal compounding period We can calculate the optimal compounding period that maximizes our staking rewards analytically by using the following formula: -![](/../../src/images/) +![](/../../docs_v2/src/images/docs/reward_equation.png) Where: M = Total stake at time D - S= Initial Stake \ f = Transaction Fee \ k = Reward Rate - m = Number Of Compounding Transactions \ n = Compounding Period + S = Initial Stake + f = Transaction Fee + k = Reward Rate + m = Number Of Compounding Transactions + n = Compounding Period D = m x n = Total Staking Time -We will now find the value that maximizes reward by taking the first derivative with respect to n and finding the root in the interval (0,D]: +We will now find the value that maximizes reward by taking the first derivative with respect to _n_ and finding the root in the interval _(0,D]_: ```py copy import numpy as np @@ -215,7 +218,7 @@ plt.title('Maximizing Rewards') plt.grid() ``` -![](/../../src/images/docs/) +![](/../../docs_v2/src/images/docs/cosmpy-graph-optimal-period.png) Finally, we can compare the compounding staking rewards to a simple non-compounding strategy: @@ -261,7 +264,7 @@ plt.legend() plt.yscale('log') ``` -![](/../../src/images/cosmpy_stake_optimizer/) +![](/../../docs_v2/src/images/docs/cosmpy_stake_optimizer.png) Now that we have presented the concepts and ideas behind the optimizer, have a look at the abbreviated version of the code below: diff --git a/pages/guides/fetch-network/cosmpy/use-cases/wallet-top-up.mdx b/pages/guides/fetch-network/cosmpy/use-cases/wallet-top-up.mdx new file mode 100644 index 000000000..bd5292ea5 --- /dev/null +++ b/pages/guides/fetch-network/cosmpy/use-cases/wallet-top-up.mdx @@ -0,0 +1,27 @@ +# Wallet top-up 💵 + +## Introduction + +If you are performing multiple transactions from a certain `task_wallet`, you can set an algorithm to keep that wallet address topped-up. For this use case, we will use three different wallets: `wallet`, `authz_wallet`, and `task_wallet`. Wallet will be the main wallet address that we don't want to give full access to, therefore we will authorize `authz_wallet` to send a certain amount of tokens from wallet to `task_wallet` every time `task_wallet` balance falls below a certain `minimum_balance` threshold. This way, `task_wallet` can keep performing transactions using the main wallet's tokens by being topped-up by `authz_wallet`. + +Let's start by defining `wallet`, `authz_wallet` and `task_wallet` address. + + +1. +```py copy +from cosmpy.aerial.wallet import LocalWallet +from cosmpy.crypto.keypairs import PrivateKey +from cosmpy.aerial.client import LedgerClient, NetworkConfig + +ledger = LedgerClient(NetworkConfig.latest_stable_testnet()) + +# Define wallets with any private keys +wallet = LocalWallet(PrivateKey("F7w1yHq1QIcQiSqV27YSwk+i1i+Y4JMKhkpawCQIh6s=")) + +authz_wallet = LocalWallet( + PrivateKey("KI5AZQcr+FNl2usnSIQYpXsGWvBxKLRDkieUNIvMOV8=") +) + +# Define any task_wallet address +task_wallet_address = 'fetch1ay6grfwhlm00wydwa3nw0x2u44qz4hg2uku8dc' +``` \ No newline at end of file