A python library for interacting with the Swivel Finance Protocol
Project description
A python library for interaction with the Swivel protocol
________ ___ __ ___ ___ ___ _______ ___ ________ ___ ___
|\ ____\|\ \ |\ \|\ \|\ \ / /|\ ___ \ |\ \ |\ __ \|\ \ / /|
\ \ \___|\ \ \ \ \ \ \ \ \ \ / / | \ __/|\ \ \ \ \ \|\ \ \ \/ / /
\ \_____ \ \ \ __\ \ \ \ \ \ \/ / / \ \ \_|/_\ \ \ \ \ ____\ \ / /
\|____|\ \ \ \|\__\_\ \ \ \ \ / / \ \ \_|\ \ \ \____ __\ \ \___|\/ / /
____\_\ \ \____________\ \__\ \__/ / \ \_______\ \_______\\__\ \__\ __/ / /
|\_________\|____________|\|__|\|__|/ \|_______|\|_______\|__|\|__||\___/ /
\|_________| \|___|/
About
Swivel.py is a collection of what we call Higher Order Contracts. These are language specific, Python in this case, constructs that abstract and encapsulate the tedious and boilerplate logic of working with low-level smart contracts.
Installation
pip install -r requirements.txt
Running Tests
pytest
Examples
Before we dive into the specifics, you should take a look at the classes in action. There is an entire repo dedicated to just this. Scrivel shows everything from basic reads to more involved writes to entire market-making operations.
Remember that Swivel.py is a library with a very specific, and narrow, use case. Present the Swivel protocol as an object oriented, pythonic collection ready to be used in your own scripting.
Vendors
One principal concept to grasp is that Swivel.py relies on what in refers to as a vendor
to handle low-level
chores related to the Ethereum network. Web3.py is a supported vendor at this time, and there may be more in
the future.
W3
The W3 Vendor class is a Web3.py specific Vendor that can be used with the Swivel.py Higher Order Contracts.
Constructed with a Web3.py specific Provider
, our class exposes (hoists) some useful methods from within
Web3 itself.
You can see the Vendor class in use throught the Scrivel examples
Signer
Our W3 class also constructs and appends a Signer class to itself. This is an abstraction of the
EIP712 logic needed to sign a Swivel Order offline. You generally will not interact with the Signer
directly as the Vendor itself exposes a sign_order
method. Also note that, in order to sign an order
offline you need to be in possession of the private key associated with the address of the order's
maker. See here for an example.
Private Key
As mentioned above, you must have acess to the private key intented to be used for signing transactions offline and it should be
exported to your shell as PRIVATE_KEY
, this is used locally and never broadcast or exposed in any way.
Higher Order Contracts
There are classes present for exposing the A.P.I (ABI) of deployed Swivel smart contracts in a pyhtonic way. Primarily
- swivel.py
- market_place.py
will be your places of contact, as shown in Scrivel. There is a contract present for the VaultTracker which, while typically used internally by the MarketPlace, can be used in isolation to check balances.
The H.O.Cs are constructed with a Vendor instance, then connected to a deployment via the at
method. Once established,
the contract is ready to be used.
Opts
Note that all H.O.C method signatures match the exposed signatures of Swivel smart contract methods. There is also,
in every case, an optional opts
keyword that may be passed. This argument can be omitted in call
(read) type
operations generally, but in send
(write) type operations the user should hydrate important values such as
gas, gasPrice etc...
Call, Send (and Transact)
Another principal concept to grasp when using Swivel Higher Order Contracts is that the exposed methods do not
attempt an actual low level call
or send
(read and write) type operation. Instead, a tuple of length 2
is always returned from any method invocation. The 2nd item in the tuple is always the opts
dictionary (or None).
Operations that are send
(write) type will always assure that the from
key is set with the users address. Any
other important values (gas, gasPrice, etc...) should be set by the user by using the opts
dictionary
as described above.
The first item of the tuple will be, depending on the type (call, send), a callable or transactable object.
Callable objects may be wrapped in the call
helper to execute. Public constants and variables as well as any
non-state-changing getters fall under this category. Swivel calls for example. Transactable objects
are, intentionally, handled differently and are the reason why Swivel.py returns a tuple from contract method invocations.
State changing send type operations require a signature, unless the account being used in unlocked. You are likely
not working with unlocked accounts (except perhaps if you are hosting your own node and have your account unlocked locally, see below).
Swivel.py exposes methods on the Vendor class (build_transaction, sign_transaction, etc...) to facilitate signing
offline (given you have exported the correct private key to your shell). The convenience method send
builds, signs
and broadcasts an offline transaction. See here for
some examples.
In the case you are working with unlocked accounts you can use the transact
helper directly as there is no need to sign
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
File details
Details for the file swivel-2.2.0.tar.gz
.
File metadata
- Download URL: swivel-2.2.0.tar.gz
- Upload date:
- Size: 36.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84d57f509f67f45b5419d57bf958e1add45e4702fa533fd35a7dacef1ef17303 |
|
MD5 | 82c73f505292a19080e64eb1c56c0134 |
|
BLAKE2b-256 | 388fe23714f9741402c2bb3beeee8e8c8d37ee0a1e81f7f0e4a3341d8e5c1503 |
File details
Details for the file swivel-2.2.0-py3-none-any.whl
.
File metadata
- Download URL: swivel-2.2.0-py3-none-any.whl
- Upload date:
- Size: 40.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6dd655d90f00c73fb50ac4e6fbb826449b5f05dc7400aea3e9eac660c965b00c |
|
MD5 | 5f355a9f1596afd5364d83551d210df2 |
|
BLAKE2b-256 | 5e0015ea7ae30ec07454c811dc12aaf03fb4189bafb78008e00ae303cd6b4a37 |