npm package: @ls-arkade/payments
Description: SDK for Liberty Square's Arkade
Latest version: 0.1.17

# Liberty Square Arkade: Payments

## Install the Typescript package

```bash
yarn add @ls-arkade/payments
```

## Setup the package in your app

Setup the program:
```ts
import { program, transactions } from "@ls-arkade/payments";

const { connection } = useConnection();
const wallet = useWallet();

const lsPaymentsProgram = program(connection, wallet);
```

## Fetch config account
Each game has its own config onchain. See Notion's API doc for more info.

```ts
const configAccount = await lsPaymentsProgram.account.configAccount.fetch(
  new PublicKey(DEVNET_WHEEL_OF_FATE_CONFIG_ADDRESS)
);
```

## Fetch pools
You can fetch each pool and display its total vault SOL amount.

```ts
import { pda, DAILY_POOL, MONTHLY_POOL, WEEKLY_POOL } from "@ls-arkade/payments";


const [dailyPoolPubkey] = pda.findPoolVault(configAccountKey.publicKey, DAILY_POOL);
const [weeklyPoolPubkey] = pda.findPoolVault(configAccountKey.publicKey, WEEKLY_POOL);
const [monthlyPoolPubkey] = pda.findPoolVault(configAccountKey.publicKey, MONTHLY_POOL);
```
 
## Buy Paid Arkade SPL token with SOL:
The user can buy Arkade tokens from the UI.

Available options for one transaction:
- Individual token(s): user can buy `1`/`2`/`3`/`4`/`5` tokens (`0.045 SOL` per)
- Packs: user can buy one or two packs of `25` tokens in one tx (`0.88 SOL` per)

Each tx has a micro fee: 
- `0.0044 SOL` for each individual ticket tx
- `0.0088 SOL` for each pack tx

```ts
const sourceTokenAccount = getAssociatedTokenAddressSync(
  configAccount.paymentTokenMint,
  wallet.publicKey!
);

if (!sourceTokenAccount) {
  throw new Error("You don't have enough tokens to start a game.")
}

// Ticket amount can be 1/2/3/4/5 for single tickets or 25/50 for packs
const ticketAmount = 1; 

const buySplTx = await transactions.buySpl(
  new PublicKey(DEVNET_WHEEL_OF_FATE_CONFIG_ADDRESS),
  sourceTokenAccount,
  ticketAmount,
  // new PublicKey("FkgPYryuNgmJepPRDHoFhMo9bAZVSR7D2GQnCjd2AJQ8") // one of the valid NFT held by current player (or undefined)
);

await lsPaymentsProgram.provider.sendAndConfirm(buySplTx, [], { skipPreflight: true });
```

## Buy Free Arkade SPL token with Karbon:
The user can buy Arkade tokens from the UI.

Available options for one transaction:
- Individual token(s): user can buy `1`/`2`/`3`/`4`/`5` tokens (`0.045 SOL` per)
- Packs: user can buy one or two packs of `25` tokens in one tx (`0.88 SOL` per)

Each tx has a micro fee: 
- `0.0044 SOL` for each individual ticket tx
- `0.0088 SOL` for each pack tx

```ts
import { KARBON_TOKEN } from "@ls-arkade/payments";

const sourceTokenAccount = getAssociatedTokenAddressSync(
  KARBON_TOKEN,
  wallet.publicKey!
);

if (!sourceTokenAccount) {
  throw new Error("You don't have enough tokens to start a game.")
}

// Ticket amount can be 1/2/3/4/5 for single tickets or 25/50 for packs
const ticketAmount = 1; 

const buySplTx = await transactions.buySpl(
  new PublicKey(DEVNET_WHEEL_OF_FATE_CONFIG_ADDRESS),
  sourceTokenAccount,
  ticketAmount,
  provider.publicKey,
  undefined, // new PublicKey("FkgPYryuNgmJepPRDHoFhMo9bAZVSR7D2GQnCjd2AJQ8") // one of the valid NFT held by current player (or undefined)
  KARBON_TOKEN
);

await lsPaymentsProgram.provider.sendAndConfirm(buySplTx, [], { skipPreflight: true });
```

## Start a game:
This methods can be passed an NFT mint belonging to the tx signer. 

If the user owns:
- a squirrel: the cost is 1 token
- a "the hallowed": the cost is 2 tokens
- none of the above: the cost is 3 tokens

Final cost is calculated onchain.

```ts
const sourceTokenAccount = getAssociatedTokenAddressSync(
  configAccount.paymentTokenMint,
  wallet.publicKey!
);

const sourceTokenAccountInfo = await connection.getAccountInfo(sourceTokenAccount);

if (!sourceTokenAccountInfo) {
  const createATATx = new Transaction().add(createAssociatedTokenAccountInstruction(
    wallet.publicKey!,
    sourceTokenAccount,
    wallet.publicKey!,
    configAccount.paymentTokenMint
  ));
  await lsPaymentsProgram.provider.sendAndConfirm!(createATATx);
}

const handlePaymentTxn = await transactions.handlePayment(
  new PublicKey(DEVNET_WHEEL_OF_FATE_CONFIG_ADDRESS),
  sourceTokenAccount
  wallet.publicKey!,
  // new PublicKey("FkgPYryuNgmJepPRDHoFhMo9bAZVSR7D2GQnCjd2AJQ8") // one of the valid NFT held by current player (or undefined)
);

await lsPayments.provider.sendAndConfirm!(handlePaymentTxn, [], { skipPreflight: true });
```

## Start a free game:
This methods can be passed an NFT mint belonging to the tx signer. 

If the user owns:
- a squirrel: the cost is 1 token
- a "the hallowed": the cost is 2 tokens
- none of the above: the cost is 3 tokens

Final cost is calculated onchain.

```ts
import { ARKADE_FREE_PLAY } from "@ls-arkade/payments";

const sourceTokenAccount = getAssociatedTokenAddressSync(
  ARKADE_FREE_PLAY,
  wallet.publicKey!
);

const sourceTokenAccountInfo = await connection.getAccountInfo(sourceTokenAccount);

if (!sourceTokenAccountInfo) {
  const createATATx = new Transaction().add(createAssociatedTokenAccountInstruction(
    wallet.publicKey!,
    sourceTokenAccount,
    wallet.publicKey!,
    ARKADE_FREE_PLAY
  ));
  await lsPaymentsProgram.provider.sendAndConfirm!(createATATx);
}

const handlePaymentTxn = await transactions.handlePayment(
  new PublicKey(DEVNET_WHEEL_OF_FATE_CONFIG_ADDRESS),
  sourceTokenAccount
  wallet.publicKey!,
  undefined, // new PublicKey("FkgPYryuNgmJepPRDHoFhMo9bAZVSR7D2GQnCjd2AJQ8") // one of the valid NFT held by current player
  ARKADE_FREE_PLAY
);

await lsPayments.provider.sendAndConfirm!(handlePaymentTxn, [], { skipPreflight: true });
```