Skip to content

Commit

Permalink
fix type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
yurushao committed Jan 27, 2025
1 parent c3dde1d commit 87344a6
Show file tree
Hide file tree
Showing 20 changed files with 126 additions and 120 deletions.
18 changes: 9 additions & 9 deletions anchor/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ wallet = "~/.config/solana/id.json"

[scripts]
test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_crud"
# test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_mint"
# test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_investor"
# test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_drift"
# test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_marinade"
# test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_staking"
# test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_jupiter"
# test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_openfunds"
# test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_wsol"
# test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_policy_hook"
#test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_mint"
#test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_investor"
#test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_drift"
#test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_marinade"
#test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_staking"
#test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_jupiter"
#test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_openfunds"
#test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_wsol"
#test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_policy_hook"

[test]
startup_wait = 50000
Expand Down
4 changes: 2 additions & 2 deletions anchor/src/client/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,8 @@ export class BaseClient {
StateModel.fromOnchainAccounts(
s.publicKey,
s.account,
openfundsCache.get(s.account.metadata.pubkey.toBase58()),
mintCache.get(s.account.mints[0] ? s.account.mints[0].toBase58() : ""),
openfundsCache.get(s.account.metadata?.pubkey.toBase58() || ""),
mintCache.get(s.account.mints[0]?.toBase58() || ""),
this.program.programId,
),
);
Expand Down
2 changes: 2 additions & 0 deletions anchor/src/client/drift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ export class DriftClient {
const driftState = await getDriftStateAccountPublicKey(this.DRIFT_PROGRAM);

const tx = await this.base.program.methods
// @ts-ignore
.driftPlaceOrders([orderParams])
.accountsPartial({
state: statePda,
Expand Down Expand Up @@ -590,6 +591,7 @@ export class DriftClient {
);

const tx = await this.base.program.methods
// @ts-ignore
.driftCancelOrders(marketType, marketIndex, direction)
.accountsPartial({
state: glamState,
Expand Down
2 changes: 1 addition & 1 deletion anchor/src/client/investor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class InvestorClient {
if (!stateModel) {
stateModel = await this.base.fetchState(statePda);
}
let remainingAccounts = stateModel.assets.flatMap((asset) => {
let remainingAccounts = (stateModel.assets || []).flatMap((asset) => {
const assetMeta = this.base.getAssetMeta(asset.toBase58());
const vaultAta = this.base.getVaultAta(
statePda,
Expand Down
3 changes: 0 additions & 3 deletions anchor/src/client/shareclass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ export class ShareClassClient {
const openfunds = this.base.getOpenfundsPda(state);
const shareClassMint = this.base.getShareClassPda(state, shareClassId);

// @ts-ignore Type instantiation is excessively deep and possibly infinite.
return await this.base.program.methods
.closeShareClass(shareClassId)
.accounts({
state,
metadata: openfunds,
shareClassMint,
})
.instruction();
Expand All @@ -80,7 +78,6 @@ export class ShareClassClient {
.closeShareClass(shareClassId)
.accounts({
state,
metadata: openfunds,
shareClassMint,
})
.rpc();
Expand Down
12 changes: 7 additions & 5 deletions anchor/src/client/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
ShareClassOpenfundsModel,
CreatedModel,
Metadata,
StateIdlModel,
StateModelType,
} from "../models";
import { WSOL } from "../constants";

Expand All @@ -43,12 +45,12 @@ export class StateClient {
const mints = stateModel.mints;
stateModel.mints = [];

if (mints.length > 1) {
if (mints && mints.length > 1) {
throw new Error("Multiple mints not supported");
}

// No share class, only need to initialize the fund
if (mints.length === 0) {
if (mints && mints.length === 0) {
const txSig = await this.base.program.methods
.initializeState(stateModel)
.accountsPartial({
Expand All @@ -60,7 +62,7 @@ export class StateClient {
return [txSig, statePda];
}

if (singleTx) {
if (mints && mints.length > 0 && singleTx) {
const initStateIx = await this.base.program.methods
.initializeState(stateModel)
.accountsPartial({
Expand Down Expand Up @@ -92,12 +94,12 @@ export class StateClient {
.rpc();

const addShareClassTxs = await Promise.all(
mints.map(async (shareClass, j: number) => {
(mints || []).map(async (shareClass, j: number) => {
const shareClassMint = this.base.getShareClassPda(statePda, j);

// FIXME: setting rawOpenfunds to null is a workarond for
// Access violation in stack frame 5 at address 0x200005ff8 of size 8
shareClass.rawOpenfunds = null;
// shareClass.rawOpenfunds = null;
return await this.base.program.methods
.addShareClass(shareClass)
.accounts({
Expand Down
21 changes: 11 additions & 10 deletions anchor/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class StateModel extends StateIdlModel {
throw new Error("Fund ID not set");
}
const [pda, _bump] = PublicKey.findProgramAddressSync(
[Buffer.from("treasury"), this.id.toBuffer()],
[Buffer.from("vault"), this.id.toBuffer()],
this.glamProgramId,
);
return pda;
Expand All @@ -101,28 +101,29 @@ export class StateModel extends StateIdlModel {
throw new Error("Fund ID not set");
}
const [pda, _] = PublicKey.findProgramAddressSync(
[Buffer.from("openfunds"), this.id.toBuffer()],
[Buffer.from("metadata"), this.id.toBuffer()],
this.glamProgramId,
);
return pda;
}

get productType() {
return this.accountType;
// @ts-ignore
return Object.keys(this.accountType)[0];
}

get shareClassMints() {
if (this.mints.length > 0 && !this.id) {
if (this.mints && this.mints.length > 0 && !this.id) {
// If share classes are set, fund ID should be set as well
throw new Error("Fund ID not set");
}
return this.mints.map((_, i) =>
return (this.mints || []).map((_, i) =>
ShareClassModel.mintAddress(this.id!, i, this.glamProgramId),
);
}

get sparkleKey() {
if (this.mints.length === 0) {
if (!this.mints || this.mints.length === 0) {
return this.idStr;
}
return this.shareClassMints[0].toBase58() || this.idStr;
Expand Down Expand Up @@ -182,7 +183,7 @@ export class StateModel extends StateIdlModel {
// Build the array of ShareClassModel
stateAccount.mints.forEach((_, i) => {
const shareClassIdlModel = {} as any;
shareClassIdlModel["fundId"] = statePda;
shareClassIdlModel["statePubkey"] = statePda;

stateAccount.params[i + 1].forEach((param) => {
const name = Object.keys(param.name)[0];
Expand Down Expand Up @@ -336,7 +337,7 @@ export class ShareClassModel extends ShareClassIdlModel {
glamProgramId: PublicKey = GLAM_PROGRAM_ID_DEFAULT,
): PublicKey {
const [pda, _] = PublicKey.findProgramAddressSync(
[Buffer.from("share"), Uint8Array.from([idx % 256]), statePda.toBuffer()],
[Buffer.from("mint"), Uint8Array.from([idx % 256]), statePda.toBuffer()],
glamProgramId,
);
return pda;
Expand Down Expand Up @@ -463,8 +464,8 @@ export class ManagerModel implements ManagerModelType {
export type CreatedModelType = IdlTypes<Glam>["createdModel"];
export class CreatedModel implements CreatedModelType {
key: number[]; // Uint8Array;
createdBy: PublicKey | null;
createdAt: BN | null;
createdBy: PublicKey;
createdAt: BN;

constructor(obj: Partial<CreatedModelType>) {
this.key = obj.key ?? [0, 0, 0, 0, 0, 0, 0, 0];
Expand Down
36 changes: 18 additions & 18 deletions anchor/src/react/glam.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,24 @@ const glamStatesListAtom = atomWithStorage<GlamStateCache[]>(

// In order to properly deser states, we need to
// convert string -> pubkey (and maybe more in future)
const deserializeGlamStateCache = (f: any) => {
if (!f) {
const deserializeGlamStateCache = (s: any) => {
if (!s) {
return undefined;
}
if (typeof f.pubkey === "string") {
f.address = f.pubkey;
f.pubkey = new PublicKey(f.pubkey);
if (typeof s.pubkey === "string") {
s.address = s.pubkey;
s.pubkey = new PublicKey(s.pubkey);
}
return f as GlamStateCache;
return s as GlamStateCache;
};

const toStateCache = (f: StateModel) => {
const toStateCache = (s: StateModel) => {
return {
pubkey: f.id,
sparkleKey: f.sparkleKey,
address: f.idStr,
name: f.name,
product: f.productType,
pubkey: s.id,
sparkleKey: s.sparkleKey,
address: s.idStr,
name: s.name,
product: s.productType,
} as GlamStateCache;
};

Expand Down Expand Up @@ -197,14 +197,14 @@ export function GlamProvider({

// Find a list of glam states that the wallet has access to
const glamStatesList = [] as GlamStateCache[];
glamStateModels.forEach((f: StateModel) => {
if (wallet?.publicKey?.equals(f.owner!.pubkey!)) {
const stateCache = toStateCache(f);
glamStateModels.forEach((s: StateModel) => {
if (wallet?.publicKey?.equals(s.owner!.pubkey!)) {
const stateCache = toStateCache(s);
glamStatesList.push(stateCache);
} else {
f.delegateAcls.forEach((acl: any) => {
(s.delegateAcls || []).forEach((acl: any) => {
if (wallet?.publicKey?.equals(acl.pubkey)) {
glamStatesList.push(toStateCache(f));
glamStatesList.push(toStateCache(s));
}
});
}
Expand Down Expand Up @@ -238,7 +238,7 @@ export function GlamProvider({
);
const glamState = await glamClient.fetchState(activeGlamState?.pubkey);
console.log("delegate acls:", glamState.delegateAcls);
setDelegateAcls(glamState.delegateAcls);
setDelegateAcls(glamState.delegateAcls || []);
}
};

Expand Down
25 changes: 11 additions & 14 deletions cli/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as anchor from "@coral-xyz/anchor";
import {
StateModel,
IntegrationName,
Integration,
WSOL,
getPriorityFeeEstimate,
GlamClient,
Expand Down Expand Up @@ -209,7 +209,6 @@ program
.accounts({
state: statePda,
shareClassMint: glamClient.getShareClassPda(statePda, 0),
metadata: glamClient.getOpenfundsPda(statePda),
})
.instruction();
preInstructions.push(closeShareClassIx);
Expand All @@ -219,7 +218,6 @@ program
.closeState()
.accounts({
state: statePda,
metadata: glamClient.getOpenfundsPda(statePda),
})
.preInstructions(preInstructions);

Expand Down Expand Up @@ -347,6 +345,7 @@ delegate
permissions: permissions.map((p) => ({
[p]: {},
})),
expiresAt: new anchor.BN(0),
},
]);
console.log("txSig:", txSig);
Expand Down Expand Up @@ -399,14 +398,14 @@ integration
}

const stateModel = await glamClient.fetchState(statePda);
const cnt = stateModel.integrationAcls.length;
const cnt = stateModel.integrations.length;
console.log(
`${stateModel.name} (${statePda.toBase58()}) has ${cnt} integration${
cnt > 1 ? "s" : ""
} enabled`,
);
for (let [i, acl] of stateModel.integrationAcls.entries()) {
console.log(`[${i}] ${Object.keys(acl.name)[0]}`);
for (let [i, integ] of stateModel.integrations.entries()) {
console.log(`[${i}] ${Object.keys(integ)[0]}`);
}
});

Expand Down Expand Up @@ -442,8 +441,8 @@ integration
}

const stateModel = await glamClient.fetchState(statePda);
const acl = stateModel.integrationAcls.find(
(integ) => Object.keys(integ.name)[0] === integration,
const acl = stateModel.integrations.find(
(integ) => Object.keys(integ)[0] === integration,
);
if (acl) {
console.log(
Expand All @@ -453,10 +452,8 @@ integration
}

const updated = new StateModel({
integrationAcls: [
...stateModel.integrationAcls,
{ name: { [integration]: {} } as IntegrationName, features: [] },
],
// @ts-ignore
integrations: [...stateModel.integrations, { [integration]: {} }],
});

try {
Expand Down Expand Up @@ -494,8 +491,8 @@ integration

const stateModel = await glamClient.fetchState(statePda);
const updated = new StateModel({
integrationAcls: stateModel.integrationAcls.filter(
(integ) => Object.keys(integ.name)[0] !== integration,
integrations: stateModel.integrations.filter(
(integ) => Object.keys(integ)[0] !== integration,
),
});

Expand Down
6 changes: 4 additions & 2 deletions playground/src/app/(mint)/mint/context/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ export default function MintContextPage() {
fundDomicileAlpha2: fund.rawOpenfunds?.fundDomicileAlpha2,
},
shareClass: {
iSIN: fund.mints[0].rawOpenfunds?.isin,
shareClassCurrency: fund.mints[0].rawOpenfunds?.shareClassCurrency,
iSIN: fund.mints ? fund.mints[0].rawOpenfunds?.isin : null,
shareClassCurrency: fund.mints
? fund.mints[0].rawOpenfunds?.shareClassCurrency
: null,
},
});

Expand Down
7 changes: 3 additions & 4 deletions playground/src/app/(mint)/mint/create/createMintForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,12 @@ export default function MultiStepForm() {
});

const glamState = {
accountType: { mint: {} },
name: basicInfoFormData.name,
isEnabled: true,
enabled: true,
rawOpenfunds: {
fundDomicileAlpha2: openfundsData.fund.fundDomicileAlpha2,
} as Partial<FundOpenfundsModel>,
// @ts-ignore
integrationAcls: [{ name: { mint: {} }, features: [] }],
company: {
fundGroupName: openfundsData.company.fundGroupName,
} as Partial<CompanyModel>,
Expand All @@ -140,7 +139,7 @@ export default function MultiStepForm() {
mints: [
{
uri: "",
fundId: null,
statePubkey: null,
imageUri: "",
allowlist: [],
blocklist: [],
Expand Down
Loading

0 comments on commit 87344a6

Please sign in to comment.