usdt
This commit is contained in:
parent
34bd7d66b3
commit
6779da9882
178
src/bets.json
178
src/bets.json
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"address": "JAf3ZkQ469okXAzA6BKJeKBb9ZkCtZanULaUsapskoyn",
|
"address": "Haj94DF925qNRgcoRwQfNsVLKgSmFhG4bjgtvusMkkpD",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"name": "bets",
|
"name": "bets",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
|
|
@ -41,15 +41,53 @@
|
||||||
"writable": true,
|
"writable": true,
|
||||||
"signer": true
|
"signer": true
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "owner_referrer",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "joiner_referrer",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "system_program",
|
"name": "system_program",
|
||||||
"address": "11111111111111111111111111111111"
|
"address": "11111111111111111111111111111111"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "bet_vault_token_account",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "fee_wallet_token_account",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "owner_referrer_token_account",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "joiner_referrer_token_account",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "token_program",
|
||||||
|
"address": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"args": [
|
"args": [
|
||||||
{
|
{
|
||||||
"name": "winner",
|
"name": "winner",
|
||||||
"type": "pubkey"
|
"type": "pubkey"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "userid",
|
||||||
|
"type": "string"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
@ -112,6 +150,20 @@
|
||||||
{
|
{
|
||||||
"name": "system_program",
|
"name": "system_program",
|
||||||
"address": "11111111111111111111111111111111"
|
"address": "11111111111111111111111111111111"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "payer_token_account",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "bet_vault_token_account",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "token_program",
|
||||||
|
"address": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"args": [
|
"args": [
|
||||||
|
|
@ -130,6 +182,20 @@
|
||||||
{
|
{
|
||||||
"name": "nonce",
|
"name": "nonce",
|
||||||
"type": "u64"
|
"type": "u64"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "is_native_sol",
|
||||||
|
"type": "bool"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "token_mint",
|
||||||
|
"type": {
|
||||||
|
"option": "pubkey"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "token_decimals",
|
||||||
|
"type": "u8"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
@ -205,6 +271,20 @@
|
||||||
{
|
{
|
||||||
"name": "system_program",
|
"name": "system_program",
|
||||||
"address": "11111111111111111111111111111111"
|
"address": "11111111111111111111111111111111"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "payer_token_account",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "bet_vault_token_account",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "token_program",
|
||||||
|
"address": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"args": [
|
"args": [
|
||||||
|
|
@ -217,6 +297,62 @@
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "refund_bet",
|
||||||
|
"discriminator": [
|
||||||
|
209,
|
||||||
|
182,
|
||||||
|
226,
|
||||||
|
96,
|
||||||
|
55,
|
||||||
|
121,
|
||||||
|
83,
|
||||||
|
183
|
||||||
|
],
|
||||||
|
"accounts": [
|
||||||
|
{
|
||||||
|
"name": "bets_list",
|
||||||
|
"writable": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "bet_vault",
|
||||||
|
"writable": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "owner",
|
||||||
|
"writable": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "payer",
|
||||||
|
"writable": true,
|
||||||
|
"signer": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "system_program",
|
||||||
|
"address": "11111111111111111111111111111111"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "bet_vault_token_account",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "joiner_token_account",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "token_program",
|
||||||
|
"address": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"args": [
|
||||||
|
{
|
||||||
|
"name": "owner",
|
||||||
|
"type": "pubkey"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"accounts": [
|
"accounts": [
|
||||||
|
|
@ -250,8 +386,28 @@
|
||||||
"errors": [
|
"errors": [
|
||||||
{
|
{
|
||||||
"code": 6000,
|
"code": 6000,
|
||||||
"name": "CustomError",
|
"name": "BetNotFilled",
|
||||||
"msg": "Custom error message"
|
"msg": "Bet is not filled yet!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"code": 6001,
|
||||||
|
"name": "BetAlreadyFilled",
|
||||||
|
"msg": "Bet is already filled"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"code": 6002,
|
||||||
|
"name": "InvalidWinner",
|
||||||
|
"msg": "The winner must be either the owner or the joiner of the bet."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"code": 6003,
|
||||||
|
"name": "InvalidFeeCollector",
|
||||||
|
"msg": "Please use the correct fee collector address"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"code": 6004,
|
||||||
|
"name": "JoinerAccountNotProvided",
|
||||||
|
"msg": ""
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"types": [
|
"types": [
|
||||||
|
|
@ -283,6 +439,20 @@
|
||||||
{
|
{
|
||||||
"name": "wager",
|
"name": "wager",
|
||||||
"type": "u64"
|
"type": "u64"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "is_native_sol",
|
||||||
|
"type": "bool"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "token_mint",
|
||||||
|
"type": {
|
||||||
|
"option": "pubkey"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "token_decimals",
|
||||||
|
"type": "u8"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
@ -306,7 +476,7 @@
|
||||||
{
|
{
|
||||||
"name": "FEE_COLLECTOR",
|
"name": "FEE_COLLECTOR",
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"value": "\"cocD4r4yNpHxPq7CzUebxEMyLki3X4d2Y3HcTX5ptUc\""
|
"value": "\"9esrj2X33pr5og6fdkDMjaW6fdnnb9hT1cWshamxTdL4\""
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "SEED",
|
"name": "SEED",
|
||||||
|
|
|
||||||
178
src/bets.ts
178
src/bets.ts
|
|
@ -5,7 +5,7 @@
|
||||||
* IDL can be found at `target/idl/bets.json`.
|
* IDL can be found at `target/idl/bets.json`.
|
||||||
*/
|
*/
|
||||||
export type Bets = {
|
export type Bets = {
|
||||||
"address": "JAf3ZkQ469okXAzA6BKJeKBb9ZkCtZanULaUsapskoyn",
|
"address": "Haj94DF925qNRgcoRwQfNsVLKgSmFhG4bjgtvusMkkpD",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"name": "bets",
|
"name": "bets",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
|
|
@ -47,15 +47,53 @@ export type Bets = {
|
||||||
"writable": true,
|
"writable": true,
|
||||||
"signer": true
|
"signer": true
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "ownerReferrer",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "joinerReferrer",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "systemProgram",
|
"name": "systemProgram",
|
||||||
"address": "11111111111111111111111111111111"
|
"address": "11111111111111111111111111111111"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "betVaultTokenAccount",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "feeWalletTokenAccount",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "ownerReferrerTokenAccount",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "joinerReferrerTokenAccount",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "tokenProgram",
|
||||||
|
"address": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"args": [
|
"args": [
|
||||||
{
|
{
|
||||||
"name": "winner",
|
"name": "winner",
|
||||||
"type": "pubkey"
|
"type": "pubkey"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "userid",
|
||||||
|
"type": "string"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
@ -118,6 +156,20 @@ export type Bets = {
|
||||||
{
|
{
|
||||||
"name": "systemProgram",
|
"name": "systemProgram",
|
||||||
"address": "11111111111111111111111111111111"
|
"address": "11111111111111111111111111111111"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "payerTokenAccount",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "betVaultTokenAccount",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "tokenProgram",
|
||||||
|
"address": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"args": [
|
"args": [
|
||||||
|
|
@ -136,6 +188,20 @@ export type Bets = {
|
||||||
{
|
{
|
||||||
"name": "nonce",
|
"name": "nonce",
|
||||||
"type": "u64"
|
"type": "u64"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "isNativeSol",
|
||||||
|
"type": "bool"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "tokenMint",
|
||||||
|
"type": {
|
||||||
|
"option": "pubkey"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "tokenDecimals",
|
||||||
|
"type": "u8"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
@ -211,6 +277,20 @@ export type Bets = {
|
||||||
{
|
{
|
||||||
"name": "systemProgram",
|
"name": "systemProgram",
|
||||||
"address": "11111111111111111111111111111111"
|
"address": "11111111111111111111111111111111"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "payerTokenAccount",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "betVaultTokenAccount",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "tokenProgram",
|
||||||
|
"address": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"args": [
|
"args": [
|
||||||
|
|
@ -223,6 +303,62 @@ export type Bets = {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "refundBet",
|
||||||
|
"discriminator": [
|
||||||
|
209,
|
||||||
|
182,
|
||||||
|
226,
|
||||||
|
96,
|
||||||
|
55,
|
||||||
|
121,
|
||||||
|
83,
|
||||||
|
183
|
||||||
|
],
|
||||||
|
"accounts": [
|
||||||
|
{
|
||||||
|
"name": "betsList",
|
||||||
|
"writable": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "betVault",
|
||||||
|
"writable": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "owner",
|
||||||
|
"writable": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "payer",
|
||||||
|
"writable": true,
|
||||||
|
"signer": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "systemProgram",
|
||||||
|
"address": "11111111111111111111111111111111"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "betVaultTokenAccount",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "joinerTokenAccount",
|
||||||
|
"writable": true,
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "tokenProgram",
|
||||||
|
"address": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"args": [
|
||||||
|
{
|
||||||
|
"name": "owner",
|
||||||
|
"type": "pubkey"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"accounts": [
|
"accounts": [
|
||||||
|
|
@ -256,8 +392,28 @@ export type Bets = {
|
||||||
"errors": [
|
"errors": [
|
||||||
{
|
{
|
||||||
"code": 6000,
|
"code": 6000,
|
||||||
"name": "customError",
|
"name": "betNotFilled",
|
||||||
"msg": "Custom error message"
|
"msg": "Bet is not filled yet!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"code": 6001,
|
||||||
|
"name": "betAlreadyFilled",
|
||||||
|
"msg": "Bet is already filled"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"code": 6002,
|
||||||
|
"name": "invalidWinner",
|
||||||
|
"msg": "The winner must be either the owner or the joiner of the bet."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"code": 6003,
|
||||||
|
"name": "invalidFeeCollector",
|
||||||
|
"msg": "Please use the correct fee collector address"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"code": 6004,
|
||||||
|
"name": "joinerAccountNotProvided",
|
||||||
|
"msg": ""
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"types": [
|
"types": [
|
||||||
|
|
@ -289,6 +445,20 @@ export type Bets = {
|
||||||
{
|
{
|
||||||
"name": "wager",
|
"name": "wager",
|
||||||
"type": "u64"
|
"type": "u64"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "isNativeSol",
|
||||||
|
"type": "bool"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "tokenMint",
|
||||||
|
"type": {
|
||||||
|
"option": "pubkey"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "tokenDecimals",
|
||||||
|
"type": "u8"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
@ -312,7 +482,7 @@ export type Bets = {
|
||||||
{
|
{
|
||||||
"name": "feeCollector",
|
"name": "feeCollector",
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"value": "\"cocD4r4yNpHxPq7CzUebxEMyLki3X4d2Y3HcTX5ptUc\""
|
"value": "\"9esrj2X33pr5og6fdkDMjaW6fdnnb9hT1cWshamxTdL4\""
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "seed",
|
"name": "seed",
|
||||||
|
|
|
||||||
55
src/close.ts
55
src/close.ts
|
|
@ -5,11 +5,17 @@ import { clusterUrl, cocSk, getRandomInt, testerSk } from "./shared";
|
||||||
const IDL = require('./bets.json');
|
const IDL = require('./bets.json');
|
||||||
|
|
||||||
async function close(){
|
async function close(){
|
||||||
const keypair = Keypair.fromSecretKey(Uint8Array.from(testerSk));
|
const keypair = Keypair.fromSecretKey(Uint8Array.from(cocSk));
|
||||||
const cocKeypair = Keypair.fromSecretKey(Uint8Array.from(cocSk));
|
const cocKeypair = Keypair.fromSecretKey(Uint8Array.from(cocSk));
|
||||||
|
|
||||||
const connection = new Connection(clusterUrl);
|
const connection = new Connection(clusterUrl, {
|
||||||
const provider = new AnchorProvider(connection, new Wallet(keypair));
|
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);
|
const program:Program<Bets> = new Program<Bets>(IDL, provider);
|
||||||
|
|
||||||
let solBalance = (await connection.getBalance(keypair.publicKey))/ LAMPORTS_PER_SOL;
|
let solBalance = (await connection.getBalance(keypair.publicKey))/ LAMPORTS_PER_SOL;
|
||||||
|
|
@ -17,27 +23,50 @@ async function close(){
|
||||||
|
|
||||||
const [bet_list_pda] = await PublicKey.findProgramAddress([Buffer.from("bets_list")], program.programId);
|
const [bet_list_pda] = await PublicKey.findProgramAddress([Buffer.from("bets_list")], program.programId);
|
||||||
console.log(`Bets list PDA : ${bet_list_pda}`);
|
console.log(`Bets list PDA : ${bet_list_pda}`);
|
||||||
|
|
||||||
|
try {
|
||||||
const bet_list = await program.account.betsList.fetch(bet_list_pda);
|
const bet_list = await program.account.betsList.fetch(bet_list_pda);
|
||||||
for (const bet of bet_list.bets) {
|
for (const bet of bet_list.bets) {
|
||||||
|
try {
|
||||||
const betAcc = await program.account.betVault.fetch(bet);
|
const betAcc = await program.account.betVault.fetch(bet);
|
||||||
const winner = betAcc.owner;
|
const winner = betAcc.owner;
|
||||||
console.log(`Closing ${bet}`);
|
console.log(`Closing ${bet}`);
|
||||||
const tx = await program.methods.closeBet(winner).accounts({
|
|
||||||
|
const tx = await program.methods.closeBet(winner, betAcc.ownerId).accounts({
|
||||||
betVault: bet,
|
betVault: bet,
|
||||||
betsList: bet_list_pda,
|
betsList: bet_list_pda,
|
||||||
winner: winner,
|
winner: winner,
|
||||||
feeWallet: cocKeypair.publicKey
|
feeWallet: new PublicKey("9esrj2X33pr5og6fdkDMjaW6fdnnb9hT1cWshamxTdL4"),
|
||||||
}).rpc();
|
ownerReferrer: new PublicKey("BaTgUMPsk8fZoEMwig81Pw1KHmPpoYt31oiR5Qr2c8XR"),
|
||||||
|
joinerReferrer: new PublicKey("9esrj2X33pr5og6fdkDMjaW6fdnnb9hT1cWshamxTdL4"),
|
||||||
|
}).rpc({
|
||||||
|
skipPreflight: false,
|
||||||
|
maxRetries: 3
|
||||||
|
});
|
||||||
console.log(`tx: ${tx}`);
|
console.log(`tx: ${tx}`);
|
||||||
|
|
||||||
const hash = await connection.confirmTransaction(tx);
|
const confirmation = await connection.confirmTransaction(tx);
|
||||||
console.log(`tx complete: ${hash}`);
|
|
||||||
|
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;
|
solBalance = (await connection.getBalance(keypair.publicKey))/ LAMPORTS_PER_SOL;
|
||||||
|
|
||||||
console.log(`Tester ${keypair.publicKey} has ${solBalance} 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);
|
||||||
close();
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
import { Connection, PublicKey, Keypair, clusterApiUrl, LAMPORTS_PER_SOL } from "@solana/web3.js";
|
import { Connection, PublicKey, Keypair, clusterApiUrl, LAMPORTS_PER_SOL, SystemProgram, Transaction } from "@solana/web3.js";
|
||||||
import { Bets } from "./bets";
|
import { Bets } from "./bets";
|
||||||
import { AnchorProvider, BN, Program, Wallet } from "@coral-xyz/anchor";
|
import { AnchorProvider, BN, Program, Wallet } from "@coral-xyz/anchor";
|
||||||
import { clusterUrl, cocSk, getRandomInt, testerSk } from "./shared";
|
import { clusterUrl, cocSk, getRandomInt, testerSk } from "./shared";
|
||||||
|
import { createAssociatedTokenAccountInstruction, getAccount, getAssociatedTokenAddress, getAssociatedTokenAddressSync, TOKEN_PROGRAM_ID } from "@solana/spl-token";
|
||||||
const IDL = require('./bets.json');
|
const IDL = require('./bets.json');
|
||||||
|
|
||||||
async function create() {
|
async function create() {
|
||||||
|
|
@ -10,6 +11,7 @@ async function create(){
|
||||||
const provider = new AnchorProvider(connection, new Wallet(keypair));
|
const provider = new AnchorProvider(connection, new Wallet(keypair));
|
||||||
const program: Program<Bets> = new Program<Bets>(IDL, provider);
|
const program: Program<Bets> = new Program<Bets>(IDL, provider);
|
||||||
|
|
||||||
|
// Check and request SOL balance if needed
|
||||||
let solBalance = (await connection.getBalance(keypair.publicKey)) / LAMPORTS_PER_SOL;
|
let solBalance = (await connection.getBalance(keypair.publicKey)) / LAMPORTS_PER_SOL;
|
||||||
console.log(`Tester ${keypair.publicKey} has ${solBalance} SOL`);
|
console.log(`Tester ${keypair.publicKey} has ${solBalance} SOL`);
|
||||||
if (solBalance <= 1) {
|
if (solBalance <= 1) {
|
||||||
|
|
@ -23,15 +25,83 @@ async function create(){
|
||||||
console.log(`Tester now has ${solBalance} SOL`);
|
console.log(`Tester now has ${solBalance} SOL`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const [bet_list_pda] = await PublicKey.findProgramAddress([Buffer.from("bets_list")], program.programId);
|
// Get bets list PDA
|
||||||
|
const [bet_list_pda] = await PublicKey.findProgramAddress(
|
||||||
|
[Buffer.from("bets_list")],
|
||||||
|
program.programId
|
||||||
|
);
|
||||||
console.log(`Bets list PDA : ${bet_list_pda}`);
|
console.log(`Bets list PDA : ${bet_list_pda}`);
|
||||||
|
|
||||||
const nonce = getRandomInt(100000000000);
|
const nonce = getRandomInt(100000000000);
|
||||||
const tx = await program.methods.createBet(new BN(100000000),"tester", "tetris", new BN(nonce)).accounts({
|
|
||||||
|
// Find bet vault PDA
|
||||||
|
const [bet_vault_pda] = await PublicKey.findProgramAddress(
|
||||||
|
[
|
||||||
|
Buffer.from("bet_vault"),
|
||||||
|
keypair.publicKey.toBuffer(),
|
||||||
|
Buffer.from("tetris"),
|
||||||
|
new BN(nonce).toArrayLike(Buffer, "le", 8)
|
||||||
|
],
|
||||||
|
program.programId
|
||||||
|
);
|
||||||
|
|
||||||
|
// For native SOL bet
|
||||||
|
const isNativeSol = false;
|
||||||
|
const tokenMint = new PublicKey("8iFREvVdmLKxVeibpC5VLRr1S6X5dm7gYR3VCU1wpump");
|
||||||
|
|
||||||
|
// Create the bet vault token account if it doesn't exist
|
||||||
|
let betVaultTokenAccount;
|
||||||
|
if (!isNativeSol) {
|
||||||
|
// For SPL tokens, create the token account
|
||||||
|
betVaultTokenAccount = await getAssociatedTokenAddress(
|
||||||
|
tokenMint,
|
||||||
|
bet_vault_pda,
|
||||||
|
true // allowOwnerOffCurve = true because it's a PDA
|
||||||
|
);
|
||||||
|
|
||||||
|
// Create the token account if it doesn't exist
|
||||||
|
try {
|
||||||
|
await getAccount(connection, betVaultTokenAccount);
|
||||||
|
} catch {
|
||||||
|
const createAtaIx = createAssociatedTokenAccountInstruction(
|
||||||
|
keypair.publicKey, // payer
|
||||||
|
betVaultTokenAccount, // ata
|
||||||
|
bet_vault_pda, // owner
|
||||||
|
tokenMint // mint
|
||||||
|
);
|
||||||
|
|
||||||
|
const tx = await connection.sendTransaction(
|
||||||
|
new Transaction().add(createAtaIx),
|
||||||
|
[keypair]
|
||||||
|
);
|
||||||
|
await connection.confirmTransaction(tx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get payer's token account
|
||||||
|
const payerTokenAccount = await getAssociatedTokenAddress(
|
||||||
|
tokenMint,
|
||||||
|
keypair.publicKey
|
||||||
|
);
|
||||||
|
|
||||||
|
// Create the bet
|
||||||
|
const tx = await program.methods.createBet(
|
||||||
|
new BN(100000000),
|
||||||
|
"tester",
|
||||||
|
"tetris",
|
||||||
|
new BN(nonce),
|
||||||
|
isNativeSol,
|
||||||
|
tokenMint,
|
||||||
|
9 // decimals for SOL
|
||||||
|
).accounts({
|
||||||
betsList: bet_list_pda,
|
betsList: bet_list_pda,
|
||||||
|
payer: keypair.publicKey,
|
||||||
|
payerTokenAccount: isNativeSol ? null : payerTokenAccount,
|
||||||
|
betVaultTokenAccount: isNativeSol ? null : betVaultTokenAccount,
|
||||||
}).rpc();
|
}).rpc();
|
||||||
|
|
||||||
console.log(`create tx : ${tx}`);
|
console.log(`create tx : ${tx}`);
|
||||||
await connection.confirmTransaction(tx);
|
await connection.confirmTransaction(tx);
|
||||||
|
|
||||||
console.log(`tx complete`);
|
console.log(`tx complete`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,10 @@ async function get(){
|
||||||
owner: betAcc.owner + " : " + betAcc.ownerId,
|
owner: betAcc.owner + " : " + betAcc.ownerId,
|
||||||
joiner: betAcc.joiner + " : " + betAcc.joinerId,
|
joiner: betAcc.joiner + " : " + betAcc.joinerId,
|
||||||
wager: betAcc.wager.toNumber() + `: ${betAcc.wager.toNumber() / LAMPORTS_PER_SOL}SOL`,
|
wager: betAcc.wager.toNumber() + `: ${betAcc.wager.toNumber() / LAMPORTS_PER_SOL}SOL`,
|
||||||
balance: solBalance + " SOL"
|
balance: solBalance,
|
||||||
|
is_native_sol: betAcc.isNativeSol,
|
||||||
|
token_mint: betAcc.tokenMint,
|
||||||
|
token_decimals: betAcc.tokenDecimals,
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user