feat: created tiplink for Sol and spl tokens

This commit is contained in:
shivaji43
2024-12-26 12:33:55 +05:30
parent 8d299244fc
commit 18ddd247c3
6 changed files with 368 additions and 7 deletions

View File

@@ -35,6 +35,7 @@ import {
getOwnedAllDomains,
resolveAllDomains,
create_gibwork_task,
create_TipLink,
} from "../tools";
import {
CollectionDeployment,
@@ -337,4 +338,7 @@ export class SolanaAgentKit {
payer ? new PublicKey(payer) : undefined,
);
}
async createTiplink(amount: number , splmintAddress?:PublicKey){
return create_TipLink(this,amount,splmintAddress)
}
}

View File

@@ -1229,6 +1229,45 @@ export class SolanaCreateGibworkTask extends Tool {
}
}
export class SolanaTipLinkTool extends Tool {
name = "solana_tiplink";
description = `Create a TipLink for transferring SOL.
Provide the amount of SOL you want to transfer. The tool will generate a TipLink URL for the recipient to claim the funds.
Inputs:
amountSol: number, e.g., 1 (Amount of SOL to transfer)`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
protected async _call(input: string): Promise<string> {
try {
const amountSol : number = parseFloat(input);
if (isNaN(amountSol) || amountSol <= 0) {
throw new Error("Invalid amount. Please provide a valid amount of SOL.");
}
const { url, signature } = await this.solanaKit.createTiplink(amountSol);
return JSON.stringify({
status: "success",
url,
signature,
message: "TipLink created successfully.",
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "UNKNOWN_ERROR",
});
}
}
}
export function createSolanaTools(solanaKit: SolanaAgentKit) {
return [
new SolanaBalanceTool(solanaKit),
@@ -1263,5 +1302,6 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) {
new SolanaGetMainDomain(solanaKit),
new SolanaResolveAllDomainsTool(solanaKit),
new SolanaCreateGibworkTask(solanaKit),
new SolanaTipLinkTool(solanaKit),
];
}

View File

@@ -0,0 +1,106 @@
import { TipLink } from "@tiplink/api";
import {
Transaction,
SystemProgram,
LAMPORTS_PER_SOL,
sendAndConfirmTransaction,
PublicKey,
ComputeBudgetProgram,
} from "@solana/web3.js";
import {
getAssociatedTokenAddress,
createTransferInstruction,
getMint,
createAssociatedTokenAccountInstruction,
} from "@solana/spl-token";
import { SolanaAgentKit } from "../index";
const MINIMUM_SOL_BALANCE = 0.003 * LAMPORTS_PER_SOL;
export async function create_TipLink(
agent: SolanaAgentKit,
amount: number,
splmintAddress?: PublicKey,
): Promise<{ url: string, signature: string }> {
try {
const tiplink = await TipLink.create();
if (!splmintAddress) {
const transaction = new Transaction();
transaction.add(
SystemProgram.transfer({
fromPubkey: agent.wallet_address,
toPubkey: tiplink.keypair.publicKey,
lamports: amount * LAMPORTS_PER_SOL,
}),
);
const signature = await sendAndConfirmTransaction(
agent.connection,
transaction,
[agent.wallet],
{ commitment: "confirmed" }
);
return {
url: tiplink.url.toString(),
signature,
};
} else {
const fromAta = await getAssociatedTokenAddress(splmintAddress, agent.wallet_address);
const toAta = await getAssociatedTokenAddress(splmintAddress, tiplink.keypair.publicKey);
const mintInfo = await getMint(agent.connection, splmintAddress);
const adjustedAmount = amount * Math.pow(10, mintInfo.decimals);
const transaction = new Transaction();
transaction.add(
ComputeBudgetProgram.setComputeUnitPrice({
microLamports: 5000
})
);
transaction.add(
SystemProgram.transfer({
fromPubkey: agent.wallet_address,
toPubkey: tiplink.keypair.publicKey,
lamports: MINIMUM_SOL_BALANCE,
})
);
transaction.add(
createAssociatedTokenAccountInstruction(
agent.wallet_address,
toAta,
tiplink.keypair.publicKey,
splmintAddress
)
);
transaction.add(
createTransferInstruction(
fromAta,
toAta,
agent.wallet_address,
adjustedAmount
)
);
const signature = await sendAndConfirmTransaction(
agent.connection,
transaction,
[agent.wallet],
{ commitment: "confirmed" }
);
return {
url: tiplink.url.toString(),
signature,
};
}
} catch (error: any) {
console.error("Error creating TipLink or sending funds:", error.message);
throw new Error(`Failed to create TipLink: ${error.message}`);
}
}

View File

@@ -37,3 +37,4 @@ export * from "./openbook_create_market";
export * from "./pyth_fetch_price";
export * from "./create_gibwork_task";
export * from "./create_tiplinks"