Solidity Reference

Go up to the main CCC docs page (md)

This document is meant to be a series of reminders about Solidity programming tasks, all in one place.

SPDX license line

The first line of your program, it might look like:

// SPDX-License-Identifier: Unlicensed

The fill list can be found here. For this class, you can use any one you want. A few examples:

Paying from a contract

(bool success, ) = payable(a).call{value: v}("");
require(success, "Failed to transfer ETH");

Getting the balance of a contract

This only pertains to the spring 2022 semester; outside that semester, just call address(this).balance.

Having a contract get it’s own balance should be easy – just address(this).balance. But for the spring 2022 semester, that is not working due to a misconfiguration in how the blockchain was setup – it complains about SELFBALANCE being an unknown opcode. So we need a work-around: a contract has to ask something else to get it’s own balance.

Consider this contract:

// SPDX-License-Identifier: Unlicensed
pragma solidity ^0.8.16;
contract GetBalance {
    function getBalance(address a) public view returns (uint) {
        return a.balance;
    }
}

Once deployed, you can call the getBalance() method to get one’s own balance. This is deployed at address 0x1E2c5E21c3b9cFD67bedF28e04fDd295AecbC500 on the course blockchain.

If you need to be able to switch between this code on the course blockchain, and Remix (in which you can just call address(this).balance), you can use the following method:

// This is SUCH AND UGLY HACK, but it will work for the spring 2022 semester
function getSelfBalance() public view returns (uint) {
    if ( block.number > 70000 )
        return GetBalance(0x1E2c5E21c3b9cFD67bedF28e04fDd295AecbC500).getBalance(address(this));
    else
        return address(this).balance;
}