kandi background
Explore Kits

solidity | Solidity , the Smart Contract Programming Language | Blockchain library

 by   ethereum C++ Version: v0.8.13 License: GPL-3.0

 by   ethereum C++ Version: v0.8.13 License: GPL-3.0

Download this library from

kandi X-RAY | solidity Summary

solidity is a C++ library typically used in Blockchain, Ethereum applications. solidity has no bugs, it has no vulnerabilities, it has a Strong Copyleft License and it has medium support. You can download it from GitHub.
You can talk to us on Gitter and Matrix, tweet at us on Twitter or create a new topic in the Solidity forum. Questions, feedback, and suggestions are welcome!. Solidity is a statically typed, contract-oriented, high-level language for implementing smart contracts on the Ethereum platform. For a good overview and starting point, please check out the official Solidity Language Portal.
Support
Support
Quality
Quality
Security
Security
License
License
Reuse
Reuse

kandi-support Support

  • solidity has a medium active ecosystem.
  • It has 16189 star(s) with 3878 fork(s). There are 616 watchers for this library.
  • There were 3 major release(s) in the last 12 months.
  • There are 953 open issues and 4067 have been closed. On average issues are closed in 82 days. There are 84 open pull requests and 0 closed requests.
  • It has a neutral sentiment in the developer community.
  • The latest version of solidity is v0.8.13
solidity Support
Best in #Blockchain
Average in #Blockchain
solidity Support
Best in #Blockchain
Average in #Blockchain

quality kandi Quality

  • solidity has 0 bugs and 0 code smells.
solidity Quality
Best in #Blockchain
Average in #Blockchain
solidity Quality
Best in #Blockchain
Average in #Blockchain

securitySecurity

  • solidity has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
  • solidity code analysis shows 0 unresolved vulnerabilities.
  • There are 0 security hotspots that need review.
solidity Security
Best in #Blockchain
Average in #Blockchain
solidity Security
Best in #Blockchain
Average in #Blockchain

license License

  • solidity is licensed under the GPL-3.0 License. This license is Strong Copyleft.
  • Strong Copyleft licenses enforce sharing, and you can use them when creating open source projects.
solidity License
Best in #Blockchain
Average in #Blockchain
solidity License
Best in #Blockchain
Average in #Blockchain

buildReuse

  • solidity releases are available to install and integrate.
  • Installation instructions, examples and code snippets are available.
  • It has 4772 lines of code, 240 functions and 72 files.
  • It has high code complexity. Code complexity directly impacts maintainability of the code.
solidity Reuse
Best in #Blockchain
Average in #Blockchain
solidity Reuse
Best in #Blockchain
Average in #Blockchain
Top functions reviewed by kandi - BETA

kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample Here

Get all kandi verified functions for this library.

Get all kandi verified functions for this library.

solidity Key Features

Solidity, the Smart Contract Programming Language

Example

copy iconCopydownload iconDownload
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0;

contract HelloWorld {
    function helloWorld() external pure returns (string memory) {
        return "Hello, World!";
    }
}

What is the best practice of copying from array to array in Solidity?

copy iconCopydownload iconDownload
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

contract TestLoop {
    uint32[4] testArray;

    function setArrayWithLoop(uint32[4] memory array) public {
        for(uint256 i = 0; i < array.length; i++)
            testArray[i] = array[i];
    }

    function setArrayWithoutLoop(uint32[4] memory array) public {
        testArray = array;
    }

    function show() public view returns (uint32[4] memory) {
        return testArray;
    }
}

contract NoLoop {
    uint32[4] testArray;

    constructor(uint32[4] memory array) {
        testArray = array;
    }

    function show() public view returns (uint32[4] memory) {
        return testArray;
    }
}

contract Loop {
    uint32[4] testArray;

    constructor (uint32[4] memory array) {
        for(uint256 i = 0; i < array.length; i++)
            testArray[i] = array[i];
    }

    function show() public view returns (uint32[4] memory) {
        return testArray;
    }
}
from brownie import TestLoop, NoLoop, Loop, accounts

def function_calls():
    contract = TestLoop.deploy({'from': accounts[0]})
    print('set array in loop')
    contract.setArrayWithLoop([1, 2, 3, 4], {'from': accounts[1]})
    print('array ', contract.show(), '\n\n')

    print('set array by copy from memory to storage')
    contract.setArrayWithoutLoop([10, 9, 8, 7], {'from': accounts[2]})
    print('array ', contract.show(), '\n\n')

