@duelo/core
v0.1.26
Published
sdk for Duelo Tournaments
Downloads
4
Readme
Duelo program library
Install the Typescript package
yarn add @duelo/core
Setup the package in your app
Setup Duelo to work with the connected wallet:
import { useAnchorWallet, useConnection } from "@solana/wallet-adapter-react";
import { DueloCore } from "@duelo/core";
import { DueloCore, DUELO_PROGRAM_ID, IDL } from "@duelo/core";
const anchorWallet = useAnchorWallet() as Wallet;
const { connection } = useConnection();
const duelo = new DueloCore(connection, anchorWallet, IDL, DUELO_PROGRAM_ID);
(async () => await duelo.fetchTournamentAcc(LUCHA_MANIA_TOURNAMENT_ID))();
Methods available on duelo/core
Fetch tournament account:
async fetchTournamentAcc(tournamentKey: PublicKey): Promise<TournamentAccount>
Create tournament account:
/!\ this will be handled by a script in mainnet
// This can be any NFT
const collectionNft = await createNft(provider, provider.publicKey);
const startTimestamp = Number((new Date().getTime() / 1000).toFixed(0));
// Change this to start the tournament whenever you need it to start
const inSevenDays = 86400 * 7;
// Tournament starts in 7 days
const { tournamentAccountKey } = await duelo.createTournament(
collectionNft.mintKey,
new BN(startTimestamp + inSevenDays)
);
await duelo.fetchTournamentAcc(tournamentAccountKey);
Mint a new Duelo NFT:
async mintDueloNft(
tournamentAccountId: PublicKey,
collectionMint: PublicKey
): Promise<{ mintId: PublicKey }>
Fetch a Luchador skills:
async fetchSkillsAcc(
tournamentKey: PublicKey,
mintKey: PublicKey
): Promise<NftSkillsAccount>
Example response:
{
"health": 100,
"defense": 100,
"speed": 100,
"attack": 100,
"availablePoints": 200,
"nftMint": "<nft_mint_address>",
"owner": "<owner_wallet_address>",
"bump": 254
}
Update a Luchador skills:
type UpdateAttributesArgs = {
attack: number;
health: number;
speed: number;
defense: numbe;
}
async updateDueloNftSkills(
tournamentAccountId: PublicKey,
nftMint: PublicKey,
updateSkillzArgs: UpdateAttributesArgs
): Promise<{ mintId: PublicKey }>
Create a bracket:
/!\ this will be handled by a script in mainnet
const { bracketKey, instructions, signers, gamesKey } = await duelo.createBracket(
tournamentAccountKey,
// Data from fetchTournament, this is the index of the bracket to be created (make sure to have fresh data)
tournamentAccountData.nextBracket,
// Max number of games based on number of NFTs minted
maxGames,
// Similar to the timestamp used to create the tournament
endTimestamp
);
await provider.sendAndConfirm(
new Transaction().add(...instructions.map(ix => (ix))),
signers,
{ skipPreflight: true }
);
Add two matches to a bracket:
/!\ this will be handled by a script in mainnet
await duelo.createMatch(tournamentAccountKey, bracketKey, gamesKey, [dueloNft1, dueloNft2]);
await duelo.createMatch(tournamentAccountKey, bracketKey, gamesKey, [dueloNft3, dueloNft4]);
Fetch a bracket matches:
async fetchCurrentBracketGames(
tournamentKey: PublicKey
): Promise<GamesAccount>
This method will return a list of all the games of the current bracket.
If you want to get the games of a previous bracket, set the selectBracket
param to this bracket (between 0 and tournament.currentBracket
).
The data format is as follows:
{
total: 2,
max: 2,
games: [
{
game: PublicKey,
nftLeft: PublicKey,
nftRight: PublicKey,
},
{
game: PublicKey,
nftLeft: PublicKey,
nftRight: PublicKey,
}
]
}
You then need to fetch the games accounts data:
async fetchGameAcc(
gameKey: PublicKey
): Promise<GameAccount>
Fetch my current matches:
async fetchMyCurrentBracketGames(
tournamentKey: PublicKey,
nfts: PublicKey[],
selectBracket?: number
): Promise<GamesAccount>
This method will return a list of all the games from the current bracket that contain the nfts
passed as parameters.
If you want to get the games of a previous bracket, set the selectBracket
param to this bracket (between 0 and tournament.currentBracket
).
The format is the same as the previous method's.
Full example
import { Wallet } from "@coral-xyz/anchor";
import { PublicKey } from "@solana/web3.js";
import { useAnchorWallet, useWallet, useConnection } from "@solana/wallet-adapter-react";
import { DUELO_PROGRAM_ID, DueloCore, IDL } from "@duelo/core";
import { WalletDisconnectButton, WalletMultiButton } from "@solana/wallet-adapter-react-ui";
const MintDueloNFT = () => {
const anchorWallet = useAnchorWallet() as Wallet;
const { connection } = useConnection();
const duelo = new DueloCore(connection, anchorWallet, IDL, DUELO_PROGRAM_ID);
async function click() {
const tournamentKey = new PublicKey("<LUCHA_MANIA_TOURNAMENT_ID>");
const tournament = await duelo.fetchTournamentAcc(tournamentKey);
const { mintId } = await duelo.mintDueloNft(tournamentKey, tournament.collectionMint);
const originalSkills = await duelo.fetchAttributesAcc(tournamentKey, mintId);
console.log('skills: ', originalSkills);
// Response
// ----
// {
// "health": 100,
// "defense": 100,
// "speed": 100,
// "attack": 100,
// "availablePoints": 200,
// "nftMint": "11111111111111111111111111111111",
// "owner": "11111111111111111111111111111111",
// "bump": 254
// }
await duelo.updateDueloNftSkills(tournamentKey, mintId, {
"health": 120,
"defense": 150,
"speed": 110,
"attack": 120,
});
const skillsAfterUpdate = await duelo.fetchAttributesAcc(tournamentKey, mintId);
console.log('skills after update: ', skillsAfterUpdate);
// Response
// ----
// {
// "health": 120,
// "defense": 150,
// "speed": 110,
// "attack": 120,
// "availablePoints": 100,
// "nftMint": "11111111111111111111111111111111",
// "owner": "11111111111111111111111111111111",
// "bump": 254
// }
}
return <>
<div>
{!anchorWallet && <WalletMultiButton />}
{anchorWallet &&
<>
<WalletDisconnectButton />
<button onClick={click}>
CLICK ME
</button>
</>}
</div>
</>
}
export default MintDueloNFT;
Fetch all assets
You can also fetch all the assets from the tournament with this method on the duelo class:
async fetchMyTournamentAssets(
collectionMint: PublicKey,
owner?: PublicKey,
page?: number,
limit?: number
): Promise<any>
The result, containing all the NFTs from this collection, will look like this:
{
"jsonrpc": "2.0",
"result": {
"total": 12,
"limit": 1000,
"page": 1,
"items": [
{}, {},
{}, {},
{}, {},
{}, {},
{}, {},
{}, {}
]
},
"id": "duelo-assets-search"
}
You can also fetch assets belonging to just one wallet by adding the owner
param to the function call, as described in the function signature.
Run tests (local dev, for Rust programmers)
Start your local validator (from .anchor
dir)
solana-test-validator -r \
--mint 4YonHNa8dXeDSFpfpoLoGU3cB4YCtk8Q5hHDxL3Y3GFx \
--bpf-program metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s ../tests/mainnet-programs/metadata.so \
--bpf-program auth9SigNpDKz4sJJ1DfCTuZrZNSAgh9sFD3rboVmgg ../tests/mainnet-programs/mpl-auth.so
Run the Duelo tests (from root dir)
anchor test --provider.cluster localnet --skip-local-validator
Check your transactions on explorer.solana.com (turn on custom endpoint to localnet address).