Skip to content

Commit

Permalink
Load lock stats form GO API instead of chain
Browse files Browse the repository at this point in the history
  • Loading branch information
martincik committed May 29, 2024
1 parent 59c69c7 commit 05fb435
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 65 deletions.
1 change: 1 addition & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
PROGRAM_ID=6HW8dXjtiTGkD4jzXs7igdFmZExPpmwUrRN5195xGup
SOLANA_RPC_URL=https://wrpc.accessprotocol.co
SOLANA_NETWORK="mainnet-beta"
GO_API_URL=https://go-api.accessprotocol.co
FEE_PAYER_URL=https://go-api.accessprotocol.co/pay-fees
UNSTAKE_BASE_URL=https://hub.accessprotocol.co/creators
REWARDS_BASE_URL=https://hub.accessprotocol.co/rewards
Expand Down
1 change: 1 addition & 0 deletions .env.staging
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
PROGRAM_ID=9LPrKE24UaN9Bsf5rXCS4ZGor9VmjAUxkLCMKHr73sdV
SOLANA_NETWORK=devnet
SOLANA_RPC_URL=https://api.devnet.solana.com
GO_API_URL=https://st-go-api.accessprotocol.co
FEE_PAYER_URL=https://st-go-api.accessprotocol.co/pay-fees
UNSTAKE_BASE_URL=https://st-app.accessprotocol.co/creators
REWARDS_BASE_URL=https://st-app.accessprotocol.co/rewards
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"core-js": "2",
"preact": "^10.2.1",
"rc-util": "^5.24.4",
"react-input-slider": "^6.0.1"
"react-input-slider": "^6.0.1",
"zod": "^3.23.8"
},
"resolutions": {
"buffer": "6.0.3",
Expand Down
93 changes: 29 additions & 64 deletions src/layout/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
useRef,
useState,
} from 'preact/hooks';
import { PublicKey } from '@solana/web3.js';
import BN from 'bn.js';
import { Router, RouteComponent } from '../layout/Router';
import { Actions } from '../routes/Actions';
import { Stake } from '../routes/Stake';
Expand All @@ -19,10 +17,9 @@ import { WalletConnectButton } from '../components/wallet-adapter/ui/WalletConne
import { WalletModalButton } from '../components/wallet-adapter/ui/WalletModalButton';
import { useWallet } from '../components/wallet-adapter/useWallet';
import { ConfigContext } from '../AppContext';
import { BondAccount, BondV2Account, getBondV2Accounts, getBondAccounts, StakeAccount } from '@accessprotocol/js';
import env from '../libs/env';
import { useConnection } from '../components/wallet-adapter/useConnection';
import { clsxp } from '../libs/utils';
import { offchainBasicSubscriptionsSchema } from '../validations/subscriptions';

