Custom Anchor Program

By far the most powerful feature of the Godot Solana SDK is the ability to integrate custom smart contract into your game through AnchorProgram class. The following example Tiny Adventure uses a smart contract heavily influenced by Solplay Jonas's tutorial video

Add AnchorProgram Node

In the root of your game's Scene Tree, or as an autoload, add AnchorProgram node into your game. One AnchorProgram is responsible for communication with one smart contract, so if your game needed more smart contracts, you'd add more nodes and to each one assign a different Pid (Program ID)

Import the IDL

Every Anchor smart contract comes with an IDL file that stores information on functions, accounts and other things the contract has. To be able to call a program, you need to have their respective IDL file imported into Godot.

Drag the IDL json file into Json File slot in the node and click Try From Json File to generate the IDL dictionary. This dictionary is going to allow to view and access the data within your scripts very trivial.

To know what accounts and arguments are needed for different instructions to be called, you can inspect the IDL dictionary. It is a very important thing to learn, because you will keep getting back to it for double checking the "Magic Strings" you need to input.

For example, here you see the program has 4 instructions. By clicking any dictionary in the array, you see that this one is called "restartLevel" and it has 4 accounts needed to be passed in and 1 argument:

The argument, as you can see, is chestPrice which needs to be a u64 integer. By checking all accounts and arguments, you will be ready to call this function!

Call The Function!

The following code restarts the Tiny Adventure game, by sending x amount of SOL as the chest prize, which the player would reclaim after reaching the game's end.

func init_game() -> void:
	var tiny_adventure_key:Pubkey = Pubkey.new_from_string(tiny_adventure_pid))
	var level_pda:Pubkey = Pubkey.new_pda(["Level1"],tiny_adventure_key)
	var vault_pda:Pubkey = Pubkey.new_pda(["Vault1"],tiny_adventure_key)
	
	var prize_in_lamports:int = int(chest_prize*pow(10,9))
	var instructions:Array[Instruction]
	
	var init_ix:Instruction = anchor_program.build_instruction("restartLevel",[
		level_pda, #gamedata
		vault_pda, #gamevault
		SolanaService.wallet.get_kp(), #signer
		SystemProgram.get_pid() #system program
	],
	AnchorProgram.u64(prize_in_lamports))
	
	instructions.append(init_ix)
	var tx_id:String = await SolanaService.transaction_processor.sign_transaction(SolanaService.wallet.get_kp(),instructions,"finalized")

Please note that the PDA here was quite simple, consisting of just 1 parameter, so it was easily created using Pubkey.new_pda() function. For a more complex PDA with more variables, it must be created from combining bytes. Check here for the explanation.

For a full demo of TinyAdventure, check SolanaSDK -> Demos -> TinyAdventureDemo inside the imported addon.

There is also a far more complex Highscore Demo using SOAR program by Magicblock. You can find it in SolanaSDK -> Demos -> HighscoreDemo in the addon!

Last updated