{"id":20001898,"url":"https://github.com/pooltogether/sortition-sum-tree-factory","last_synced_at":"2025-07-05T05:33:14.757Z","repository":{"id":43967574,"uuid":"251172317","full_name":"pooltogether/sortition-sum-tree-factory","owner":"pooltogether","description":null,"archived":false,"fork":false,"pushed_at":"2023-01-24T01:50:08.000Z","size":1791,"stargazers_count":10,"open_issues_count":13,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-14T20:56:45.906Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pooltogether.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-30T01:31:49.000Z","updated_at":"2023-09-29T00:10:44.000Z","dependencies_parsed_at":"2023-02-13T10:16:05.492Z","dependency_job_id":null,"html_url":"https://github.com/pooltogether/sortition-sum-tree-factory","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/pooltogether/sortition-sum-tree-factory","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pooltogether%2Fsortition-sum-tree-factory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pooltogether%2Fsortition-sum-tree-factory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pooltogether%2Fsortition-sum-tree-factory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pooltogether%2Fsortition-sum-tree-factory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pooltogether","download_url":"https://codeload.github.com/pooltogether/sortition-sum-tree-factory/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pooltogether%2Fsortition-sum-tree-factory/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260898756,"owners_count":23079263,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T05:19:03.460Z","updated_at":"2025-07-05T05:33:14.739Z","avatar_url":"https://github.com/pooltogether.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SortitionSumTreeFactory\n\n[![CircleCI](https://circleci.com/gh/pooltogether/sortition-sum-tree-factory.svg?style=svg)](https://circleci.com/gh/pooltogether/sortition-sum-tree-factory)\n\nThis is a data structure that allows efficient O(log(n)) weighted selection.  This package is an extraction from the Kleros project.  \n\nFor an explanation of the code see the [Medium article](https://medium.com/kleros/an-efficient-data-structure-for-blockchain-sortition-15d202af3247).\n\nFor more information on Sum Trees read [Introduction to Sum Tree](https://www.fcodelabs.com/2019/03/18/Sum-Tree-Introduction/)\n\n## Attribution\n\nThe majority of this code was written by [Enrique Piqueras](https://twitter.com/epiqueras1).\n\nThe data structure was designed by [Clément Lesaege](https://twitter.com/clesaege).\n\nThanks to the Kleros team for MIT licensing this *extremely useful* code!\n\n# Setup\n\nTo install use yarm or npm and install `sortition-sum-tree-factory`:\n\n```sh\n$ yarn add sortition-sum-tree-factory\n```\n\n```sh\n$ npm i sortition-sum-tree-factory\n```\n\n# Usage\n\nThe SortitionSumTreeFactory is a library that should be attached to the struct SortitionSumTreeFactory.SortitionSumTrees.  This data structure allows you to create many different sortition sum trees.\n\nFor example, here we use the library to create a single global sortition sum tree:\n\n```solidity\ncontract WeightedSelection {\n    bytes32 constant private TREE_KEY = keccak256(\"PoolTogether/SingleRandomWinnerPrizeStrategy\");\n    uint256 constant private MAX_TREE_LEAVES = 5;\n\n    using SortitionSumTreeFactory for SortitionSumTreeFactory.SortitionSumTrees;\n\n    SortitionSumTreeFactory.SortitionSumTrees sumTreeFactory;\n\n    constructor () public {\n        sortitionSumTrees.createTree(TREE_KEY, MAX_TREE_LEAVES);\n    }\n}\n```\n\nLet's assume the sortition sum tree is storing user token balances.\n\nNow you can set the balances using the `set` function:\n\n```solidity\nfunction updateBalanceOf(address user, uint256 amount) external override {\n    sortitionSumTrees.set(TREE_KEY, amount, bytes32(uint256(user)));\n}\n```\n\nWhen we want to select someone proportionally we can use `draw`:\n\n```solidity\nfunction randomlyDrawUser() public view returns (address) {\n    bytes32 entropy = blockhash(1);\n    uint256 token = UniformRandomNumber.uniform(uint256(entropy), bound);\n    return address(uint256(sortitionSumTrees.draw(TREE_KEY, token)));\n}\n```\n\nThe probability that a user is selected is proportional to their token balance.\n\nNote the use of [UniformRandomNumber](https://github.com/pooltogether/uniform-random-number).  This library eliminates modulo bias when constraining large numbers into a smaller set.\n\n# Development\n\nInstall the dependencies:\n\n```sh\n$ yarn\n```\n\nNow run the tests:\n\n```sh\n$ yarn test\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpooltogether%2Fsortition-sum-tree-factory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpooltogether%2Fsortition-sum-tree-factory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpooltogether%2Fsortition-sum-tree-factory/lists"}