My Image
CoursesQuizzesProblemsContestsSmartBooks
Contest!

No results found

LOGINREGISTER
My ProgressCoursesQuizzesProblemsContestsSmartbooks
Published on 7 Nov 2021
Transfer ownership of a Smart Contract
Write a contract where Contract Ownership can be transferred.
img
Shweta Darekar
0
Like
843

Basic smart contract to transfer ownership of the smart contract.

  • Declare compiler version:

    pragma solidity 0.8.2;
  • Create contract :

    pragma solidity 0.8.2;
    
    contract MyContract{
    
    }
  • Constructor :

    Declare a state variable Owner which will be the address type. Create a constructor and set deployer as a default owner.

    pragma solidity 0.8.2;
    
    contract MyContract{
           
             address public Owner;
              constructor(){
                 Owner = msg.sender;
             }
    
    }

       "msg.sender" will be the person who currently connecting with the contract or sender of the message. 

  •   Create function transferOwnership() :

    pragma solidity 0.8.2;
      
    contract MyContract{
          address public Owner;
             constructor(){
                 Owner = msg.sender;
             }
    
         function transferOwnership(address _newOwner) public {
                  require(msg.sender == Owner);
                  Owner = _newOwner;  
              }
    }

    This function will get address of new owner as a parameter. require statement allows us to verify that only exisiting owner can call this function. 

Enjoyed the SmartBook?
Like
logo
contact@dapp-world.com
Katraj, Pune, Maharashtra, India - 411048

Follow Us

linkedintwitteryoutubediscordinstagram

Products

  • SmartBooks
  • Courses
  • Quizzes
  • Assessments

Support

  • Contact Us
  • FAQ
  • Privacy Policy
  • T&C

Backed By

ah! ventures

Copyright 2023 - All Rights Reserved.

Recommended from DAppWorld
img
1 May 2021
How to connect Ganache with Metamask and deploy Smart contracts on remix without
Set up your development environment with (Metamask + Ganache + Remix) and skip truffle :)
3 min read
11494
5
img
8 Jul 2021
How to interact with smart contarct from backend node js
call and send functions from backend server side using nodejs
3 min read
8069
2
img
18 Aug 2021
Send transaction with web3 using python
Introduction to web3.py and sending transaction on testnet
3 min read
6215
5
img
5 Aug 2021
Deploy Smart Contract on Polygon POS using Hardhat
how to deploy smart contracts on polygon pos chain using hardhat both mainnet and testnet ?
3 min read
5533
3