def deploy_no_loop():
    print('deploy NoLoop contract')
    contract = NoLoop.deploy([21, 22, 23, 24], {'from': accounts[3]})
    print('array ', contract.show(), '\n\n')

def deploy_loop():
    print('deploy Loop contract')
    contract = Loop.deploy([31, 32, 33, 34], {'from': accounts[3]})
    print('array ', contract.show(), '\n\n')

def main():
    function_calls()
    deploy_no_loop()
    deploy_loop()
compiler:
  solc:
    version: 0.8.13
    optimizer:
      enabled: true
      runs: 1
Running 'scripts/test_loop.py::main'...
Transaction sent: 0x8380ef4abff179f08ba9704826fc44961d212e5ee10952ed3904b5ec7828c928
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  TestLoop.constructor confirmed   Block: 1   Gas used: 251810 (2.10%)
  TestLoop deployed at: 0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87

set array in loop
Transaction sent: 0xfe72d6c878a980a9eeefee1dccdd0fe8214ee4772ab68ff0ac2b72708b7ab946
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  TestLoop.setArrayWithLoop confirmed   Block: 2   Gas used: 49454 (0.41%)

array  (1, 2, 3, 4) 


set array by copy from memory to storage
Transaction sent: 0x0106d1a7e37b155993a6d32d5cc9dc67696a55acd1cf29d2ed9dba0770436b98
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  TestLoop.setArrayWithoutLoop confirmed   Block: 3   Gas used: 41283 (0.34%)

array  (10, 9, 8, 7) 


deploy NoLoop contract
Transaction sent: 0x55ddded68300bb8f11b3b43580c58fed3431a2823bf3f82f0081c7bfce66f34d
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  NoLoop.constructor confirmed   Block: 4   Gas used: 160753 (1.34%)
  NoLoop deployed at: 0x7CA3dB74F7b6cd8D6Db1D34dEc2eA3c89a3417ec

array  (21, 22, 23, 24) 


deploy Loop contract
Transaction sent: 0x1aa64f2cd527983df84cfdca5cfd7a281ff904cca227629ec8b0b29db561c043
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 1
  Loop.constructor confirmed   Block: 5   Gas used: 153692 (1.28%)
  Loop deployed at: 0x2fb0fE4F05B7C8576F60A5BEEE35c23632Dc0C27

array  (31, 32, 33, 34)
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

contract TestLoop {
    uint32[4] testArray;

    function setArrayWithLoop(uint32[4] memory array) public {
        for(uint256 i = 0; i < array.length; i++)
            testArray[i] = array[i];
    }

    function setArrayWithoutLoop(uint32[4] memory array) public {
        testArray = array;
    }

    function show() public view returns (uint32[4] memory) {
        return testArray;
    }
}

contract NoLoop {
    uint32[4] testArray;

    constructor(uint32[4] memory array) {
        testArray = array;
    }

    function show() public view returns (uint32[4] memory) {
        return testArray;
    }
}

contract Loop {
    uint32[4] testArray;

    constructor (uint32[4] memory array) {
        for(uint256 i = 0; i < array.length; i++)
            testArray[i] = array[i];
    }

    function show() public view returns (uint32[4] memory) {
        return testArray;
    }
}
from brownie import TestLoop, NoLoop, Loop, accounts

def function_calls():
    contract = TestLoop.deploy({'from': accounts[0]})
    print('set array in loop')
    contract.setArrayWithLoop([1, 2, 3, 4], {'from': accounts[1]})
    print('array ', contract.show(), '\n\n')

    print('set array by copy from memory to storage')
    contract.setArrayWithoutLoop([10, 9, 8, 7], {'from': accounts[2]})
    print('array ', contract.show(), '\n\n')

def deploy_no_loop():
    print('deploy NoLoop contract')
    contract = NoLoop.deploy([21, 22, 23, 24], {'from': accounts[3]})
    print('array ', contract.show(), '\n\n')

def deploy_loop():
    print('deploy Loop contract')
    contract = Loop.deploy([31, 32, 33, 34], {'from': accounts[3]})
    print('array ', contract.show(), '\n\n')

def main():
    function_calls()
    deploy_no_loop()
    deploy_loop()
compiler:
  solc:
    version: 0.8.13
    optimizer:
      enabled: true
      runs: 1
