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

photograph by — Jan Kopřiva
  1. Introduction
  2. Setting Surroundings
  3. Creating a sensible contract utilizing Vyper
  4. Deploying a sensible contract to Ganache
  5. Interacting with good contracts
  6. Interacting with different good contracts
  7. Compiling code programmatically

What’s Vyper?

Why Vyper?

  • Safety: It must be potential and pure to construct safe good contracts in Vyper.
  • Language and compiler simplicity: The language and the compiler implementation ought to try to be easy.
  • Auditability: Vyper code must be maximally human-readable. Moreover, it must be maximally tough to jot down deceptive code. Simplicity for the reader is extra vital than simplicity for the author, and ease for readers with low prior expertise with Vyper (and low prior expertise with programming usually) is especially vital.
  • Bounds and overflow checking: On array accesses and arithmetic.
  • Help for signed integers and decimal fixed-point numbers
  • Decidability: It’s potential to compute a exact higher certain for the gasoline consumption of any Vyper operate name.
  • Robust typing
  • Small and comprehensible compiler code
  • Restricted assist for pure capabilities: Something marked fixed is just not allowed to alter the state.
$ python3 -m pip set up --user virtualenv
$ pip set up vyper
$ virtualenv -p python3.6 vyper-venv
$ supply vyper-venv/bin/activate
(vyper-venv) $ pip set up vyper
(vyper-venv) $ vyper --version
whats up.vy
(vyper-venv) $ vyper whats up.vy
abi
(vyper-venv) $ vyper -f json whats up.vy
json
(vyper-venv) $ vyper -f json,bytecode whats up.vy
abi, json
  1. Create a listing and initialize it with truffle init as follows:
$ mkdir hello_project
$ cd hello_project
$ truffle init
module.exports =  
networks:
"improvement":
network_id: "*",
host: "127.0.0.1",
port: 8545 // port at Ganache working
,

;
$ mkdir -p construct/contracts
$ cd construct/contracts

“abi”:
”bytecode”:
var Hiya = artifacts.require("Hiya");
module.exports = operate(deployer)
deployer.deploy(Hiya);
;
$ truffle migrate
truffle output
0x4AB3935Df0E224771663984f3617F1a78beA4E8D
$ truffle console
truffle(improvement)> Contract.at("0x4AB3935Df0E224771663984f3617F1a78beA4E8D")
truffle output
truffle(improvement)> Contract.at(“0x4AB3935Df0E224771663984f3617F1a78beA4E8D”).then(operate(occasion)return occasion.title.name(); );'Satoshi Nakamoto'
truffle(improvement)> Contract.at(“0x4AB3935Df0E224771663984f3617F1a78beA4E8D”).then(operate(occasion)  return occasion.change_name(“Vitalik Buterin”),  from: “0xb28Fc17791bf66719FBFCc65846B01Fe2726e9E2”  );
 from: ‘0xb28Fc17791bf66719FBFCc65846B01Fe2726e9E2’ 
truffle(improvement)> Contract.at(“0x4AB3935Df0E224771663984f3617F1a78beA4E8D”).then(operate(occasion)return occasion.title.name(); );‘Vitalik Buterin’
var Hiya = artifacts.require("Hiya");
module.exports = operate(deployer)
deployer.deploy(Hiya);
;

"contractName": "Hiya",
"abi": <your Hiya good contract's interface>,
"bytecode": "<your Hiya good contract's bytecode>"
donation.vy
var Donation = artifacts.require("Donation");
module.exports = operate(deployer)
deployer.deploy(Donation);
;

"contractName": "Donation",
"abi": <your Donation good contract's interface>,
"bytecode": "<your Donation good contract's bytecode>"
$ truffle migrate --reset
truffle output
$ truffle console
truffle(improvement)> Donation.at(“0x25aFF89B8a72aB4Ba6F4C831f5B1375f9BCe76A9”).then(operate(occasion)  return occasion.donation_smart_contract_call_hello_smart_contract_method.name(“0x772138489eD34095FBA6a0Af70d3C9115813CFcA”);  );
‘0x48656c6c6f2c205361746f736869204e616b616d6f746f’
compiler.vy
(vyper-venv) $ python compiler.vy

More Posts