Solidity - 035 WhileLoop

本文介绍了一个Solidity智能合约,展示了如何使用while循环遍历映射存储的区块编号,并通过事件依次发出这些存储的数值。合约包含一个存储区块数的映射、计数器以及设置和获取存储数值的方法。
摘要由CSDN通过智能技术生成

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.9;

/**

 * @title whileLoop

 * @dev Demonstrates the use of a while loop to iterate over a mapping in Solidity.

 * The contract stores block numbers at different instances and provides a function

 * to emit these stored values through an event.

 */

contract whileLoop {

    // Mapping to store block numbers. The key is an incrementing counter, and the value is the block number.

    mapping (uint => uint) blockNumber;

    // Counter to keep track of the number of entries in the mapping.

    uint counter;

    // Event to emit the stored block number.

    event uintNumber(uint);

    /**

     * @dev Stores the current block number in the mapping under the current value of `counter`.

     * Increments `counter` after storing the block number.

     */

    function setNumber() public {

        blockNumber[counter++] = block.number;

    }

    /**

     * @dev Emits the stored block numbers sequentially by iterating over the mapping

     * with a while loop. It starts from the first stored block number and continues

     * until it reaches the last stored block number based on the `counter`.

     */

    function getNumbers() public {

       uint i = 0; // Initialize the index for iteration.

       // While loop to go through each stored block number.

       while (i < counter) {

           // Emit an event with the current block number.

           emit uintNumber(blockNumber[i]);

           i = i + 1; // Increment the index to move to the next block number.

       }

    }

}

//Deploy:

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Solidity中,可以通过使用智能合约来实现ERC20代币的锁仓与释放。以下是一个简单的锁仓合约示例: ``` pragma solidity ^0.8.0; import "./IERC20.sol"; import "./SafeMath.sol"; contract TokenVesting { using SafeMath for uint256; address public beneficiary; uint256 public cliff; uint256 public start; uint256 public duration; uint256 public released; IERC20 public token; constructor( address _beneficiary, uint256 _cliff, uint256 _duration, address _token ) public { require(_beneficiary != address(0)); require(_cliff <= _duration); beneficiary = _beneficiary; cliff = _cliff; duration = _duration; start = block.timestamp; token = IERC20(_token); } function release() public { uint256 unreleased = releasableAmount(); require(unreleased > 0); released = released.add(unreleased); token.transfer(beneficiary, unreleased); } function releasableAmount() public view returns (uint256) { return vestedAmount().sub(released); } function vestedAmount() public view returns (uint256) { uint256 currentBalance = token.balanceOf(address(this)); uint256 totalBalance = currentBalance.add(released); if (block.timestamp < start.add(cliff)) { return 0; } else if (block.timestamp >= start.add(duration)) { return totalBalance; } else { return totalBalance.mul(block.timestamp.sub(start)).div(duration); } } } ``` 在这个合约中,当创建合约时,需要传入受益人地址、锁仓期、释放期、代币地址等信息。锁仓期结束后,受益人可以通过调用 `release()` 函数来释放锁仓代币。如果释放函数被调用,但是当前时间还没有到达释放期,则会抛出异常。 为了保证代币不能被提前释放,合约还实现了 cliff 的概念,即在锁仓期结束之前,代币不能被释放。当 cliff 结束之后,代币将按照线性方式释放,直到释放期结束。 需要注意的是,以上示例只是一个简单的锁仓合约示例,实际生产环境中需要更加严格地考虑各种情况和安全性问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值