Running 'scripts/test_loop.py::main'...
Transaction sent: 0x8380ef4abff179f08ba9704826fc44961d212e5ee10952ed3904b5ec7828c928
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  TestLoop.constructor confirmed   Block: 1   Gas used: 251810 (2.10%)
  TestLoop deployed at: 0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87

set array in loop
Transaction sent: 0xfe72d6c878a980a9eeefee1dccdd0fe8214ee4772ab68ff0ac2b72708b7ab946
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  TestLoop.setArrayWithLoop confirmed   Block: 2   Gas used: 49454 (0.41%)

array  (1, 2, 3, 4) 


set array by copy from memory to storage
Transaction sent: 0x0106d1a7e37b155993a6d32d5cc9dc67696a55acd1cf29d2ed9dba0770436b98
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  TestLoop.setArrayWithoutLoop confirmed   Block: 3   Gas used: 41283 (0.34%)

array  (10, 9, 8, 7) 


deploy NoLoop contract
Transaction sent: 0x55ddded68300bb8f11b3b43580c58fed3431a2823bf3f82f0081c7bfce66f34d
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  NoLoop.constructor confirmed   Block: 4   Gas used: 160753 (1.34%)
  NoLoop deployed at: 0x7CA3dB74F7b6cd8D6Db1D34dEc2eA3c89a3417ec

array  (21, 22, 23, 24) 


deploy Loop contract
Transaction sent: 0x1aa64f2cd527983df84cfdca5cfd7a281ff904cca227629ec8b0b29db561c043
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 1
  Loop.constructor confirmed   Block: 5   Gas used: 153692 (1.28%)
  Loop deployed at: 0x2fb0fE4F05B7C8576F60A5BEEE35c23632Dc0C27

array  (31, 32, 33, 34)
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

contract TestLoop {
    uint32[4] testArray;

    function setArrayWithLoop(uint32[4] memory array) public {
        for(uint256 i = 0; i < array.length; i++)
            testArray[i] = array[i];
    }

    function setArrayWithoutLoop(uint32[4] memory array) public {
        testArray = array;
    }

    function show() public view returns (uint32[4] memory) {
        return testArray;
    }
}

contract NoLoop {
    uint32[4] testArray;

    constructor(uint32[4] memory array) {
        testArray = array;
    }

    function show() public view returns (uint32[4] memory) {
        return testArray;
    }
}

contract Loop {
    uint32[4] testArray;

    constructor (uint32[4] memory array) {
        for(uint256 i = 0; i < array.length; i++)
            testArray[i] = array[i];
    }

    function show() public view returns (uint32[4] memory) {
        return testArray;
    }
}
from brownie import TestLoop, NoLoop, Loop, accounts

def function_calls():
    contract = TestLoop.deploy({'from': accounts[0]})
    print('set array in loop')
    contract.setArrayWithLoop([1, 2, 3, 4], {'from': accounts[1]})
    print('array ', contract.show(), '\n\n')

    print('set array by copy from memory to storage')
    contract.setArrayWithoutLoop([10, 9, 8, 7], {'from': accounts[2]})
    print('array ', contract.show(), '\n\n')

def deploy_no_loop():
    print('deploy NoLoop contract')
    contract = NoLoop.deploy([21, 22, 23, 24], {'from': accounts[3]})
    print('array ', contract.show(), '\n\n')

def deploy_loop():
    print('deploy Loop contract')
    contract = Loop.deploy([31, 32, 33, 34], {'from': accounts[3]})
    print('array ', contract.show(), '\n\n')

def main():
    function_calls()
    deploy_no_loop()
    deploy_loop()
compiler:
  solc:
    version: 0.8.13
    optimizer:
      enabled: true
      runs: 1
Running 'scripts/test_loop.py::main'...
Transaction sent: 0x8380ef4abff179f08ba9704826fc44961d212e5ee10952ed3904b5ec7828c928
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  TestLoop.constructor confirmed   Block: 1   Gas used: 251810 (2.10%)
  TestLoop deployed at: 0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87

set array in loop
Transaction sent: 0xfe72d6c878a980a9eeefee1dccdd0fe8214ee4772ab68ff0ac2b72708b7ab946
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  TestLoop.setArrayWithLoop confirmed   Block: 2   Gas used: 49454 (0.41%)

array  (1, 2, 3, 4) 


set array by copy from memory to storage
Transaction sent: 0x0106d1a7e37b155993a6d32d5cc9dc67696a55acd1cf29d2ed9dba0770436b98
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  TestLoop.setArrayWithoutLoop confirmed   Block: 3   Gas used: 41283 (0.34%)

