Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
previous | next
Summary
There is a lot in this PR so let's provide a summary first and then go into details. The
following changes were applied:
On the Rust Side
On the TypeScript Side
yarn api:gen
in order to get the SDK up to dateWorkflow
The following workflow is what you can use in order to add features like this.
cargo build-bpf
to update the.so
fileyarn api:gen
to update the SDKyarn amman:start
to restart amman with the updated.so
fileGame Account
The game account holds state about the TicTacToe game, mainly the current board, who are the
players and who's turn is it.
Note that it is annotated with
ShankAccount
in order to generate the proper SDK equivalent.It uses custom types like
GameState
which will be included in the SDK as well because theyare annotated with
BorshSerialize, BorshDeserialize
. This is a convention that _ shank_ usesin order to decide which types to include with the SDK.
Let's have a closer look at some of the methods of the generated TypeScript
GameAccount
,focusing on those that you might use in your API or tests.
Both are shortcuts to fetch and de/serialize the account from the blockchain. They are very
useful when you want to verify that an account was initialized/updated the way you expect.
Allows you to fund an account properly.
Is super useful to diagnose while developing as it returns a readable version of the account
that you can log to the console.
Provides a gpa builder that you can use to find accounts of this type on chain.
Creates a game account from the provided args in case and is used when the account is
deserialized from on-chain data.
Also notice that the file includes a
gameBeet
which is used to de/serialize the account.Supporting Types
Notice that additionally to the account the SDK added types that are referenced by it and the
updated init game instruction.
The Init Game Instruction
As part of the init game instruction asserts were added. Once those pass, it creates the Game PDA account, iniitializing the game such
that the player executing the transaction is set to be the first player
playerX
.In order to verify that everything checks out we now require instruction args which we added to
the Rust definition.
As a result the create method in the SDK was updated as well when we run
yarn api:gen
.Fortunately we immediately know which parts of the code we need to update due to the type
errors that result.
Additionally the documentation of the generated method informs us what is missing, namely the
instrucion args.
If we then follow definition into the generated code we can see exactly how we need to
provide them.
After we updated the test code we can run it again and it should pass.
Debugging Issues
While I was preparing the above I actually made a mistake and my transaction failed.
Fortunately amman will run even failing transactions on the validator (skipping preflight),
such that they become inspectable in the amman explorer.
Note how the color of the transaction indicates that it failed.
Scrolling down we can see the informative log which helped me triage the issue.
That same information is logged by amman to the terminal.
Finally after I fixed that issue I could verify in the explorer that my transaction completed properly.
Verifying Account State in Tests
However from looking at transaction in the explorer we couldn't verify that the game account state was properly set. We'll get to how to
make this visible in the explorer in the next step, but at any rate our tests should verify
this automatically.
To that purpose we use the
Game.fromAccountAddress
method which fetches and deserializes the gameaccount from our local validator. Then we use the spok assertion library to consisely
verify all properties on that account.
What you can Do
Start amman via
yarn amman:start
and open the amman explorer to connect.Then type
yarn t
to run your tests.In order to see failing transactions change the following line in the test as follows:
Since we're now sending the player account instead of the game account the checks fail and so
does the transaction. You can inspect the result in the amman explorer
You could also make changes to the Rust program, just remember to
cargo build-bpf
it andrestart amman via
yarn amman:start
every time you make a change.previous | next