const Main = () => {
const { publicKey, wallet, connected } = useWallet();
Expand All @@ -33,8 +30,6 @@ const Main = () => {
poolId,
classPrefix,
} = useContext(ConfigContext);
const { connection } = useConnection();

const base58 = useMemo(() => publicKey?.toBase58(), [publicKey]);
const content = useMemo(() => {
if (!wallet || !base58) {
Expand All @@ -54,74 +49,44 @@ const Main = () => {
useEffect(() => {
if (connected && element && publicKey && poolId) {
(async () => {
const [stakeAccountKey] = StakeAccount.getKey(
env.PROGRAM_ID,
publicKey,
new PublicKey(poolId)
);
let stakeAccount;
try {
stakeAccount = await StakeAccount.retrieve(
connection,
stakeAccountKey
);
} catch (e) {
console.log('No stake account found');
}
const bondAccounts = await getBondAccounts(
connection,
publicKey,
env.PROGRAM_ID
);
let baSum = new BN(0);
if (bondAccounts != null && bondAccounts.length > 0) {
try {
baSum = bondAccounts.reduce((acc, bAccount) => {
const ba = BondAccount.deserialize(bAccount.account.data);
if (ba.stakePool.toBase58() === poolId) {
acc = acc.add(ba.totalStaked);
}
return acc;
}, new BN(0));
} catch (e) {
console.log('Error parsing bond accounts', e);
}
} else {
console.log('No bond accounts found');
const response = await fetch(`${env.GO_API_URL}/subscriptions/${publicKey.toBase58()}`);
if (!response.ok) {
console.log("ERROR: ", response.statusText);
return;
}
const bondV2Accounts = await getBondV2Accounts(
connection,
publicKey,
env.PROGRAM_ID
);
let ba2Sum = new BN(0);
if (bondV2Accounts != null && bondV2Accounts.length > 0) {
try {
ba2Sum = bondV2Accounts.reduce((acc, bAccount) => {
const ba = BondV2Account.deserialize(bAccount.account.data);
if (ba.pool.toBase58() === poolId) {
acc = acc.add(ba.amount);
}
return acc;
}, new BN(0));
} catch (e) {
console.log('Error parsing bond V2 account', e);

const json = await response.json();
const data = offchainBasicSubscriptionsSchema.parse(json);

const { staked, bonds, forever } = data.reduce((acc, item) => {
if (item.pool === poolId) {
return {
staked: acc.staked + (item?.locked ?? 0),
bonds: acc.bonds + (item?.bonds ?? 0),
forever: acc.forever + (item?.forever ?? 0),
}
} else {
return acc;
}
} else {
console.log('No bond V2 accounts found');
}
}, {
staked: 0,
bonds: 0,
forever: 0
});

const connectedEvent = new CustomEvent('connected', {
detail: {
address: base58,
locked: (stakeAccount?.stakeAmount.toNumber() || 0) + ba2Sum.toNumber() + baSum.toNumber(),
staked: stakeAccount?.stakeAmount.toNumber() || 0,
airdrop: baSum.toNumber(),
forever: ba2Sum.toNumber()
locked: staked + bonds + forever,
staked,
bonds,
forever
},
bubbles: true,
cancelable: true,
composed: false, // if you want to listen on parent turn this on
});
console.log("Connected event: ", connectedEvent);
element.dispatchEvent(connectedEvent);
})();
}
Expand Down
7 changes: 7 additions & 0 deletions src/libs/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const PROGRAM_ID = process.env.PROGRAM_ID;
const TOKEN_MINT = process.env.TOKEN_MINT;
const SOLANA_RPC_URL = process.env.SOLANA_RPC_URL;
const SOLANA_NETWORK = process.env.SOLANA_NETWORK;
const GO_API_URL = process.env.GO_API_URL;
const FEE_PAYER_URL = process.env.FEE_PAYER_URL;
const UNSTAKE_BASE_URL = process.env.UNSTAKE_BASE_URL;
const REWARDS_BASE_URL = process.env.REWARDS_BASE_URL;
Expand Down Expand Up @@ -42,12 +43,17 @@ if (!GET_ACS_URL) {
throw new Error('GET_ACS_URL must be set!');
}

if (!GO_API_URL) {
throw new Error('GO_API_URL must be set!');
}

interface Config {
SOLANA_RPC_URL: string;
SOLANA_NETWORK: string;
PROGRAM_ID: PublicKey;
TOKEN_MINT: PublicKey;
FEE_PAYER_URL: string;
GO_API_URL: string;
UNSTAKE_BASE_URL: string;
REWARDS_BASE_URL: string;
GET_ACS_URL: string;
Expand All @@ -59,6 +65,7 @@ const config: Config = {
PROGRAM_ID: new PublicKey(PROGRAM_ID),
TOKEN_MINT: new PublicKey(TOKEN_MINT),
FEE_PAYER_URL,
GO_API_URL,
UNSTAKE_BASE_URL,
REWARDS_BASE_URL,
GET_ACS_URL,
Expand Down
50 changes: 50 additions & 0 deletions src/validations/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { z } from "zod";

export const serverOffchainBasicSubscription = z.object({
AssetId: z.string().optional(),
Owner: z.string(),
Pool: z.string(),
Locked: z.number().optional(),
Bonds: z.number().optional(),
Forever: z.number().optional(),
CreatedAt: z.coerce.date(),
UpdatedAt: z.coerce.date().optional(),
});

export const serverOffchainBasicSubscriptions = z.array(
serverOffchainBasicSubscription,
);

export const offchainBasicSubscriptionSchema =
serverOffchainBasicSubscription.transform((item) => ({
assetId: item.AssetId,
owner: item.Owner,
pool: item.Pool,
locked: item.Locked,
bonds: item.Bonds,
forever: item.Forever,
createdAt: item.CreatedAt,
updatedAt: item.UpdatedAt,
}));

export const offchainBasicSubscriptionsSchema =
serverOffchainBasicSubscriptions.transform((items) =>
items.map((item) => ({
assetId: item.AssetId,
owner: item.Owner,
pool: item.Pool,
locked: item.Locked,
bonds: item.Bonds,
forever: item.Forever,
createdAt: item.CreatedAt,
updatedAt: item.UpdatedAt,
})),
);

export type OffchainBasicSubscription = z.infer<
typeof offchainBasicSubscriptionSchema
>;

export type OffchainBasicSubscriptions = z.infer<
typeof offchainBasicSubscriptionsSchema
>;
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10048,3 +10048,8 @@ yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==

zod@^3.23.8:
version "3.23.8"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.23.8.tgz#e37b957b5d52079769fb8097099b592f0ef4067d"
integrity sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==

0 comments on commit 05fb435

Please sign in to comment.