array  (10, 9, 8, 7) 


deploy NoLoop contract
Transaction sent: 0x55ddded68300bb8f11b3b43580c58fed3431a2823bf3f82f0081c7bfce66f34d
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  NoLoop.constructor confirmed   Block: 4   Gas used: 160753 (1.34%)
  NoLoop deployed at: 0x7CA3dB74F7b6cd8D6Db1D34dEc2eA3c89a3417ec

array  (21, 22, 23, 24) 


deploy Loop contract
Transaction sent: 0x1aa64f2cd527983df84cfdca5cfd7a281ff904cca227629ec8b0b29db561c043
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 1
  Loop.constructor confirmed   Block: 5   Gas used: 153692 (1.28%)
  Loop deployed at: 0x2fb0fE4F05B7C8576F60A5BEEE35c23632Dc0C27

array  (31, 32, 33, 34)
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

contract TestLoop {
    uint32[4] testArray;

    function setArrayWithLoop(uint32[4] memory array) public {
        for(uint256 i = 0; i < array.length; i++)
            testArray[i] = array[i];
    }

    function setArrayWithoutLoop(uint32[4] memory array) public {
        testArray = array;
    }

    function show() public view returns (uint32[4] memory) {
        return testArray;
    }
}

contract NoLoop {
    uint32[4] testArray;

    constructor(uint32[4] memory array) {
        testArray = array;
    }

    function show() public view returns (uint32[4] memory) {
        return testArray;
    }
}

contract Loop {
    uint32[4] testArray;

    constructor (uint32[4] memory array) {
        for(uint256 i = 0; i < array.length; i++)
            testArray[i] = array[i];
    }

    function show() public view returns (uint32[4] memory) {
        return testArray;
    }
}
from brownie import TestLoop, NoLoop, Loop, accounts

def function_calls():
    contract = TestLoop.deploy({'from': accounts[0]})
    print('set array in loop')
    contract.setArrayWithLoop([1, 2, 3, 4], {'from': accounts[1]})
    print('array ', contract.show(), '\n\n')

    print('set array by copy from memory to storage')
    contract.setArrayWithoutLoop([10, 9, 8, 7], {'from': accounts[2]})
    print('array ', contract.show(), '\n\n')

def deploy_no_loop():
    print('deploy NoLoop contract')
    contract = NoLoop.deploy([21, 22, 23, 24], {'from': accounts[3]})
    print('array ', contract.show(), '\n\n')

def deploy_loop():
    print('deploy Loop contract')
    contract = Loop.deploy([31, 32, 33, 34], {'from': accounts[3]})
    print('array ', contract.show(), '\n\n')

def main():
    function_calls()
    deploy_no_loop()
    deploy_loop()
compiler:
  solc:
    version: 0.8.13
    optimizer:
      enabled: true
      runs: 1
Running 'scripts/test_loop.py::main'...
Transaction sent: 0x8380ef4abff179f08ba9704826fc44961d212e5ee10952ed3904b5ec7828c928
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  TestLoop.constructor confirmed   Block: 1   Gas used: 251810 (2.10%)
  TestLoop deployed at: 0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87

set array in loop
Transaction sent: 0xfe72d6c878a980a9eeefee1dccdd0fe8214ee4772ab68ff0ac2b72708b7ab946
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  TestLoop.setArrayWithLoop confirmed   Block: 2   Gas used: 49454 (0.41%)

array  (1, 2, 3, 4) 


set array by copy from memory to storage
Transaction sent: 0x0106d1a7e37b155993a6d32d5cc9dc67696a55acd1cf29d2ed9dba0770436b98
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  TestLoop.setArrayWithoutLoop confirmed   Block: 3   Gas used: 41283 (0.34%)

array  (10, 9, 8, 7) 


deploy NoLoop contract
Transaction sent: 0x55ddded68300bb8f11b3b43580c58fed3431a2823bf3f82f0081c7bfce66f34d
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  NoLoop.constructor confirmed   Block: 4   Gas used: 160753 (1.34%)
  NoLoop deployed at: 0x7CA3dB74F7b6cd8D6Db1D34dEc2eA3c89a3417ec

array  (21, 22, 23, 24) 


