-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52c0ada
commit 297171d
Showing
4 changed files
with
106 additions
and
13 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
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,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. | ||
|
||
|
||
|
||
|
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
27 changes: 27 additions & 0 deletions
27
pages/guides/fetch-network/cosmpy/use-cases/wallet-top-up.mdx
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,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' | ||
``` |