Skip to content

Commit

Permalink
Add Jup Fees to the platform (#98)
Browse files Browse the repository at this point in the history
# Pull Request Description

## Related Issue
Fixes # (issue number)

## Changes Made
This PR adds the following changes:
<!-- List the key changes made in this PR -->
- Added jupReferralAccount to allow devs to set Fee Accounts on trade
API (optional parameter)
- Added jupFeeBps to allow devs to set fee in bps  (optional parameter)
  
## Implementation Details
<!-- Provide technical details about the implementation -->
- Added optional Parameter to trade API

## Transaction executed by agent 
<!-- If applicable, provide example usage, transactions, or screenshots
-->
Example transaction: 

https://solscan.io/tx/35b1wz1LjU5pEALq6m5Q6Gyc3QB3ejvsjNAYoy9PdHu9WP8SzGLjHi59sRPj8hvtntVMDMEguWuu273FZ3EbUSoZ
## Prompt Used
<!-- If relevant, include the prompt or configuration used -->
```
```

## Additional Notes
<!-- Any additional information that reviewers should know -->

## Checklist
- [x] I have tested these changes locally
- [x] I have updated the documentation
- [x] I have added a transaction link
- [ ] I have added the prompt used to test it
  • Loading branch information
thearyanag authored Dec 31, 2024
2 parents a4548c2 + 5048f19 commit e623da1
Show file tree
Hide file tree
Showing 9 changed files with 634 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
OPENAI_API_KEY=
RPC_URL=
SOLANA_PRIVATE_KEY=
JUPITER_REFERRAL_ACCOUNT=
JUPITER_FEE_BPS=
590 changes: 590 additions & 0 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions src/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Connection, Keypair, PublicKey } from "@solana/web3.js";
import bs58 from "bs58";
import Decimal from "decimal.js";
import { DEFAULT_OPTIONS } from "../constants";
import { Config } from "../types";
import {
deploy_collection,
deploy_token,
Expand Down Expand Up @@ -71,17 +72,17 @@ export class SolanaAgentKit {
public connection: Connection;
public wallet: Keypair;
public wallet_address: PublicKey;
public openai_api_key: string | null;
public config: Config;

constructor(
private_key: string,
rpc_url = "https://api.mainnet-beta.solana.com",
openai_api_key: string | null = null,
config: Config,
) {
this.connection = new Connection(rpc_url);
this.wallet = Keypair.fromSecretKey(bs58.decode(private_key));
this.wallet_address = this.wallet.publicKey;
this.openai_api_key = openai_api_key;
this.config = config;
}

// Tool methods
Expand Down Expand Up @@ -414,10 +415,7 @@ export class SolanaAgentKit {
return create_TipLink(this, amount, splmintAddress);
}

async tensorListNFT(
nftMint: PublicKey,
price: number,
): Promise<string> {
async tensorListNFT(nftMint: PublicKey, price: number): Promise<string> {
return listNFTForSale(this, nftMint, price);
}

Expand Down
3 changes: 3 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ export const TOKENS = {
export const DEFAULT_OPTIONS = {
SLIPPAGE_BPS: 300,
TOKEN_DECIMALS: 9,
RERERRAL_FEE: 200,
} as const;

/**
* Jupiter API URL
*/
export const JUP_API = "https://quote-api.jup.ag/v6";
export const JUP_REFERRAL_ADDRESS =
"REFER4ZgmyYx9c6He5XfaTMiGfdLwRnkV4RPp9t9iF3";
4 changes: 2 additions & 2 deletions src/tools/create_image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export async function create_image(
n: number = 1,
) {
try {
if (!agent.openai_api_key) {
if (!agent.config.OPENAI_API_KEY) {
throw new Error("OpenAI API key not found in agent configuration");
}

const openai = new OpenAI({
apiKey: agent.openai_api_key,
apiKey: agent.config.OPENAI_API_KEY,
});

const response = await openai.images.generate({
Expand Down
2 changes: 1 addition & 1 deletion src/tools/tensor_trade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function listNFTForSale(
if (!tokenAccount || tokenAccount.amount <= 0) {
throw new Error(`You don't own this NFT (${nftMint.toString()})`);
}
} catch (e) {
} catch (error: any) {
throw new Error(
`No token account found for mint ${nftMint.toString()}. Make sure you own this NFT.`,
);
Expand Down
24 changes: 22 additions & 2 deletions src/tools/trade.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { VersionedTransaction, PublicKey } from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
import { TOKENS, DEFAULT_OPTIONS, JUP_API } from "../constants";
import {
TOKENS,
DEFAULT_OPTIONS,
JUP_API,
JUP_REFERRAL_ADDRESS,
} from "../constants";
import { getMint } from "@solana/spl-token";
/**
* Swap tokens using Jupiter Exchange
Expand All @@ -11,6 +16,7 @@ import { getMint } from "@solana/spl-token";
* @param slippageBps Slippage tolerance in basis points (default: 300 = 3%)
* @returns Transaction signature
*/

export async function trade(
agent: SolanaAgentKit,
outputMint: PublicKey,
Expand Down Expand Up @@ -38,11 +44,24 @@ export async function trade(
`&amount=${scaledAmount}` +
`&slippageBps=${slippageBps}` +
`&onlyDirectRoutes=true` +
`&maxAccounts=20`,
`&maxAccounts=20` +
`${agent.config.JUPITER_FEE_BPS ? `&platformFeeBps=${agent.config.JUPITER_FEE_BPS}` : ""}`,
)
).json();

// Get serialized transaction
let feeAccount;
if (agent.config.JUPITER_REFERRAL_ACCOUNT) {
[feeAccount] = PublicKey.findProgramAddressSync(
[
Buffer.from("referral_ata"),
new PublicKey(agent.config.JUPITER_REFERRAL_ACCOUNT).toBuffer(),
TOKENS.SOL.toBuffer(),
],
new PublicKey(JUP_REFERRAL_ADDRESS),
);
}

const { swapTransaction } = await (
await fetch("https://quote-api.jup.ag/v6/swap", {
method: "POST",
Expand All @@ -55,6 +74,7 @@ export async function trade(
wrapAndUnwrapSol: true,
dynamicComputeUnitLimit: true,
prioritizationFeeLamports: "auto",
feeAccount: feeAccount ? feeAccount.toString() : null,
}),
})
).json();
Expand Down
6 changes: 6 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { PublicKey } from "@solana/web3.js";

export interface Config {
OPENAI_API_KEY?: string;
JUPITER_REFERRAL_ACCOUNT?: string;
JUPITER_FEE_BPS?: number;
}

export interface Creator {
address: string;
percentage: number;
Expand Down
4 changes: 3 additions & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ async function initializeAgent() {
const solanaAgent = new SolanaAgentKit(
process.env.SOLANA_PRIVATE_KEY!,
process.env.RPC_URL,
process.env.OPENAI_API_KEY!,
{
OPENAI_API_KEY: process.env.OPENAI_API_KEY!,
},
);

const tools = createSolanaTools(solanaAgent);
Expand Down

0 comments on commit e623da1

Please sign in to comment.