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

Feat/create with pda #127

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Security

## @mercurial-finance/vault-sdk [2.0.2] - PR [#127](https://github.com/mercurial-finance/vault-sdk/pull/127)

### Added

- `createWithPda` function

## @mercurial-finance/vault-sdk [2.0.1] - PR [#126](https://github.com/mercurial-finance/vault-sdk/pull/126)

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion ts-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mercurial-finance/vault-sdk",
"version": "2.0.1",
"version": "2.0.2",
"description": "Mercurial Vault SDK is a typescript library that allows you to interact with Mercurial v2's vault.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
34 changes: 34 additions & 0 deletions ts-client/src/vault/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,40 @@ export default class VaultImpl implements VaultImplementation {
);
}

public static async createWithPda(
connection: Connection,
pdaInfo: PdaInfo,
opt?: {
seedBaseKey?: PublicKey;
allowOwnerOffCurve?: boolean;
cluster?: Cluster;
programId?: string;
affiliateId?: PublicKey;
affiliateProgramId?: string;
},
): Promise<VaultImpl> {
const provider = new AnchorProvider(connection, {} as any, AnchorProvider.defaultOptions());
const program = new Program<VaultIdl>(IDL as VaultIdl, opt?.programId || PROGRAM_ID, provider);

const { vaultPda, tokenVaultPda, lpMintPda, vaultState, lpSupply } = await getVaultStateByPda(pdaInfo, program);
const tokenMint = await getMint(connection, pdaInfo.tokenAddress);
return new VaultImpl(
program,
{ tokenMint, vaultPda, tokenVaultPda, vaultState, lpSupply, lpMintPda },
{
...opt,
affiliateId: opt?.affiliateId,
affiliateProgram: opt?.affiliateId
? new Program<AffiliateVaultIdl>(
AffiliateIDL as AffiliateVaultIdl,
opt?.affiliateProgramId || AFFILIATE_PROGRAM_ID,
provider,
)
: undefined,
},
);
}

public async getUserBalance(owner: PublicKey): Promise<BN> {
const isAffiliated = this.affiliateId && this.affiliateProgram;

Expand Down
8 changes: 4 additions & 4 deletions ts-client/src/vault/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ export const getVaultPdas = (tokenMint: PublicKey, programId: PublicKey, seedBas
programId,
);

const tokenVault = PublicKey.findProgramAddressSync(
const [tokenVault] = PublicKey.findProgramAddressSync(
[Buffer.from(SEEDS.TOKEN_VAULT_PREFIX), vault.toBuffer()],
programId,
);
const lpMint = PublicKey.findProgramAddressSync([Buffer.from(SEEDS.LP_MINT_PREFIX), vault.toBuffer()], programId);
const [lpMint] = PublicKey.findProgramAddressSync([Buffer.from(SEEDS.LP_MINT_PREFIX), vault.toBuffer()], programId);

return {
vaultPda: vault,
tokenVaultPda: tokenVault[0],
lpMintPda: lpMint[0],
tokenVaultPda: tokenVault,
lpMintPda: lpMint,
};
};

Expand Down
Loading