From 745bec7da58113531862d37934d3e8fd920c5af6 Mon Sep 17 00:00:00 2001 From: Kuduzow Akhmad Date: Thu, 15 Dec 2022 12:03:45 +0300 Subject: [PATCH] Added new ReferralSystem service (#30) * Added new ReferralSystem service * Version conflict resolved after rebase * Fixed http header because of backend updates * Updated package version Co-authored-by: kuduzow --- package.json | 2 +- src/OrionUnit/index.ts | 4 + src/services/ReferralSystem/index.ts | 73 +++++++++++++++++++ .../schemas/distinctAnalyticsSchema.ts | 17 +++++ src/services/ReferralSystem/schemas/index.ts | 2 + .../ReferralSystem/schemas/linkSchema.ts | 9 +++ 6 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/services/ReferralSystem/index.ts create mode 100644 src/services/ReferralSystem/schemas/distinctAnalyticsSchema.ts create mode 100644 src/services/ReferralSystem/schemas/index.ts create mode 100644 src/services/ReferralSystem/schemas/linkSchema.ts diff --git a/package.json b/package.json index 8ffaa3e..02e5656 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.15.22", + "version": "0.15.24-rc.0", "description": "Orion Protocol SDK", "main": "./lib/esm/index.js", "module": "./lib/esm/index.js", diff --git a/src/OrionUnit/index.ts b/src/OrionUnit/index.ts index b163b92..7e8c720 100644 --- a/src/OrionUnit/index.ts +++ b/src/OrionUnit/index.ts @@ -8,6 +8,7 @@ import Exchange from './Exchange'; import FarmingManager from './FarmingManager'; import { chains, envs } from '../config'; import { isValidChainId } from '../utils'; +import { ReferralSystem } from '../services/ReferralSystem'; const orionAnalyticsUrl = 'https://trade.orionprotocol.io'; @@ -49,6 +50,8 @@ export default class OrionUnit { public readonly apiUrl: string; + public readonly referralSystem: ReferralSystem; + constructor( chain: string, env: string, @@ -142,6 +145,7 @@ export default class OrionUnit { this.orionAnalytics = new OrionAnalytics(orionAnalyticsUrl); this.exchange = new Exchange(this); this.farmingManager = new FarmingManager(this); + this.referralSystem = new ReferralSystem(`${options?.api ?? customApi}/referral-api/referer`); } get siblings() { diff --git a/src/services/ReferralSystem/index.ts b/src/services/ReferralSystem/index.ts new file mode 100644 index 0000000..7db8219 --- /dev/null +++ b/src/services/ReferralSystem/index.ts @@ -0,0 +1,73 @@ +import fetchWithValidation from '../../fetchWithValidation'; +import distinctAnalyticsSchema from './schemas/distinctAnalyticsSchema'; +import linkSchema from './schemas/linkSchema'; + +type CreateLinkPayloadType = { + referer: string; + link_option: number; +}; + +type SubscribePayloadType = { + ref_target: string; + referral: string; +} + +type SignatureType = { + signature: string; +}; + +class ReferralSystem { + private apiUrl: string; + + constructor(apiUrl: string) { + this.apiUrl = apiUrl; + + this.getLink = this.getLink.bind(this); + this.getSubscribersList = this.getSubscribersList.bind(this); + this.createReferralLink = this.createReferralLink.bind(this); + this.subscribeToReferral = this.subscribeToReferral.bind(this); + } + + getLink = (refererAddress: string) => fetchWithValidation(`${this.apiUrl}/view/link`, linkSchema, { + headers: { + 'referer-address': refererAddress, + }, + }); + + getSubscribersList = (refererAddress: string) => fetchWithValidation( + `${this.apiUrl}/view/distinct-analytics`, + distinctAnalyticsSchema, + { + headers: { + 'referer-address': refererAddress, + }, + }, + ); + + createReferralLink = (payload: CreateLinkPayloadType, signature: SignatureType) => fetchWithValidation( + `${this.apiUrl}/create`, + linkSchema, + { + headers: { + 'Content-type': 'application/json', + }, + method: 'POST', + body: JSON.stringify({ payload, signature }), + }, + ); + + subscribeToReferral = (payload: SubscribePayloadType, signature: SignatureType) => fetchWithValidation( + `${this.apiUrl}/subscribe`, + linkSchema, + { + headers: { + 'Content-type': 'application/json', + }, + method: 'POST', + body: JSON.stringify({ payload, signature }), + }, + ); +} + +export * as schemas from './schemas'; +export { ReferralSystem }; diff --git a/src/services/ReferralSystem/schemas/distinctAnalyticsSchema.ts b/src/services/ReferralSystem/schemas/distinctAnalyticsSchema.ts new file mode 100644 index 0000000..bd5ded8 --- /dev/null +++ b/src/services/ReferralSystem/schemas/distinctAnalyticsSchema.ts @@ -0,0 +1,17 @@ +import { z } from 'zod'; + +const distinctAnalyticsSchema = z.object({ + referer: z.string(), + + refs_info: z.array( + z.object({ + referral_address: z.string(), + referral_earned_fees: z.number(), + referer_earned_fees: z.number(), + relative_ref_level: z.number(), + timestamp: z.number(), + }), + ), +}); + +export default distinctAnalyticsSchema; diff --git a/src/services/ReferralSystem/schemas/index.ts b/src/services/ReferralSystem/schemas/index.ts new file mode 100644 index 0000000..93c9a19 --- /dev/null +++ b/src/services/ReferralSystem/schemas/index.ts @@ -0,0 +1,2 @@ +export { default as linkSchema } from './linkSchema'; +export { default as distinctAnalyticsSchema } from './distinctAnalyticsSchema'; diff --git a/src/services/ReferralSystem/schemas/linkSchema.ts b/src/services/ReferralSystem/schemas/linkSchema.ts new file mode 100644 index 0000000..2e9022b --- /dev/null +++ b/src/services/ReferralSystem/schemas/linkSchema.ts @@ -0,0 +1,9 @@ +import { z } from 'zod'; + +const linkSchema = z.object({ + referer: z.string(), + ref_link: z.string(), + option: z.number(), +}); + +export default linkSchema;