Code Monkey home page Code Monkey logo

access-controller-contracts's Introduction

Access Controller Contracts

License version

Simple on-chain access controller contract. This contract permit an on-chain registration mechanism with a concept of slots.

Owner can specify a number of slots (maxSlotsCount). Everyone is able to register to get a slot. When the slotUsedCount eq the maxSlotsCount there is no more space to register.

With this simple approach you can use an on-chain contract to manage accesses of your contracts or even of your frontends. This is a very useful tool when you want to give progessive access.

Contracts

The contract has three state variables:

@storage_var
func AccessController_maxSlotsCount() -> (max: felt):
end

@storage_var
func AccessController_slotUsedCount() -> (entries: felt):
end

@storage_var
func AccessController_whitelist(address: felt) -> (whitelisted: felt):
end

Deploy

When deploying the contract you have to pass two args:

(
    initial_allowed_access: felt, # Number of initial slots available
    owner_address: felt           # Owner of the contract who will be able to increase # of slots
)

Management

Once you deployed the contract, you can increase the number of maximum slots available. To do that make a transaction by invoking the increaseMaxSlots function from the owner wallet. The argument is increase_max_slots_by which is the number of slots you want to add.

Other useful functions can be found here

Use in ReactJS

Using this contract is deadsimple. First of all import ABI and create your Contract object:

import { Contract, json } from "starknet";

const compiledARFController = json.parse(JSON.stringify(arfControllerAbi));
arfControllerContract: new Contract(
    compiledARFController,
    CONTROLLER_CONTRACT_ADDRESS
);

Verify if an address is allowed/registered:

accessControllerContract
    .isAllowed("0x1234...6789")
    .then((response: CallContractResponse) => {
        // response.is_allowed
    })

Permit a user to register in order to get a free slot:

accessControllerContract
    .invoke("register", [])
    .then((response: AddTransactionResponse) => {
        // Transaction added
    }).catch(() => {
        // Error
    });

Check the number of available slots:

accessControllerContract
      .freeSlotsCount()
      .then((response: CallContractResponse) => {
        // response.free_slots_count
      })

Use case

This contract has been used for Alpha Road during the first Testnet phases of the launch of our first offering: a one-click revisited AMM.

Tests

Run tests

First, install requirements:

pip install -r requirements.txt
pip install -r tests/requirements.txt

Run all tests:

pytest

Run a specific test:

pytest tests/test_AccessController.py -k test_transfer_ownership_should_fail_when_caller_is_not_owner

Linter

To make our tests readable we use a standard linter: flake8

Run linter:

flake8 . --count --exit-zero --max-complexity=10 --max-line-length=120 --statistics

Flake will only act as linter and will not help you to fix/format your python files. We recommend using: yapf

E.g:

yapf --recursive --style='{based_on_style: pep8, column_limit: 120, indent_width: 4}' -i tests

Improvements

Feel free to improve this by providing a PR.

access-controller-contracts's People

Contributors

0xlny avatar florian-bellotti avatar

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.