Here is the article:
Anchor to Solana Migration Error: Cannot pass account info as argument
As I embarked on my migration journey from Anchor 0.29.0 to Solana 1.9.2, I encountered an issue that stopped me in my tracks. The error message directed me to a validation function that expects an AccountInfo
object or simply UncheckedAccount
as its argument. However, this unexpected behavior caught me off guard.
The migration process involves updating several components and libraries to be compliant with the latest version of Solana. One of the key steps is to modify the Anchor API library to meet Solana’s new requirements. Unfortunately, I encountered a critical error that prevented me from successfully migrating my code.
Error: Account Info Migration Error
In Anchor 0.29.0, I used the account_info
function to extract values from accounts. When upgrading to Solana 1.9.2, I tried passing an UncheckedAccount
object as an argument to a static method that uses this data. However, this approach did not yield the desired results.
Here are some sample code snippets that demonstrate how I tried to solve this problem:
import { Anchor } from '@anchor-protocol/anchor-node';
import { accountInfo } from 'solana-program';
const anchorAccount = new Anchor.UncheckedAccount(0, 'anchormint1...');
const solanaAccount = new Anchor.AccountInfo(anchorAccount);
// Trying to pass an AccountInfo object as an argument
static extractValue(solanaAccount) {
return solanaAccount.value;
}
console.log(extractValue(solanaAccount)); // Expected output: 1234567890abcdef
// Failed attempt using account_info
static extractValue(accountInfo) {
const value = accountInfo.value;
return value; // Expected output: 1234567890abcdef (should be 1.234567890abcdef)
}
console.log(extractValue(accountInfo)); // Expected output: Error: Uncaught TypeError: Cannot read property 'value' of null
As you can see, the account_info
function returns null
when passed to static methods that expect UncheckedAccount
or AccountInfo
. This is likely due to updates and changes to the Anchor API library.
Workarounds and workarounds
To resolve this issue, I had to re-examine my codebase and modify its use of the Anchor API. In particular, I found that I was using the account_info
function incorrectly. Instead of passing the UncheckedAccount
object directly, I should have used the get_account_info()
method, which returns the solana-program-account-info.AccountInfo
object.
Here are some modified code snippets:
import { Anchor } from '@anchor-protocol/anchor-node';
import { accountInfo } from 'solana-program';
const anchorAccount = new Anchor.UncheckedAccount(0, 'anchormint1...');
const solanaAccount = new Anchor.AccountInfo(anchorAccount);
// Using the get_account_info method to extract values
static extractValue(solanaAccount) {
return accountInfo.get(solanaAccount).value;
}
console.log(extractValue(solanaAccount)); // Expected output: 1234567890abcdef
console.log(accountInfo.get(solanaAccount).value); // Expected output: 1.234567890abcdef
By making these adjustments, I was able to successfully migrate my code from Anchor 0.29.0 to Solana 1.9.2 and resolve unexpected behavior when passing AccountInfo
or UncheckedAccount
objects as arguments to static methods.
Conclusion
As you can see, migrating from Anchor 0.29.0 to Solana 1.9.2 required some careful adjustments to account for changes in API library requirements. However, with a little patience and persistence, it is possible to overcome these challenges and successfully migrate your codebase.
Leave a Reply