Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
thearyanag committed Dec 27, 2024
1 parent a80fb0f commit 6f68718
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 118 deletions.
5 changes: 4 additions & 1 deletion src/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,10 @@ export class SolanaAgentKit {
);
}

async rockPaperScissors(amount: number, choice: "rock" | "paper" | "scissors") {
async rockPaperScissors(
amount: number,
choice: "rock" | "paper" | "scissors",
) {
return rock_paper_scissor(this, amount, choice);
}
}
18 changes: 11 additions & 7 deletions src/langchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1230,19 +1230,20 @@ export class SolanaCreateGibworkTask extends Tool {
}

export class SolanaRockPaperScissorsTool extends Tool {
name = "rock_paper_scissors_blink";
description = `Play rock paper scissors to win the SEND coin.
Inputs(convert the input to a json string):
name = "rock_paper_scissors";
description = `Play rock paper scissors to win SEND coins.
Inputs (input is a JSON string):
choice: string, either "rock", "paper", or "scissors" (required)
amount: number, amount of SOL to play the game with, either 0.1, 0.01, or 0.005 SOL (required)`;
amount: number, amount of SOL to play with - must be 0.1, 0.01, or 0.005 SOL (required)`;

constructor(private solanaKit: SolanaAgentKit) {
super();
}

private validateInput(input: any): void {
if (input.choice !== undefined) {
throw new Error('choice is required.');
throw new Error("choice is required.");
}
if (
input.amount !== undefined &&
Expand All @@ -1258,9 +1259,12 @@ export class SolanaRockPaperScissorsTool extends Tool {
this.validateInput(parsedInput);
const result = await this.solanaKit.rockPaperScissors(
Number(parsedInput['"amount"']),
parsedInput['"choice"'].replace(/^"|"$/g, '') as "rock" | "paper" | "scissors"
parsedInput['"choice"'].replace(/^"|"$/g, "") as
| "rock"
| "paper"
| "scissors",
);

return JSON.stringify({
status: "success",
message: result,
Expand Down
2 changes: 1 addition & 1 deletion src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ export * from "./pyth_fetch_price";

export * from "./create_gibwork_task";

export * from "./rock_paper_scissor";
export * from "./rock_paper_scissor";
224 changes: 115 additions & 109 deletions src/tools/rock_paper_scissor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,125 +2,131 @@ import { sendAndConfirmTransaction, Transaction } from "@solana/web3.js";
import { SolanaAgentKit } from "../agent";

export async function rock_paper_scissor(
agent: SolanaAgentKit,
amount: number,
choice: "rock" | "paper" | "scissors",
agent: SolanaAgentKit,
amount: number,
choice: "rock" | "paper" | "scissors",
) {
try {
const res = await fetch(
`https://rps.sendarcade.fun/api/actions/bot?amount=${amount}&choice=${choice}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
account: agent.wallet.publicKey.toBase58(),
}),
},
);
try {
const res = await fetch(
`https://rps.sendarcade.fun/api/actions/bot?amount=${amount}&choice=${choice}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
account: agent.wallet.publicKey.toBase58(),
}),
},
);

const data = await res.json();
if (data.transaction) {
const txn = Transaction.from(Buffer.from(data.transaction, "base64"));
txn.sign(agent.wallet);
txn.recentBlockhash = (
await agent.connection.getLatestBlockhash()
).blockhash;
const sig = await sendAndConfirmTransaction(
agent.connection,
txn,
[agent.wallet],
{ commitment: 'confirmed' }
);
let href = data.links?.next?.href;
return await outcome(agent, sig, href);
} else {
return "failed";
}
} catch (error: any) {
console.error(error);
throw new Error(`RPS game failed: ${error.message}`);
const data = await res.json();
if (data.transaction) {
const txn = Transaction.from(Buffer.from(data.transaction, "base64"));
txn.sign(agent.wallet);
txn.recentBlockhash = (
await agent.connection.getLatestBlockhash()
).blockhash;
const sig = await sendAndConfirmTransaction(
agent.connection,
txn,
[agent.wallet],
{ commitment: "confirmed" },
);
const href = data.links?.next?.href;
return await outcome(agent, sig, href);
} else {
return "failed";
}
} catch (error: any) {
console.error(error);
throw new Error(`RPS game failed: ${error.message}`);
}
}
async function outcome(agent: SolanaAgentKit, sig: string, href: string): Promise<string> {
try {
const res = await fetch(
"https://rps.sendarcade.fun" + href, // href = /api/actions/outcome?id=...
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
account: agent.wallet.publicKey.toBase58(),
signature: sig,
}),
},
);
async function outcome(
agent: SolanaAgentKit,
sig: string,
href: string,
): Promise<string> {
try {
const res = await fetch(
"https://rps.sendarcade.fun" + href, // href = /api/actions/outcome?id=...
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
account: agent.wallet.publicKey.toBase58(),
signature: sig,
}),
},
);

const data: any = await res.json();
const title = data.title;
if (title.startsWith("You lost")) {
return title;
}
let next_href = data.links?.actions?.[0]?.href;
return title + "\n" + await won(agent, next_href)
} catch (error: any) {
console.error(error);
throw new Error(`RPS outcome failed: ${error.message}`);
const data: any = await res.json();
const title = data.title;
if (title.startsWith("You lost")) {
return title;
}
const next_href = data.links?.actions?.[0]?.href;
return title + "\n" + (await won(agent, next_href));
} catch (error: any) {
console.error(error);
throw new Error(`RPS outcome failed: ${error.message}`);
}
}
async function won(agent: SolanaAgentKit, href: string): Promise<string> {
try {
const res = await fetch(
"https://rps.sendarcade.fun" + href, // href = /api/actions/won?id=...
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
account: agent.wallet.publicKey.toBase58(),
}),
},
);
try {
const res = await fetch(
"https://rps.sendarcade.fun" + href, // href = /api/actions/won?id=...
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
account: agent.wallet.publicKey.toBase58(),
}),
},
);

