diff --git a/packages/restapi/src/lib/pushNotification/delegate.ts b/packages/restapi/src/lib/pushNotification/delegate.ts index ba548328e..95fbb3abd 100644 --- a/packages/restapi/src/lib/pushNotification/delegate.ts +++ b/packages/restapi/src/lib/pushNotification/delegate.ts @@ -50,9 +50,6 @@ export class Delegate extends PushNotificationBaseClass { add = async (delegate: string) => { try { this.checkSignerObjectExists(); - if (this.signer && !this.signer.provider) { - throw new Error('Provider is required'); - } if (validateCAIP(delegate)) { delegate = this.getAddressFromCaip(delegate); } @@ -62,6 +59,7 @@ export class Delegate extends PushNotificationBaseClass { throw new Error('Unsupported Chainid'); } const commAddress = CONFIG[this.env!][caip].EPNS_COMMUNICATOR_CONTRACT; + const commContract = this.createContractInstance( commAddress, config.ABIS.COMM, @@ -82,9 +80,6 @@ export class Delegate extends PushNotificationBaseClass { remove = async (delegate: string) => { try { this.checkSignerObjectExists(); - if (this.signer && !this.signer.provider) { - throw new Error('Provider is required'); - } if (validateCAIP(delegate)) { delegate = this.getAddressFromCaip(delegate); } diff --git a/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts b/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts index b0f0a611a..48c00feab 100644 --- a/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts +++ b/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts @@ -819,4 +819,4 @@ export class PushNotificationBaseClass { protected getAddressFromCaip(caipAddress: string): string { return caipAddress?.split(':')[caipAddress?.split(':').length - 1]; } -} +} \ No newline at end of file diff --git a/packages/restapi/tests/lib/notification/channel.test.ts b/packages/restapi/tests/lib/notification/channel.test.ts index 2ffa8a47c..79bd97480 100644 --- a/packages/restapi/tests/lib/notification/channel.test.ts +++ b/packages/restapi/tests/lib/notification/channel.test.ts @@ -5,6 +5,14 @@ dotenv.config({ path: path.resolve(__dirname, '../../../.env') }); import { PushAPI } from '../../../src/lib/pushapi/PushAPI'; import { expect } from 'chai'; import { ethers } from 'ethers'; +import { goerli, polygonMumbai, sepolia } from 'viem/chains'; +import { + createWalletClient, + http, + getContract, + createPublicClient, +} from 'viem'; +import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; describe('PushAPI.channel functionality', () => { let userAlice: PushAPI; @@ -17,6 +25,8 @@ describe('PushAPI.channel functionality', () => { let userNoChannel: PushAPI; let noChannelSigner: any; let noChannelAddress: string; + let viemUser: any; + let viemSigner: any; beforeEach(async () => { signer1 = new ethers.Wallet(`0x${process.env['WALLET_PRIVATE_KEY']}`); @@ -35,7 +45,11 @@ describe('PushAPI.channel functionality', () => { const WALLET = ethers.Wallet.createRandom(); noChannelSigner = new ethers.Wallet(WALLET.privateKey); noChannelAddress = await noChannelSigner.getAddress(); - + viemSigner = createWalletClient({ + account: privateKeyToAccount(`0x${process.env['WALLET_PRIVATE_KEY']}`), + chain: sepolia, + transport: http(), + }); enum ENV { PROD = 'prod', STAGING = 'staging', @@ -54,6 +68,8 @@ describe('PushAPI.channel functionality', () => { userBob = await PushAPI.initialize(signer1); // initialisation with a signer that has no channel userNoChannel = await PushAPI.initialize(noChannelSigner); + // viem signer + viemUser = await PushAPI.initialize(viemSigner); }); describe('channel :: info', () => { @@ -471,5 +487,54 @@ describe('PushAPI.channel functionality', () => { // console.log(res) expect(res).not.null; }, 10000000000); + + it.only('Should create channel setting viem signer', async () => { + const res = await viemUser.channel.setting([ + { + type: 1, + default: 1, + description: 'test1', + }, + { + type: 2, + default: 10, + description: 'test2', + data: { + upper: 100, + lower: 1, + }, + }, + { + type: 3, + default: { + lower: 10, + upper: 50, + }, + description: 'test3', + data: { + upper: 100, + lower: 1, + enabled: true, + ticker: 2, + }, + }, + { + type: 3, + default: { + lower: 3, + upper: 5, + }, + description: 'test4', + data: { + upper: 100, + lower: 1, + enabled: false, + ticker: 2, + }, + }, + ]); + // console.log(res) + expect(res).not.null; + }, 10000000000); }); }); diff --git a/packages/restapi/tests/lib/notification/delegate.test.ts b/packages/restapi/tests/lib/notification/delegate.test.ts index bb14b4162..6888d9b08 100644 --- a/packages/restapi/tests/lib/notification/delegate.test.ts +++ b/packages/restapi/tests/lib/notification/delegate.test.ts @@ -5,6 +5,14 @@ dotenv.config({ path: path.resolve(__dirname, '../../../.env') }); import { PushAPI } from '../../../src/lib/pushapi/PushAPI'; // Ensure correct import path import { expect } from 'chai'; import { ethers } from 'ethers'; +import { goerli, polygonMumbai, sepolia } from 'viem/chains'; +import { + createWalletClient, + http, + getContract, + createPublicClient, +} from 'viem'; +import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; // import tokenABI from './tokenABI'; describe('PushAPI.delegate functionality', () => { let userAlice: PushAPI; @@ -13,6 +21,7 @@ describe('PushAPI.delegate functionality', () => { let signer1: any; let account1: string; let signer2: any; + let viemUser: any; let account2: string; beforeEach(async () => { @@ -27,6 +36,12 @@ describe('PushAPI.delegate functionality', () => { `0x${process.env['WALLET_PRIVATE_KEY']}`, provider ); + const signer3 = createWalletClient({ + account: privateKeyToAccount(`0x${process.env['WALLET_PRIVATE_KEY']}`), + chain: sepolia, + transport: http(), + }); + account2 = await signer2.getAddress(); // initialisation with signer and provider @@ -35,6 +50,8 @@ describe('PushAPI.delegate functionality', () => { userAlice = await PushAPI.initialize(signer1); // initialisation without signer userBob = await PushAPI.initialize(signer1); + // initalisation with viem + viemUser = await PushAPI.initialize(signer3); }); describe('delegate :: add', () => { @@ -63,6 +80,14 @@ describe('PushAPI.delegate functionality', () => { expect(res).not.null; }, 100000000); + it('With viem signer and provider :: should add delegate', async () => { + const res = await viemUser.channel.delegate.add( + 'eip155:11155111:0x74415Bc4C4Bf4Baecc2DD372426F0a1D016Fa924' + ); + console.log(res); + expect(res).not.null; + }, 100000000); + it('With signer and provider :: should add delegate', async () => { const res = await userKate.channel.delegate.add( '0x74415Bc4C4Bf4Baecc2DD372426F0a1D016Fa924' @@ -110,6 +135,24 @@ describe('PushAPI.delegate functionality', () => { 'https://rpc-mumbai.maticvigil.com/v1' ); + signer2 = new ethers.Wallet( + `0x${process.env['WALLET_PRIVATE_KEY']}`, + provider + ); + userKate = await PushAPI.initialize(signer2); + const res = await userKate.channel.delegate.add( + '0x74415Bc4C4Bf4Baecc2DD372426F0a1D016Fa924' + ); + console.log(res); + expect(res).not.null; + }, 10000000); + + it('With viem signer: Should add delegate', async () => { + // create polygon mumbai provider + const provider = new ethers.providers.JsonRpcProvider( + 'https://rpc-mumbai.maticvigil.com' + ); + signer2 = new ethers.Wallet( `0x${process.env['WALLET_PRIVATE_KEY']}`, provider diff --git a/packages/restapi/tests/lib/notification/notification.test.ts b/packages/restapi/tests/lib/notification/notification.test.ts index 55c389e88..814928af9 100644 --- a/packages/restapi/tests/lib/notification/notification.test.ts +++ b/packages/restapi/tests/lib/notification/notification.test.ts @@ -200,7 +200,7 @@ describe('PushAPI.notification functionality', () => { ], } ); - console.log(res); + // console.log(res); expect(res).not.null; });