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)
}
}