solana-developers / program-examples Goto Github PK
View Code? Open in Web Editor NEWA repository of Solana program examples
A repository of Solana program examples
Hey @jpcaulfi. Recently, I found out about the Seahorse-lang and would love to add examples to the repository. Along with that, Developer Experience repository motivated me even more!
Let me know, thanks!
Hey @jpcaulfi I've made an example anchor program that I think can be added to mint-1 folder as there isn't anything currently there. Could you have a look at it and see if it's suitable? https://github.com/Amaechi-Tetteh/spltoken
Hi:
I am trying to understand the examples in this repo. While I have reasonable understanding of writing Solidity code for EVM targets, some of the code in the examples is hard for me to understand. For example, in this:
@program_id("F1ipperKF9EfD821ZbbYjS319LXYiBmjhzkkf5a26rC")
contract hello_solana {
// The constructor is used to create a new account
// Here we create a new account that stores no data and only prints messages to the program logs when the constructor is called.
@payer(payer) // The "payer" pays for the account creation
constructor() {
// We get the program ID by calling 'this';
address programId = address(this);
// Print messages to the program logs
print("Hello, Solana!");
print("Our program's Program ID: {:}".format(programId));
}
}
What is the meaning of @program_id
and how is the value F1ipperKF9EfD821ZbbYjS319LXYiBmjhzkkf5a26rC
generated or discovered?
Also, what is @payer(payer)
about? I have not run across this before in typical Solidity programming for the EVM.
I know these are newbie questions, maybe not appropriate for this issue. If so, please might you be able to tell me where I can get some questions answered?
Thanks in advance!
Hi:
I am trying to exercise a Solana program I migrated from Solidity using Solang.
I keep getting this error:
Error: SendTransactionError: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: invalid program argument
at Connection.sendEncodedTransaction (/home/reselbob/Documents/solidity-to-solana/node_modules/@solana/web3.js/lib/index.cjs.js:9560:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Connection.sendRawTransaction (/home/reselbob/Documents/solidity-to-solana/node_modules/@solana/web3.js/lib/index.cjs.js:9526:20)
at async Connection.sendTransaction (/home/reselbob/Documents/solidity-to-solana/node_modules/@solana/web3.js/lib/index.cjs.js:9517:12)
at async sendAndConfirmTransaction (/home/reselbob/Documents/solidity-to-solana/node_modules/@solana/web3.js/lib/index.cjs.js:2145:21)
at async callGetMessage (/home/reselbob/Documents/solidity-to-solana/scripts/SayHi.js:50:20) {
logs: [
'Program 5FWPj98F4TEfSvWxqYXQW4whdoLXTN2yeXfCJWfY7EjL invoke [1]',
'Program 5FWPj98F4TEfSvWxqYXQW4whdoLXTN2yeXfCJWfY7EjL consumed 34 of 200000 compute units',
'Program 5FWPj98F4TEfSvWxqYXQW4whdoLXTN2yeXfCJWfY7EjL failed: invalid program argument'
]
}
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.
Hello there,
Please I have this bug i have been facing, i have tried everything but itβs not working
this is the raw error message
Error: Raw transaction 4n7GkQVEtedpvjMU27nnfhwxJhXh6xHxCG7VV8taVtdhvEyo7BfEdkctX84zx3CEvxxFCYCLEbPiFPKzB9AsEuXi failed ({"err":{"InstructionError":[0,"PrivilegeEscalation"]}})
Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: Cross-program invocation with unauthorized signer or writable account
Please here is my code, i will really appreciate it of anyone can help
We just got an awesome implementation of closing (destroying) an account using Anchor from @dadepo - but a native one would also be dope.
Lmk if anyone can put this together.
When I run basics/favorites/anchor project, there is error like below.
Could you tell me how to fix this?
Thanks in advance
favorites
Balance: 500,000,000 SOL
1) Writes our favorites to the blockchain
Invalid arguments: favorites not provided.
2) Updates the favorites
3) Rejects transactions from unauthorized signers
0 passing (301ms)
3 failing
favorites
Writes our favorites to the blockchain:
Error: Invalid arguments: favorites not provided.
at /home/quan/workspace/favorites/node_modules/@coral-xyz/anchor/src/program/common.ts:50:15
at Array.forEach ()
at validateAccounts (node_modules/@coral-xyz/anchor/src/program/common.ts:45:14)
at ix (node_modules/@coral-xyz/anchor/src/program/namespace/instruction.ts:44:23)
at txFn (node_modules/@coral-xyz/anchor/src/program/namespace/transaction.ts:24:14)
at MethodsBuilder.rpc [as _rpcFn] (node_modules/@coral-xyz/anchor/src/program/namespace/rpc.ts:21:18)
at MethodsBuilder.rpc (node_modules/@coral-xyz/anchor/src/program/namespace/methods.ts:212:17)
favorites
Updates the favorites:
Error: null
at /home/quan/workspace/favorites/tests/favorites.ts:60:13
at Generator.throw ()
at rejected (tests/favorites.ts:29:65)
favorites
Rejects transactions from unauthorized signers:
AssertionError: expected false to be true
-false
+true
at /home/quan/workspace/favorites/tests/favorites.ts:75:14
at Generator.throw ()
at rejected (tests/favorites.ts:29:65)
Hey @jpcaulfi i would love to put this example here. I have a repo which has an example of this but it seams it needs to be updated as its 5 months old. https://github.com/0xPratik/NFT/blob/master/programs/mpl-testing/src/lib.rs
$cd program-examples/basics/account-data/native
$bash cicd.sh
Error: Function _ZN112_$LT$solana_program..instruction..InstructionError$u20$as$u20$solana_frozen_abi..abi_example..AbiEnumVisitor$GT$13visit_for_abi17hd07a5f9ab7b9f369E Stack offset of 4584 exceeded max offset of 4096 by 488 bytes, please minimize large stack variables
Maybe we can have devnet-only
folder & put examples like https://github.com/TeamRaccoons/address-lookup-table-multi-swap there
It would be nice to have a setup where the example programs can be deployed and the client tests ran against them. I think it should be possible to set this up with github actions.
If this is considered worth having, then I will be happy to help setup.
Why does the token-swap use whole u64 amounts in the following I64F64 calculation - which in my view could lead to overflow issues. The maximum Integer part from I6F64 is 2^63 -1. In their deposit_liquidity.rs file they calculated liquidity like:
pub fn deposit_liquidity(
ctx: Context<DepositLiquidity>,
amount_a: u64,
amount_b: u64,
) -> Result<()> {
...
let mut liquidity = I64F64::from_num(amount_a)
.checked_mul(I64F64::from_num(amount_b))
.unwrap()
.sqrt()
.to_num::<u64>();
That means if for example amount_a and amount_b is give in lamports which is sol * 10^9 they could only provide the square root of (2^63 - 1) / 10^9 = 3 SOL into the contract on each side. Why not cut amount/10^9 before the calculations as I64F64 has its floating precision also set to 2^-64 . Meaning, the overflow issue isnt a problem if the arguments are lamports - great for precision. But why don't they convert and calculate in SOL / use U128F0 within the contract? Or am I misunderstanding something here ?
If update the anchor.toml to localnet
[programs.localnet]
swap_example = "C3ti6PFK6PoYShRFx1BNNTQU3qeY1iVwjwCA6SjJhiuW"
[provider]
cluster = "Localnet"
wallet = "~/.config/solana/id.json"
runing the test will fail
TypeError: Cannot read properties of undefined (reading 'programId')
why and how to modify it works in local net ?
Looks like after commit e76ac6c, running the test for create account now fails.
My guess is, this is due to the hardcoding of Au21huMZuDQrbzu2Ec5ohpW5CKRqhcGV6qLawfydStGs
as program ID in the test.
Below is the error:
Create a system account
1) Create the account
0 passing (103ms)
1 failing
1) Create a system account
Create the account:
Error: failed to send transaction: Transaction simulation failed: Attempt to load a program that does not exist
at Connection.sendEncodedTransaction (node_modules/@solana/web3.js/src/connection.ts:4934:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at Connection.sendRawTransaction (node_modules/@solana/web3.js/src/connection.ts:4893:20)
at Connection.sendTransaction (node_modules/@solana/web3.js/src/connection.ts:4881:12)
at sendAndConfirmTransaction (node_modules/@solana/web3.js/src/utils/send-and-confirm-transaction.ts:31:21)
I'm trying to run the yarn test from counter/native directory.
$ pwd
~/program-examples/basics/counter/native
$ yarn test
Error: program file does not exist: ./target/deploy/counter_solana_native.so
Error: server closed unexpectedly
at ChildProcess.onClose (program-examples/basics/counter/native/node_modules/start-server-and-test/src/index.js:80:14)
at ChildProcess.emit (node:events:390:28)
at maybeClose (node:internal/child_process:1064:16)
at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)
error Command failed with exit code 1.
I think yarn should detect which directory the test is invoked from. There should be a way to know the project root and that should be prefixed to the target/deploy/counter_solana_native.so
path.
We need a place to educate people on how to use this feature & update example with new SDK versions
(node:28858) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
1) Create the address info account
2) Read the new account's data
0 passing (81ms)
2 failing
1) Account Data!
Create the address info account:
Error: AnchorError caused by account: address_info. Error Code: AccountNotInitialized. Error Number: 3012. Error Message: The program expected this account to be already initialized.
at Function.parse (node_modules/@project-serum/anchor/src/error.ts:167:14)
at translateError (node_modules/@project-serum/anchor/src/error.ts:276:35)
at MethodsBuilder.rpc [as _rpcFn] (node_modules/@project-serum/anchor/src/program/namespace/rpc.ts:35:29)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
2) Account Data!
Read the new account's data:
Error: Account does not exist ESowwGuph5bQso9ycdi7R7hKNvirUpYErzfU19ATdZhb
at AccountClient.fetch (node_modules/@project-serum/anchor/src/program/namespace/account.ts:154:13)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Anchor CI builds and tests with 0.30.1,
hence projects with 0.30.0 and lower have to upgraded.
can't use toolchain flag in Anchor.toml because it uses avm (throws warning if not error)
can't use avm with stable rust
Why are we comparing invariant here with pool_a.amount * pool_a.amount
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.