Skip to content

Commit

Permalink
fix: merge alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammeds1992 committed Oct 8, 2023
2 parents 3b12aa7 + 16dd007 commit e476962
Show file tree
Hide file tree
Showing 12 changed files with 7,007 additions and 5,369 deletions.
1,490 changes: 1,490 additions & 0 deletions packages/restapi/PushNotificationLowLevelAPI.md

Large diffs are not rendered by default.

10,663 changes: 5,314 additions & 5,349 deletions packages/restapi/README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/restapi/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"options": {
"preset": "angular",
"commitMessageFormat": "ci(${projectName}): 🎉 cut release to ${projectName}-v${version}",
"postTargets": ["restapi:build", "restapi:ci-publish"]
"postTargets": ["socket:build", "restapi:build", "restapi:ci-publish"]
}
},
"ci-version-beta": {
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/alias/getAliasInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const getAliasInfo = async (
const aliasChainId:number = ALIAS_CHAIN_ID[aliasChain][env];

const _alias = getCAIPWithChainId(alias, aliasChainId, 'Alias');
console.log(_alias)
// console.log(_alias)
const API_BASE_URL = getAPIBaseUrls(env);
const apiEndpoint = `${API_BASE_URL}/v1/alias`;
const requestUrl = `${apiEndpoint}/${_alias}/channel`;
Expand Down
7 changes: 4 additions & 3 deletions packages/restapi/src/lib/channels/subscribeV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import {
} from './signature.helpers';
import Constants, { ENV } from '../constants';
import { SignerType } from '../types';

export type SubscribeOptionsV2Type = {
signer: SignerType;
channelAddress: string;
userAddress: string;
userSetting?: string;
settings?: string;
verifyingContractAddress?: string;
env?: ENV;
onSuccess?: () => void;
Expand All @@ -28,7 +29,7 @@ export const subscribeV2 = async (options: SubscribeOptionsV2Type) => {
signer,
channelAddress,
userAddress,
userSetting = undefined,
settings = undefined,
verifyingContractAddress,
env = Constants.ENV.PROD,
onSuccess,
Expand Down Expand Up @@ -72,7 +73,7 @@ export const subscribeV2 = async (options: SubscribeOptionsV2Type) => {
channelCAIPDetails.address,
userCAIPDetails.address,
'Subscribe',
userSetting
settings
),
};
// sign a message using EIP712
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,4 @@ export const VIEM_CONFIG = {
EPNS_COMMUNICATOR_CONTRACT: '0x630b152e4185c63D7177c656b56b26f878C61572',
},
},
};
};
26 changes: 17 additions & 9 deletions packages/restapi/src/lib/pushNotification/PushNotificationTypes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ProgressHookType } from '../types';
import { GetAliasInfoOptionsType } from '../alias';
import {
ADDITIONAL_META_TYPE,
} from '../../lib/payloads/constants';
import { ADDITIONAL_META_TYPE } from '../../lib/payloads/constants';

