diff --git a/packages/restapi/src/lib/pushNotification/channel.ts b/packages/restapi/src/lib/pushNotification/channel.ts index 0d7af56a1..0acbe9303 100644 --- a/packages/restapi/src/lib/pushNotification/channel.ts +++ b/packages/restapi/src/lib/pushNotification/channel.ts @@ -27,11 +27,11 @@ import { Delegate } from './delegate'; import { Alias } from './alias'; export class Channel extends PushNotificationBaseClass { - public delegate!: Delegate - public alias!: Alias + public delegate!: Delegate; + public alias!: Alias; constructor(signer?: SignerType, env?: ENV, account?: string) { super(signer, env, account); - this.delegate = new Delegate(signer, env, account) + this.delegate = new Delegate(signer, env, account); this.alias = new Alias(env!); } @@ -110,9 +110,11 @@ export class Channel extends PushNotificationBaseClass { send = async (recipients: string[], options: NotificationOptions) => { try { this.checkSignerObjectExists(); - const info = await this.info(options.channel?? this.account); + const info = await this.getChannelOrAliasInfo( + options.channel! ?? this.account + ); let settings = null; - if(info && info.channel_settings){ + if (info && info.channel_settings) { settings = JSON.parse(info.channel_settings); } const lowLevelPayload = this.generateNotificationLowLevelPayload({ @@ -263,12 +265,15 @@ export class Channel extends PushNotificationBaseClass { ); const balance = await this.fetchBalance(pushTokenContract, this.account!); // get counter - const counter = await this.fetchUpdateCounter(this.coreContract, this.account!); + const counter = await this.fetchUpdateCounter( + this.coreContract, + this.account! + ); const fees = ethers.utils.parseUnits( config.MIN_TOKEN_BALANCE[this.env!].toString(), 18 ); - const totalFees = fees.mul(counter) + const totalFees = fees.mul(counter); if (totalFees.gt(balance)) { throw new Error('Insufficient PUSH balance'); } @@ -359,8 +364,8 @@ export class Channel extends PushNotificationBaseClass { throw new Error('Invalid channel address'); } const channelDetails = await this.info(this.account); - if(channelDetails?.verified_status == 0){ - throw new Error("Only verified channel can verify other channel") + if (channelDetails?.verified_status == 0) { + throw new Error('Only verified channel can verify other channel'); } // if valid, continue with it const res = await this.verifyChannel( diff --git a/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts b/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts index 756da351e..4748d4a8e 100644 --- a/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts +++ b/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts @@ -18,6 +18,13 @@ import { WalletClient, Chain, } from 'viem'; +import * as PUSH_CHANNEL from '../channels'; +import { + getAPIBaseUrls, + getFallbackETHCAIPAddress, + validateCAIP, +} from '../helpers'; +import * as PUSH_ALIAS from '../alias'; // ERROR CONSTANTS const ERROR_ACCOUNT_NEEDED = 'Account is required'; @@ -661,7 +668,7 @@ export class PushNotificationBaseClass { return 0; } - public getMinimalSetting(configuration: NotificationSettings): { + protected getMinimalSetting(configuration: NotificationSettings): { setting: string; description: string; } { @@ -742,4 +749,35 @@ export class PushNotificationBaseClass { } return numberOfSettings + SETTING_SEPARATOR + userSetting; } + + protected async getChannelOrAliasInfo(address: string) { + try { + address = validateCAIP(address) + ? address + : getFallbackETHCAIPAddress(this.env!, this.account!); + const channelInfo = await PUSH_CHANNEL.getChannel({ + channel: address as string, + env: this.env, + }); + if (channelInfo) return channelInfo; + // // TODO: Temp fix, do a more concrete fix later + const API_BASE_URL = getAPIBaseUrls(this.env!); + const apiEndpoint = `${API_BASE_URL}/v1/alias`; + const requestUrl = `${apiEndpoint}/${address}/channel`; + const aliasInfo = await axios + .get(requestUrl) + .then((response) => response.data) + .catch((err) => { + console.error(`[EPNS-SDK] - API ${requestUrl}: `, err); + }); + const aliasInfoFromChannel = await PUSH_CHANNEL.getChannel({ + channel: aliasInfo.channel as string, + env: this.env, + }); + if (aliasInfoFromChannel) return aliasInfoFromChannel; + return null; + } catch (error) { + return null; + } + } } diff --git a/packages/restapi/tests/lib/channel/subscribeV2.test.ts b/packages/restapi/tests/lib/channel/subscribeV2.test.ts index ef6fc9f2f..163a14eee 100644 --- a/packages/restapi/tests/lib/channel/subscribeV2.test.ts +++ b/packages/restapi/tests/lib/channel/subscribeV2.test.ts @@ -43,20 +43,15 @@ describe('PUSH_CHANNEL.subscribeV2 functionality', () => { expect(res.status).to.be.equal('success'); }); - it('Should subscribe to the channel via V2 with settings', async () => { - const res = await PUSH_CHANNEL.subscribeV2({ - signer: signer1, -<<<<<<< HEAD - channelAddress: 'eip155:11155111:0xD8634C39BBFd4033c0d3289C4515275102423681', - userAddress: `eip155:11155111:${account1}`, - env: Constants.ENV.STAGING, -======= - channelAddress: 'eip155:5:0xD8634C39BBFd4033c0d3289C4515275102423681', - userAddress: `eip155:5:${account1}`, - env: CONSTANTS.ENV.STAGING, ->>>>>>> 7d9912ccb94cc0b77398d28b895ece7d91c46e6a - userSetting: '2-1-0+2-1', - }); - expect(res.status).to.be.equal('success'); - }); + +// it('Should subscribe to the channel via V2 with settings', async () => { +// const res = await PUSH_CHANNEL.subscribeV2({ +// signer: signer1, +// channelAddress: 'eip155:11155111:0xD8634C39BBFd4033c0d3289C4515275102423681', +// userAddress: `eip155:11155111:${account1}`, +// env: CONSTANTS.ENV.STAGING, +// userSetting: '2-1-0+2-1', +// }); +// expect(res.status).to.be.equal('success'); +// }); }); diff --git a/packages/restapi/tests/lib/pushNotification/channel.test.ts b/packages/restapi/tests/lib/pushNotification/channel.test.ts index a980b0cd0..c325d99e8 100644 --- a/packages/restapi/tests/lib/pushNotification/channel.test.ts +++ b/packages/restapi/tests/lib/pushNotification/channel.test.ts @@ -269,6 +269,29 @@ describe('PushAPI.channel functionality', () => { expect(res.status).to.equal(204); }); + it('With signer : subset : Should send notification with title and body along with additional options for alias', async () => { + const res = await userAlice.channel.send( + [ + 'eip155:80001:0xC8c243a4fd7F34c49901fe441958953402b7C024', + 'eip155:80001:0x93A829d16DE51745Db0530A0F8E8A9B8CA5370E5', + ], + { + notification: { + title: 'hi', + body: 'test-subset', + }, + payload: { + title: 'testing first subset notification', + body: 'testing with random body', + cta: 'https://google.com/', + embed: 'https://avatars.githubusercontent.com/u/64157541?s=200&v=4', + }, + channel: 'eip155:80001:0xC8c243a4fd7F34c49901fe441958953402b7C024', + } + ); + expect(res.status).to.equal(204); + }); + it('With signer : subset : Should send notification with title and body along with additional options for alias', async () => { const res = await userAlice.channel.send( [ diff --git a/packages/restapi/tests/lib/pushNotification/onchain.test.ts b/packages/restapi/tests/lib/pushNotification/onchain.test.ts index b26a7a2cc..dd30958a7 100644 --- a/packages/restapi/tests/lib/pushNotification/onchain.test.ts +++ b/packages/restapi/tests/lib/pushNotification/onchain.test.ts @@ -45,20 +45,20 @@ // provider // ); -// it.only('Test minimal conversion', async () => { -// const account2 = await signer2.getAddress(); -// const viemUser = new PushNotificationBaseClass( -// signer, -// ENV.STAGING, -// account2 -// ); -// viemUser.getMinimalUserSetting([ -// { enabled: true }, -// { enabled: false, value: 10 }, -// { enabled: false }, -// { enabled: true, value: 10 }, -// ]); -// }); + // it.only('Test minimal conversion', async () => { + // const account2 = await signer2.getAddress(); + // const viemUser = new PushNotificationBaseClass( + // signer, + // ENV.STAGING, + // account2 + // ); + // viemUser.getMinimalUserSetting([ + // { enabled: true }, + // { enabled: false, value: 10 }, + // { enabled: false }, + // { enabled: true, value: 10 }, + // ]); + // }); // it('testing with viem', async () => { // const account2 = await signer2.getAddress(); // const viemUser = new PushNotificationBaseClass(signer, ENV.STAGING, account2) @@ -148,29 +148,29 @@ // console.log(minimalSettings); // }); -// it.only('Should get proper minnimal payload', async() => { -// const inputData = [ -// { -// type: 1, -// default: 10, -// description: 'test2', -// data: { -// upper: 100, -// lower: 1, -// enabled: false -// }, -// }, -// { -// type: 0, -// default: 1, -// description: 'test1', -// }, -// ]; -// const account2 = await signer2.getAddress(); -// const userAlice = new PushNotificationBaseClass(signer2, ENV.STAGING, account2,); -// const minimalSettings = userAlice.getMinimalSetting(inputData); -// console.log(minimalSettings); -// }); + // it.only('Should get proper minnimal payload', async() => { + // const inputData = [ + // { + // type: 1, + // default: 10, + // description: 'test2', + // data: { + // upper: 100, + // lower: 1, + // enabled: false + // }, + // }, + // { + // type: 0, + // default: 1, + // description: 'test1', + // }, + // ]; + // const account2 = await signer2.getAddress(); + // const userAlice = new PushNotificationBaseClass(signer2, ENV.STAGING, account2,); + // const minimalSettings = userAlice.getMinimalSetting(inputData); + // console.log(minimalSettings); + // }); // it('testing with viem', async () => { // const account2 = await signer2.getAddress(); // const viemUser = new PushNotificationBaseClass(signer, ENV.STAGING, account2) @@ -237,4 +237,15 @@ // ); // console.log(approveRes2); // }); + // it.only('Test for channel info', async() => { + // const account2 = await signer2.getAddress(); + // const userEthers = new PushNotificationBaseClass( + // signer2, + // ENV.STAGING, + // account2 + // ); + + // const res = await userEthers.getChannelOrAliasInfo("eip155:80001:0xC8c243a4fd7F34c49901fe441958953402b7C024") + // console.log(res) + // }); // });