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 4 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
101 changes: 101 additions & 0 deletions managers/paymaster-operations-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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.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);
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved

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.validatePaymasterAddress(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.fundPaymaster(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.withdrawPaymaster(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.coverCostPaymaster(blockchain, tokenAmount);
}
} catch (error) {
console.error('Error covering cost:', error);
}
}
}
61 changes: 57 additions & 4 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 @@ -144,8 +148,8 @@ export default class BlockchainServiceBase {
blockchain.name.startsWith('otp')
? DEFAULT_GAS_PRICE.OTP
: blockchain.name.startsWith('base')
? DEFAULT_GAS_PRICE.BASE
: DEFAULT_GAS_PRICE.GNOSIS,
? DEFAULT_GAS_PRICE.BASE
: DEFAULT_GAS_PRICE.GNOSIS,
'Gwei',
);
}
Expand Down Expand Up @@ -1144,8 +1148,8 @@ export default class BlockchainServiceBase {
blockchain.name.startsWith('otp')
? DEFAULT_GAS_PRICE.OTP
: blockchain.name.startsWith('base')
? DEFAULT_GAS_PRICE.BASE
: DEFAULT_GAS_PRICE.GNOSIS,
? DEFAULT_GAS_PRICE.BASE
: DEFAULT_GAS_PRICE.GNOSIS,
'Gwei',
);
}
Expand Down Expand Up @@ -1204,4 +1208,53 @@ 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);
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved

return { deployPaymaster: id, paymasterAddress };
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

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

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

async coverCostPaymaster(blockchain, tokenAmount) {
BogBogdan marked this conversation as resolved.
Show resolved Hide resolved
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);
}


}