Solana, post 2 đź’Ą
In the previous host we setup our env: server, wallet + HTML page. Now it is time to talk about programming model.
If you don't know about solitidy, you are blessed, because solana is so different that it took me really a long time to understand it. Try to forget everything as much as you can.
So, the first thing we did yesterday, was creating a program account. What it means exactly - we have asked Solana to create a storage, where we placed the executable of our code. As a reminder:
solana program deploy $HOME/example-helloworld/dist/program/helloworld.so --program-id dist/program/helloworld-keypair.json
Generally, it is a storage for ./a.out. Today we are going to create a "file" (it is called account) for the user's specific needs. Each user will have their own files. Then we will give to our ./a.out (main executable) a write access to our user's file. 3, 2, 1, GO!
So, the first part is to generate an account from the client side, transfer some value (called lamports) to it, and then transfer an ownership to this account to the program.
// This is our account address, randomly generated. const keypair = solanaWeb3.Keypair.generate(); const instruction = solanaWeb3.SystemProgram.createAccount({ keys: [ { pubkey: solana.publicKey, isSigner: true, isWritable: true }, { pubkey: keypair.publicKey, isSigner: true, isWritable: true }, ], fromPubkey: solana.publicKey, newAccountPubkey: keypair.publicKey, lamports: 10000000, space: 10000, programId: new solanaWeb3.PublicKey("3cbqMCdkwiTbWyCv3i7gss8STqGw6ggzvg8ztt5msnD4"), }); // ^^^^^ programId is an owner of the account, // keys is an array of keys whose signatures are required to perform // this operation, this is our wallet (as some funds will be transferred) // and the account, as it's ownership will be transferred. const bh = (await connection.getRecentBlockhash()).blockhash; const transaction = new solanaWeb3.Transaction({ feePayer: window.solana.publicKey, recentBlockhash: bh, }); transaction.add(instruction); // Walled signature is generated by the line after the next one. // And the next line is to sign a transaction by the account's key. transaction.partialSign(keypair); const signedTransaction = await window.solana.signTransaction(transaction); await connection.sendRawTransaction(signedTransaction.serialize()); // Checking we have it... const acc = await connection.getAccountInfo(keypair.publicKey); console.log(acc);