Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openfunds: fix various fields (ids, symbol...) #221

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion anchor/programs/glam/src/state/model/openfunds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ impl From<&CompanyModel> for Vec<CompanyField> {
),
(
model.fund_website_of_man_co,
CompanyFieldName::FundWebsiteofManCo,
CompanyFieldName::FundWebsiteOfManCo,
),
]
.iter()
Expand Down
2 changes: 1 addition & 1 deletion anchor/programs/glam/src/state/openfunds/company.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum CompanyFieldName {
AuditorName,
CityofManCo,
EmailAddressOfManCo, // impl
FundWebsiteofManCo, // impl
FundWebsiteOfManCo, // impl
IsUNPRISignatory,
PhoneCountryCodeofManCo,
PhoneNumberofManCo,
Expand Down
116 changes: 107 additions & 9 deletions anchor/src/client/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ import {
getAccount,
getAssociatedTokenAddressSync,
getMint,
unpackMint,
Mint,
getExtensionData,
ExtensionType,
} from "@solana/spl-token";
import { TokenMetadata, unpack } from "@solana/spl-token-metadata";
import { WSOL, USDC } from "../constants";

import { Glam, GlamIDL, GlamProgram, getGlamProgramId } from "../glamExports";
import { ClusterOrCustom, GlamClientConfig } from "../clientConfig";
Expand Down Expand Up @@ -612,6 +618,32 @@ export class BaseClient {
return this.program.account.fundMetadataAccount.fetch(openfunds);
}

public async fetchShareClassAccount(
fundPDA: PublicKey,
shareId: number
): Promise<Mint> {
const shareClassMint = this.getShareClassPDA(fundPDA, shareId);
const connection = this.provider.connection;
return await getMint(
connection,
shareClassMint,
connection.commitment,
TOKEN_2022_PROGRAM_ID
);
}

getAssetIdFromCurrency(currency: string): string {
switch (currency.toUpperCase()) {
case "SOL":
case "WSOL":
return WSOL.toBase58();
case "USD":
case "USDC":
return USDC.toBase58();
}
return "";
}

remapKeyValueArray(vec: Array<any>): any {
return vec.reduce((prev, el) => {
prev[Object.keys(el.name)[0]] = el.value;
Expand All @@ -621,14 +653,48 @@ export class BaseClient {

getOpenfundsFromAccounts(
fundAccount: FundAccount,
openfundsAccount: FundMetadataAccount
openfundsAccount: FundMetadataAccount,
mints: any[]
): any {
let shareClasses = openfundsAccount.shareClasses.map((shareClass, i) => ({
id: fundAccount.shareClasses[i],
...this.remapKeyValueArray(shareClass),
}));
let shareClasses = openfundsAccount.shareClasses.map((shareClass, i) => {
let shareClassSymbol;
let shareClassCurrencyId;
let hasPermanentDelegate;

const mint = mints[i];
if (mint) {
const data = getExtensionData(
ExtensionType.TokenMetadata,
mint.tlvData
);
const metadata = data ? unpack(data) : ({} as TokenMetadata);
const permanentDelegate = getExtensionData(
ExtensionType.PermanentDelegate,
mint.tlvData
);

shareClassSymbol = metadata?.symbol;
hasPermanentDelegate = permanentDelegate ? "yes" : "no";
}

const remapped = this.remapKeyValueArray(shareClass);
shareClassCurrencyId = this.getAssetIdFromCurrency(
remapped.shareClassCurrency
);

return {
id: fundAccount.shareClasses[i],
// custom share class fields
shareClassId: fundAccount.shareClasses[i].toBase58(),
shareClassSymbol,
shareClassCurrencyId,
hasPermanentDelegate,
...remapped,
};
});
let fundManagers = openfundsAccount.fundManagers.map((fundManager) => ({
pubkey: fundAccount.manager,
portfolioManagerId: fundAccount.manager.toBase58(),
...this.remapKeyValueArray(fundManager),
}));

Expand Down Expand Up @@ -661,7 +727,8 @@ export class BaseClient {
fundModelFromAccounts(
fundPDA: PublicKey,
fundAccount: FundAccount,
openfundsAccount: FundMetadataAccount
openfundsAccount: FundMetadataAccount,
firstShareClass: Mint
): FundModel {
//TODO rebuild model from accounts
let fundModel = this.getFundModel(fundAccount);
Expand All @@ -677,7 +744,11 @@ export class BaseClient {

let fund = {
...fundModel,
...this.getOpenfundsFromAccounts(fundAccount, openfundsAccount),
fundId: fundPDA,
fundUri: `https://playground.glam.systems/products/${fundPDA}`,
...this.getOpenfundsFromAccounts(fundAccount, openfundsAccount, [
firstShareClass,
]),
};

fund.idStr = fundPDA.toBase58();
Expand All @@ -696,7 +767,13 @@ export class BaseClient {
public async fetchFund(fundPDA: PublicKey): Promise<FundModel> {
const fundAccount = await this.fetchFundAccount(fundPDA);
const openfundsAccount = await this.fetchFundMetadataAccount(fundPDA);
return this.fundModelFromAccounts(fundPDA, fundAccount, openfundsAccount);
const firstShareClass = await this.fetchShareClassAccount(fundPDA, 0);
return this.fundModelFromAccounts(
fundPDA,
fundAccount,
openfundsAccount,
firstShareClass
);
}

public async fetchAllFunds(): Promise<FundModel[]> {
Expand All @@ -708,12 +785,33 @@ export class BaseClient {
openfundsCache.set(of.publicKey.toBase58(), of.account);
});

/* fetch first mint */
let mintCache = new Map<string, Mint>();
const connection = this.provider.connection;
const mintAddresses = (fundAccounts || [])
.map((f) => f.account.shareClasses[0])
.filter((addr) => !!addr);
const mintAccounts = await connection.getMultipleAccountsInfo(
mintAddresses
);
(mintAccounts || []).forEach((info, j) => {
const mintInfo = unpackMint(
mintAddresses[j],
info,
TOKEN_2022_PROGRAM_ID
);
mintCache.set(mintAddresses[j].toBase58(), mintInfo);
});

const funds = (fundAccounts || []).map((f) =>
this.fundModelFromAccounts(
f.publicKey,
f.account,
openfundsCache.get(f.account.openfunds.toBase58()) ||
({} as FundMetadataAccount)
({} as FundMetadataAccount),
mintCache.get(
f.account.shareClasses[0] ? f.account.shareClasses[0].toBase58() : ""
) || ({} as Mint)
)
);

Expand Down
56 changes: 3 additions & 53 deletions anchor/target/idl/glam.json
Original file line number Diff line number Diff line change
Expand Up @@ -3714,58 +3714,8 @@
"errors": [
{
"code": 6000,
"name": "FundNotActive",
"msg": "Fund is not active"
},
{
"code": 6001,
"name": "InvalidShareClass",
"msg": "Share class not allowed to subscribe"
},
{
"code": 6002,
"name": "InvalidAssetSubscribe",
"msg": "Asset not allowed to subscribe"
},
{
"code": 6003,
"name": "InvalidPricingOracle",
"msg": "Invalid oracle for asset price"
},
{
"code": 6004,
"name": "InvalidRemainingAccounts",
"msg": "Invalid accounts: the transaction is malformed"
},
{
"code": 6005,
"name": "InvalidTreasuryAccount",
"msg": "Invalid treasury ata"
},
{
"code": 6006,
"name": "InvalidSignerAccount",
"msg": "Invalid signer ata"
},
{
"code": 6007,
"name": "InvalidAssetPrice",
"msg": "Invalid asset price"
},
{
"code": 6008,
"name": "InvalidStableCoinPriceForSubscribe",
"msg": "Subscription not allowed: invalid stable coin price"
},
{
"code": 6009,
"name": "SubscribeRedeemPaused",
"msg": "Fund is paused for subscription and redemption"
},
{
"code": 6010,
"name": "InvalidPolicyAccount",
"msg": "Policy account is mandatory"
"name": "NotAuthorized",
"msg": "Signer is not authorized"
}
],
"types": [
Expand Down Expand Up @@ -3855,7 +3805,7 @@
"name": "EmailAddressOfManCo"
},
{
"name": "FundWebsiteofManCo"
"name": "FundWebsiteOfManCo"
},
{
"name": "IsUNPRISignatory"
Expand Down
56 changes: 3 additions & 53 deletions anchor/target/types/glam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3720,58 +3720,8 @@ export type Glam = {
"errors": [
{
"code": 6000,
"name": "fundNotActive",
"msg": "Fund is not active"
},
{
"code": 6001,
"name": "invalidShareClass",
"msg": "Share class not allowed to subscribe"
},
{
"code": 6002,
"name": "invalidAssetSubscribe",
"msg": "Asset not allowed to subscribe"
},
{
"code": 6003,
"name": "invalidPricingOracle",
"msg": "Invalid oracle for asset price"
},
{
"code": 6004,
"name": "invalidRemainingAccounts",
"msg": "Invalid accounts: the transaction is malformed"
},
{
"code": 6005,
"name": "invalidTreasuryAccount",
"msg": "Invalid treasury ata"
},
{
"code": 6006,
"name": "invalidSignerAccount",
"msg": "Invalid signer ata"
},
{
"code": 6007,
"name": "invalidAssetPrice",
"msg": "Invalid asset price"
},
{
"code": 6008,
"name": "invalidStableCoinPriceForSubscribe",
"msg": "Subscription not allowed: invalid stable coin price"
},
{
"code": 6009,
"name": "subscribeRedeemPaused",
"msg": "Fund is paused for subscription and redemption"
},
{
"code": 6010,
"name": "invalidPolicyAccount",
"msg": "Policy account is mandatory"
"name": "notAuthorized",
"msg": "Signer is not authorized"
}
],
"types": [
Expand Down Expand Up @@ -3861,7 +3811,7 @@ export type Glam = {
"name": "emailAddressOfManCo"
},
{
"name": "fundWebsiteofManCo"
"name": "fundWebsiteOfManCo"
},
{
"name": "isUnpriSignatory"
Expand Down
2 changes: 1 addition & 1 deletion playground/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
3 changes: 1 addition & 2 deletions playground/src/app/products/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { DataTable } from "./components/data-table";
import { columns } from "./components/columns";
import React from "react";
import { testProducts } from "./data/testProducts";
import PageContentWrapper from "@/components/PageContentWrapper";
import { useGlam } from "@glam/anchor/react";

Expand All @@ -14,7 +13,7 @@ export default function Products() {
id: f.idStr,
imageKey: f.imageKey,
name: f.name,
symbol: "GLAM",
symbol: f.shareClasses[0]?.shareClassSymbol || "",
baseAsset: f.fundCurrency,
inception: f.fundLaunchDate,
status: "active",
Expand Down