deploy Loop contract
Transaction sent: 0x1aa64f2cd527983df84cfdca5cfd7a281ff904cca227629ec8b0b29db561c043
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 1
  Loop.constructor confirmed   Block: 5   Gas used: 153692 (1.28%)
  Loop deployed at: 0x2fb0fE4F05B7C8576F60A5BEEE35c23632Dc0C27

array  (31, 32, 33, 34)

PIP failed to build package cytoolz

copy iconCopydownload iconDownload
python -m pip install --user cython
python -m pip install --user cytoolz
python -m pip install --user eth-brownie
STEP1: python -m pip install --user cython
STEP2: python -m pip install --user cytoolz
STEP3: python -m pip install --user eth-brownie
STEP4: python -m pip install --user pipx
STEP5: python -m pipx ensurepath
STEP6: RESTARTED TERMINAL
STEP7: pipx install eth-brownie

Declaration Error: Undeclared identifier Solidity

copy iconCopydownload iconDownload
mapping(address => Person) mapPerson;

mapping(address => Company) mapCompany;
struct Person{
    address addr;
    string name;
    string surname;
}
mapping(address => Person) mapPerson;
mapping(address => Person) mapPerson;

mapping(address => Company) mapCompany;
struct Person{
    address addr;
    string name;
    string surname;
}
mapping(address => Person) mapPerson;
contract Lab03 is Interface{
    // Add here the mappings and remove them from the struct
    mapping(address => Person) mapPerson;
    mapping(address => Company) mapCompany;

    Person p;
    Company c;
 ...
//SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.7.0;

struct Person{
    address addr;
    string name;
    string surname;
}

struct Company{
    address addr;
    string name;
}


interface Interface{

    function addPerson(address addr, string memory name, string memory surname) external;
    
    function addCompany(address addr, string memory name) external;

    function getPerson(address addrFind) external view returns (address addr);

    function getCompany(address addrFind) external view returns (address addr);
}
    

library Lib{
    // define struct with mapping
    struct LibPerson {
        mapping(address => Person) mapPerson;
    }

    struct LibCompany {
        mapping(address => Company) mapCompany;
    }

    function addPerson(LibPerson storage lp, address addr, string memory name, string memory surname) public{
        addr = msg.sender;
        lp.mapPerson[addr] = Person(addr, name, surname);
    }

    function addCompany(LibCompany storage lc, address addr, string memory name) public{
        addr = msg.sender;
        lc.mapCompany[addr] = Company(addr, name);
    }    

    function getCompany(LibCompany storage lc, address addrFind) view external returns(address){
        return lc.mapCompany[addrFind].addr;
    }   

    function getPerson(LibPerson storage lp, address addrFind) view external returns(address){
        return lp.mapPerson[addrFind].addr;
    }     
}

contract Lab03 is Interface{
    // define state variable for two struct present in the library
    Lib.LibPerson libP;
    Lib.LibCompany libC;
    Person p;
    Company c;

    function addPerson(address addr, string memory name, string memory surname) public override {
        Lib.addPerson(libP, addr, name, surname);
        emit addPersonEvent(addr, name, surname);
    }

    function addCompany(address addr, string memory name) public override{
        Lib.addCompany(libC, addr, name);
        emit addCompanyEvent(addr, name);
    }

    function getPerson(address _addressFind) public override view returns (address addr) {
        return Lib.getPerson(libP, _addressFind);
    }

    function getCompany(address _addressFind) public override view returns (address addr) {
        return Lib.getCompany(libC, _addressFind);
    }

    event addPersonEvent(address addr, string name, string surname);
    event addCompanyEvent(address addr, string name);
}
contract Lab03 is Interface{
    // Add here the mappings and remove them from the struct
    mapping(address => Person) mapPerson;
    mapping(address => Company) mapCompany;

    Person p;
    Company c;
 ...
//SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.7.0;

struct Person{
    address addr;
    string name;
    string surname;
}

struct Company{
    address addr;
    string name;
}


interface Interface{

    function addPerson(address addr, string memory name, string memory surname) external;
    
    function addCompany(address addr, string memory name) external;

    function getPerson(address addrFind) external view returns (address addr);

    function getCompany(address addrFind) external view returns (address addr);
}
    

library Lib{
    // define struct with mapping
    struct LibPerson {
        mapping(address => Person) mapPerson;
    }

    struct LibCompany {
        mapping(address => Company) mapCompany;
    }

    function addPerson(LibPerson storage lp, address addr, string memory name, string memory surname) public{
        addr = msg.sender;
        lp.mapPerson[addr] = Person(addr, name, surname);
    }

    function addCompany(LibCompany storage lc, address addr, string memory name) public{
        addr = msg.sender;
        lc.mapCompany[addr] = Company(addr, name);
    }    

    function getCompany(LibCompany storage lc, address addrFind) view external returns(address){
        return lc.mapCompany[addrFind].addr;
    }   

    function getPerson(LibPerson storage lp, address addrFind) view external returns(address){
        return lp.mapPerson[addrFind].addr;
    }     
}

contract Lab03 is Interface{
    // define state variable for two struct present in the library
    Lib.LibPerson libP;
    Lib.LibCompany libC;
    Person p;
    Company c;

    function addPerson(address addr, string memory name, string memory surname) public override {
        Lib.addPerson(libP, addr, name, surname);
        emit addPersonEvent(addr, name, surname);
    }

    function addCompany(address addr, string memory name) public override{
        Lib.addCompany(libC, addr, name);
        emit addCompanyEvent(addr, name);
    }

    function getPerson(address _addressFind) public override view returns (address addr) {
        return Lib.getPerson(libP, _addressFind);
    }

    function getCompany(address _addressFind) public override view returns (address addr) {
        return Lib.getCompany(libC, _addressFind);
    }

    event addPersonEvent(address addr, string name, string surname);
    event addCompanyEvent(address addr, string name);
}

The transaction declared chain ID 5777, but the connected node is on 1337

copy iconCopydownload iconDownload
transaction = 
 SimpleStorage.constructor().buildTransaction( {
    "gasPrice": w3.eth.gas_price, 
    "chainId": chain_id, 
    "from": my_address, 
    "nonce": nonce, 
})
print(transaction)
chain_id = 5777
 chain_id = 1377
chain_id = 5777
 chain_id = 1377
transaction = SimpleStorage.constructor().buildTransaction(
{
    "gasPrice": w3.eth.gas_price,
    "chainId": w3.eth.chain_id,
    "from": my_address,
    "nonce": nonce,
}

getting error when i deploy the NFT with ETH

copy iconCopydownload iconDownload
const { API_URL, PRIVATE_KEY } = process.env;
// ASSUMING you pass correct private key here
const PRIVATE_KEY  = process.env.PRIVATE_KEY;
const { API_URL, PRIVATE_KEY } = process.env;
// ASSUMING you pass correct private key here
const PRIVATE_KEY  = process.env.PRIVATE_KEY;

How to locally unit-test Chainlink's Verifiable Random Function?

copy iconCopydownload iconDownload
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";

contract MockVRFCoordinator {
    uint256 internal counter = 0;

    function requestRandomWords(
        bytes32,
        uint64,
        uint16,
        uint32,
        uint32
    ) external returns (uint256 requestId) {
        VRFConsumerBaseV2 consumer = VRFConsumerBaseV2(msg.sender);
        uint256[] memory randomWords = new uint256[](1);
        randomWords[0] = counter;
        consumer.rawFulfillRandomWords(requestId, randomWords);
        counter += 1;
    }
}

TypeError: nft.createToken(...) is not a function error

copy iconCopydownload iconDownload
 import { ethers } from "ethers";
 const provider = new ethers.providers.JsonRpcProvider();
  const yourContract = new ethers.Contract(nftAddress, NFT.abi, provider);
 const [nftAddress, setNftAddress] = useState("")
  
  async function deployContract(){
    const Item = await ethers.getContractFactory("Item")
    const nft = await Item.deploy(marketAddress)
    await nft.deployed()
    setNftAddress(nft.address)   
  }
 
 import { ethers } from "ethers";
 const provider = new ethers.providers.JsonRpcProvider();
  const yourContract = new ethers.Contract(nftAddress, NFT.abi, provider);
 const [nftAddress, setNftAddress] = useState("")
  
  async function deployContract(){
    const Item = await ethers.getContractFactory("Item")
    const nft = await Item.deploy(marketAddress)
    await nft.deployed()
    setNftAddress(nft.address)   
  }
 
 import { ethers } from "ethers";
 const provider = new ethers.providers.JsonRpcProvider();
  const yourContract = new ethers.Contract(nftAddress, NFT.abi, provider);
 const [nftAddress, setNftAddress] = useState("")
  
  async function deployContract(){
    const Item = await ethers.getContractFactory("Item")
    const nft = await Item.deploy(marketAddress)
    await nft.deployed()
    setNftAddress(nft.address)   
  }
 

How to transfer eth from an account to a contract?

copy iconCopydownload iconDownload
 function pay() public payable {
            //  msg.value is the amount of wei sent with the message to the contract. 
            // with this you are setting a minimum amount
            require(msg.value > .01 ether);
            // add your logic
    }
function addStaker() public payable {
    require(msg.value == 1 ether);
}

ParserError: Source file requires different compiler version

copy iconCopydownload iconDownload
pragma solidity >=0.4.22 <0.9.0;
 pragma solidity >=0.4.22 <0.8.0;
pragma solidity >=0.4.22 <0.9.0;
 pragma solidity >=0.4.22 <0.8.0;
function FulfillRandomness(bytes32 _requestId, uint256 _randomness)
        internal
        override
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0;

How to pass array of structs as argument on etherscan (tuple[])?

copy iconCopydownload iconDownload
[["0x0000", "10000000000", false],["0x11111", "20000000000", true]]

Community Discussions

Trending Discussions on solidity
  • What is the best practice of copying from array to array in Solidity?
  • PIP failed to build package cytoolz
  • Declaration Error: Undeclared identifier Solidity
  • The transaction declared chain ID 5777, but the connected node is on 1337
  • getting error when i deploy the NFT with ETH
  • How to locally unit-test Chainlink's Verifiable Random Function?
  • TypeError: nft.createToken(...) is not a function error
  • Brownie testing for reverted transactions does not work with pytest.raises() or brownie.reverts()
  • How to transfer eth from an account to a contract?
  • Is there a difference between casting to Interface and to a contract instance?
Trending Discussions on solidity

QUESTION

What is the best practice of copying from array to array in Solidity?

Asked 2022-Apr-10 at 21:23

I am trying to save gas by optimize code. In a flash, however, I was wondered what is the best practice of copying from array to array in Solidity.

I present two option. One is copying by pointer (I guess) and the other is using for-loop.

TestOne.sol

contract TestContract {
    uint32[4] testArray;

    constructor(uint32[4] memory seeds) {
        testArray = seeds; // execution costs: 152253
    }

    function Show() public returns (uint32[4] memory) {
        return testArray;
    }
}

TestTwo.sol

contract TestContract {
    uint32[4] testArray;

    constructor(uint32[4] memory seeds) {
        for(uint i = 0; i < 4; i++) {
            testArray[i] = seeds[i];  // execution costs: 150792
        }
    }

    function Show() public returns (uint32[4] memory) {
        return testArray;
    }
}

I tested with Remix (Ethereum Online IDE), 0.8.13 Solidity Compiler with Enable optimization (200)

Discussion of test result

We can see that, TestOne used 152253 gas for execution costs, and TestTwo used 150792 gas for execution costs.

The funny thing is that, for-loop used less gas than just assigning pointer. In my little thought, for-loop would be more assembly codes than the other. (There would be, at least, assigning uint i, substitute 4 times, check conditions 4 times (whether i < 4), increase i++ 4 times etc.)

I suspected the "optimization" of solidity compiler. But, after doing same small experiment without "Enable optimization", it does same result that for-loop used less gas. (198846 vs. 198464)

The Question is
  1. Why do above things happened?

  2. What is the best practice of copying from array to array? Is there any copy function like C++'s std::copy() ?

ANSWER

Answered 2022-Apr-10 at 21:23

The best practice is copy array from memory to storage without looping over their items. However contract optimization in this example is tricky. The official documentation says as follow:

If you want the initial contract deployment to be cheaper and the later function executions to be more expensive, set it to --optimize-runs=1. If you expect many transactions and do not care for higher deployment cost and output size, set --optimize-runs to a high number.

To illustrate above, consider following contracts:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

contract TestLoop {
    uint32[4] testArray;

    function setArrayWithLoop(uint32[4] memory array) public {
        for(uint256 i = 0; i < array.length; i++)
            testArray[i] = array[i];
    }

    function setArrayWithoutLoop(uint32[4] memory array) public {
        testArray = array;
    }

    function show() public view returns (uint32[4] memory) {
        return testArray;
    }
}

contract NoLoop {
    uint32[4] testArray;

    constructor(uint32[4] memory array) {
        testArray = array;
    }

    function show() public view returns (uint32[4] memory) {
        return testArray;
    }
}

contract Loop {
    uint32[4] testArray;

    constructor (uint32[4] memory array) {
        for(uint256 i = 0; i < array.length; i++)
            testArray[i] = array[i];
    }

    function show() public view returns (uint32[4] memory) {
        return testArray;
    }
}

and script written by using brownie:

from brownie import TestLoop, NoLoop, Loop, accounts

def function_calls():
    contract = TestLoop.deploy({'from': accounts[0]})
    print('set array in loop')
    contract.setArrayWithLoop([1, 2, 3, 4], {'from': accounts[1]})
    print('array ', contract.show(), '\n\n')

    print('set array by copy from memory to storage')
    contract.setArrayWithoutLoop([10, 9, 8, 7], {'from': accounts[2]})
    print('array ', contract.show(), '\n\n')

def deploy_no_loop():
    print('deploy NoLoop contract')
    contract = NoLoop.deploy([21, 22, 23, 24], {'from': accounts[3]})
    print('array ', contract.show(), '\n\n')

def deploy_loop():
    print('deploy Loop contract')
    contract = Loop.deploy([31, 32, 33, 34], {'from': accounts[3]})
    print('array ', contract.show(), '\n\n')

def main():
    function_calls()
    deploy_no_loop()
    deploy_loop()

with following brownie-config.yaml:

compiler:
  solc:
    version: 0.8.13
    optimizer:
      enabled: true
      runs: 1

which gives following outputs:

Running 'scripts/test_loop.py::main'...
Transaction sent: 0x8380ef4abff179f08ba9704826fc44961d212e5ee10952ed3904b5ec7828c928
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  TestLoop.constructor confirmed   Block: 1   Gas used: 251810 (2.10%)
  TestLoop deployed at: 0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87

set array in loop
Transaction sent: 0xfe72d6c878a980a9eeefee1dccdd0fe8214ee4772ab68ff0ac2b72708b7ab946
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  TestLoop.setArrayWithLoop confirmed   Block: 2   Gas used: 49454 (0.41%)

array  (1, 2, 3, 4) 


set array by copy from memory to storage
Transaction sent: 0x0106d1a7e37b155993a6d32d5cc9dc67696a55acd1cf29d2ed9dba0770436b98
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  TestLoop.setArrayWithoutLoop confirmed   Block: 3   Gas used: 41283 (0.34%)

array  (10, 9, 8, 7) 


deploy NoLoop contract
Transaction sent: 0x55ddded68300bb8f11b3b43580c58fed3431a2823bf3f82f0081c7bfce66f34d
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
  NoLoop.constructor confirmed   Block: 4   Gas used: 160753 (1.34%)
  NoLoop deployed at: 0x7CA3dB74F7b6cd8D6Db1D34dEc2eA3c89a3417ec

array  (21, 22, 23, 24) 


deploy Loop contract
Transaction sent: 0x1aa64f2cd527983df84cfdca5cfd7a281ff904cca227629ec8b0b29db561c043
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 1
  Loop.constructor confirmed   Block: 5   Gas used: 153692 (1.28%)
  Loop deployed at: 0x2fb0fE4F05B7C8576F60A5BEEE35c23632Dc0C27

array  (31, 32, 33, 34)
Conclusions
  1. When we consider contract function call optimization, then usage of memory to storage copy, here is more info, is more gas efficient than copy by for loop. Compare gas used from function setArrayWithoutLoop and function setArrayWithLoop.
  2. When we consider contract deployment optimization it seems there is opposite situation to this in conclusion 1.
  3. Most important: Contract constructor is called only once in contract lifetime, just when the contract is deployed to chain. So the most often there is an function call optimization rather than contract deployment optimization. This leads to conclusion 1.

Source https://stackoverflow.com/questions/71819186

Community Discussions, Code Snippets contain sources that include Stack Exchange Network

Vulnerabilities

No vulnerabilities reported

Install solidity

Instructions about how to build and install the Solidity compiler can be found in the Solidity documentation.

Support

The Solidity documentation is hosted at Read the docs.

DOWNLOAD this Library from

Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases
Explore Kits

Save this library and start creating your kit

Explore Related Topics

Share this Page

share link
Reuse Pre-built Kits with solidity
Consider Popular Blockchain Libraries
Try Top Libraries by ethereum
Compare Blockchain Libraries with Highest Support
Compare Blockchain Libraries with Highest Quality
Compare Blockchain Libraries with Highest Security
Compare Blockchain Libraries with Permissive License
Compare Blockchain Libraries with Highest Reuse
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases
Explore Kits

Save this library and start creating your kit

  • © 2022 Open Weaver Inc.