Final modifications
Let's modify our new_puzzle
method a bit, and demonstrate how a smart contract author might use base64-encoded arguments.
In the previous chapter we had a fairly long NEAR CLI command that called the new_puzzle
, providing it the parameters for all the clues. Having these lengthy parameters on the CLI might get cumbersome. There may be issues needing to escape single or double quotes, and each operating system may wish for a different format on the Terminal or Command Prompt.
We're going to send all the arguments as a base64-encoded string, and make this a bit simpler. For this, we're going to use Base64VecU8
from the SDK.
Base64VecU8
is great for binary payloadsWhat we're doing makes sense, but it's worth noting that it's perhaps more common to use Base64VecU8
when sending binary parameters.
Read more about it here.
First we'll set up a struct for the arguments we're expecting:
Loading...
Then we modify our new_puzzle
method like so:
Loading...
We can take our original arguments and base64 encode them, using whatever method you prefer. There are plenty of online tool, Terminal commands, and open source applications like Boop.
We'll copy this:
{
"answer_pk": "ed25519:7PkKPmVUXcupA5oU8d6TbgyMwzFe8tPV6eV1KGwgo9xg",
"dimensions": {
"x": 11,
"y": 10
},
"answers": [
{
"num": 1,
"start": {
"x": 0,
"y": 1
},
"direction": "Across",
"length": 12,
"clue": "NEAR transactions are more ______ instead of atomic."
},
{
"num": 2,
"start": {
"x": 6,
"y": 0
},
"direction": "Down",
"length": 7,
"clue": "In a smart contract, when performing an Action, you use this in Rust."
},
{
"num": 3,
"start": {
"x": 9,
"y": 0
},
"direction": "Down",
"length": 6,
"clue": "In docs.rs when you search for the near-sdk crate, these items a considered a what: collections, env, json_types."
},
{
"num": 4,
"start": {
"x": 1,
"y": 1
},
"direction": "Down",
"length": 10,
"clue": "A series of words that can deterministically generate a private key."
},
{
"num": 5,
"start": {
"x": 1,
"y": 3
},
"direction": "Across",
"length": 3,
"clue": "When doing high-level cross-contract calls, we import this that ends in _contract. When calling ourselves in a callback, it is convention to call it THIS_self."
},
{
"num": 6,
"start": {
"x": 0,
"y": 8
},
"direction": "Across",
"length": 8,
"clue": "Use this to determine the execution outcome of a cross-contract call or Action."
},
{
"num": 7,
"start": {
"x": 4,
"y": 6
},
"direction": "Across",
"length": 4,
"clue": "You chain this syntax onto a promise in order to schedule a callback afterward."
}
]
}
and base64 encode it:
Now we can build and run the new crossword puzzle contract as we have before:
cargo near build