Coder Social home page Coder Social logo

quicswap / near-multicall Goto Github PK

View Code? Open in Web Editor NEW
5.0 0.0 2.0 1.77 MB

bundle cross-contract calls for powerful DAO proposals

Home Page: https://quicswap.github.io/multicall-UI/

License: MIT License

TypeScript 4.50% WebAssembly 95.40% JavaScript 0.03% Shell 0.07%
blockchain near-protocol

near-multicall's Introduction

This repository is archived.

This repository is archived and will not receive any updates or accept issues or pull requests.
development of Near-multicall contracts will continue on a new repository.

To report bugs in this repo please open an issue on the new repository.

If you have found a critical security bug, please exercise responsible disclosure and e-mail [email protected].


multicall.near

tests workflow

bundle cross-contract calls for powerful DAO proposals

Quick Start

  1. Setup near-cli.
  2. Install dependencies: yarn
  3. Compile: yarn build:release
  4. Deploy:
    near deploy $CONTRACT_ADDRESS ./build/multicall_release.wasm
  5. Initialize the contract:
    near call $CONTRACT_ADDRESS init '{"admin_accounts":["$YOUR_ACCOUNT"],"croncat_manager":"$CRONCAT_MANAGER","job_bond":"100000000000000000000000"}' --amount 0.1 --accountId $YOUR_ADDRESS

where

  • $YOUR_ACCOUNT is the account you're using to interact with the contract
  • $CONTRACT_ADDRESS is the address you want to deploy the contract at
  • $CRONCAT_MANAGER is the address for croncat's manager contract, can be found here.

Architecture

This project consists of three main features:

  1. The main method in this contract is:
    multicall ( calls: BatchCall[][] )
    It executes a bunch of BatchCall arrays.
    Each BatchCall has information for making a batch of function-calls: a target address and an array on function-calls to execute on that target. Each function-call has a function name, arguments encoded in base64, gas to use (u64 encoded as string) and amount of yoctoNEAR attached deposit (u128 encoded as string).
    Batches inside one array run one after another, as a promise chain.
    Different arrays of batches run in parallel.
    Example:

    calls = [
        [ Batch_11, Batch_12, Batch_13 ],
        [ Batch_21, Batch_22]
    ]
    

    In this example we have 2 arrays of batches. In the first one Batch_12 waits for Batch_11 and Batch_13 waits for Batch_12. In the second array Batch_22 waits for Batch_21. Both arrays start executing in the same block and are independent of each other.

  2. Permissioned interactions with the contract through whitelisting of addresses:
    Due to the async nature of Near, funds can sit in the contract during multiple blocks awaiting the execution of cross-contract calls. To prevent stealing of funds, we require an address to be whitelisted before calling one of the contract's critical methods. there are two main whitelists:
    admins whitelist holds addresses that can interact with the contract, they can add or remove others from the whitelist.
    tokens whitelist holds token addresses that can be attached to function calls, as the contract implements ft_on_transfer.
    The contract's address is whitelisted by default, this allows nesting multiple contract methods for convenience.

  3. Jobs:
    Multicall executions can be scheduled to run in recurring fashion, made possible by integrating croncat. Anyone can register a job on the multicall contract, but an admin has to approve it. Admins can pause/resume job executions and also edit a job's multicall arguments.
    The following must be specified when creating a job:

    function job_add (
        job_schedules: BatchCall[][], // multicall arguments
        job_cadence: string, // cron expression
        job_trigger_gas: u64,
        job_trigger_deposit: u128,
        job_total_budget: u128,
        job_runs_max: u64,
        job_start_at: u64 = context.blockTimestamp
    ): u32 

Example Calls

Call structure

example multicall arguments:

{
    "calls": [
        [ 
            {
                "address": "hello.lennczar.testnet",
                "actions": [
                    {
                        "func": "hello",
                        "args": "eyJ0aGluZyI6IldvcmxkIn0=", // base64 encoding for {"thing":"World"}
                        "gas": "10000000000000",
                        "depo": "0"
                    },
                    {
                        "func": "hello",
                        "args": "eyJ0aGluZyI6IldvcmxkIn0=", // base64 encoding for {"thing":"World"}
                        "gas": "10000000000000",
                        "depo": "0"
                    }
                ]
            },
            {
                "address": "hello.lennczar.testnet",
                "actions": [
                    {
                        "func": "hello",
                        "args": "eyJ0aGluZyI6IldvcmxkIn0=", // base64 encoding for {"thing":"World"}
                        "gas": "10000000000000",
                        "depo": "0"
                    }
                ]
            }
        ],
        [
            {
                "address": "hello.lennczar.testnet",
                "actions": [
                    {
                        "func": "hello",
                        "args": "eyJ0aGluZyI6IldvcmxkIn0=", // base64 encoding for {"thing":"World"}
                        "gas": "10000000000000",
                        "depo": "0"
                    }
                ]
            }
        ]
    ]
}