export type SubscriptionOptions = {
account?: string;
Expand All @@ -16,6 +14,12 @@ export type ChannelInfoOptions = {
export type SubscribeUnsubscribeOptions = {
onSuccess?: () => void;
onError?: (err: Error) => void;
settings?: UserSetting[];
};

export type UserSetting = {
enabled: boolean;
value?: number;
};

export type AliasOptions = Omit<GetAliasInfoOptionsType, 'env'>;
Expand Down Expand Up @@ -50,6 +54,10 @@ export type IPayload = {
body?: string;
cta?: string;
embed?: string;
index?: {
index: number;
value?: number;
};
meta?: {
domain?: string;
type: `${ADDITIONAL_META_TYPE}+${number}`;
Expand Down Expand Up @@ -92,13 +100,13 @@ export type CreateChannelOptions = {
};

export type NotificationSetting = {
type: number,
default: number,
description: string,
type: number;
default: number;
description: string;
data?: {
upper: number;
lower: number;
}
}
};
};

export type NotificationSettings = NotificationSetting[]
export type NotificationSettings = NotificationSetting[];
9 changes: 6 additions & 3 deletions packages/restapi/src/lib/pushNotification/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class Notification extends PushNotificationBaseClass {
options?: SubscribeUnsubscribeOptions
) => {
try {
const { onSuccess, onError } = options || {};
const { onSuccess, onError, settings } = options || {};
// Vaidatiions
// validates if signer object is present
this.checkSignerObjectExists();
Expand All @@ -142,11 +142,14 @@ export class Notification extends PushNotificationBaseClass {
this.account!,
parseInt(caipDetail?.networkId as string)
);
return await PUSH_CHANNEL.subscribe({
// convert the setting to minimal version
const minimalSetting = this.getMinimalUserSetting(settings!)
return await PUSH_CHANNEL.subscribeV2({
signer: this.signer!,
channelAddress: channel,
userAddress: userAddressInCaip,
env: this.env,
settings: settings? '' : minimalSetting,
onSuccess: onSuccess,
onError: onError,
});
Expand Down Expand Up @@ -188,7 +191,7 @@ export class Notification extends PushNotificationBaseClass {
this.account!,
parseInt(caipDetail?.networkId as string)
);
return await PUSH_CHANNEL.unsubscribe({
return await PUSH_CHANNEL.unsubscribeV2({
signer: this.signer!,
channelAddress: channel,
userAddress: userAddressInCaip,
Expand Down
44 changes: 44 additions & 0 deletions packages/restapi/src/lib/pushNotification/pushNotificationBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
NotificationOptions,
CreateChannelOptions,
NotificationSettings,
UserSetting,
} from './PushNotificationTypes';
import CONFIG, * as config from '../config';
import { getAccountAddress } from '../chat/helpers';
Expand All @@ -27,6 +28,8 @@ const LENGTH_UPPER_LIMIT = 125;
const LENGTH_LOWER_LIMTI = 1;
const SETTING_DELIMITER = '-';
const SETTING_SEPARATOR = '+';
const SLIDER_TYPE = 2;
const BOOLEAN_TYPE = 1;

export const FEED_MAP = {
INBOX: false,
Expand Down Expand Up @@ -142,6 +145,20 @@ export class PushNotificationBaseClass {
}
const notificationType = this.getNotificationType(recipients, channel);
const identityType = IDENTITY_TYPE.DIRECT_PAYLOAD;
// fetch the minimal version based on conifg that was passed
let index;
if (options.payload?.index) {
if (options.payload?.index.value) {
index =
options.payload.index.index +
SETTING_DELIMITER +
SLIDER_TYPE +
SETTING_DELIMITER +
options.payload.index.value;
} else {
index = options.payload.index.index + SETTING_DELIMITER + BOOLEAN_TYPE;
}
}
const notificationPayload: ISendNotificationInputOptions = {
signer: signer,
channel: channel,
Expand All @@ -157,6 +174,7 @@ export class PushNotificationBaseClass {
etime: options.config?.expiry,
silent: options.config?.silent,
additionalMeta: options.payload?.meta,
index: options.payload?.index ? index : '',
},
recipients: notificationType.recipient,
graph: options.advanced?.graph,
Expand Down Expand Up @@ -681,4 +699,30 @@ export class PushNotificationBaseClass {
description: notificationSettingDescription.replace(/^\+/, ''),
};
}

protected getMinimalUserSetting(setting: UserSetting[]) {
let userSetting = '';
let numberOfSettings = 0;
for (let i = 0; i < setting.length; i++) {
const ele = setting[i];
const enabled = ele.enabled ? 1 : 0;
if (ele.enabled) numberOfSettings++;
// slider type
if (Object.keys(ele).includes('value')) {
userSetting =
userSetting +
SLIDER_TYPE +
SETTING_DELIMITER +
enabled +
SETTING_DELIMITER +
ele.value;
} else {
// boolean type
userSetting = userSetting + BOOLEAN_TYPE + SETTING_DELIMITER + enabled;
}
if (i != setting.length - 1)
userSetting = userSetting + SETTING_SEPARATOR;
}
return numberOfSettings + SETTING_SEPARATOR + userSetting;
}
}
128 changes: 128 additions & 0 deletions packages/restapi/tests/lib/pushNotification/base.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// import * as path from 'path';
// import * as dotenv from 'dotenv';
// dotenv.config({ path: path.resolve(__dirname, '../../../.env') });
// import { expect } from 'chai';
// import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
// import { PushNotificationBaseClass } from '../../../src/lib/pushNotification/pushNotificationBase';
// import * as config from '../../../src/lib/config';
// import {
// createWalletClient,
// http,
// getContract,
// createPublicClient,
// } from 'viem';
// import { abi } from './tokenABI';
// import { goerli, polygonMumbai } from 'viem/chains';
// import { BigNumber, ethers } from 'ethers';

// enum ENV {
// PROD = 'prod',
// STAGING = 'staging',
// DEV = 'dev',
// /**
// * **This is for local development only**
// */
// LOCAL = 'local',
// }
// describe.only('test', () => {
// const signer = createWalletClient({
// account: privateKeyToAccount(`0x${process.env['WALLET_PRIVATE_KEY']}`),
// chain: goerli,
// transport: http('https://goerli.blockpi.network/v1/rpc/public'),
// });

// const signer3 = createWalletClient({
// account: privateKeyToAccount(`0x${process.env['WALLET_PRIVATE_KEY']}`),
// chain: polygonMumbai,
// transport: http(),
// });

// const provider = new ethers.providers.JsonRpcProvider(
// 'https://goerli.blockpi.network/v1/rpc/public'
// );
// const signer2 = new ethers.Wallet(
// `0x${process.env['WALLET_PRIVATE_KEY']}`,
// 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('testing with viem', async () => {
// const account2 = await signer2.getAddress();
// const viemUser = new PushNotificationBaseClass(signer, ENV.STAGING, account2)
// const contract = viemUser.createContractInstance("0xd4E3ceC407cD36d9e3767cD189ccCaFBF549202C", config.ABIS.CORE, goerli)
// const res = await viemUser.fetchUpdateCounter(contract, account2);
// console.log(res)
// const viemContract = await userViem.createContractInstance(
// '0x2b9bE9259a4F5Ba6344c1b1c07911539642a2D33',
// abi,
// goerli
// );
// const balance = await userViem.fetchBalance(
// viemContract,
// '0xD8634C39BBFd4033c0d3289C4515275102423681'
// );
// console.log(balance);
// const allowance = await userViem.fetchAllownace(
// viemContract,
// '0xD8634C39BBFd4033c0d3289C4515275102423681',
// '0xd4E3ceC407cD36d9e3767cD189ccCaFBF549202C'
// );
// console.log(allowance);
// const approveAmount = ethers.BigNumber.from(10000);
// const approveRes = await userViem.approveToken(
// viemContract,
// '0xd4E3ceC407cD36d9e3767cD189ccCaFBF549202C',
// approveAmount
// );
// console.log(approveRes);

// const addDelegate = await userViem.delegate.add(
// 'eip155:5:0xD8634C39BBFd4033c0d3289C4515275102423681'
// );
// console.log(addDelegate);
// });

// it.only('test with ethers', async () => {
// const account2 = await signer2.getAddress();
// const userEthers = new PushNotificationBaseClass(signer2, ENV.STAGING, account2,);
// const contract = userEthers.createContractInstance("0xd4E3ceC407cD36d9e3767cD189ccCaFBF549202C", config.ABIS.CORE, goerli)
// const res = await userEthers.fetchUpdateCounter(contract, account2);
// console.log(res)
// const ethersContract = await userEthers.createContractInstance(
// '0x2b9bE9259a4F5Ba6344c1b1c07911539642a2D33',
// abi,
// goerli
// );
// const balance2 = await userEthers.fetchBalance(
// ethersContract,
// '0xD8634C39BBFd4033c0d3289C4515275102423681'
// );
// console.log(balance2);
// const allowance2 = await userEthers.fetchAllownace(
// ethersContract,
// '0xD8634C39BBFd4033c0d3289C4515275102423681',
// '0xd4E3ceC407cD36d9e3767cD189ccCaFBF549202C'
// );
// console.log(allowance2);
// const approveAmount2 = ethers.BigNumber.from(10000);
// const approveRes2 = await userEthers.approveToken(
// ethersContract,
// '0xd4E3ceC407cD36d9e3767cD189ccCaFBF549202C',
// approveAmount2
// );
// console.log(approveRes2);
// });
// });
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,4 @@ const ConnectButtonDiv = styled.div<IThemeProps>`
@media ${device.mobileL} {
font-size: 12px;
}
`;
`;
1 change: 0 additions & 1 deletion packages/uiweb/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2471,7 +2471,6 @@ clsx@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/clsx/-/clsx-2.0.0.tgz#12658f3fd98fafe62075595a5c30e43d18f3d00b"
integrity sha512-rQ1+kcj+ttHG0MKVGBUXwayCCF1oh39BF5COIpRzuCEv8Mwjv0XucrI2ExNTOn9IlLifGClWQcU9BrZORvtw6Q==

color-convert@^1.9.0:
version "1.9.3"
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.3.tgz#bb71850690e1f136567de629d2d5471deda4c1e8"
Expand Down

0 comments on commit e476962

Please sign in to comment.