-
Notifications
You must be signed in to change notification settings - Fork 317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't execute a Solana program compiled from Solidity code with Solang #59
Comments
With Solang, you won't be able to directly compile a Ethereum Solidity contract and deploy it as a Solana program. For example, below is an approximate equivalent for your @program_id("3fuYYeHetUivCR7HWJ9n5AtiPq7Ztm2n45caxaYzG1Uy")
contract SayHi {
@payer(payer)
constructor() {
print("Hi from Solidity code");
}
} To interact with the program once it's deployed, you can modify the The reason for using the Anchor client is because there is likely an 8 byte discriminator required as part of the instruction data for the program to know which instruction on the program to invoke (I'm not entirely sure how Solang does it). const {
Connection,
Keypair,
Transaction,
PublicKey,
sendAndConfirmTransaction,
} = require("@solana/web3.js");
const { extractKeys } = require("./KeyExtractor");
const { Program, AnchorProvider } = require("@coral-xyz/anchor");
// Generated IDL file
const IDL = require("../build/SayHi.json");
const CONTRACT_ADDRESS = "3fuYYeHetUivCR7HWJ9n5AtiPq7Ztm2n45caxaYzG1Uy";
async function callGetMessage() {
const connection = new Connection('http://127.0.0.1:8899', 'confirmed');
const programId = new PublicKey(CONTRACT_ADDRESS)
const keyData = await extractKeys();
const wallet = keyData.keyPair;
// Set up Anchor client using program IDL
const provider = new AnchorProvider(connection, wallet, {});
const program = new Program(IDL, programId, provider);
// Generate random keypair for "data account" created by "new" instruction
const dataAccount = Keypair.generate();
// Create instruction to invoke "new" instruction on program
const instruction = await program.methods
.new() // For Solang, the constructor is always the "new" instruction
.accounts({ dataAccount: dataAccount.publicKey })
.instruction();
// Add instruction to transaction
const transaction = new Transaction().add(instruction);
transaction.feePayer = wallet.publicKey;
// Send transaction
const txHash = await sendAndConfirmTransaction(
connection,
transaction,
[wallet, dataAccount]
);
console.log(`Transaction confirmed: ${txHash}`);
const transactionResponse = await connection.getTransaction(txHash);
// Print out the transaction program logs
console.log(JSON.stringify(transactionResponse.meta.logMessages, null, 2));
}
// Call the function to interact with the Solana program
callGetMessage().catch((error) => {
console.error("Error:", error);
}); Below are the steps I used to get the example running. I'm not sure why, but the keypair for the program ID wasn't getting output anywhere so I generated one to use instead. First, generate a new keypair to use as the address of the program ID.
The output should be similar to the following:
Copy the // SayHi.Sol
@program_id("B2FSjQhJMstz2db9C9RRwG4TqFWdPR7UPjZY5VUSPQUV") // SayHi.js
const CONTRACT_ADDRESS = "B2FSjQhJMstz2db9C9RRwG4TqFWdPR7UPjZY5VUSPQUV"; Then run:
Start a local validator in a separate terminal:
Then deploy the program, specifying filepath to the
Once the program is deployed, run the
The output should be similar to the following:
Let me know if it this solves the issue you were encountering! |
Thanks @ZYJLiu I owe you one. So it looks as if there is no getting away from Anchor. True? |
@ZYJLiu has tried in his explanation. My addition to his is that @reselbob why ignore Anchor? Yet you need to accept the fact that Solana is not another EVM-compatible Blockchain and that means the copy-paste of existing Solidity contracts is not possible (at least for now). You will first need to understand Solana itself and then see how to adapt the existing contract (if possible yet with Solang) to fit Solana architecture. Meanwhile, you can take my Solang Solidity for Solana 101 Course to get a bit familiar with Solana and Solang if a beginner free at https://dprogramminguniversity.com/freecourses/solang-solidity-for-solana-course-101/ |
Thank you for your thoughtful response. |
Hi:
I am trying to exercise a Solana program I migrated from Solidity using Solang.
I keep getting this error:
I demonstrate the issue in this 4 minute video.
For purposes of research, I am intentionally avoiding using Anchor.
Here is the Solidity code that I migrated.
Here is the client script I used to try to execute the program.
Here is the entire project on GitHub.
If the problem is due to some mistake on my part, in all sincerity I ask your forgiveness.
For what it's worth, this problem has been plaguing me for days.
Thanks in advance for any help and direction you can provide.
The text was updated successfully, but these errors were encountered: