3

It would be extremely useful if I could create a mint account from a JSON file to test my anchor program.

I have this in my Anchor.toml to add the account when I run anchor test

[[test.validator.account]]
address = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
filename = "tests/seeds/EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v.json"

In the json file I would like to have a spl-token mint but I don't know the fields I should add, I tried this:

{
    "pubkey": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
    "account": {
        "lamports": 146540070945,
        "data": [
            "AQAAABzjWe1aAS4E+hQrnHUaHF6Hz9CgFhuchf/TG3jN/Nj2j72OKE7jEQAGAQEAAAAqnl7btTwEZ5CY/3sSZRcUQ0/AjFYqmjuGEQXmctQicw==",
            "base64"
        ],
        "owner": "HzYyRXzKNdTc5rnY9kasVAxvs2j2zDLQRbEk1VnX6KAk",
        "executable": false,
        "rentEpoch": 335
    }
}

Is there an example of a mint account that is initialized from a JSON file?

2 Answers 2

2

If you are intending to test much of SPL related logic within your smart contract, you can set it up for each test manually. This is (roughly) the script I used at the start of my test file. It is quite old and may be out of date as much of the syntax changes almost weekly.

import {
    createMint,
    createAccount,
    getAccount,
    getOrCreateAssociatedTokenAccount,
    transfer,
    mintTo,
    TOKEN_PROGRAM_ID,
    ASSOCIATED_TOKEN_PROGRAM_ID,
} from "@solana/spl-token";

describe("Tests", async () => {
    // Configure the client to use the local cluster.
    anchor.setProvider(anchor.AnchorProvider.env());
    const provider = anchor.AnchorProvider.local();
    const LAMPORTS_PER_SOL = 1000000000;            
    const person1 = anchor.web3.Keypair.generate();                
    const PaYeR = anchor.web3.Keypair.generate();
    const mintAuthSC = anchor.web3.Keypair.generate();    
    const mintKeypairSC = anchor.web3.Keypair.generate();            
    let mintSC: PublicKey;           
    let person1ATA;               
    
    before(async () => {
        // SOL Top-ups for all accounts used
        {        
            await provider.connection.confirmTransaction(
                await provider.connection.requestAirdrop(
                    PaYeR.publicKey,
                    2 * LAMPORTS_PER_SOL
                )
            );
            await provider.connection.confirmTransaction(
                await provider.connection.requestAirdrop(
                    mintAuthSC.publicKey,
                    2 * LAMPORTS_PER_SOL
                )
            );
            await provider.connection.confirmTransaction(
                await provider.connection.requestAirdrop(
                    person1.publicKey,
                    2 * LAMPORTS_PER_SOL
                )
            );  
        }
    
        // Stablecoin mint
        mintSC = await createMint(
            provider.connection,
            PaYeR,
            mintAuthSC.publicKey,
            mintAuthSC.publicKey,
            10,
            mintKeypairSC,
            undefined,
            TOKEN_PROGRAM_ID
        ); 
        
        // Initialise ATA
        person1ATA = await getOrCreateAssociatedTokenAccount(
            provider.connection,
            PaYeR,
            mintSC,
            person1.publicKey
        ); 
        
        // Top up test account with SPL
        await mintTo(
            provider.connection,
            PaYeR,
            mintSC,
            person1ATA.address,
            mintAuthSC,
            100,
            [],
            undefined,
            TOKEN_PROGRAM_ID
        );
    });
        

    it("TEST 1", async () => {
        <YOUR UNIT TEST THAT INTERACTS WITH SPL-PROGRAM>
    });

});
1

Check out this documentation. This might help.

https://betterprogramming.pub/how-to-mint-nfts-on-solana-using-rust-and-metaplex-f66bac717cb8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.