Log the value of greeting_account.counter just before and just after the increment operation
Within the main program (main.ts
file), I added an additional reportGreetings()
call, before calling sayHello()
-> so that the structure is:
main.ts
...
// Find out how many times that account has been greeted BEFORE the incrementor
await reportGreetings(true);
// Say hello to an account
await sayHello();
// Find out how many times that account has been greeted AFTER the incrementor
await reportGreetings(false);
...
But it’s useful to differentiate whether the reportGreetings()
call is coming before the incrementor or after the incrementor, in the logs.
To do this, the reportGreetings()
function should be changed.
The change can be as simple as adding a single boolean argument to the reportGreetings()
function to follow the path where if it is before the incrementor, it says so in the logs, and if not then it says after incrementing.
The additional change in the function body is at the final console.log
part where we can simple tack on to the end ${beforeIncrement ? "Before the incrementor" : "After the incrementor"}
.
So the revised reportGreetings()
function looks like this:
hello_world.ts
export async function reportGreetings(beforeIncrement: boolean): Promise<void> {
const accountInfo = await connection.getAccountInfo(greetedPubkey);
if (accountInfo === null) {
throw 'Error: cannot find the greeted account';
}
const greeting = borsh.deserialize(
GreetingSchema,
GreetingAccount,
accountInfo.data,
);
console.log(
greetedPubkey.toBase58(),
'has been greeted',
greeting.counter,
'time(s)',
`${beforeIncrement ? "Before the incrementor" : "After the incrementor"}`
);
}
And in the main program looks like this:
main.ts
async function main() {
console.log("Let's say hello to a Solana account...");
// Establish connection to the cluster
await establishConnection();
// Determine who pays for the fees
await establishPayer();
// Check if the program has been deployed
await checkProgram();
// THIS WAS ADDED
// Find out how many times that account has been greeted BEFORE the incrementor
await reportGreetings(true);
// Say hello to an account
await sayHello();
// THIS WAS CHANGED
// Find out how many times that account has been greeted AFTER the incrementor
await reportGreetings(false);
console.log('Success');
}
EDIT
The question specification was not to make different logs on the client, but rather to add system logging, so instead you should need to go to the smart contract located at /src/program-rust/src/lib.rs
and in the process_instruction()
public function, 4 lines before the end of the function, you should find greeting_account.counter += 1;
.
At this point, you should add a msg!("Greeted {} time(s)!", greeting_account.counter);
before and after it to have a counter log before and after the increment.
Change the increment operation to: (If the counter is 0, add 1 to it…otherwise, multiply the counter by 2 )
This one is a little harder…
Within the file src/program-rust/src/lib.rs
, the entrypoint!(process_instruction);
calls the public function process_instruction()
which does a whole bunch, including processing the counter increment and storing it.
...
// Increment and store the number of times the account has been greeted
let mut greeting_account = GreetingAccount::try_from_slice(&account.data.borrow())?;
greeting_account.counter += 1;
greeting_account.serialize(&mut &mut account.data.borrow_mut()[..])?;
...
This can be modified to do a check before the counter increment, such that:
...
// Increment and store the number of times the account has been greeted
let mut greeting_account = GreetingAccount::try_from_slice(&account.data.borrow())?;
if greeting_account.counter == 0 {
greeting_account.counter += 1;
} else {
greeting_account.counter *= 2;
}
greeting_account.serialize(&mut &mut account.data.borrow_mut()[..])?;
...
Doing the check to see if greeting_account.counter != 0
will then double the count and store the number each time the program is run.