0.1.17 • Published 1 month ago

@ls-arkade/payments v0.1.17

Weekly downloads
-
License
-
Repository
-
Last release
1 month ago

Liberty Square Arkade: Payments

Install the Typescript package

yarn add @ls-arkade/payments

Setup the package in your app

Setup the program:

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.

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.

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
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
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.

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.

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 });
0.1.15

1 month ago

0.1.16

1 month ago

0.1.17

1 month ago

0.1.13

3 months ago

0.1.14

3 months ago

0.1.12

4 months ago

0.1.10

5 months ago

0.1.11

5 months ago

0.1.9

5 months ago

0.1.8

5 months ago

0.1.7

5 months ago

0.1.6

5 months ago

0.1.5

5 months ago

0.1.4

5 months ago

0.1.3

5 months ago

0.1.2

5 months ago

0.1.1

5 months ago