Skip to content

Commit

Permalink
T22 sdk update
Browse files Browse the repository at this point in the history
  • Loading branch information
solonk8 committed Mar 29, 2024
1 parent 69e7209 commit 7ffebbf
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 24 deletions.
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"@metaplex-foundation/mpl-token-metadata": "^3.1.2",
"old-mpl-token-metadata": "npm:@metaplex-foundation/[email protected]",
"@project-serum/anchor": "^0.26.0",
"@solana/spl-token": "^0.3.5",
"@solana/spl-token": "^0.4.1",
"@solana/spl-token-metadata": "^0.1.2",
"@solana/web3.js": "^1.65.0"
},
"devDependencies": {
Expand Down
69 changes: 56 additions & 13 deletions sdk/src/metadataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import {
import { Metaplex } from '@metaplex-foundation/js';
import { Creator, Metadata, TokenStandard } from 'old-mpl-token-metadata';
import { AccountInfo, Connection, PublicKey } from '@solana/web3.js';
import { Mint, unpackMint } from '@solana/spl-token';
import {
ExtensionType,
getExtensionData,
getMetadataPointerState,
Mint,
TOKEN_2022_PROGRAM_ID,
unpackMint,
} from '@solana/spl-token';
import { TokenMetadata, unpack } from '@solana/spl-token-metadata';

export class MetadataProviderError extends Error {
name = 'MetadataProviderError';
Expand All @@ -30,16 +38,17 @@ export interface MetadataProvider {
get mintAddress(): PublicKey;
get tokenProgram(): PublicKey;
get sellerFeeBasisPoints(): number;
get splTokenMetadata(): TokenMetadata | undefined;
}

export class RpcMetadataProvider implements MetadataProvider {
constructor(
private readonly mint: PublicKey,
public readonly metadataAccount: Metadata,
public readonly metadataAccount: Metadata | undefined,
public readonly mintAccount: Mint & { tokenProgramId: PublicKey },
public readonly mintStateAccount: MintStateWithAddress | undefined,
) {
if (!mint.equals(metadataAccount.mint)) {
if (!!metadataAccount && !mint.equals(metadataAccount.mint)) {
throw new MetadataProviderError('mint and metadata mismatch');
}
}
Expand All @@ -58,15 +67,19 @@ export class RpcMetadataProvider implements MetadataProvider {
mint,
]);

if (!metadataAi) {
throw new MetadataProviderError('metadata not found');
}
if (!mintAi) {
throw new MetadataProviderError('mint not found');
}
const mintParsed = unpackMint(mint, mintAi, mintAi.owner);
if (
!metadataAi &&
!getMetadataPointerState(mintParsed)?.metadataAddress?.equals(mint)
) {
throw new MetadataProviderError('metadata not found');
}
return RpcMetadataProvider.loadFromAccountInfos(mint, mintStateAddress, {
metadata: metadataAi,
mint: mintAi,
metadata: metadataAi,
mintState: mintStateAi,
});
}
Expand All @@ -76,13 +89,15 @@ export class RpcMetadataProvider implements MetadataProvider {
mintStateAddress: PublicKey,
accounts: {
mint: AccountInfo<Buffer>;
metadata: AccountInfo<Buffer>;
metadata: AccountInfo<Buffer> | null;
mintState: AccountInfo<Buffer> | null;
},
): RpcMetadataProvider {
return new RpcMetadataProvider(
mint,
Metadata.fromAccountInfo(accounts.metadata)[0],
accounts.metadata
? Metadata.fromAccountInfo(accounts.metadata)[0]
: undefined,
{
...unpackMint(mint, accounts.mint, accounts.mint.owner),
tokenProgramId: accounts.mint.owner,
Expand All @@ -92,15 +107,15 @@ export class RpcMetadataProvider implements MetadataProvider {
}

get creators(): Creator[] {
return this.metadataAccount.data.creators ?? [];
return this.metadataAccount?.data.creators ?? [];
}

get tokenStandard(): TokenStandard | undefined {
return this.metadataAccount.tokenStandard ?? undefined;
return this.metadataAccount?.tokenStandard ?? undefined;
}

get ruleset(): PublicKey | undefined {
return this.metadataAccount.programmableConfig?.ruleSet ?? undefined;
return this.metadataAccount?.programmableConfig?.ruleSet ?? undefined;
}

get mintState(): MintStateWithAddress | undefined {
Expand All @@ -116,7 +131,26 @@ export class RpcMetadataProvider implements MetadataProvider {
}

get sellerFeeBasisPoints(): number {
return this.metadataAccount.data.sellerFeeBasisPoints;
return this.metadataAccount?.data.sellerFeeBasisPoints ?? 0;
}

get splTokenMetadata(): TokenMetadata | undefined {
if (
!getMetadataPointerState(this.mintAccount)?.metadataAddress?.equals(
this.mint,
)
) {
return undefined;
}

const data = getExtensionData(
ExtensionType.TokenMetadata,
this.mintAccount.tlvData,
);
if (data === null) {
return undefined;
}
return unpack(data);
}
}

Expand All @@ -134,3 +168,12 @@ function parseMintState(
return undefined;
}
}

export function doesTokenExtensionExist(
mintContext: MetadataProvider,
): boolean {
return (
mintContext.tokenProgram.equals(TOKEN_2022_PROGRAM_ID) &&
!!mintContext.splTokenMetadata
);
}
84 changes: 76 additions & 8 deletions sdk/src/mmmClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
getTokenRecordPDA,
} from './pda';
import {
doesTokenExtensionExist,
MetadataProvider,
MintStateWithAddress,
RpcMetadataProvider,
Expand Down Expand Up @@ -287,9 +288,29 @@ export class MMMClient {
let builder:
| ReturnType<MmmMethodsNamespace['solOcpFulfillBuy']>
| ReturnType<MmmMethodsNamespace['solFulfillBuy']>
| ReturnType<MmmMethodsNamespace['solMip1FulfillBuy']>;
| ReturnType<MmmMethodsNamespace['solMip1FulfillBuy']>
| ReturnType<MmmMethodsNamespace['solExtFulfillBuy']>;

if (ocpMintState) {
if (doesTokenExtensionExist(mintContext)) {
builder = this.program.methods.solExtFulfillBuy(args).accountsStrict({
payer,
owner: this.poolData.owner,
cosigner: this.poolData.cosigner,
referral: this.poolData.referral,
pool: this.poolData.pool,
buysideSolEscrowAccount,
assetMint,
payerAssetAccount: assetTokenAccount,
sellsideEscrowTokenAccount,
ownerTokenAccount,
allowlistAuxAccount: allowlistAuxAccount ?? SystemProgram.programId,
sellState,
systemProgram: SystemProgram.programId,
tokenProgram: mintContext.tokenProgram,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
rent: SYSVAR_RENT_PUBKEY,
});
} else if (ocpMintState) {
builder = this.program.methods.solOcpFulfillBuy(args).accountsStrict({
payer,
owner: this.poolData.owner,
Expand Down Expand Up @@ -438,9 +459,26 @@ export class MMMClient {
let builder:
| ReturnType<MmmMethodsNamespace['solOcpFulfillSell']>
| ReturnType<MmmMethodsNamespace['solFulfillSell']>
| ReturnType<MmmMethodsNamespace['solMip1FulfillSell']>;
| ReturnType<MmmMethodsNamespace['solMip1FulfillSell']>
| ReturnType<MmmMethodsNamespace['solExtFulfillSell']>;

if (ocpMintState) {
if (doesTokenExtensionExist(mintContext)) {
builder = this.program.methods.solExtFulfillSell(args).accountsStrict({
payer,
owner: this.poolData.owner,
cosigner: this.poolData.cosigner,
referral: this.poolData.referral,
pool: this.poolData.pool,
buysideSolEscrowAccount,
assetMint,
sellsideEscrowTokenAccount,
payerAssetAccount,
sellState,
systemProgram: SystemProgram.programId,
tokenProgram: mintContext.tokenProgram,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
});
} else if (ocpMintState) {
builder = this.program.methods
.solOcpFulfillSell({
assetAmount: args.assetAmount,
Expand Down Expand Up @@ -586,9 +624,23 @@ export class MMMClient {
let builder:
| ReturnType<MmmMethodsNamespace['ocpDepositSell']>
| ReturnType<MmmMethodsNamespace['depositSell']>
| ReturnType<MmmMethodsNamespace['mip1DepositSell']>;
| ReturnType<MmmMethodsNamespace['mip1DepositSell']>
| ReturnType<MmmMethodsNamespace['extDepositSell']>;

if (ocpMintState) {
if (doesTokenExtensionExist(mintContext)) {
builder = this.program.methods.extDepositSell(args).accountsStrict({
owner: this.poolData.owner,
cosigner: this.poolData.cosigner,
pool: this.poolData.pool,
assetMint,
assetTokenAccount,
sellsideEscrowTokenAccount,
sellState,
systemProgram: SystemProgram.programId,
tokenProgram: mintContext.tokenProgram,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
});
} else if (ocpMintState) {
builder = this.program.methods.ocpDepositSell(args).accountsStrict({
owner: this.poolData.owner,
pool: this.poolData.pool,
Expand Down Expand Up @@ -698,9 +750,25 @@ export class MMMClient {
let builder:
| ReturnType<MmmMethodsNamespace['ocpWithdrawSell']>
| ReturnType<MmmMethodsNamespace['withdrawSell']>
| ReturnType<MmmMethodsNamespace['mip1WithdrawSell']>;
| ReturnType<MmmMethodsNamespace['mip1WithdrawSell']>
| ReturnType<MmmMethodsNamespace['extWithdrawSell']>;

if (ocpMintState) {
if (doesTokenExtensionExist(mintContext)) {
builder = this.program.methods.extWithdrawSell(args).accountsStrict({
owner: this.poolData.owner,
cosigner: this.poolData.cosigner,
pool: this.poolData.pool,
assetMint,
assetTokenAccount,
sellsideEscrowTokenAccount,
buysideSolEscrowAccount,
allowlistAuxAccount: allowlistAuxAccount ?? SystemProgram.programId,
sellState,
systemProgram: SystemProgram.programId,
tokenProgram: mintContext.tokenProgram,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
});
} else if (ocpMintState) {
builder = this.program.methods.ocpWithdrawSell(args).accountsStrict({
owner: this.poolData.owner,
pool: this.poolData.pool,
Expand Down

0 comments on commit 7ffebbf

Please sign in to comment.