const data: any = await res.json();
if (data.transaction) {
const txn = Transaction.from(Buffer.from(data.transaction, "base64"));
txn.partialSign(agent.wallet);
await agent.connection.sendRawTransaction(txn.serialize(),{ preflightCommitment: 'confirmed' }); }
else {
return "Failed to claim prize.";
}
let next_href = data.links?.next?.href;
return await postWin(agent, next_href);
} catch (error: any) {
console.error(error);
throw new Error(`RPS outcome failed: ${error.message}`);
const data: any = await res.json();
if (data.transaction) {
const txn = Transaction.from(Buffer.from(data.transaction, "base64"));
txn.partialSign(agent.wallet);
await agent.connection.sendRawTransaction(txn.serialize(), {
preflightCommitment: "confirmed",
});
} else {
return "Failed to claim prize.";
}
const next_href = data.links?.next?.href;
return await postWin(agent, next_href);
} catch (error: any) {
console.error(error);
throw new Error(`RPS outcome failed: ${error.message}`);
}
}
async function postWin(agent: SolanaAgentKit, href: string): Promise<string> {
try {
const res = await fetch(
"https://rps.sendarcade.fun" + href, // href = /api/actions/postwin?id=...
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
account: agent.wallet.publicKey.toBase58(),
}),
},
);
try {
const res = await fetch(
"https://rps.sendarcade.fun" + href, // href = /api/actions/postwin?id=...
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
account: agent.wallet.publicKey.toBase58(),
}),
},
);

const data: any = await res.json();
const title = data.title;
return "Prize claimed Successfully" + "\n" + title;
} catch (error: any) {
console.error(error);
throw new Error(`RPS outcome failed: ${error.message}`);
}
}
const data: any = await res.json();
const title = data.title;
return "Prize claimed Successfully" + "\n" + title;
} catch (error: any) {
console.error(error);
throw new Error(`RPS outcome failed: ${error.message}`);
}
}

0 comments on commit 6f68718

Please sign in to comment.