Git Product home page Git Product logo

summingpriorityqueue's Introduction

Solidity Summing Priority Queue

NPM Package CI

An utility library implementing summing priority queue in solidity

Installation

npm install @mihanix/summing-priority-queue

Interface

totalEnqueuedAmount

uint256 totalEnqueuedAmount;

Sum of all enqueued value's.

enqueue

function enqueue(uint256 key, uint256 value) public;

Adds an element with key and value to the heap. Also adds value to totalEnqueuedAmount.

dequeue

function dequeue() public;

Popps an element with the lowest key and substracts corresponding value from totalEnqueuedAmount

top

function top() public returns(uint256);

Returns the lowest enqueued key

drain

function drain(uint256 ts) public;

Dequeues all elements lower then ts. Could be useful some times.

Example Usage

An example usage of asset delayed locking (stock vesting analogy)

pragma solidity ^0.8.7;

import "@mihanix/summing-priority-queue/contracts/SummingPriorityQueue.sol";

contract MyLockingContract {
    using SummingPriorityQueue for SummingPriorityQueue.Heap;

    // @notice user balances
    mapping(address => uint256) _balances;
    // @notice vesting locks
    mapping(address => SummingPriorityQueue.Heap) internal _withdrawLocks;

    // @notice lock an amount of tokens for timeOffset for holder 
    function _lock(address holder, uint256 amount, uint256 timeOffset) internal {
        _withdrawLocks[holder].enqueue(amount, block.timestamp + timeOffset);
    }

    // @notice get locked amount of tokens
    function _getLockedAmount(address holder) internal returns(uint256) {
        _withdrawLocks[holder].drain(block.timestamp); // dequeue outdated locks 
        return _withdrawLocks[holder].totalEnqueuedAmount;
    }

    // @notice withdraw tokens and check vesting locks
    function withdraw(uint256 amount) external {
        uint256 lockedAmount = _getLockedAmount(msg.sender);
        require(amount + lockedAmount <= _balances[msg.sender], "locked-up");
        /* 
         * Other business-logic...
         */
    }

}

summingpriorityqueue's People

Contributors

mihanixa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

summingpriorityqueue's Issues

How can I reach you out?

I'm impressed by this library.
Can you share your twitter, telegram or discord?
BTW my twitter is @LEVXeth so you can DM me as well if you want.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.