|
| 1 | +import { describe, it } from 'node:test'; |
| 2 | +import * as anchor from '@coral-xyz/anchor'; |
| 3 | +import { PublicKey } from '@solana/web3.js'; |
| 4 | +import { BankrunProvider } from 'anchor-bankrun'; |
| 5 | +import { startAnchor } from 'solana-bankrun'; |
| 6 | +import type { AccountData } from '../target/types/account_data'; |
| 7 | + |
| 8 | +const IDL = require('../target/idl/account_data.json'); |
| 9 | +const PROGRAM_ID = new PublicKey(IDL.address); |
| 10 | + |
| 11 | +describe('Account Data!', async () => { |
| 12 | + const context = await startAnchor('', [{ name: 'account_data', programId: PROGRAM_ID }], []); |
| 13 | + const provider = new BankrunProvider(context); |
| 14 | + |
| 15 | + const payer = provider.wallet as anchor.Wallet; |
| 16 | + const program = new anchor.Program<AccountData>(IDL, provider); |
| 17 | + |
| 18 | + // Generate a new keypair for the addressInfo account |
| 19 | + const [addressInfoAccount] = PublicKey.findProgramAddressSync([Buffer.from('address_info'), payer.publicKey.toBuffer()], program.programId); |
| 20 | + |
| 21 | + it('Create the address info account', async () => { |
| 22 | + console.log(`Payer Address : ${payer.publicKey}`); |
| 23 | + console.log(`Address Info Acct : ${addressInfoAccount}`); |
| 24 | + |
| 25 | + // Instruction Ix data |
| 26 | + const addressInfo = { |
| 27 | + name: 'Joe C', |
| 28 | + houseNumber: 136, |
| 29 | + street: 'Mile High Dr.', |
| 30 | + city: 'Solana Beach', |
| 31 | + }; |
| 32 | + |
| 33 | + await program.methods |
| 34 | + .createAddressInfo(addressInfo.name, addressInfo.houseNumber, addressInfo.street, addressInfo.city) |
| 35 | + .accounts({ |
| 36 | + addressInfo: addressInfoAccount, |
| 37 | + payer: payer.publicKey, |
| 38 | + }) |
| 39 | + .rpc(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("Read the new account's data", async () => { |
| 43 | + const addressInfo = await program.account.addressInfo.fetch(addressInfoAccount); |
| 44 | + console.log(`Name : ${addressInfo.name}`); |
| 45 | + console.log(`House Num: ${addressInfo.houseNumber}`); |
| 46 | + console.log(`Street : ${addressInfo.street}`); |
| 47 | + console.log(`City : ${addressInfo.city}`); |
| 48 | + }); |
| 49 | +}); |
0 commit comments