Merge branch 'main' into OP-2812-show-order-route-path-and-benefits

# Conflicts:
#	package.json
This commit is contained in:
Mikhail Gladchenko
2022-12-16 08:00:06 +00:00
6 changed files with 112 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@orionprotocol/sdk", "name": "@orionprotocol/sdk",
"version": "0.15.23-rc.2", "version": "0.15.25-rc.0",
"description": "Orion Protocol SDK", "description": "Orion Protocol SDK",
"main": "./lib/esm/index.js", "main": "./lib/esm/index.js",
"module": "./lib/esm/index.js", "module": "./lib/esm/index.js",
@@ -37,7 +37,7 @@
"url": "https://github.com/orionprotocol/sdk/issues" "url": "https://github.com/orionprotocol/sdk/issues"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^18.11.15", "@types/node": "^18.11.9",
"@types/socket.io-client": "1.4.33", "@types/socket.io-client": "1.4.33",
"@types/uuid": "^8.3.4", "@types/uuid": "^8.3.4",
"@types/ws": "^8.5.3", "@types/ws": "^8.5.3",
@@ -57,14 +57,14 @@
"webpack-cli": "^4.9.2" "webpack-cli": "^4.9.2"
}, },
"dependencies": { "dependencies": {
"@ethersproject/abstract-signer": "^5.7.0", "@ethersproject/abstract-signer": "^5.6.0",
"@ethersproject/providers": "^5.7.2", "@ethersproject/providers": "^5.6.2",
"@lukeed/csprng": "^1.0.1", "@lukeed/csprng": "^1.0.1",
"@orionprotocol/contracts": "0.0.10", "@orionprotocol/contracts": "0.0.10",
"bignumber.js": "^9.0.2", "bignumber.js": "^9.0.2",
"buffer": "^6.0.3", "buffer": "^6.0.3",
"crypto-browserify": "^3.12.0", "crypto-browserify": "^3.12.0",
"ethers": "^5.7.2", "ethers": "^5.6.2",
"isomorphic-unfetch": "^3.1.0", "isomorphic-unfetch": "^3.1.0",
"isomorphic-ws": "^5.0.0", "isomorphic-ws": "^5.0.0",
"just-clone": "^5.0.1", "just-clone": "^5.0.1",
@@ -72,8 +72,8 @@
"stream-browserify": "^3.0.0", "stream-browserify": "^3.0.0",
"tiny-invariant": "^1.2.0", "tiny-invariant": "^1.2.0",
"uuid": "^8.3.2", "uuid": "^8.3.2",
"ws": "^8.11.0", "ws": "^8.5.0",
"zod": "^3.20.2" "zod": "^3.17.3"
}, },
"homepage": "https://github.com/orionprotocol/sdk#readme", "homepage": "https://github.com/orionprotocol/sdk#readme",
"files": [ "files": [

View File

@@ -8,6 +8,7 @@ import Exchange from './Exchange';
import FarmingManager from './FarmingManager'; import FarmingManager from './FarmingManager';
import { chains, envs } from '../config'; import { chains, envs } from '../config';
import { isValidChainId } from '../utils'; import { isValidChainId } from '../utils';
import { ReferralSystem } from '../services/ReferralSystem';
const orionAnalyticsUrl = 'https://trade.orionprotocol.io'; const orionAnalyticsUrl = 'https://trade.orionprotocol.io';
@@ -49,6 +50,8 @@ export default class OrionUnit {
public readonly apiUrl: string; public readonly apiUrl: string;
public readonly referralSystem: ReferralSystem;
constructor( constructor(
chain: string, chain: string,
env: string, env: string,
@@ -142,6 +145,7 @@ export default class OrionUnit {
this.orionAnalytics = new OrionAnalytics(orionAnalyticsUrl); this.orionAnalytics = new OrionAnalytics(orionAnalyticsUrl);
this.exchange = new Exchange(this); this.exchange = new Exchange(this);
this.farmingManager = new FarmingManager(this); this.farmingManager = new FarmingManager(this);
this.referralSystem = new ReferralSystem(`${options?.api ?? customApi}/referral-api/referer`);
} }
get siblings() { get siblings() {

View File

@@ -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 };

View File

@@ -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;

View File

@@ -0,0 +1,2 @@
export { default as linkSchema } from './linkSchema';
export { default as distinctAnalyticsSchema } from './distinctAnalyticsSchema';

View File

@@ -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;