ape-tokens: tokenlists plugin for Ape
Project description
Quick Start
A series of utilities for working with tokens, based on the py-tokenlists.
Dependencies
- python3 version 3.8 up to 3.12.
Installation
via pip
You can install the latest release via pip:
pip install ape-tokens
via setuptools
You can clone the repository and use setuptools for the most up-to-date version:
git clone https://github.com/ApeWorX/ape-tokens.git
cd ape-tokens
python3 setup.py install
Quick Usage
CLI Usage
First, install a token list, such as the 1inch token list, which contains many tokens that you can use:
ape tokens install tokens.1inch.eth
To see all the tokens you can use, run command:
ape tokens list-tokens
To see other available CLI commands, run:
ape tokens --help
Configuration
You can configure this plugin (and by extension, configure py-tokenlists) using the config file:
# ape-config.yaml
tokens:
default: "My Default List"
required:
- name: "My Default List"
uri: "http://example.com/tokenlist.json"
You can also configure this plugin via Environment Variable:
APE_TOKENS_DEFAULT="My Default List"
APE_TOKENS_REQUIRED='[{"name":"My Default List","uri":"http://example.com/tokenlist.json"}]'
Configuration like this may be useful for operating in a cloud environment
Ape Console Extras
The tokens manager object is very useful for improving your ape experience.
You can install it as a "console namespace extra" by adding the following lines to your project's ./ape_console_extras.py or your global $HOME/.ape/ape_console_extras.py:
...
try:
from ape_tokens import tokens
except ImportError:
pass # Plugin not installed, skip
...
This way, whenever you use ape console (with this plugin installed) you will have tokens available immediately without having to import it!
Python Usage
One of the main reasons to use the ape-tokens plugin is to have nicer UX for providing token amounts to contract transactions.
For example, let's say you have a smart-contract named MyContract with a function provideLinkToken() that takes a decimal value of LINK tokens.
The following is an example script that deploys the contract and makes a transaction by expressing the value of LINK as 8.23 LINK:
from ape import accounts, project
my_account = accounts[0]
contract = my_account.deploy(project.MyContract)
tx = contract.provideLinkTokens("8.23 LINK", sender=me)
Alternatively, if you need the converted value returned to you, you can use the convert tool from the root ape namespace:
from ape import convert
convert("100.1234 BAT", int)
This plugin also provides a conversion function for addresses, for example if you want to use a .swap function that takes two address inputs which are expected to be tokens (as well as a 3rd argument which is an amount of the 1st token), you can do:
tx = swapper.swap("BAT", "LINK", "10 BAT", sender=me)
To get information about a token, including its contract address, you can do so by importing the tokens member from the root ape_tokens namespace:
from ape_tokens import tokens
bat = tokens["BAT"]
print(bat.address)
print(bat.symbol())
You can also work with the tokens object in an iterable context, such as iterating over a set of tokens in your token list:
from ape_tokens import tokens
for token in tokens:
print(token.balanceOf(me))
Or in a mapping context (similar to using it like a dict):
assert "BAT" in tokens
if bat := tokens.get("BAT"):
print("BAT is in our token list!")
Testing
ape-tokens also ships a test-token contract for using tokens in your Ape tests.
from ape_tokens.testing import MockERC20
def test_deposit(accounts):
owner = accounts[0]
# Setup some test tokens.
usdc = MockERC20.deploy(owner, "USD Coin", "USDC", 6, sender=owner)
weth = MockERC20.deploy(owner, "Wrapped Ether", "WETH", 18, sender=owner)
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ape_tokens-0.8.9.tar.gz.
File metadata
- Download URL: ape_tokens-0.8.9.tar.gz
- Upload date:
- Size: 36.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e400b33ff9ac95a02acb0838d9b1003fc166bb8824fb38aea1fa2ddc3f9b6f3a
|
|
| MD5 |
ea6f2fd9b06ffd9faa688b396c9675fa
|
|
| BLAKE2b-256 |
11aedc194f038f71af592e729d04747455a10ed974b2ca331ed748d5e3dc113f
|
Provenance
The following attestation bundles were made for ape_tokens-0.8.9.tar.gz:
Publisher:
publish.yaml on ApeWorX/ape-tokens
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ape_tokens-0.8.9.tar.gz -
Subject digest:
e400b33ff9ac95a02acb0838d9b1003fc166bb8824fb38aea1fa2ddc3f9b6f3a - Sigstore transparency entry: 1282834562
- Sigstore integration time:
-
Permalink:
ApeWorX/ape-tokens@a16158ed3eae4b063c64c30e3bac4419d0ad01e6 -
Branch / Tag:
refs/tags/v0.8.9 - Owner: https://github.com/ApeWorX
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@a16158ed3eae4b063c64c30e3bac4419d0ad01e6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file ape_tokens-0.8.9-py3-none-any.whl.
File metadata
- Download URL: ape_tokens-0.8.9-py3-none-any.whl
- Upload date:
- Size: 26.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9507a6cdc9def6bf2ca9783b546f02c7cbe5bf783eb5890a1b55b1af4c961072
|
|
| MD5 |
e2b5c5479a277863cfc9abedc791008c
|
|
| BLAKE2b-256 |
919dda94133d8ffe7363697c7377508ffb5d68897741131a0e302cf034b37ce9
|
Provenance
The following attestation bundles were made for ape_tokens-0.8.9-py3-none-any.whl:
Publisher:
publish.yaml on ApeWorX/ape-tokens
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ape_tokens-0.8.9-py3-none-any.whl -
Subject digest:
9507a6cdc9def6bf2ca9783b546f02c7cbe5bf783eb5890a1b55b1af4c961072 - Sigstore transparency entry: 1282834567
- Sigstore integration time:
-
Permalink:
ApeWorX/ape-tokens@a16158ed3eae4b063c64c30e3bac4419d0ad01e6 -
Branch / Tag:
refs/tags/v0.8.9 - Owner: https://github.com/ApeWorX
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@a16158ed3eae4b063c64c30e3bac4419d0ad01e6 -
Trigger Event:
release
-
Statement type: