This commit is contained in:
Sewmina Dilshan 2024-10-30 08:14:11 +05:30
commit ecd99a3735
13 changed files with 3870 additions and 0 deletions

41
.gitignore vendored Normal file
View File

@ -0,0 +1,41 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
*.swp
pids
logs
results
tmp
# Build
public/css/main.css
# Coverage reports
coverage
# API keys and secrets
.env
# Dependency directory
node_modules
bower_components
# Editors
.idea
*.iml
# OS metadata
.DS_Store
Thumbs.db
# Ignore built ts files
dist/**/*
# ignore yarn.lock
yarn.lock

11
eslint.config.mjs Normal file
View File

@ -0,0 +1,11 @@
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";
export default [
{files: ["**/*.{js,mjs,cjs,ts}"]},
{languageOptions: { globals: globals.browser }},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];

2611
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

29
package.json Normal file
View File

@ -0,0 +1,29 @@
{
"name": "ticket_store",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"init": "ts-node src/init.ts",
"add": "ts-node src/add_seller.ts",
"purchase": "ts-node src/purchase.ts",
"get": "ts-node src/get.ts",
"build": "tsc"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"@eslint/js": "^9.13.0",
"eslint": "^9.13.0",
"globals": "^15.11.0",
"typescript": "^5.6.3",
"typescript-eslint": "^8.11.0"
},
"dependencies": {
"@coral-xyz/anchor": "^0.30.1",
"@solana/spl-token": "^0.4.9",
"@solana/web3.js": "^1.95.4",
"bs58": "^6.0.0"
}
}

52
src/add_seller.ts Normal file
View File

@ -0,0 +1,52 @@
import { Connection, PublicKey, Keypair, clusterApiUrl, Transaction, sendAndConfirmTransaction } from "@solana/web3.js";
import {createAssociatedTokenAccountInstruction, getAssociatedTokenAddressSync} from "@solana/spl-token";
import { TicketStore } from "./ticket_store";
import { AnchorProvider, BN, Program, Wallet } from "@coral-xyz/anchor";
import { clusterUrl, cocSk, ticketsMint, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, ticketsSk } from "./keys";
const IDL = require('./ticket_store.json');
async function Init(){
const keypair = Keypair.fromSecretKey(Uint8Array.from(cocSk));
const mint = Keypair.fromSecretKey(Uint8Array.from(ticketsSk));
const connection = new Connection(clusterUrl);
const provider = new AnchorProvider(connection, new Wallet(keypair));
const program: Program<TicketStore> = new Program<TicketStore>(IDL, provider);
console.log(`Adding seller ${keypair.publicKey} with mint of ${ticketsMint}`);
// const [sellerTicketATA] = await PublicKey.findProgramAddressSync([keypair.publicKey.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), ticketsMint.toBuffer()], ASSOCIATED_TOKEN_PROGRAM_ID);
const sellerTicketATA = await getAssociatedTokenAddressSync(mint.publicKey, keypair.publicKey, false, TOKEN_PROGRAM_ID);
console.log(`seller tickets ATA: ${sellerTicketATA.toBase58()}`);
try{
const sellerTicketsBalance = await connection.getTokenAccountBalance(sellerTicketATA);
console.log(`Seller has ${sellerTicketsBalance.value.uiAmount} Tickets, Putting 100 to sale`);
}catch{
console.log("No ATA for seller found, Lets create one");
let tx = new Transaction();
tx.add(
createAssociatedTokenAccountInstruction(
keypair.publicKey, // payer
sellerTicketATA, // ata
keypair.publicKey, // owner
ticketsMint, // mint
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
)
);
const tx_hash = await sendAndConfirmTransaction(connection, tx, [keypair,mint], {
commitment: "finalized",
})
console.log(`create ata txhash: ${tx_hash}`);
}
const tx = await program.methods.addSeller(new BN(100)).accounts({
mint:ticketsMint,
tokenProgram: TOKEN_PROGRAM_ID
}).rpc();
console.log(tx);
}
Init();

46
src/get.ts Normal file
View File

@ -0,0 +1,46 @@
import { Connection, PublicKey, Keypair, clusterApiUrl } from "@solana/web3.js";
import { TicketStore } from "./ticket_store";
import { AnchorProvider, Program, Wallet } from "@coral-xyz/anchor";
import { clusterUrl, cocSk, ticketsMint } from "./keys";
import { ASSOCIATED_PROGRAM_ID, TOKEN_PROGRAM_ID } from "@coral-xyz/anchor/dist/cjs/utils/token";
const IDL = require('./ticket_store.json');
async function Init(){
const keypair = Keypair.fromSecretKey(Uint8Array.from(cocSk));
const connection = new Connection(clusterUrl);
const provider = new AnchorProvider(connection, new Wallet(keypair));
const program: Program<TicketStore> = new Program<TicketStore>(IDL, provider);
console.log(program.programId);
const [sellers_reg_pda] = await PublicKey.findProgramAddress([Buffer.from("sales_reg")], program.programId);
console.log(`Sellers Reg PDA: ${sellers_reg_pda}`);
const sellers_reg_acc = await program.account.sellersRegistry.fetch(sellers_reg_pda);
console.log(sellers_reg_acc);
sellers_reg_acc.salesPdas.forEach(async salesPda => {
const salesAcc = await program.account.sales.fetch(salesPda);
console.log(salesAcc);
});
}
async function PrintSalesAcc(salesPda, program, connection, keypair){
const salesAcc = await program.account.sales.fetch(salesPda);
const [sellerAta] = await PublicKey.findProgramAddress([keypair.publicKey.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), salesAcc.mint.toBuffer()], ASSOCIATED_PROGRAM_ID);
const sellerTokenHoldings = await connection.getTokenAccountBalance(sellerAta);
const output = {
seller: salesAcc.seller.toBase58(),
seller_mint: salesAcc.mint.toBase58(),
sellers_ata: sellerAta.toBase58(),
sellers_ata_balance: sellerTokenHoldings
}
console.log("====Sales Account====")
console.log(output);
}
Init();

18
src/init.ts Normal file
View File

@ -0,0 +1,18 @@
import { Connection, PublicKey, Keypair, clusterApiUrl } from "@solana/web3.js";
import { TicketStore } from "./ticket_store";
import { AnchorProvider, Program, Wallet } from "@coral-xyz/anchor";
import { clusterUrl, cocSk } from "./keys";
const IDL = require('./ticket_store.json');
async function Init(){
const keypair = Keypair.fromSecretKey(Uint8Array.from(cocSk));
const connection = new Connection(clusterUrl);
const provider = new AnchorProvider(connection, new Wallet(keypair));
const program: Program<TicketStore> = new Program<TicketStore>(IDL, provider);
console.log(program.programId);
await program.methods.initialize().rpc();
}
Init();

11
src/keys.ts Normal file
View File

@ -0,0 +1,11 @@
import { clusterApiUrl, PublicKey } from "@solana/web3.js";
export const cocSk = [202,150,67,41,155,133,176,172,9,100,150,190,239,37,69,73,18,16,76,65,164,197,99,134,240,151,112,65,61,122,95,41,9,44,6,237,108,123,86,90,144,27,1,160,101,95,239,35,53,91,195,220,22,214,2,84,132,37,20,236,133,242,104,197];
export const testerSk = [0,86,239,216,67,18,45,223,17,96,119,58,187,90,175,61,72,117,44,13,224,255,64,74,222,14,50,134,240,250,14,212,13,59,115,13,19,107,33,227,1,184,184,96,20,214,181,23,53,244,82,197,36,189,83,82,134,211,83,200,67,14,143,90];
export const ticketsSk = [229,133,46,6,15,114,88,149,178,253,116,33,217,111,94,244,105,170,38,6,63,11,240,208,100,188,213,19,214,172,25,104,13,66,62,178,5,210,60,27,205,73,155,208,220,115,21,71,229,90,136,12,171,103,219,77,230,84,214,49,107,123,186,61];
export const ticketsMint = new PublicKey("tktUDLZhFGb9VW9zDxZ7HYDFuBooEf8daZEvPbBY7at");
export const clusterUrl = clusterApiUrl("devnet");
export const TOKEN_PROGRAM_ID = new PublicKey('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb');
export const ASSOCIATED_TOKEN_PROGRAM_ID = new PublicKey('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL');

73
src/purchase.ts Normal file
View File

@ -0,0 +1,73 @@
import { Connection, PublicKey, Keypair, clusterApiUrl } from "@solana/web3.js";
import { TicketStore } from "./ticket_store";
import { AnchorProvider, BN, Program, Wallet } from "@coral-xyz/anchor";
import { clusterUrl, testerSk, ticketsMint } from "./keys";
import { ASSOCIATED_PROGRAM_ID, TOKEN_PROGRAM_ID } from "@coral-xyz/anchor/dist/cjs/utils/token";
const IDL = require('./ticket_store.json');
async function Init(){
const keypair = Keypair.fromSecretKey(Uint8Array.from(testerSk));
const connection = new Connection(clusterUrl);
const provider = new AnchorProvider(connection, new Wallet(keypair));
const program: Program<TicketStore> = new Program<TicketStore>(IDL, provider);
let solBalance = await connection.getBalance(keypair.publicKey);
console.log(`Tester ${keypair.publicKey} has ${solBalance} SOL`);
if(solBalance <= 1){
console.log("Requesting airdrop due to insufficient funds");
try{
await connection.requestAirdrop(keypair.publicKey, 5);
}catch{
console.log("Airdrop failed");
}
solBalance = await connection.getBalance(keypair.publicKey);
console.log(`Tester now has ${solBalance} SOL`);
}
let ticketsBalanceBefore = 0;
const [buyerAta] = await PublicKey.findProgramAddress([keypair.publicKey.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), ticketsMint.toBuffer()], ASSOCIATED_PROGRAM_ID);
try{
console.log(`buyers ATA is: ${buyerAta.toBase58()}`);
ticketsBalanceBefore = (await connection.getTokenAccountBalance(buyerAta)).value.uiAmount;
console.log(`Tester has ${ticketsBalanceBefore} tickets already`);
}catch{
console.log("Tester doesn't even has an ATA for this token");
}
const [sellers_reg_pda] = await PublicKey.findProgramAddress([Buffer.from("sales_reg")], program.programId);
console.log(`Sellers Reg PDA: ${sellers_reg_pda}`);
const sellers_reg_acc = await program.account.sellersRegistry.fetch(sellers_reg_pda);
console.log(sellers_reg_acc);
let chosenSellerOwner;
for(let i =0; i < sellers_reg_acc.salesPdas.length; i++){
const salesPda = sellers_reg_acc.salesPdas[i];
const salesAcc = await program.account.sales.fetch(salesPda);
const sellerTokenHoldings = await connection.getTokenAccountBalance(salesAcc.vault);
const output = {
owner: salesAcc.seller.toBase58(),
seller_vault_balance: sellerTokenHoldings
}
if(sellerTokenHoldings.value.uiAmount > 10){
chosenSellerOwner = salesAcc.seller;
}
console.log("====Sales Account====")
console.log(output);
}
console.log(`Buying from seller account ${chosenSellerOwner}`);
const tx = await program.methods.purchaseTickets(new BN(1)).accounts({
seller:chosenSellerOwner,
mint: ticketsMint,
tokenProgram: TOKEN_PROGRAM_ID
}).rpc();
console.log(`purchase method called : ${tx}`)
await connection.confirmTransaction(tx);
const newTicketsBalance = (await connection.getTokenAccountBalance(buyerAta)).value.uiAmount;
console.log(`Tester had ${ticketsBalanceBefore} tickets, now he has ${newTicketsBalance} tickets.`);
}
Init();

8
src/test.ts Normal file
View File

@ -0,0 +1,8 @@
import { Keypair } from "@solana/web3.js";
import bs58 from 'bs58';
const secretKey = "";
const keypair =Keypair.fromSecretKey(
bs58.decode(secretKey)
);
console.log(keypair.publicKey);

476
src/ticket_store.json Normal file
View File

@ -0,0 +1,476 @@
{
"address": "C47ThCBJcgyx3axKT5J2PCH4nNHtjxiWK1mMxTY7kFrU",
"metadata": {
"name": "ticket_store",
"version": "0.1.0",
"spec": "0.1.0",
"description": "Created with Anchor"
},
"instructions": [
{
"name": "add_seller",
"discriminator": [
146,
192,
28,
179,
211,
8,
182,
81
],
"accounts": [
{
"name": "seller",
"writable": true,
"signer": true
},
{
"name": "sales",
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [
115,
97,
108,
101,
115
]
},
{
"kind": "account",
"path": "seller"
}
]
}
},
{
"name": "mint"
},
{
"name": "vault",
"writable": true,
"pda": {
"seeds": [
{
"kind": "account",
"path": "sales"
},
{
"kind": "account",
"path": "token_program"
},
{
"kind": "account",
"path": "mint"
}
],
"program": {
"kind": "const",
"value": [
140,
151,
37,
143,
78,
36,
137,
241,
187,
61,
16,
41,
20,
142,
13,
131,
11,
90,
19,
153,
218,
255,
16,
132,
4,
142,
123,
216,
219,
233,
248,
89
]
}
}
},
{
"name": "sellers_registry",
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [
115,
97,
108,
101,
115,
95,
114,
101,
103
]
}
]
}
},
{
"name": "system_program",
"address": "11111111111111111111111111111111"
},
{
"name": "token_program"
},
{
"name": "associated_token_program",
"address": "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"
}
],
"args": [
{
"name": "tickets_count",
"type": "u64"
}
]
},
{
"name": "initialize",
"discriminator": [
175,
175,
109,
31,
13,
152,
155,
237
],
"accounts": [
{
"name": "signer",
"writable": true,
"signer": true
},
{
"name": "sellers_registry",
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [
115,
97,
108,
101,
115,
95,
114,
101,
103
]
}
]
}
},
{
"name": "system_program",
"address": "11111111111111111111111111111111"
}
],
"args": []
},
{
"name": "purchase_tickets",
"discriminator": [
146,
121,
85,
207,
182,
70,
169,
155
],
"accounts": [
{
"name": "buyer",
"writable": true,
"signer": true
},
{
"name": "seller",
"writable": true
},
{
"name": "mint"
},
{
"name": "buyer_token_account",
"writable": true,
"pda": {
"seeds": [
{
"kind": "account",
"path": "buyer"
},
{
"kind": "account",
"path": "token_program"
},
{
"kind": "account",
"path": "mint"
}
],
"program": {
"kind": "const",
"value": [
140,
151,
37,
143,
78,
36,
137,
241,
187,
61,
16,
41,
20,
142,
13,
131,
11,
90,
19,
153,
218,
255,
16,
132,
4,
142,
123,
216,
219,
233,
248,
89
]
}
}
},
{
"name": "sales",
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [
115,
97,
108,
101,
115
]
},
{
"kind": "account",
"path": "seller"
}
]
}
},
{
"name": "vault",
"writable": true,
"pda": {
"seeds": [
{
"kind": "account",
"path": "sales"
},
{
"kind": "account",
"path": "token_program"
},
{
"kind": "account",
"path": "mint"
}
],
"program": {
"kind": "const",
"value": [
140,
151,
37,
143,
78,
36,
137,
241,
187,
61,
16,
41,
20,
142,
13,
131,
11,
90,
19,
153,
218,
255,
16,
132,
4,
142,
123,
216,
219,
233,
248,
89
]
}
}
},
{
"name": "system_program",
"address": "11111111111111111111111111111111"
},
{
"name": "token_program"
},
{
"name": "associated_token_program",
"address": "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"
}
],
"args": [
{
"name": "amount",
"type": "u64"
}
]
}
],
"accounts": [
{
"name": "Sales",
"discriminator": [
173,
165,
151,
131,
107,
95,
166,
32
]
},
{
"name": "SellersRegistry",
"discriminator": [
53,
71,
248,
136,
244,
177,
37,
10
]
}
],
"errors": [
{
"code": 6000,
"name": "CustomError",
"msg": "Custom error message"
},
{
"code": 6001,
"name": "InsufficientFunds",
"msg": "Insufficient funds to purchase a ticket, Recharge your wallet and try again"
}
],
"types": [
{
"name": "Sales",
"type": {
"kind": "struct",
"fields": [
{
"name": "seller",
"type": "pubkey"
},
{
"name": "vault",
"type": "pubkey"
},
{
"name": "mint",
"type": "pubkey"
},
{
"name": "bump",
"type": "u8"
}
]
}
},
{
"name": "SellersRegistry",
"type": {
"kind": "struct",
"fields": [
{
"name": "sales_pdas",
"type": {
"vec": "pubkey"
}
}
]
}
}
],
"constants": [
{
"name": "ID",
"type": "string",
"value": "\"\""
},
{
"name": "SELLERS_REGISTRY_SEED",
"type": {
"array": [
"u8",
9
]
},
"value": "[115, 97, 108, 101, 115, 95, 114, 101, 103]"
}
]
}

482
src/ticket_store.ts Normal file
View File

