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 6 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
49 changes: 49 additions & 0 deletions examples/demo-paymaster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import PaymasterOperationsManager from '../managers/paymaster-operations-manager.js';
import BaseServiceManager from '../services/base-service-manager.js';

const config = {
nodeApiUrl: 'https://example.com/api',
blockchain: {
network: 'Base',
name: 'Base',
hubContract: '0xYourDeployedContractAddressHere',
rpc: 'https://base.rpc.url.here',
publicKey: '0xYourBasePublicKeyHere',
privateKey: '0xYourBasePrivateKeyHere'
},
};

(async () => {
try {

const baseServiceManager = new BaseServiceManager(config);
const services = baseServiceManager.getServices();

const paymasterManager = new PaymasterOperationsManager({
blockchainService: services.blockchainService,
inputService: services.inputService,
validationService: services.validationService
});

const deployedAddress = await paymasterManager.deployPaymasterContract({ blockchain: 'Ethereum' });
console.log('Deployed Address:', deployedAddress);

await paymasterManager.addAllowedAddress('0x1dD2C730a2BcD26d6aEf7DCCF171FC2AB1384d14', { blockchain: 'Ethereum' });
console.log('Added allowed address.');

await paymasterManager.removeAllowedAddress('0x1dD2C730a2BcD26d6aEf7DCCF171FC2AB1384d14', { blockchain: 'Ethereum' });
console.log('Removed allowed address.');

await paymasterManager.fundPaymaster(BigInt(1000000), { blockchain: 'Ethereum' });
console.log('Funded Paymaster.');

await paymasterManager.withdraw('0x1dD2C730a2BcD26d6aEf7DCCF171FC2AB1384d14', BigInt(500000), { blockchain: 'Ethereum' });
console.log('Withdrawal complete.');

await paymasterManager.coverCost(BigInt(200000), { blockchain: 'Ethereum' });
console.log('Covered cost.');

} catch (error) {
console.error('Error during tests:', error);
}
})();
102 changes: 102 additions & 0 deletions managers/paymaster-operations-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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.validatePaymasterAddress(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 paymasterAddressContract = await this.callContractFunction(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename this to receipt

'PaymasterManager',
'constructor',
[],
blockchain,
);

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

return paymasterAddress;
}

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

validatePaymasterAddress(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);
}


}