How can I push json on the blockchain and get it using anchor project. I am trying to push simple json like this:
[
{
"text": "What is React?",
"code": "001",
"Options": ["Framework", "Frontend Language", "User Interface", "Backend"]
}
]
I am trying with calculator project example like this (lib.rs):
use std::vec;
use anchor_lang:: prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod myfirstproject {
use super::*;
pub fn create(ctx: Context < Create >, init_message: String) -> Result<()> {
let calculator = &mut ctx.accounts.calculator;
calculator.greeting = init_message;
Ok(())
}
pub fn add(ctx: Context<Addition>, num1: i64, num2: i64) -> Result<()> {
let calculator = &mut ctx.accounts.calculator;
calculator.result = num1 + num2;
Ok(())
}
pub fn pushjson(ctx:Context<PushingData>, myjson:Vec::new) -> Result<()>{
let calculator = &mut ctx.accounts.calculator;
calculator.data_array = myjson;
Ok(())
}
}
#[derive(Accounts)]
pub struct Create < 'info> {
#[account(init, payer = user, space = 264)]
pub calculator: Account < 'info, Calculator>,
#[account(mut)]
pub user: Signer < 'info>,
pub system_program: Program < 'info, System>
}
#[derive(Accounts)]
pub struct Addition<'info> {
#[account(mut)]
pub calculator: Account<'info, Calculator>
}
#[derive(Accounts)]
pub struct PushingData<'info> {
#[account(mut)]
pub calculator: Account<'info, Calculator>
}
#[account]
pub struct Calculator {
pub greeting: String,
pub result: i64,
pub remainder: i64,
pub data_array: vec![]
}
(test file myfirstproject.ts)
const assert = require('assert')
const anchor1 = require('@project-serum/anchor')
const { SystemProgram } = anchor1.web3
describe('myfirstproject', () => {
const provider = anchor1.AnchorProvider.env();
anchor1.setProvider(provider)
const calculator = anchor1.web3.Keypair.generate()
const program = anchor1.workspace.Myfirstproject
it('Creates a calculator', async () => {
await program.rpc.create("Welcome to Solana calculator", {
accounts: {
calculator: calculator.publicKey,
user: provider.wallet.publicKey,
systemProgram: SystemProgram.programId
},
signers: [calculator]
})
const account = await program.account.calculator.fetch(calculator.publicKey)
assert.ok(account.greeting === "Welcome to Solana calculator")
})
it('Add two numbers', async () => {
await program.rpc.add(new anchor1.BN(2), new anchor1.BN(3), {
accounts: {
calculator: calculator.publicKey
}
})
const account = await program.account.calculator.fetch(calculator.publicKey)
assert.ok(account.result.eq(new anchor1.BN(5)))
})
it('Push Json Data', async () => {
let myArray = [
{
"text": "What is React?",
"code": "001",
"Options": ["Framework", "Frontend Language", "User Interface", "Backend"]
}
]
await program.rpc.pushjson(myArray, {
accounts: {
calculator: calculator.publicKey
}
})
const account = await program.account.calculator.fetch(calculator.publicKey)
assert.ok(account.data_array.eq(myArray))
})
})
I am not able to make it work, how to push the json data on the blockchain or first of all how to test it locally with the anchor project.
If someone can help.
Thanks