@ -0,0 +1,482 @@
/**
* Program IDL in camelCase format in order to be used in JS/TS.
*
* Note that this is only a type helper and is not the actual IDL. The original
* IDL can be found at `target/idl/ticket_store.json`.
*/
export type TicketStore = {
"address": "C47ThCBJcgyx3axKT5J2PCH4nNHtjxiWK1mMxTY7kFrU",
"metadata": {
"name": "ticketStore",
"version": "0.1.0",
"spec": "0.1.0",
"description": "Created with Anchor"
},
"instructions": [
{
"name": "addSeller",
"discriminator": [
146,
192,
28,
179,
211,
8,
182,
81
],
"accounts": [
{
"name": "seller",
"writable": true,
"signer": true
},
{
"name": "sales",
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [
115,
97,
108,
101,
115
]
},
{
"kind": "account",
"path": "seller"
}
]
}
},
{
"name": "mint"
},
{
"name": "vault",
"writable": true,
"pda": {
"seeds": [
{
"kind": "account",
"path": "sales"
},
{
"kind": "account",
"path": "tokenProgram"
},
{
"kind": "account",
"path": "mint"
}
],
"program": {
"kind": "const",
"value": [
140,
151,
37,
143,
78,
36,
137,
241,
187,
61,
16,
41,
20,
142,
13,
131,
11,
90,
19,
153,
218,
255,
16,
132,
4,
142,
123,
216,
219,
233,
248,
89
]
}
}
},
{
"name": "sellersRegistry",
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [
115,
97,
108,
101,
115,
95,
114,
101,
103
]
}
]
}
},
{
"name": "systemProgram",
"address": "11111111111111111111111111111111"
},
{
"name": "tokenProgram"
},
{
"name": "associatedTokenProgram",
"address": "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"
}
],
"args": [
{
"name": "ticketsCount",
"type": "u64"
}
]
},
{
"name": "initialize",
"discriminator": [
175,
175,
109,
31,
13,
152,
155,
237
],
"accounts": [
{
"name": "signer",
"writable": true,
"signer": true
},
{
"name": "sellersRegistry",
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [
115,
97,
108,
101,
115,
95,
114,
101,
103
]
}
]
}
},
{
"name": "systemProgram",
"address": "11111111111111111111111111111111"
}
],
"args": []
},
{
"name": "purchaseTickets",
"discriminator": [
146,
121,
85,
207,
182,
70,
169,
155
],
"accounts": [
{
"name": "buyer",
"writable": true,
"signer": true
},
{
"name": "seller",
"writable": true
},
{
"name": "mint"
},
{
"name": "buyerTokenAccount",
"writable": true,
"pda": {
"seeds": [
{
"kind": "account",
"path": "buyer"
},
{
"kind": "account",
"path": "tokenProgram"
},
{
"kind": "account",
"path": "mint"
}
],
"program": {
"kind": "const",
"value": [
140,
151,
37,
143,
78,
36,
137,
241,
187,
61,
16,
41,
20,
142,
13,
131,
11,
90,
19,
153,
218,
255,
16,
132,
4,
142,
123,
216,
219,
233,
248,
89
]
}
}
},
{
"name": "sales",
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [
115,
97,
108,
101,
115
]
},
{
"kind": "account",
"path": "seller"
}
]
}
},
{
"name": "vault",
"writable": true,
"pda": {
"seeds": [
{
"kind": "account",
"path": "sales"
},
{
"kind": "account",
"path": "tokenProgram"
},
{
"kind": "account",
"path": "mint"
}
],
"program": {
"kind": "const",
"value": [
140,
151,
37,
143,
78,
36,
137,
241,
187,
61,
16,
41,
20,
142,
13,
131,
11,
90,
19,
153,
218,
255,
16,
132,
4,
142,
123,
216,
219,
233,
248,
89
]
}
}
},
{
"name": "systemProgram",
"address": "11111111111111111111111111111111"
},
{
"name": "tokenProgram"
},
{
"name": "associatedTokenProgram",
"address": "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"
}
],
"args": [
{
"name": "amount",
"type": "u64"
}
]
}
],
"accounts": [
{
"name": "sales",
"discriminator": [
173,
165,
151,
131,
107,
95,
166,
32
]
},
{
"name": "sellersRegistry",
"discriminator": [
53,
71,
248,
136,
244,
177,
37,
10
]
}
],
"errors": [
{
"code": 6000,
"name": "customError",
"msg": "Custom error message"
},
{
"code": 6001,
"name": "insufficientFunds",
"msg": "Insufficient funds to purchase a ticket, Recharge your wallet and try again"
}
],
"types": [
{
"name": "sales",
"type": {
"kind": "struct",
"fields": [
{
"name": "seller",
"type": "pubkey"
},
{
"name": "vault",
"type": "pubkey"
},
{
"name": "mint",
"type": "pubkey"
},
{
"name": "bump",
"type": "u8"
}
]
}
},
{
"name": "sellersRegistry",
"type": {
"kind": "struct",
"fields": [
{
"name": "salesPdas",
"type": {
"vec": "pubkey"
}
}
]
}
}
],
"constants": [
{
"name": "id",
"type": "string",
"value": "\"\""
},
{
"name": "sellersRegistrySeed",
"type": {
"array": [
"u8",
9
]
},
"value": "[115, 97, 108, 101, 115, 95, 114, 101, 103]"
}
]
};

12
tsconfig.json Normal file
View File

@ -0,0 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"rootDir": "./src",
"target": "es6",
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist"
},
"lib": ["es2015"]
}