Skip to content

Commit

Permalink
Update browser wallet for Whirlpools SDK (#182)
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:
- Created a custom Wallet class to support browser use of Orca tools


## Transaction executed by agent 
Example transaction:
https://solscan.io/tx/GNmgxM4ZRHURDWcgmhLmBrSG5RgF9yw5B4jzEXzWX2jpNvCbUsQyaMdkkH77au7pisJ89hJdrcs8dv54U8H3J8V?cluster=devnet

## Prompt Used
Using Devnet
```
i want to open a single sided position in pool 7MghXMotsf9CLr6Z9obV8g9Kt2y1Mqn6wXm2mdPhRnMe
```
then provide details

## Additional Notes

## Checklist
- [x] I have tested these changes locally
- [ ] I have updated the documentation
- [x] I have added a transaction link
- [x] I have added the prompt used to test it
  • Loading branch information
thearyanag authored Jan 10, 2025
2 parents fc92d44 + e5e25b0 commit 88023df
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 136 deletions.
248 changes: 129 additions & 119 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/tools/orca_close_position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
VersionedTransaction,
} from "@solana/web3.js";
import { SolanaAgentKit } from "../agent";
import { Wallet } from "@coral-xyz/anchor";
import { Wallet } from "../utils/keypair";
import {
ORCA_WHIRLPOOL_PROGRAM_ID,
WhirlpoolContext,
Expand Down
2 changes: 1 addition & 1 deletion src/tools/orca_create_clmm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
VersionedTransaction,
} from "@solana/web3.js";
import { SolanaAgentKit } from "../agent";
import { Wallet } from "@coral-xyz/anchor";
import { Wallet } from "../utils/keypair";
import { Decimal } from "decimal.js";
import {
ORCA_WHIRLPOOL_PROGRAM_ID,
Expand Down
3 changes: 2 additions & 1 deletion src/tools/orca_create_single_sided_liquidity_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
VersionedTransaction,
} from "@solana/web3.js";
import { SolanaAgentKit } from "../agent";
import { BN, Wallet } from "@coral-xyz/anchor";
import { BN } from "@coral-xyz/anchor";
import { Wallet } from "../utils/keypair";
import { Decimal } from "decimal.js";
import {
PDAUtil,
Expand Down
2 changes: 1 addition & 1 deletion src/tools/orca_open_centered_position_with_liquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
VersionedTransaction,
} from "@solana/web3.js";
import { SolanaAgentKit } from "../agent";
import { Wallet } from "@coral-xyz/anchor";
import { Wallet } from "../utils/keypair";
import { Decimal } from "decimal.js";
import {
ORCA_WHIRLPOOL_PROGRAM_ID,
Expand Down
24 changes: 12 additions & 12 deletions src/tools/orca_open_single_sided_position.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {
Keypair,
PublicKey,
TransactionInstruction,
TransactionMessage,
VersionedTransaction,
} from "@solana/web3.js";
import { SolanaAgentKit } from "../agent";
import { Wallet } from "@coral-xyz/anchor";
import { Wallet } from "../utils/keypair";
import { Decimal } from "decimal.js";
import {
ORCA_WHIRLPOOL_PROGRAM_ID,
Expand Down Expand Up @@ -119,17 +120,17 @@ export async function orcaOpenSingleSidedPosition(
lowerTick,
upperTick,
]);
let txIds: string = "";
let instructions: TransactionInstruction[] = [];
let signers: Keypair[] = [];
if (txBuilderTickArrays !== null) {
const txPayloadTickArrays = await txBuilderTickArrays.build();
const txPayloadTickArraysDecompiled = TransactionMessage.decompile(
(txPayloadTickArrays.transaction as VersionedTransaction).message,
);
const instructions = txPayloadTickArraysDecompiled.instructions;
const signers = txPayloadTickArrays.signers as Keypair[];

const tickArrayTxId = await sendTx(agent, instructions, signers);
txIds += tickArrayTxId + ",";
instructions = instructions.concat(
txPayloadTickArraysDecompiled.instructions,
);
signers = signers.concat(txPayloadTickArrays.signers as Keypair[]);
}

const tokenExtensionCtx: TokenExtensionContextForPool = {
Expand Down Expand Up @@ -161,14 +162,13 @@ export async function orcaOpenSingleSidedPosition(
const txPayloadDecompiled = TransactionMessage.decompile(
(txPayload.transaction as VersionedTransaction).message,
);
const instructions = txPayloadDecompiled.instructions;
const signers = txPayload.signers as Keypair[];
instructions = instructions.concat(txPayloadDecompiled.instructions);
signers = signers.concat(txPayload.signers as Keypair[]);

const positionTxId = await sendTx(agent, instructions, signers);
txIds += positionTxId;
const txId = await sendTx(agent, instructions, signers);

return JSON.stringify({
transactionIds: txIds,
transactionIds: txId,
positionMint: positionMint.toString(),
});
} catch (error) {
Expand Down
30 changes: 29 additions & 1 deletion src/utils/keypair.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
import { Keypair } from "@solana/web3.js";
import { Keypair, PublicKey, Transaction, VersionedTransaction } from "@solana/web3.js";
import bs58 from "bs58";

export const keypair = Keypair.generate();

console.log(keypair.publicKey.toString());
console.log(bs58.encode(keypair.secretKey));


export class Wallet {
private _signer: Keypair;

constructor(signer: Keypair) {
this._signer = signer;
}

async signTransaction<T extends Transaction | VersionedTransaction>(tx: T): Promise<T> {
if (tx instanceof Transaction) {
tx.sign(this._signer);
} else if (tx instanceof VersionedTransaction) {
tx.sign([this._signer]);
} else {
throw new Error("Unsupported transaction type");
}
return tx;
}

async signAllTransactions<T extends Transaction | VersionedTransaction>(txs: T[]): Promise<T[]> {
return Promise.all(txs.map((tx) => this.signTransaction(tx)));
}

get publicKey(): PublicKey {
return this._signer.publicKey;
}
}

0 comments on commit 88023df

Please sign in to comment.