Skip to content

Commit

Permalink
api: add jupiter swap
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0ece committed May 28, 2024
1 parent d3a5864 commit 4c9f9c0
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 202 deletions.
13 changes: 3 additions & 10 deletions anchor/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,26 @@ seeds = false
skip-lint = false

[programs.devnet]
custody = "Gcu1vbed9bwpfwU9PCnJw8QanQfVHfETWxAK3EZczbgo"
glam = "Gco1pcjxCMYjKJjSNJ7mKV7qezeUTE7arXJgy7PAPNRc"
policy = "Gpo1jXtEFepqyPQWTG7oDJrg8rye8JL3zcsczHzXKqLt"
pricing = "Gpr1WZZXAty2L9eiMwZPC7ra69vMFojhdqDuiRHkQQQp"
strategy = "Gst1YGe5vKURSWfCqM7GhZys1oML9E9t91WRyV2rt4yS"

[programs.localnet]
custody = "Gcu1vbed9bwpfwU9PCnJw8QanQfVHfETWxAK3EZczbgo"
glam = "Gco1pcjxCMYjKJjSNJ7mKV7qezeUTE7arXJgy7PAPNRc"
policy = "Gpo1jXtEFepqyPQWTG7oDJrg8rye8JL3zcsczHzXKqLt"
pricing = "Gpr1WZZXAty2L9eiMwZPC7ra69vMFojhdqDuiRHkQQQp"
strategy = "Gst1YGe5vKURSWfCqM7GhZys1oML9E9t91WRyV2rt4yS"

[registry]
url = "https://api.apr.dev"

[provider]
cluster = "localnet"
#cluster = "devnet"
#cluster = "mainnet"
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_crud"
#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_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_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 devnet"
Expand Down
8 changes: 4 additions & 4 deletions anchor/programs/glam/src/instructions/jupiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ impl anchor_lang::Id for Jupiter {

#[derive(Accounts)]
pub struct JupiterSwap<'info> {
#[account(mut)]
pub manager: Signer<'info>,
#[account(has_one = manager, has_one = treasury)]
pub fund: Box<Account<'info, FundAccount>>,

#[account(mut, seeds = [b"treasury".as_ref(), fund.key().as_ref()], bump)]
pub treasury: SystemAccount<'info>,

#[account(has_one = manager, has_one = treasury)]
pub fund: Box<Account<'info, FundAccount>>,
#[account(mut)]
pub manager: Signer<'info>,

pub jupiter_program: Program<'info, Jupiter>,
pub system_program: Program<'info, System>,
Expand Down
7 changes: 6 additions & 1 deletion anchor/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import * as anchor from "@coral-xyz/anchor";

import { GlamClientConfig } from "./clientConfig";
import { BaseClient } from "./client/base";
import {
BaseClient,
JUPITER_API_DEFAULT as _JUPITER_API_DEFAULT
} from "./client/base";
import { DriftClient } from "./client/drift";
import { JupiterClient } from "./client/jupiter";
import { MarinadeClient } from "./client/marinade";
import { WSolClient } from "./client/wsol";

export const JUPITER_API_DEFAULT = _JUPITER_API_DEFAULT;

export class GlamClient extends BaseClient {
drift: DriftClient;
jupiter: JupiterClient;
Expand Down
17 changes: 16 additions & 1 deletion anchor/src/client/base.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import * as anchor from "@coral-xyz/anchor";
import { Program, IdlAccounts } from "@coral-xyz/anchor";
import {
AnchorProvider,
IdlAccounts,
Program,
Wallet
} from "@coral-xyz/anchor";
import {
ComputeBudgetProgram,
Connection,
PublicKey,
Signer,
TransactionSignature
} from "@solana/web3.js";
import {
Expand All @@ -18,10 +24,13 @@ import { FundModel, FundOpenfundsModel } from "../models";
type FundAccount = IdlAccounts<Glam>["fundAccount"];
type FundMetadataAccount = IdlAccounts<Glam>["fundMetadataAccount"];

export const JUPITER_API_DEFAULT = "https://quote-api.jup.ag/v6";

export class BaseClient {
provider: anchor.Provider;
program: GlamProgram;
programId: PublicKey;
jupiterApi: string;

public constructor(config?: GlamClientConfig) {
this.programId = getGlamProgramId(config?.cluster || "devnet");
Expand All @@ -48,12 +57,18 @@ export class BaseClient {
anchor.setProvider(this.provider);
this.program = anchor.workspace.Glam as GlamProgram;
}

this.jupiterApi = config?.jupiterApi || JUPITER_API_DEFAULT;
}

getManager(): PublicKey {
return this.provider?.publicKey || new PublicKey(0);
}

getWalletSigner(): Signer {
return ((this.provider as AnchorProvider).wallet as Wallet).payer;
}

getFundModel(fund: any): FundModel {
return new FundModel(fund) as FundModel;
}
Expand Down
Loading

0 comments on commit 4c9f9c0

Please sign in to comment.