From cf9e391ecfb7282f2f4580c1858deba5856d34c0 Mon Sep 17 00:00:00 2001 From: Kuduzow Akhmad Date: Tue, 24 Jan 2023 10:37:57 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D0=BE=D0=B4=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=BE=D0=BB=D1=83?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B0=D0=B4=D1=80=D0=B5=D1=81?= =?UTF-8?q?=D0=B0=20=D1=80=D0=B5=D1=84=D0=B5=D1=80=D1=80=D0=B0=D0=BB=D0=B0?= =?UTF-8?q?=20(#32)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Добавлен новый метод для получения адреса реферрала * Апнута версия пакета * Добавлена привязка контекста к методу * Апнута версия пакета * Доработа логика генерации apiUrl для тестинг-окружения реферальной системы * URL custom-окружение обрабатывается теперь также как testing * analytics schema updated * fix zod schema by add z.record Co-authored-by: kuduzow --- package.json | 4 +-- src/OrionUnit/index.ts | 2 +- src/services/ReferralSystem/index.ts | 36 +++++++++++++++---- .../schemas/distinctAnalyticsSchema.ts | 9 +++-- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index eb48507..898a692 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "@orionprotocol/sdk", - "version": "0.15.29", + "name": "@orionprotocol/sdk", + "version": "0.15.27-rc.7", "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 7e8c720..b991580 100644 --- a/src/OrionUnit/index.ts +++ b/src/OrionUnit/index.ts @@ -145,7 +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`); + this.referralSystem = new ReferralSystem(options?.api ?? customApi, env); } get siblings() { diff --git a/src/services/ReferralSystem/index.ts b/src/services/ReferralSystem/index.ts index 7db8219..81c698a 100644 --- a/src/services/ReferralSystem/index.ts +++ b/src/services/ReferralSystem/index.ts @@ -19,23 +19,47 @@ type SignatureType = { class ReferralSystem { private apiUrl: string; - constructor(apiUrl: string) { - this.apiUrl = apiUrl; + constructor(apiUrl: string, env: string) { + this.apiUrl = ReferralSystem.getActualApiUrl(apiUrl, env); this.getLink = this.getLink.bind(this); this.getSubscribersList = this.getSubscribersList.bind(this); this.createReferralLink = this.createReferralLink.bind(this); this.subscribeToReferral = this.subscribeToReferral.bind(this); + this.getMyReferral = this.getMyReferral.bind(this); } - getLink = (refererAddress: string) => fetchWithValidation(`${this.apiUrl}/view/link`, linkSchema, { + // ресурсы реферальной системы в тестинг окружении имеют вид + // testing.orionprotocol.io/referral-api вместо обычного + // testing.orionprotocol.io/bsc-testnet/referral-api, поэтому лишняя часть вырезается + static getActualApiUrl = (apiUrl: string, env: string) => { + if (env === 'testing' || env === 'custom') { + const { protocol, hostname } = new URL(apiUrl); + + return `${protocol}//${hostname}/referral-api`; + } + + return `${apiUrl}/referral-api`; + }; + + getLink = (refererAddress: string) => fetchWithValidation(`${this.apiUrl}/referer/view/link`, linkSchema, { headers: { 'referer-address': refererAddress, }, }); + getMyReferral = (myWalletAddress: string) => fetchWithValidation( + `${this.apiUrl}/referral/view/link`, + linkSchema, + { + headers: { + referral: myWalletAddress, + }, + }, + ); + getSubscribersList = (refererAddress: string) => fetchWithValidation( - `${this.apiUrl}/view/distinct-analytics`, + `${this.apiUrl}/referer/view/distinct-analytics`, distinctAnalyticsSchema, { headers: { @@ -45,7 +69,7 @@ class ReferralSystem { ); createReferralLink = (payload: CreateLinkPayloadType, signature: SignatureType) => fetchWithValidation( - `${this.apiUrl}/create`, + `${this.apiUrl}/referer/create`, linkSchema, { headers: { @@ -57,7 +81,7 @@ class ReferralSystem { ); subscribeToReferral = (payload: SubscribePayloadType, signature: SignatureType) => fetchWithValidation( - `${this.apiUrl}/subscribe`, + `${this.apiUrl}/referer/subscribe`, linkSchema, { headers: { diff --git a/src/services/ReferralSystem/schemas/distinctAnalyticsSchema.ts b/src/services/ReferralSystem/schemas/distinctAnalyticsSchema.ts index bd5ded8..620aef0 100644 --- a/src/services/ReferralSystem/schemas/distinctAnalyticsSchema.ts +++ b/src/services/ReferralSystem/schemas/distinctAnalyticsSchema.ts @@ -2,16 +2,21 @@ import { z } from 'zod'; const distinctAnalyticsSchema = z.object({ referer: z.string(), - - refs_info: z.array( + refs_info: z.record( + z.string(), z.object({ referral_address: z.string(), referral_earned_fees: z.number(), referer_earned_fees: z.number(), relative_ref_level: z.number(), + reward_record_hash: z.string(), timestamp: z.number(), + latest_timestamp: z.number(), + latest_block: z.number(), }), ), + total_earned: z.number(), + total_sent_to_governance: z.number(), }); export default distinctAnalyticsSchema;