Implementing Smart Contracts Using Vyper — A Python Approach | by Abhishek Chauhan | Mar, 2022

photo by — Jan Kopřiva
  1. Introduction
  2. Setting Environment
  3. Creating a smart contract using Vyper
  4. Deploying a smart contract to Ganache
  5. Interacting with smart contracts
  6. Interacting with other smart contracts
  7. Compiling code programmatically

What is Vyper?

Why Vyper?

  • Security: It should be possible and natural to build secure smart contracts in Vyper.
  • Language and compiler simplicity: The language and the compiler implementation should strive to be simple.
  • Auditability: Vyper code should be maximally human-readable. Furthermore, it should be maximally difficult to write a misleading code. Simplicity for the reader is more important than simplicity for the writer, and simplicity for readers with low prior experience with Vyper (and low prior experience with programming in general) is particularly important.
  • Bounds and overflow checking: On array accesses and arithmetic.
  • Support for signed integers and decimal fixed-point numbers
  • Decidability: It is possible to compute a precise upper bound for the gas consumption of any Vyper function call.
  • Strong typing
  • Small and understandable compiler code
  • Limited support for pure functions: Anything marked constant is not allowed to change the state.
$ python3 -m pip install --user virtualenv
$ pip install vyper
$ virtualenv -p python3.6 vyper-venv
$ source vyper-venv/bin/activate
(vyper-venv) $ pip install vyper
(vyper-venv) $ vyper --version
hello.vy
(vyper-venv) $ vyper hello.vy
abi
(vyper-venv) $ vyper -f json hello.vy
json
(vyper-venv) $ vyper -f json,bytecode hello.vy
abi, json
  1. Create a directory and initialize it with truffle init as follows:
$ mkdir hello_project
$ cd hello_project
$ truffle init
module.exports = { 
networks: {
"development": {
network_id: "*",
host: "127.0.0.1",
port: 8545 // port at Ganache running
},
}
};
$ mkdir -p build/contracts
$ cd build/contracts
{
“abi”:
”bytecode”:
}
var Hello = artifacts.require("Hello");
module.exports = function(deployer){
deployer.deploy(Hello);
};
$ truffle migrate
truffle output
0x4AB3935Df0E224771663984f3617F1a78beA4E8D
$ truffle console
truffle(development)> Contract.at("0x4AB3935Df0E224771663984f3617F1a78beA4E8D")
truffle output
truffle(development)> Contract.at(“0x4AB3935Df0E224771663984f3617F1a78beA4E8D”).then(function(instance){return instance.name.call(); });'Satoshi Nakamoto'
truffle(development)> Contract.at(“0x4AB3935Df0E224771663984f3617F1a78beA4E8D”).then(function(instance) { return instance.change_name(“Vitalik Buterin”), { from: “0xb28Fc17791bf66719FBFCc65846B01Fe2726e9E2” } });
{ from: ‘0xb28Fc17791bf66719FBFCc65846B01Fe2726e9E2’ }
truffle(development)> Contract.at(“0x4AB3935Df0E224771663984f3617F1a78beA4E8D”).then(function(instance){return instance.name.call(); });‘Vitalik Buterin’
var Hello = artifacts.require("Hello");
module.exports = function(deployer){
deployer.deploy(Hello);
};
{
"contractName": "Hello",
"abi": <your Hello smart contract's interface>,
"bytecode": "<your Hello smart contract's bytecode>"
}
donation.vy
var Donation = artifacts.require("Donation");
module.exports = function(deployer) {
deployer.deploy(Donation);
};
{
"contractName": "Donation",
"abi": <your Donation smart contract's interface>,
"bytecode": "<your Donation smart contract's bytecode>"
}
$ truffle migrate --reset
truffle output
$ truffle console
truffle(development)> Donation.at(“0x25aFF89B8a72aB4Ba6F4C831f5B1375f9BCe76A9”).then(function(instance) { return instance.donation_smart_contract_call_hello_smart_contract_method.call(“0x772138489eD34095FBA6a0Af70d3C9115813CFcA”); } );
‘0x48656c6c6f2c205361746f736869204e616b616d6f746f’
compiler.vy
(vyper-venv) $ python compiler.vy

Leave a Comment