Skip to content

Commit

Permalink
move lamports normalization to the front end, fix round vault bug for…
Browse files Browse the repository at this point in the history
… placeBet from player with 0 bets in Round
  • Loading branch information
bgoober committed Jul 7, 2024
1 parent 4edb69c commit 9d737b8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
19 changes: 15 additions & 4 deletions app/web/components/dashboard/dashboard-feature.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ export default function DashboardFeature() {
const handleBetOver = async () => {
console.log(`Betting OVER with ${solAmountOver} SOL`);
if (!program || !wallet) return;
const amount = new BN(solAmountOver);
// Assuming solAmountUnder is a string representing the SOL amount,
// convert it to a BigNumber representing lamports.
// 1 SOL = 1,000,000,000 lamports
const lamportsPerSol = new BN(1_000_000_000);
const amountInSol = new BN(solAmountOver); // This might need parsing if solAmountUnder is not already a BN compatible format
const amountInLamports = amountInSol.mul(lamportsPerSol);
const betnumber = 1;

const [global] = web3.PublicKey.findProgramAddressSync(
Expand Down Expand Up @@ -85,7 +90,7 @@ export default function DashboardFeature() {
);
console.log(`round: `, round.toBase58());
const tx = await program.methods
.placeBet(amount, betnumber, round_number)
.placeBet(amountInLamports, betnumber, round_number)
.accounts({
player: wallet?.publicKey,
house: house.publicKey,
Expand All @@ -106,7 +111,13 @@ export default function DashboardFeature() {
const handleBetUnder = async () => {
console.log(`Betting UNDER with ${solAmountUnder} SOL`);
if (!program || !wallet) return;
const amount = new BN(solAmountUnder);

// Assuming solAmountUnder is a string representing the SOL amount,
// convert it to a BigNumber representing lamports.
// 1 SOL = 1,000,000,000 lamports
const lamportsPerSol = new BN(1_000_000_000);
const amountInSol = new BN(solAmountUnder); // This might need parsing if solAmountUnder is not already a BN compatible format
const amountInLamports = amountInSol.mul(lamportsPerSol);

// console.log("Program:", program);
// console.log("Methods available:", program?.methods);
Expand Down Expand Up @@ -144,7 +155,7 @@ export default function DashboardFeature() {
);
console.log(`round: `, round.toBase58());
const tx = await program.methods
.placeBet(amount, betnumber, round_number)
.placeBet(amountInLamports, betnumber, round_number)
.accounts({
player: wallet?.publicKey,
house: house.publicKey,
Expand Down
4 changes: 3 additions & 1 deletion programs/over_under/src/contexts/init_round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ pub struct RoundC<'info> {
pub round: Box<Account<'info, Round>>,

// vault pda of the round account
#[account(seeds = [b"vault", round.key().as_ref()], bump)]
/// DOCS: mut must be placed with the vault during initRound or a non-House player will not be able to call placeBet without the House key playing the first bet of each Round (this throws an unkown action undefined error for the player).
/// With the mut, any player can call placeBet in a Round with 0 bets.
#[account(mut, seeds = [b"vault", round.key().as_ref()], bump)]
pub vault: SystemAccount<'info>,

pub system_program: Program<'info, System>,
Expand Down
2 changes: 1 addition & 1 deletion programs/over_under/src/contexts/place_bet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ impl<'info> BetC<'info> {
let ctx = CpiContext::new(self.system_program.to_account_info(), accounts);

// We multiply the amount x 1 million here to equalize the bet into 1 whole SOL
transfer(ctx, amount*1_000_000_000)
transfer(ctx, amount)
}
}
8 changes: 7 additions & 1 deletion tests/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,14 @@ describe("over_under", () => {
const amountBN = new BN(10);
const roundNumberBN = new BN(round_number);

// Assuming solAmountUnder is a string representing the SOL amount,
// convert it to a BigNumber representing lamports.
// 1 SOL = 1,000,000,000 lamports
const lamportsPerSol = new BN(1_000_000_000);
const amountInLamports = amountBN.mul(lamportsPerSol);

const tx = await program.methods
.placeBet(amountBN, 1, roundNumberBN) // Use BN objects for the first and third arguments
.placeBet(amountInLamports, 1, roundNumberBN) // Use BN objects for the first and third arguments
.accounts({
house: keypair.publicKey,
global,
Expand Down

0 comments on commit 9d737b8

Please sign in to comment.