Solana Gaming DeFi SDK(Mobile)
You can add the package to your unity project via Window--Package Manager--Add package from git URL, paste the address https://github.com/Canoe-Finance/Solana-Gaming-DeFi-SDK.git and click add. If you are using an older version of unity, you can download our repository, then select the package.json file through Window--Package Manager--Add package from disk to import.
- 1.If you have an older version of Unity that doesn't have imported Newtonsoft.Json just import it.
- 2.After importing the wallet Unity will throw unity-plastic error. Just restart Unity.
- 3.Create a new or use an existing GameObject in your scene, select it, click Add Component in the inspector, input CanoeDeFi to search, and add it to the Gameobject. Make sure this GameObject won't be destroyed.
- 4.Set the environment of solnet you prefer(MainNet/TestNet/DevNet) on the CanoeDeFi script in your GameObject.
The CanoeDeFi script is a singleton, you can use it anywhere by calling [CanoeDeFi.Instance] easily.
- In the beginning, you can use this method to determine whether the user has logged in. If not, allow users to import or create new wallets. This method will return true if the user created or imported a wallet.
bool tf = CanoeDeFi.Instance.HasWallet();
- Creating a new wallet is divided into two steps: generating a mnemonic, and logging in using the password and the mnemonic just generated.
- Generate and return the mnemonic. You should display these mnemonics on the UI. And make sure the user has written it down.
Mnemonic mnemonic = CanoeDeFi.Instance.GenerateNewWallet();
- After confirming that the user has written it down, use the mnemonic phrase just generated and the password entered by the user to log in.
CanoeDeFi.Instance.LoginWithNewGeneratedWallet(mnemonic, password);
If the user already has an account, this method can be called with the mnemonic entered by the user. Mnemonics are 12 or 24 words separated by spaces.
If the wallet is restored successfully, it will return true, and the user's wallet will be saved, otherwise, it will return false.
bool tf = CanoeDeFi.Instance.RestoreWalletWithMenmonic(mnemonic, password);
If you know that the user has logged in before through the previous method, you can let the user enter the password to log in.
If the password is correct, it will return true, otherwise, it will return false.
bool tf = CanoeDeFi.Instance.LoginWithPwd(password);
Get the user's Solana account balance. It should be noted that this method is asynchronous.
double count = await CanoeDeFi.Instance.GetSolAmmountAsync();
Pass the address and amount entered by the user to initiate a transfer request. It returns a value of RequestResult, you can check it for the result and more details.
It's worth noting that for Solana sol, 1000000000 amounts stand for 1(UI, user input), so you need to convert it before passing in.
RequestResult<string> transferResult = await CanoeDeFi.Instance.TransferSol(toPublicKey,
umount);
Users may have several tokens in the game, you can get the list by calling this function. The return value is a list of TokenAccount, which contains each token's info, and amount.
TokenAccount[] tokenAccounts = await CanoeDeFi.Instance.GetOwnedTokenAccounts();
It will be used for users' transfer and consumption in the game, this is the most complex function so far, but it's not hard to understand. Overall this function transfers any kind of token to the given address.
We have a very detailed description for each parameter in the code comments.
RequestResult<string> requestResult = await
CanoeDeFi.Instance.TransferToken(sourceTokenAccount, toWalletAccount, sourceAccountOwner,
tokenMint, amount);
Swap between SOL and token, or different kinds of the token is very useful. We implemented an aggregated dex Jupiter, which can be used by calling the method[JupiterSwapRequest()], passing parameters of inputMint, outputMint, amount, shippage, and the callback action. while jupiter's swap is done, the callback function will be called. The result should be handled in your callback function.
CanoeDeFi.Instance.JupiterSwapRequest(inputMint, outputMint, amout, shippage, callbackAction<string>);
Last modified 1yr ago