npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

functions-kit

v1.0.6

Published

tool kit for using functions in daps

Downloads

13

Readme

cloudbeam functions-kit Sdk Integration Documentation

const FunctionsKit = require("functions-kit");
const {ethers} = require("ethers");
const dotenv = require("dotenv").config();

//Addresses are for devnet testing
const addresses = {
    FunctionConsumer: "0x7C70c7412dc99F5040CA2A01a48DB82Fa0d3D073", 
    PayMaster: "0xe9C82e8a953DBff1882701f39F233bB13eF4Afda",
    FuncReg: "0x93AE40312412b7c88322B440ceDd0eB1026Bf319",
    FuncClient: "0x09ed74E9F18b1AB61B38fA17DF6F107D1702fEC5"
  }

  const PRIVATE_KEY = process.env.PRIVATE_KEY;

const functionKit = new FunctionsKit({ 
    rpcUrl: rpcUrl, 
    funcClientAddress: addresses.FuncClient, 
    funcRegAddress: addresses.FuncReg, 
    payMasterAddress: addresses.PayMaster
});


/**
 * @notice registers a new automated function
 * @param {string} source 
 * @param {Array<string>} args 
 * @param {number} interval 
 * @param {string} tokenType 
 * @param {number} returnType // The refers to the return from the function call. 0 = string, 1 = uint256, 2 = int256
 * @returns {number} functionId
 */
const registerAutoFunction = async (source, args, interval, tokenType, returnType) => {
    try{
        const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
        const wallet = new ethers.Wallet(PRIVATE_KEY)
        
        //Signer can be gotten from metamask if SDK is implemented in a web app
        const signer = wallet.connect(provider);
        
        const functionId = await functionKit.registerAutoFunction(signer, tokenType, source, args, secret, interval, returnType);
        return functionId;
    }catch(e){
        console.log(e.message)
    }
}

/**
 * @notice registers a new default function
 * @param {string} caller // contract or EOA (externally owned address) able to call the make request function
 * @param {string} tokenType 
 * @param {number} returnType // The refers to the return from the function call. 0 = string, 1 = uint256, 2 = int256
 * @returns {number} id
 */
const registerDefaultFunction = async (caller, tokenType, returnType) => {
    try{
        const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
        const wallet = new ethers.Wallet(PRIVATE_KEY)
        
        //Signer can be gotten from metamask if SDK is implemented in a web app
        const signer = wallet.connect(provider);
        
        const functionId = await functionKit.registerFunction(signer,caller, tokenType, returnType);
        return functionId;
    }catch(e){
        console.log(e.message)
    }
}

/**
 * @notice changes the currebt state of the function can only be called by function Admin
 * @param {number} functionId 
 * @returns {string} current function state
 */
const changeFunctionStage = async (functionId) => {
    try{
        const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
        const wallet = new ethers.Wallet(PRIVATE_KEY)
        
        //Signer can be gotten from metamask if SDK is implemented in a web app
        const signer = wallet.connect(provider);

        toogleFunctionState = await functionKit.toogleFunctionState(signer, 1);
    }catch(e){
        console.log(e.message)
    }
}

/**
 * @notice makes a function request. can only be called by the approved caller
 * @param {string} source 
 * @param {Array<string>} args 
 * @param {string} secret
 * @returns {number} functionId
 */
const makeRequest = async (source, args, functionId, secret) => {
    try{
        const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
        const wallet = new ethers.Wallet(PRIVATE_KEY)
        
        //Signer can be gotten from metamask if SDK is implemented in a web app
        const signer = wallet.connect(provider);
        
        const request = await functionKit.makeRequest(signer, functionId, source, args, secret);
        return request;
    }catch(e){
        console.log(e.message)
    }
}

/**
 * @notice tops up admin balance for spesified token
 * @param {number} amount amount to be added 
 * @param {string} tokenType
 * @returns {boolean} on success returns true else returns error
 */

const topUpAdminBalance = async (amount, tokenType) => {
    try{
        const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
        const wallet = new ethers.Wallet(PRIVATE_KEY)
        //Signer can be gotten from metamask if SDK is implemented in a web app
        const signer = wallet.connect(provider);
        const topUp = await functionKit.topup(signer, amount, tokenType);
        return topUp;
    }catch(e){
        console.log(e.message)
    }
}

/**
 * @notice withdraws admin balance for spesified token
 * @param {string} tokenType
 * @returns {boolean} on success returns true else returns error
 */

const adminWithdraw = async (tokenType) => {
    try{
        const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
        const wallet = new ethers.Wallet(PRIVATE_KEY)
        //Signer can be gotten from metamask if SDK is implemented in a web app
        const signer = wallet.connect(provider);
        const withdrawn = await functionKit.adminWithdraw(signer,tokenType);
        return withdrawn;
    }catch(e){
        console.log(e.message)
    }
}

/**
 * @notice stops and delete the function whose Id was passed
 * @param {number} functionId id of function to deprecate 
 */
const deprecateFunction = async (functionId)=> {
    try{
        const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
        const wallet = new ethers.Wallet(PRIVATE_KEY)
        //Signer can be gotten from metamask if SDK is implemented in a web app
        const signer = wallet.connect(provider);
        const deprecatedId = await functionKit.cancelRegFunctions(signer, functionId);
        return deprecatedId;
    }catch(e){
        console.log(e.message)
    }
}

/**
 * @notice gets all admins functions
 * @param {string} adminAddress  
 */
const getAdminFunctions = async (adminAddress) => {
    try{
        const adminFunctions = await functionKit.getAdminFunctions(adminAddress);
        return adminFunctions;
    }catch(e){
        console.log(e.message)
    }
}

/**
 * @notice get admins balance for spesific tokens
 * @param {string} adminAddress  
 * @param {string} tokenType 
 */
const getAdminBalance = async (adminAddress, tokenType) => {
    try{
        const balance = await functionKit.getAdminBalance(adminAddress, tokenType);
        return balance;
    }catch(e){
        console.log(e.message)
    }
}

/**
 * @notice gets spesific function by id
 * @param {number} functionId  
 */
const getRegisteredFunction = async (functionId) => {
    try{
        const functionById = await functionKit.getRegFunction(functionId);
        return functionById;
    }catch(e){
        console.log(e.message)
    }
}


/**
 * @notice gets all requests for a functionId
 * @param {number} functionId
 * @returns {Array}  
 */
const getAllRequest = async (functionId) => {
    try{
        const allRequest = await functionKit.getAllRequest(functionId);
        return allRequest;
    }catch(e){
        console.log(e.message)
    }
}

/**
 * @notice gets latest request for a functionId
 * @param {number} functionId  
 */
const getLatestRequest = async (functionId) => {
    try{
        const latestRequest = await functionKit.getLatestRequest(functionId);
        return latestRequest;
    }catch(e){
        console.log(e.message)
    }
}

/**
 * @notice gets latest request for a functionId
 * @param {number} functionId  
 * @param {string} requestId 
 */
const getRequestById = async (functionId, requestId) => {
    try{
        const request = await functionKit.getRequestById(functionId, requestId);
        return request;
    }catch(e){
        console.log(e.message)
    }
}