Skip to content

Commit

Permalink
fix: Merge branch 'main' into deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammeds1992 committed Dec 4, 2023
2 parents 9f87f1b + 8de11ff commit 751432b
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 56 deletions.
21 changes: 10 additions & 11 deletions packages/restapi/src/lib/pushNotification/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,23 @@ export class Channel extends PushNotificationBaseClass {
*/
subscribers = async (options?: ChannelInfoOptions) => {
try {
const {
channel = this.account
? getFallbackETHCAIPAddress(this.env!, this.account!)
: null,
} = options || {};

let channel = options?.channel
? options.channel
: this.account
? getFallbackETHCAIPAddress(this.env!, this.account!)
: null;
this.checkUserAddressExists(channel!);
if (!validateCAIP(channel!)) {
throw new Error('Invalid CAIP');
}
channel = validateCAIP(channel!)
? channel
: getFallbackETHCAIPAddress(this.env!, channel!);
if (options && options.page) {
return await PUSH_CHANNEL.getSubscribers({
channel: channel!,
env: this.env,
page: options.page,
limit: options.limit ?? 10,
setting: options.setting?? false,
category: options.category
setting: options.setting ?? false,
category: options.category,
});
} else {
/** @dev - Fallback to deprecated method when page is not provided ( to ensure backward compatibility ) */
Expand Down
51 changes: 25 additions & 26 deletions packages/restapi/src/lib/pushNotification/delegate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ENV } from '../constants';
import { ENV } from '../constants';
import { SignerType } from '../types';
import { ChannelInfoOptions } from './PushNotificationTypes';
import CONFIG, * as config from '../config';
Expand All @@ -18,15 +18,21 @@ export class Delegate extends PushNotificationBaseClass {
*/
get = async (options?: ChannelInfoOptions) => {
try {
const {
channel = this.account
? getFallbackETHCAIPAddress(this.env!, this.account!)
: null,
} = options || {};
// const {
// channel = this.account
// ? getFallbackETHCAIPAddress(this.env!, this.account!)
// : null,
// } = options || {};
let channel = options?.channel
? options.channel
: this.account
? getFallbackETHCAIPAddress(this.env!, this.account!)
: null;
this.checkUserAddressExists(channel!);
channel = validateCAIP(channel!)
? channel
: getFallbackETHCAIPAddress(this.env!, channel!);
this.checkUserAddressExists(channel!);
if (!validateCAIP(channel!)) {
throw new Error('Invalid CAIP');
}
return await PUSH_CHANNEL.getDelegates({
channel: channel!,
env: this.env,
Expand All @@ -43,14 +49,14 @@ export class Delegate extends PushNotificationBaseClass {
*/
add = async (delegate: string) => {
try {
if (!validateCAIP(delegate)) {
throw new Error('Invalid CAIP');
this.checkSignerObjectExists();
if (this.signer && !this.signer.provider) {
throw new Error('Provider is required');
}

const networkDetails = await this.getChianId(this.signer!);
if (networkDetails !== parseInt(delegate.split(':')[1])) {
return new Error('Signer and CAIP chain id doesnt match');
if (validateCAIP(delegate)) {
delegate = this.getAddressFromCaip(delegate);
}
const networkDetails = await this.getChianId(this.signer!);
const caip = `eip155:${networkDetails}`;
if (!CONFIG[this.env!][caip] || !config.VIEM_CONFIG[this.env!][caip]) {
throw new Error('Unsupported Chainid');
Expand All @@ -61,10 +67,7 @@ export class Delegate extends PushNotificationBaseClass {
config.ABIS.COMM,
config.VIEM_CONFIG[this.env!][caip].NETWORK
);
const addDelegateRes = await this.addDelegator(
commContract,
delegate.split(':')[2]
);
const addDelegateRes = await this.addDelegator(commContract, delegate);
return { transactionHash: addDelegateRes };
} catch (error) {
throw new Error(`Push SDK Error: Contract : delegate::add : ${error}`);
Expand All @@ -82,14 +85,10 @@ export class Delegate extends PushNotificationBaseClass {
if (this.signer && !this.signer.provider) {
throw new Error('Provider is required');
}
if (!validateCAIP(delegate)) {
throw new Error('Invalid CAIP');
if (validateCAIP(delegate)) {
delegate = this.getAddressFromCaip(delegate);
}

const networkDetails = await this.getChianId(this.signer!);
if (networkDetails !== parseInt(delegate.split(':')[1])) {
return new Error('Signer and CAIP chain id doesnt match');
}
const caip = `eip155:${networkDetails}`;
if (!CONFIG[this.env!][caip] || !config.VIEM_CONFIG[this.env!][caip]) {
throw new Error('Unsupported Chainid');
Expand All @@ -102,7 +101,7 @@ export class Delegate extends PushNotificationBaseClass {
);
const removeDelegateRes = await this.removeDelegator(
commContract,
delegate.split(':')[2]
delegate
);
return { transactionHash: removeDelegateRes };
} catch (error) {
Expand Down
21 changes: 11 additions & 10 deletions packages/restapi/src/lib/pushNotification/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
getFallbackETHCAIPAddress,
} from '../helpers';


import { PushNotificationBaseClass } from './pushNotificationBase';
// ERROR CONSTANTS
const ERROR_CHANNEL_NEEDED = 'Channel is needed';
Expand Down Expand Up @@ -45,22 +44,20 @@ export class Notification extends PushNotificationBaseClass {
options?: FeedsOptions
) => {
const {
account = this.account
? getFallbackETHCAIPAddress(this.env!, this.account!)
: null,
page = Constants.PAGINATION.INITIAL_PAGE,
limit = Constants.PAGINATION.LIMIT,
channels = [],
raw = false,
} = options || {};
try {
const account = options?.account
? options.account
: this.account
? getFallbackETHCAIPAddress(this.env!, this.account!)
: null;
// guest mode and valid address check
this.checkUserAddressExists(account!);
if (!validateCAIP(account!)) {
throw new Error('Invalid CAIP');
}
const nonCaipAccount =
account?.split(':')[account?.split(':').length - 1];
const nonCaipAccount = this.getAddressFromCaip(account!);
if (channels.length == 0) {
// else return the response
return await PUSH_USER.getFeeds({
Expand Down Expand Up @@ -90,11 +87,15 @@ export class Notification extends PushNotificationBaseClass {
subscriptions = async (options?: SubscriptionOptions) => {
try {
const {
account = this.account,
// TODO: to be used once pagination is implemeted at API level
page = Constants.PAGINATION.INITIAL_PAGE,
limit = Constants.PAGINATION.LIMIT,
} = options || {};
const account = options?.account
? options.account
: this.account
? getFallbackETHCAIPAddress(this.env!, this.account!)
: null;
this.checkUserAddressExists(account!);
return await PUSH_USER.getSubscriptions({
user: account!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -824,4 +824,8 @@ export class PushNotificationBaseClass {
return null;
}
}

protected getAddressFromCaip(caipAddress: string): string {
return caipAddress?.split(':')[caipAddress?.split(':').length - 1];
}
}
18 changes: 18 additions & 0 deletions packages/restapi/tests/lib/notification/channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ describe('PushAPI.channel functionality', () => {
expect(res).not.null;
});

it('Without signer and account : Should return response as address is passed', async () => {
const res = await userBob.channel.subscribers({
channel: '0x93A829d16DE51745Db0530A0F8E8A9B8CA5370E5',
});
// console.log(res)
expect(res).not.null;
});

it('Without signer and account : Should return response as address is passed', async () => {
const res = await userBob.channel.subscribers({
channel: '0x93A829d16DE51745Db0530A0F8E8A9B8CA5370E5',
page: 1,
limit: 10,
});
// console.log(res)
expect(res).not.null;
});

it('Without signer and account : Should return response for alias address', async () => {
const res = await userBob.channel.subscribers({
channel: 'eip155:80001:0x93A829d16DE51745Db0530A0F8E8A9B8CA5370E5',
Expand Down
42 changes: 34 additions & 8 deletions packages/restapi/tests/lib/notification/delegate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ describe('PushAPI.delegate functionality', () => {
expect(res).not.null;
}, 100000000);

it('With signer and provider :: should add delegate', async () => {
const res = await userKate.channel.delegate.add(
'0x74415Bc4C4Bf4Baecc2DD372426F0a1D016Fa924'
);
console.log(res);
expect(res).not.null;
}, 100000000);

it('With signer and provider :: should throw error as delegate caip and provider doesnt match', async () => {
await expect(() =>
userKate.channel.delegate.add(
Expand All @@ -71,6 +79,24 @@ describe('PushAPI.delegate functionality', () => {
).to.Throw;
});

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
);
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(
Expand All @@ -85,7 +111,7 @@ describe('PushAPI.delegate functionality', () => {
const res = await userKate.channel.delegate.add(
'eip155:80001:0x74415Bc4C4Bf4Baecc2DD372426F0a1D016Fa924'
);
// console.log(res);
// console.log(res);
expect(res).not.null;
}, 10000000);
});
Expand Down Expand Up @@ -147,19 +173,19 @@ describe('PushAPI.delegate functionality', () => {
it.skip('Without signer and account : Should throw error', async () => {
await expect(() => userBob.channel.delegate.get()).to.Throw;
});
it('Without signer : Should throw error for non-caip format', async () => {
await expect(() =>
userBob.channel.delegate.get({
channel: '0x74415Bc4C4Bf4Baecc2DD372426F0a1D016Fa924',
})
).to.Throw;
it('Without signer : Should get delegates', async () => {
const res = await userBob.channel.delegate.get({
channel: '0xD8634C39BBFd4033c0d3289C4515275102423681',
});
// console.log(res)
expect(res).not.null
});

it('Without signer : Should fetch delegates', async () => {
const res = await userBob.channel.delegate.get({
channel: 'eip155:11155111:0xD8634C39BBFd4033c0d3289C4515275102423681',
});
console.log(res);
// console.log(res);
expect(res).not.null;
});

Expand Down
20 changes: 19 additions & 1 deletion packages/restapi/tests/lib/notification/notification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ describe('PushAPI.notification functionality', () => {
expect(response).not.null;
});

it('Should return feeds when signer with provider is used', async () => {
const response = await userKate.notification.list('SPAM', {
account: "0xD8634C39BBFd4033c0d3289C4515275102423681"
});
// console.log(response)
expect(response).not.null;
});

it('Should return feeds when viem is used', async () => {
const response = await userViem.notification.list('SPAM');
console.log(response);
Expand Down Expand Up @@ -227,7 +235,17 @@ describe('PushAPI.notification functionality', () => {
const response = await userAlice.notification.subscriptions({
account: 'eip155:80001:0xD8634C39BBFd4033c0d3289C4515275102423681',
});
// console.log(response);
// console.log(response);
expect(response).not.null;
expect(response.lenth).not.equal(0);
});


it('Signer with account: Should return response', async () => {
const response = await userKate.notification.subscriptions({
account: '0xD8634C39BBFd4033c0d3289C4515275102423681',
});
// console.log(response);
expect(response).not.null;
expect(response.lenth).not.equal(0);
});
Expand Down

0 comments on commit 751432b

Please sign in to comment.