Skip to content

Commit

Permalink
Bugfix: Throw error before requesting signature when creating account
Browse files Browse the repository at this point in the history
  • Loading branch information
bone-house committed Sep 16, 2023
1 parent 599eabb commit 677bfa8
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-carrots-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gamba-core": patch
---

Bugfix: Throw error before requesting signature when creating account
3 changes: 2 additions & 1 deletion packages/gamba-core/src/EventFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Connection, PublicKey } from '@solana/web3.js'
import { Event } from './Event'
import { PROGRAM_ID } from './constants'
import { ParsedGambaTransaction, fetchTransactionsWithEvents, parseTransactionEvents } from './utils'
import { ParsedGambaTransaction } from './types'
import { fetchTransactionsWithEvents, parseTransactionEvents } from './utils'

export interface EventFetcherParams {
/**
Expand Down
7 changes: 4 additions & 3 deletions packages/gamba-core/src/GambaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { GambaError, clientError } from './GambaError'
import { parseHouseAccount, parseUserAccount, parseWalletAccount } from './parsers'
import { State, createState } from './state'
import { Wallet } from './types'
// import NodeWallet from '@coral-xyz/anchor/dist/cjs/nodewallet'
import { GambaAnchorClient } from './methods'

async function makeAndSendTransaction(
Expand Down Expand Up @@ -54,7 +53,6 @@ export class GambaClient {
* A connection to a fullnode JSON RPC endpoint
* @param wallet
* The web3 wallet to use for signing and interracting with the contract.
* If none is set an inline burner wallet is created
*/
constructor(
connection: Connection,
Expand All @@ -65,7 +63,6 @@ export class GambaClient {
} else {
// Create a fake inline wallet if none is provided
const keypair = new Keypair
// this.wallet = new NodeWallet(keypair)
this.fakeWallet = true
this.wallet = {
payer: keypair,
Expand Down Expand Up @@ -105,8 +102,12 @@ export class GambaClient {

private async sendTransaction(
instruction: TransactionInstruction | Promise<TransactionInstruction>,
params?: {requiresAccount?: boolean},
) {
try {
if (params?.requiresAccount && !this.state.user.created) {
throw clientError('AccountNotInitialized')
}
if (this.fakeWallet) {
throw clientError('WalletNotConnected')
}
Expand Down
2 changes: 1 addition & 1 deletion packages/gamba-core/src/GambaError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const getGambaClientError = (error: unknown) => {
}

type ProgramErrorCode = Gamba['errors'][number]['name']
type GambaClientError = 'WalletNotConnected'
type GambaClientError = 'WalletNotConnected' | 'AccountNotInitialized'
type ErrorCode = 'GenericClientError' | 'InsufficentFunds' | 'AccountNotInitialized' | GambaClientError | ProgramErrorCode

export const clientError = (gambaClientError: GambaClientError) => ({ gambaClientError })
Expand Down
31 changes: 18 additions & 13 deletions packages/gamba-core/src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ export interface GambaPlayParams {
deductFees?: boolean
}

export type SendFunction = (instruction: TransactionInstruction | Promise<TransactionInstruction>, params?: {requiresAccount?: boolean}) => Promise<SentTransaction>

export class GambaAnchorClient {
private program: GambaProgram

wallet: Wallet

sender: (instruction: TransactionInstruction | Promise<TransactionInstruction>) => Promise<SentTransaction>
sender: SendFunction

get addresses() {
const wallet = this.wallet.publicKey
Expand All @@ -32,7 +34,7 @@ export class GambaAnchorClient {
constructor(
connection: Connection,
wallet: Wallet,
sender: (instruction: TransactionInstruction | Promise<TransactionInstruction>) => Promise<SentTransaction>,
sender: SendFunction,
) {
const anchorProvider = new AnchorProvider(
connection,
Expand Down Expand Up @@ -62,6 +64,7 @@ export class GambaAnchorClient {
creator: params.creator,
})
.instruction(),
{ requiresAccount: true },
)
}

Expand Down Expand Up @@ -126,16 +129,18 @@ export class GambaAnchorClient {
associatedTokenAccount: PublicKey,
amountToRedeem: number,
) => {
return this.program.methods
.redeemBonusToken(new BN(amountToRedeem))
.accounts({
mint,
tokenProgram: TOKEN_PROGRAM_ID,
from: associatedTokenAccount,
authority: this.addresses.wallet,
user: this.addresses.user,
house: this.addresses.house,
})
.instruction()
return this.sender(
this.program.methods
.redeemBonusToken(new BN(amountToRedeem))
.accounts({
mint,
tokenProgram: TOKEN_PROGRAM_ID,
from: associatedTokenAccount,
authority: this.addresses.wallet,
user: this.addresses.user,
house: this.addresses.house,
})
.instruction(),
)
}
}
10 changes: 8 additions & 2 deletions packages/gamba-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export interface ParsedUser {


export interface ParsedHouse {
// publicKey: PublicKey
// state: HouseState | null
created: boolean
rng: PublicKey | null
bonusMint: PublicKey | null
Expand All @@ -58,3 +56,11 @@ export interface GameResult {
payout: number
profit: number
}

export interface ParsedGambaTransaction {
signature: string
time: number
event: {
gameResult?: GameResult
}
}
22 changes: 7 additions & 15 deletions packages/gamba-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getAssociatedTokenAddressSync } from '@solana/spl-token'
import { AccountInfo, Connection, LAMPORTS_PER_SOL, ParsedTransactionWithMeta, PublicKey, SignaturesForAddressOptions } from '@solana/web3.js'
import { IDL, PROGRAM_ID } from './constants'
import { parseBetSettledEvent, parsePlayEvent } from './parsers'
import { GameEvent, GameResult, HouseState, UserState } from './types'
import { GameEvent, GameResult, HouseState, ParsedGambaTransaction, UserState } from './types'

const accountsCoder = new BorshAccountsCoder(IDL)
const eventParser = new EventParser(PROGRAM_ID, new BorshCoder(IDL))
Expand Down Expand Up @@ -31,6 +31,10 @@ export const hmac256 = async (secretKey: string, message: string, algorithm = 'S
return hashHex
}

export const getGameHash = (rngSeed: string, clientSeed: string, nonce: number) => {
return hmac256(rngSeed, [clientSeed, nonce].join('-'))
}

// ....
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const bnToNumber = (bn: any) => {
Expand Down Expand Up @@ -63,15 +67,11 @@ export const decodeAccount = <T>(accountName: string, account: AccountInfo<Buffe
}

export const decodeUser = (account: AccountInfo<Buffer> | null) => {
return decodeAccount('user', account)
return decodeAccount<UserState>('user', account)
}

export const decodeHouse = (account: AccountInfo<Buffer> | null) => {
return decodeAccount('house', account)
}

export const getGameHash = (rngSeed: string, clientSeed: string, nonce: number) => {
return hmac256(rngSeed, [clientSeed, nonce].join('-'))
return decodeAccount<HouseState>('house', account)
}

export const getTokenAccount = async (
Expand Down Expand Up @@ -107,14 +107,6 @@ export const parseTransactionEvents = (
return { gameResult, gameResultOld }
}

export interface ParsedGambaTransaction {
signature: string
time: number
event: {
gameResult?: GameResult
}
}

/**
* Tries to find Gamba data in a transaction
*/
Expand Down

0 comments on commit 677bfa8

Please sign in to comment.