This example calls the function hello(thing: string): string in the contract hello.lennczar.testnet.
We see two arrays of batches: in the first array we have 2 batches that will be run as a promise chain (i.e. second batch will wait for the first batch). The first batch calls the function twice and the second batch calls it only once. In the second array we have one batch that calls the function once, it will run in parallel independently of the two previously mentioned batches.
Running this results in the following transaction

(OUTDATED) Use case: SputnikDAO custom function proposals

The following is an example created as part of a Near Metabuidl Challenge. A DAO proposal was created with the JSON below.
The resulting transaction can be seen here (link uses outdated code).

{
   "actions": [
      [
          {
             "addr": "ref-finance-101.testnet",
             "func": "storage_deposit",
             "args": "eyJhY2NvdW50X2lkIjoibXVsdGljYWxsLmNobHVmZjEudGVzdG5ldCJ9", // base64 for {"account_id":"multicall.chluff1.testnet"}
             "gas": "6000000000000",
             "depo": "1250000000000000000000"
          },
          {
             "addr": "wrap.testnet",
             "func": "near_deposit",
             "args": "e30=", // base64 for {}
             "gas": "4000000000000",
             "depo": "100000000000000000000000"
          },
          {
             "addr": "wrap.testnet",
             "func": "ft_transfer_call",
             "args": "eyJyZWNlaXZlcl9pZCI6InJlZi1maW5hbmNlLTEwMS50ZXN0bmV0IiwiYW1vdW50IjoiMTAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwIiwgIm1zZyI6IiJ9", // base64 for {"receiver_id":"ref-finance-101.testnet","amount":"100000000000000000000000", "msg":""}
             "gas": "40000000000000",
             "depo": "1"
          },
          {
             "addr": "ref-finance-101.testnet",
             "func": "swap",
             "args": "eyJhY3Rpb25zIjpbeyJwb29sX2lkIjo2LCJ0b2tlbl9pbiI6IndyYXAudGVzdG5ldCIsImFtb3VudF9pbiI6IjEwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMCIsInRva2VuX291dCI6Im51c2RjLmZ0LWZpbi50ZXN0bmV0IiwibWluX2Ftb3VudF9vdXQiOiIxIn1dfQ==" // base64 for {"actions":[{"pool_id":6,"token_in":"wrap.testnet","amount_in":"100000000000000000000000","token_out":"nusdc.ft-fin.testnet","min_amount_out":"1"}]},
             "gas": "20000000000000",
             "depo": "1"
          },
          {
             "addr": "ref-finance-101.testnet",
             "func": "get_deposits",
             "args": "eyJhY2NvdW50X2lkIjoibXVsdGljYWxsLmNobHVmZjEudGVzdG5ldCJ9" // base64 for {"account_id":"multicall.chluff1.testnet"},
             "gas": "5000000000000",
             "depo": "0"
          },
          {
             "addr": "multicall.chluff1.testnet",
             "func": "withdraw_from_ref",
             "args": "eyJyZWZfYWRkcmVzcyI6InJlZi1maW5hbmNlLTEwMS50ZXN0bmV0IiwidG9rZW5zIjpbIm51c2RjLmZ0LWZpbi50ZXN0bmV0Il0sInJlY2VpdmVyX2lkIjoidm95YWdlci5zcHV0bmlrdjIudGVzdG5ldCIsIndpdGhkcmF3YWxfZ2FzIjoiNTUwMDAwMDAwMDAwMDAiLCJ0b2tlbl90cmFuc2Zlcl9nYXMiOiI0MDAwMDAwMDAwMDAwIiwiZGVwb3NpdCI6IjEifQ==", // base64 for {"ref_address":"ref-finance-101.testnet","tokens":["nusdc.ft-fin.testnet"],"receiver_id":"voyager.sputnikv2.testnet","withdrawal_gas":"55000000000000","token_transfer_gas":"4000000000000","deposit":"1"}
             "gas": "95000000000000",
             "depo": "0"
          }
      ]
   ]
}

Note: we recommend making the add_proposal transaction using near-cli, as the SputnikDAO UI only allocates 150 TeraGas per default to the custom function call. This might not be sufficient.

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.