This commit is contained in:
Sewmina Dilshan 2025-06-18 23:26:00 +05:30
parent 4a46b7d7df
commit 2e6b581fcc
6 changed files with 405 additions and 60 deletions

View File

@ -7,6 +7,49 @@
"description": "Created with Anchor"
},
"instructions": [
{
"name": "clear_bets_list",
"discriminator": [
128,
73,
20,
28,
69,
26,
247,
185
],
"accounts": [
{
"name": "bets_list",
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [
98,
101,
116,
115,
95,
108,
105,
115,
116
]
}
]
}
},
{
"name": "payer",
"writable": true,
"signer": true
}
],
"args": []
},
{
"name": "close_bet",
"discriminator": [
@ -72,7 +115,40 @@
},
{
"name": "bet_vault",
"writable": true
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [
98,
101,
116,
95,
118,
97,
117,
108,
116
]
},
{
"kind": "account",
"path": "bet_vault.owner",
"account": "BetVault"
},
{
"kind": "account",
"path": "bet_vault.game_id",
"account": "BetVault"
},
{
"kind": "account",
"path": "bet_vault.nonce",
"account": "BetVault"
}
]
}
},
{
"name": "winner",
@ -580,7 +656,40 @@
},
{
"name": "bet_vault",
"writable": true
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [
98,
101,
116,
95,
118,
97,
117,
108,
116
]
},
{
"kind": "account",
"path": "bet_vault.owner",
"account": "BetVault"
},
{
"kind": "account",
"path": "bet_vault.game_id",
"account": "BetVault"
},
{
"kind": "account",
"path": "bet_vault.nonce",
"account": "BetVault"
}
]
}
},
{
"name": "fee_wallet",
@ -1184,6 +1293,10 @@
"type": {
"kind": "struct",
"fields": [
{
"name": "nonce",
"type": "u64"
},
{
"name": "game_id",
"type": "string"

View File

@ -13,6 +13,49 @@ export type Bets = {
"description": "Created with Anchor"
},
"instructions": [
{
"name": "clearBetsList",
"discriminator": [
128,
73,
20,
28,
69,
26,
247,
185
],
"accounts": [
{
"name": "betsList",
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [
98,
101,
116,
115,
95,
108,
105,
115,
116
]
}
]
}
},
{
"name": "payer",
"writable": true,
"signer": true
}
],
"args": []
},
{
"name": "closeBet",
"discriminator": [
@ -78,7 +121,40 @@ export type Bets = {
},
{
"name": "betVault",
"writable": true
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [
98,
101,
116,
95,
118,
97,
117,
108,
116
]
},
{
"kind": "account",
"path": "bet_vault.owner",
"account": "betVault"
},
{
"kind": "account",
"path": "bet_vault.game_id",
"account": "betVault"
},
{
"kind": "account",
"path": "bet_vault.nonce",
"account": "betVault"
}
]
}
},
{
"name": "winner",
@ -586,7 +662,40 @@ export type Bets = {
},
{
"name": "betVault",
"writable": true
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [
98,
101,
116,
95,
118,
97,
117,
108,
116
]
},
{
"kind": "account",
"path": "bet_vault.owner",
"account": "betVault"
},
{
"kind": "account",
"path": "bet_vault.game_id",
"account": "betVault"
},
{
"kind": "account",
"path": "bet_vault.nonce",
"account": "betVault"
}
]
}
},
{
"name": "feeWallet",
@ -1190,6 +1299,10 @@ export type Bets = {
"type": {
"kind": "struct",
"fields": [
{
"name": "nonce",
"type": "u64"
},
{
"name": "gameId",
"type": "string"

View File

@ -25,7 +25,6 @@ async function close(){
console.log(`Bets list PDA : ${bet_list_pda}`);
const tx = await program.methods.clearBetsList().accounts({
betsList: bet_list_pda
}).rpc();
console.log(`clear tx: ${tx}`);

View File

@ -1,7 +1,7 @@
import { Connection, PublicKey, Keypair, clusterApiUrl, LAMPORTS_PER_SOL, SystemProgram } from "@solana/web3.js";
import { Bets } from "./bets";
import { AnchorProvider, BN, Program, Wallet } from "@coral-xyz/anchor";
import { clusterUrl, cocSk, feeWallet, getRandomInt, testerSk } from "./shared";
import { ATA_PROGRAM_ID, TOKEN_PROGRAM_ID, clusterUrl, cocSk, feeWallet, getRandomInt, testerSk } from "./shared";
import { getAssociatedTokenAddress } from "@solana/spl-token";
const IDL = require('./bets.json');
@ -9,6 +9,12 @@ async function close(){
const keypair = Keypair.fromSecretKey(Uint8Array.from(cocSk));
const cocKeypair = Keypair.fromSecretKey(Uint8Array.from(cocSk));
if (process.argv.length < 3) {
console.error("Please provide a bet address as an argument");
process.exit(1);
}
const bet = new PublicKey(process.argv[2]);
const connection = new Connection(clusterUrl, {
commitment: 'confirmed',
confirmTransactionInitialTimeout: 60000
@ -25,64 +31,85 @@ async function close(){
const [bet_list_pda] = await PublicKey.findProgramAddress([Buffer.from("bets_list")], program.programId);
console.log(`Bets list PDA : ${bet_list_pda}`);
try {
const bet_list = await program.account.betsList.fetch(bet_list_pda);
const betAcc = await program.account.betVault.fetch(bet);
const ownerRefferer = new PublicKey("BaTgUMPsk8fZoEMwig81Pw1KHmPpoYt31oiR5Qr2c8XR");
const joinerRefferer = feeWallet;
const winner = betAcc.owner;
console.log(`Closing ${bet}`);
for (const bet of bet_list.bets) {
try {
const betAcc = await program.account.betVault.fetch(bet);
if(betAcc.tokenMint.toString() != "11111111111111111111111111111111"){
console.log("Token mint is not default");
const tx = await program.methods.deductFeesToken(winner, betAcc.ownerId).accounts({
betsList: bet_list_pda,
feeWallet: feeWallet,
ownerReferrer: ownerRefferer,
joinerReferrer: joinerRefferer,
tokenMint: betAcc.tokenMint,
tokenProgram: TOKEN_PROGRAM_ID
}).rpc({
skipPreflight: false,
maxRetries: 3
});
await connection.confirmTransaction(tx);
console.log(`fee tx: ${tx}`);
const ownerRefferer = new PublicKey("BaTgUMPsk8fZoEMwig81Pw1KHmPpoYt31oiR5Qr2c8XR");
const joinerRefferer = feeWallet;
const tx2 = await program.methods.closeBetToken(winner, betAcc.ownerId).accounts({
betsList: bet_list_pda,
winner: winner,
tokenMint: betAcc.tokenMint,
tokenProgram: TOKEN_PROGRAM_ID
}).rpc({
skipPreflight: false,
maxRetries: 3
});
console.log(`close tx: ${tx2}`);
const confirmation = await connection.confirmTransaction(tx2);
const winner = betAcc.owner;
console.log(`Closing ${bet}`);
const tx = await program.methods.deductFees(winner, betAcc.ownerId).accounts({
betVault: bet,
betsList: bet_list_pda,
feeWallet: feeWallet,
ownerReferrer: ownerRefferer,
joinerReferrer: joinerRefferer,
}).rpc({
skipPreflight: false,
maxRetries: 3
});
await connection.confirmTransaction(tx);
console.log(`fee tx: ${tx}`);
const tx2 = await program.methods.closeBet(winner, betAcc.ownerId).accounts({
betVault: bet,
betsList: bet_list_pda,
winner: winner,
}).rpc({
skipPreflight: false,
maxRetries: 3
});
console.log(`close tx: ${tx2}`);
const confirmation = await connection.confirmTransaction(tx2);
if (confirmation.value.err) {
console.error(`Transaction failed: ${confirmation.value.err}`);
continue;
}
console.log(`tx complete: ${tx}`);
solBalance = (await connection.getBalance(keypair.publicKey))/ LAMPORTS_PER_SOL;
console.log(`Tester ${keypair.publicKey} has ${solBalance} SOL`);
} catch (err) {
console.error(`Error processing bet ${bet}:`, err);
continue;
}
if (confirmation.value.err) {
console.error(`Transaction failed: ${confirmation.value.err}`);
}
} catch (err) {
console.error('Error fetching bet list:', err);
throw err;
console.log(`tx complete: ${tx}`);
}else{
console.log("Token mint is default");
const tx = await program.methods.deductFees(winner, betAcc.ownerId).accounts({
betVault: bet,
betsList: bet_list_pda,
feeWallet: feeWallet,
ownerReferrer: ownerRefferer,
joinerReferrer: joinerRefferer,
}).rpc({
skipPreflight: false,
maxRetries: 3
});
await connection.confirmTransaction(tx);
console.log(`fee tx: ${tx}`);
const tx2 = await program.methods.closeBet(winner, betAcc.ownerId).accounts({
betVault: bet,
betsList: bet_list_pda,
winner: winner,
}).rpc({
skipPreflight: false,
maxRetries: 3
});
console.log(`close tx: ${tx2}`);
const confirmation = await connection.confirmTransaction(tx2);
if (confirmation.value.err) {
console.error(`Transaction failed: ${confirmation.value.err}`);
}
console.log(`tx complete: ${tx}`);
}
}
close().catch(err => {

91
src/close_all.ts Normal file
View File

@ -0,0 +1,91 @@
import { Connection, PublicKey, Keypair, clusterApiUrl, LAMPORTS_PER_SOL, SystemProgram } from "@solana/web3.js";
import { Bets } from "./bets";
import { AnchorProvider, BN, Program, Wallet } from "@coral-xyz/anchor";
import { clusterUrl, cocSk, feeWallet, getRandomInt, testerSk } from "./shared";
import { getAssociatedTokenAddress } from "@solana/spl-token";
const IDL = require('./bets.json');
async function close(){
const keypair = Keypair.fromSecretKey(Uint8Array.from(cocSk));
const cocKeypair = Keypair.fromSecretKey(Uint8Array.from(cocSk));
const connection = new Connection(clusterUrl, {
commitment: 'confirmed',
confirmTransactionInitialTimeout: 60000
});
const provider = new AnchorProvider(connection, new Wallet(keypair), {
commitment: 'confirmed',
preflightCommitment: 'confirmed',
});
const program:Program<Bets> = new Program<Bets>(IDL, provider);
let solBalance = (await connection.getBalance(keypair.publicKey))/ LAMPORTS_PER_SOL;
console.log(`Tester ${keypair.publicKey} has ${solBalance} SOL`);
const [bet_list_pda] = await PublicKey.findProgramAddress([Buffer.from("bets_list")], program.programId);
console.log(`Bets list PDA : ${bet_list_pda}`);
try {
const bet_list = await program.account.betsList.fetch(bet_list_pda);
for (const bet of bet_list.bets) {
try {
const betAcc = await program.account.betVault.fetch(bet);
const ownerRefferer = new PublicKey("BaTgUMPsk8fZoEMwig81Pw1KHmPpoYt31oiR5Qr2c8XR");
const joinerRefferer = feeWallet;
const winner = betAcc.owner;
console.log(`Closing ${bet}`);
const tx = await program.methods.deductFees(winner, betAcc.ownerId).accounts({
betVault: bet,
betsList: bet_list_pda,
feeWallet: feeWallet,
ownerReferrer: ownerRefferer,
joinerReferrer: joinerRefferer,
}).rpc({
skipPreflight: false,
maxRetries: 3
});
await connection.confirmTransaction(tx);
console.log(`fee tx: ${tx}`);
const tx2 = await program.methods.closeBet(winner, betAcc.ownerId).accounts({
betVault: bet,
betsList: bet_list_pda,
winner: winner,
}).rpc({
skipPreflight: false,
maxRetries: 3
});
console.log(`close tx: ${tx2}`);
const confirmation = await connection.confirmTransaction(tx2);
if (confirmation.value.err) {
console.error(`Transaction failed: ${confirmation.value.err}`);
continue;
}
console.log(`tx complete: ${tx}`);
solBalance = (await connection.getBalance(keypair.publicKey))/ LAMPORTS_PER_SOL;
console.log(`Tester ${keypair.publicKey} has ${solBalance} SOL`);
} catch (err) {
console.error(`Error processing bet ${bet}:`, err);
continue;
}
}
} catch (err) {
console.error('Error fetching bet list:', err);
throw err;
}
}
close().catch(err => {
console.error('Fatal error:', err);
process.exit(1);
});

View File

@ -30,6 +30,7 @@ async function get(){
tokenBalance = tokenAccData.amount;
}
console.log({
address: bet.toString(),
game_id: betAcc.gameId,
owner: betAcc.owner + " : " + betAcc.ownerId,
joiner: betAcc.joiner + " : " + betAcc.joinerId,
@ -38,6 +39,7 @@ async function get(){
token_account: isToken ? tokenAccount.toString() : "N/A",
token_mint: isToken ? betAcc.tokenMint.toString() : "N/A",
token_balance: isToken ? tokenBalance : "N/A",
nonce: betAcc.nonce.toNumber()
});
})
}