Python SDK
#
Prerequisite: Sign up for an MaticVigil developer accountTo use the MaticVigil Python SDK, you need to be signed up for a developer account on https://mainnet.maticvigil.com
You can choose any of the following approaches.
mv-cli
(Recommended )#
Sign up via the CLI tool Follow the instructions sequentially from these steps detailed in the links below.
Install the CLI tool
Generate a new MaticVigil invite code and complete signup on CLI
This approach allows you to have control on your MaticVigil developer account from your command line itself without having to switch to the Web UI.
Once you have completed this section, head to Quickstart.
#
Sign up on the web UIOnce you have signed up on the web UI
- you have to manually export your settings from the dashboard
- To copy the downloaded file into
~/.maticvigil/settings.json
run the following commands in your terminal
Note: The
~
in the above file path denotes your home directory on Linux/MacOS
#
Quickstart#
Install MaticVigil Python SDKChoose any of the following approaches.
Either of them should install a python module maticvigil
pip install
from Github repo#
pip install git+https://github.com/blockvigil/maticvigil-python-sdk.git
Do a localpip install
aftergit clone
#
#
Test importLaunch python in interactive mode and test if you can import the module. if you do not see any ImportError
or other exceptions, you would want to move on to exploring the SDK further.
#
Initialize MaticVigil API instanceYou can find the following usage examples in the examples/
directory as well.
EVCore
#
If you configure the logging module appropriately, passing verbose=True
to the above initialization will begin printing debug information on to the console.
Here is an example:
#
Deploy a contractFind the microblog.sol
Solidity smart contract in the examples
directory of the SDK github repo.
The contract will be deployed at the address displayed above.
#
Accessing previously deployed contractsYou can access operations on a contract previously deployed through MaticVigil by calling the generate_contract_sdk()
function on the EVCore
instance.
All the functions defined in the Solidity smart contract can be accessed by the same names against this contract instance. Read on to find out how.
#
Reading from a contractWe will be using the contract instance from the above example.
Example source code: examples/contract_read.py
#
Writing to a contract#
Changing the microblog titleExample source code: examples/contract_write_changeBlogTitle.py
#
Adding a new postExample source code: examples/contract_write_addPost.py
Transactions that change the state of a smart contract take anywhere between 5-15 seconds to get confirmed on the blockchain.
This makes it necessary to build an asynchronous pattern for your blockchain applications. Let us take a look at it in the next section.
#
Webhook integrations: Receive JSON payloads from events and other contract activityMaticVigil does the hard work of monitoring your contracts, transactions on them and events that may be emitted by transactions triggering certain logic.
You can use these updates to enhance your blockchain applications and execute further logic "off-chain". Connect to multiple services like recording a new entry on Airtable, create a new Google Doc, executing IFTTT recipes; the possibilities are literally endless.
#
Step 1: Set up a publicly available URL to which MaticVigil can push these updates.For this example, we shall use ngrok
to setup a publicly available URL in the format https://<randomstring>.ngrok.com
. The tool receives HTTP payloads on this public URL and forwards them to a local HTTP server (running on your computer).
Image Credit: Ngrok.com
Technically this is known as HTTP tunnelling but let's not worry about semantics right now.
Follow the instructions on ngrok.com to install and setup the tool.
Then run it on the terminal with the following command.
./ngrok http 8044
This will forward HTTP requests to a local server running on port 8044
. In the next step, we fire up this local server.
#
Step 2: Run the local HTTP serverWe have included the code for this in examples/sample_webhook_listener.py
. Open a new tab/window in your terminal and run it
python sample_webhook_listener.py
This starts a tornado server running on port 8044. The example code logs every received payload on the terminal.
ngrok
URL with MaticVigil to receive updates#
Step 3: Register the public Example code: examples/webhook_integrations.py
Copy the https
URL against the Forwarding information from the running ngrok
console. It will look something like this:
Continuing from the microblog.sol
example, let us attempt to receive an update the NewPost
event that is emitted when you call addPost()
on the contract instance.
addPost()
and observe the local HTTP server#
Step 4: Call Apart from the terminal where ngrok
is running, open up two more new terminal tabs or windows.
- Run the
examples/sample_webhook_listener.py
file - In another one, run the following code snippet as demonstrated in the section Writing to a contract
This should generate a transaction to the contract. Shift to the terminal where sample_webhook_listener.py
is running.
You should see the NewPost
event being delivered to the script.