Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added paymaster functions #213

Open
wants to merge 10 commits into
base: v8/kcs-integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions managers/paymaster-operations-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { ethers } from 'ethers';
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
import { resolveUAL } from '../services/utilities.js';
import { INCENTIVE_TYPE } from '../constants.js';

export default class PaymasterOperationsManager {

constructor(services) {
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
this.blockchainService = services.blockchainService;
this.inputService = services.inputService;
this.nodeApiService = services.nodeApiService;
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
this.validationService = services.validationService;
}

/**
* Sets allowance to a given quantity of tokens.
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
* @async
* @param {BigInt} tokenAmount - The amount of tokens (Wei) to set the allowance.
* @param {Object} [options={}] - Additional options for increasing allowance - currently only blockchain option expected.
* @param {string} recipient - The address of the recipient (used for operations like withdrawal or funding).
* @param {string} hubAddress - The address of the recipient (used for operations like withdrawal or funding).
* @returns {Object} Object containing hash of blockchain transaction and status.
*/

async deployPaymasterContract(options, hubAddress) {
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
try {

const blockchain = this.inputService.getBlockchain(options);
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved

if(this.validationService.validateBlockchain(blockchain) && this.validationService.validateAddress(hubAddress))
{
await this.blockchainService.deployPaymasterContractFunction(blockchain, hubAddress);
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
}

} catch (error) {
console.error("Error deploying Paymaster contract:", error);
}
}

async addAllowedAddress(options) {
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
try {

const blockchain = this.inputService.getBlockchain(options);
const public_address = await this.blockchainService.getPublicKey(blockchain);

if(this.validationService.validateBlockchain(blockchain) && this.validationService.validateAddress(public_address))
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
{
await this.blockchainService.addAllowedAddressFunction(blockchain, public_address);
}

} catch (error) {
console.error("Error adding allowed address:", error);
}
}

async removeAllowedAddress(options) {
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
try {

const blockchain = this.inputService.getBlockchain(options);
const public_address = await this.blockchainService.getPublicKey(blockchain);

if(this.validationService.validateBlockchain(blockchain) && this.validationService.validateAddress(public_address))
{
await this.blockchainService.removeAllowedAddressFunction(blockchain, public_address);
}

} catch (error) {
console.error("Error removing allowed address:", error);
}
}


async fundPaymaster(options, tokenAmount) {
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
try {

const blockchain = this.inputService.getBlockchain(options);

if(this.validationService.validateBlockchain(blockchain) && this.validationService.validateTokenAmount(tokenAmount))
{
await this.blockchainService.fundFunction(blockchain, tokenAmount);
}

} catch (error) {
console.error("Error funding paymaster:", error);
}
}

async withdraw(options, recipient, tokenAmount) {
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
try {

const blockchain = this.inputService.getBlockchain(options);

if(this.validationService.validateBlockchain(blockchain) && this.validationService.validateTokenAmount(tokenAmount) && this.validationService.validateAddress(recipient))
{
await this.blockchainService.withdrawFunction(blockchain, recipient, tokenAmount);
}

} catch (error) {
console.error("Error withdrawing:", error);
}
}

async coverCost(options, tokenAmount) {
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
try {

const blockchain = this.inputService.getBlockchain(options);

if(this.validationService.validateBlockchain(blockchain) && this.validationService.validateTokenAmount(tokenAmount))
{
await this.blockchainService.coverCostFunction(blockchain, tokenAmount);
}

} catch (error) {
console.error("Error covering cost:", error);
}
}
}
35 changes: 35 additions & 0 deletions services/blockchain-service/blockchain-service-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const KnowledgeCollectionAbi = require('dkg-evm-module/abi/KnowledgeCollection.j
const KnowledgeCollectionStorageAbi = require('dkg-evm-module/abi/KnowledgeCollectionStorage.json');
const AskStorageAbi = require('dkg-evm-module/abi/AskStorage.json');
const ChronosAbi = require('dkg-evm-module/abi/Chronos.json');
const PaymasterAbi = require('dkg-evm-module/abi/Paymaster.json');

export default class BlockchainServiceBase {
constructor(config = {}) {
Expand All @@ -50,6 +51,7 @@ export default class BlockchainServiceBase {
this.abis.KnowledgeCollectionStorage = KnowledgeCollectionStorageAbi;
this.abis.AskStorage = AskStorageAbi;
this.abis.Chronos = ChronosAbi;
this.abis.Paymaster = PaymasterAbi;

this.abis.KnowledgeCollectionStorage.filter((obj) => obj.type === 'event').forEach(
(event) => {
Expand Down Expand Up @@ -1204,4 +1206,37 @@ export default class BlockchainServiceBase {
convertToWei(ether) {
return Web3.utils.toWei(ether.toString(), 'ether');
}

//Paymaster functions
async deployPaymasterContractFunction(blockchain, hubAddress) {
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved

return this.callContractFunction('Paymaster', 'constructor', [hubAddress], blockchain);
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
}

async addAllowedAddressFunction(blockchain, public_adress) {
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved

return this.callContractFunction('Paymaster', 'addAllowedAddress', [public_adress], blockchain);
}

async removeAllowedAddressFunction(blockchain, public_adress) {

return this.callContractFunction('Paymaster', 'removeAllowedAddress', [public_adress], blockchain);
}

async fundFunction(blockchain, tokenAmount) {
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved

return this.callContractFunction('Paymaster', 'fundPaymaster', [tokenAmount], blockchain);

}

async withdrawFunction(blockchain, recipient, tokenAmount) {

return this.callContractFunction('Paymaster', 'withdraw', [], blockchain, recipient, tokenAmount);
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
}

async coverCostFunction(blockchain, tokenAmount) {

return this.callContractFunction('Paymaster', 'coverCost', [], blockchain, tokenAmount);
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
}

}