Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
112 changes: 112 additions & 0 deletions managers/paymaster-operations-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@

export default class PaymasterOperationsManager {

constructor(services) {
this.blockchainService = services.blockchainService;
this.inputService = services.inputService;
this.validationService = services.validationService;
}

/**
* @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).
* @returns {Object} Object containing hash of blockchain transaction and status.
*/

async deployPaymasterContract(options) {
try {

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


if(this.validationService.validateBlockchain(blockchain))
{
const paymasterAddress = await this.blockchainService.deployPaymasterContract(blockchain);

return paymasterAddress;
}

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

async addAllowedAddress(addresToBeWhitelested, options) {
try {

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

if(this.validationService.validatePaymasterAdress(blockchain, addresToBeWhitelested))
{
await this.blockchainService.addAllowedAddress(blockchain, addresToBeWhitelested);
}

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

async removeAllowedAddress(addresToBeWhitelested, options) {
try {

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


if(this.validationService.validatePaymasterAdress(blockchain, addresToBeWhitelested))
{
await this.blockchainService.removeAllowedAddress(blockchain, addresToBeWhitelested);
}

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


async fundPaymaster(tokenAmount, options) {
try {

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

if(this.validationService.validatePaymasterToken(blockchain,tokenAmount))
{
await this.blockchainService.fund(blockchain, tokenAmount);
}

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

async withdraw(recipient, tokenAmount, options) {
try {

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

if(this.validationService.validatePaymasterTokenAdress(blockchain, tokenAmount, recipient))
{
await this.blockchainService.withdraw(blockchain, recipient, tokenAmount);
}

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

async coverCost(tokenAmount, options) {
try {

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

if(this.validationService.validatePaymasterToken(blockchain, tokenAmount))
{
await this.blockchainService.coverCost(blockchain, tokenAmount);
}

} catch (error) {
console.error("Error covering cost:", error);
}
}
}
46 changes: 46 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,8 @@ 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');
const PaymasterManagerAbi = require('dkg-evm-module/abi/PaymasterManager.json');

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

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

//Paymaster functions
async deployPaymasterContract(blockchain) {

const paymasterAddress = await this.callContractFunction('PaymasterManager', 'constructor', [], blockchain);

let { id } = await this.decodeEventLogs(
paymasterAddress,
'deployPaymaster',
blockchain,
);

return { deployPaymaster: id, paymasterAddress };

}

async addAllowedAddressFunction(blockchain, public_adress) {

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

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

}

async withdrawFunction(blockchain, recipient, tokenAmount) {

return this.callContractFunction('Paymaster', 'withdraw', [recipient, tokenAmount], blockchain);
}

async coverCostFunction(blockchain, tokenAmount) {

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

}
20 changes: 20 additions & 0 deletions services/validation-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,4 +773,24 @@ export default class ValidationService {
minimumNumberOfFinalizationConfirmations,
);
}

//Paymaster validator

validatePaymasterAdress(blockchain, hubAddress) {
this.validateBlockchain(blockchain);
this.validateAddress(hubAddress);
}

validatePaymasterToken(blockchain, tokenAmount) {
this.validateBlockchain(blockchain);
this.validateTokenAmount(tokenAmount);
}

validatePaymasterTokenAdress(blockchain, tokenAmount, recipient) {
this.validateBlockchain(blockchain);
this.validateTokenAmount(tokenAmount);
this.validateAddress(recipient);
}


}