Blockchain
Solana (SOL)
Solana (SOL)
์์ฝ
- ์ค์ํ๋ฅผ ๋ฒ์ด๋์ง ๋ชปํจ. ์ฌ๋จ์์ ๋คํธ์ํฌ๋ฅผ ๊ป๋ค ์ผฐ๋ค ํ ์ ์์
- ๋ ธ๋๊ฐ ์์ฒญ ์ ์ฃฝ์ (์ฌํด๋ง ๋ช ๋ฒ ์ค์ง๋์๋์ง..)
- ์๋ผ๋์ ์๋ผ๋ ํ๋ก๊ทธ๋จ(์ปจํธ๋ํธ) ๊ฐ๋ฐ์ธ์ด๊ฐ ํต์ผ๋์ด ์๋ค๋๊ฒ ๋งค์ฐ ๋งค๋ ฅํฌ์ธํธ
- ์ฌ๋จ์์ ์ ๊ณตํ๋ ํด๋ค์ด ๋๋ฌด ํ์์ ์. ์์ด๋์ด๋ง ์๋ค๋ฉด ๋์ถฉ ์์ํ ์ ์์ ์ ๋
- ์ด๊ฒ๋ mempool์ด ์์
How to create and manage address or account?
Using Private Key
const account = Keypair.generate()
console.log(JSON.stringify(account.publicKey.toBase58()))
// output: "gVazpxjimX3EP4mto53pEi4YSE36KP2nDwyEvLcKjmR"
Using Mnemonic
const mnemonic =
'pill tomorrow foster begin walnut borrow virtual kick shift mutual shoe scatter'
const seed = bip39.mnemonicToSeedSync(mnemonic, '') // (mnemonic, password)
// BIP39
const keypair = Keypair.fromSeed(seed.slice(0, 32))
// BIP44
for (let i = 0; i < 10; i++) {
const path = `m/44'/501'/${i}'/0'`
const keypair = Keypair.fromSeed(derivePath(path, seed.toString('hex')).key)
console.log(`${path} => ${keypair.publicKey.toBase58()}`)
}
// Check if a given public key has an associated private key
const key = new PublicKey('5oNDL3swdJJF1g9DzJiZ4ynHXgszjAEpUkxVYejchzrY')
console.log(PublicKey.isOnCurve(key.toBytes()))
How to sign and verify messages with wallets
const message = "The quick brown fox jumps over the lazy dog";
const messageBytes = decodeUTF8(message);
const signature = nacl.sign.detached(messageBytes, keypair.secretKey);
const result = nacl.sign.detached.verify(
messageBytes,
signature,
keypair.publicKey.toBytes()
);
console.log(result);
-------------------------------------------------------------------------------
{
let recoverTx = Transaction.populate(Message.from(realDataNeedToSign));
recoverTx.addSignature(feePayer.publicKey, Buffer.from(feePayerSignature));
recoverTx.addSignature(alice.publicKey, Buffer.from(aliceSignature));
console.log(
`txhash: ${await connection.sendRawTransaction(recoverTx.serialize())}`
);
}
-------------------------------------------------------------------------------
{
let recoverTx = Transaction.populate(Message.from(realDataNeedToSign), [
bs58.encode(feePayerSignature),
bs58.encode(aliceSignature),
]);
console.log(
`txhash: ${await connection.sendRawTransaction(recoverTx.serialize())}`
);
}
How to query balance of address on blockchain?
How to query transaction?
How to add a memo to a tx?
const transferTransaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: fromKeypair.publicKey,
toPubkey: toKeypair.publicKey,
lamports: lamportsToSend,
}),
)
await transferTransaction.add(
new TransactionInstruction({
keys: [
{ pubkey: fromKeypair.publicKey, isSigner: true, isWritable: true },
],
data: Buffer.from('Data to send in transaction', 'utf-8'),
programId: new PublicKey('MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr'),
}),
)
await sendAndConfirmTransaction(connection, transferTransaction, [fromKeypair])
How to estimate fee?
getEstimatedFee
const recentBlockhash = await connection.getLatestBlockhash()
const transaction = new Transaction({
recentBlockhash: recentBlockhash.blockhash,
}).add(
SystemProgram.transfer({
fromPubkey: payer.publicKey,
toPubkey: payee.publicKey,
lamports: 10,
}),
)
const fees = await transaction.getEstimatedFee(connection)
console.log(`Estimated SOL transfer cost: ${fees} lamports`)
// Estimated SOL transfer cost: 5000 lamports
// getFeeForMessage
const message = new Message(messageParams)
const fees = await connection.getFeeForMessage(message)
console.log(`Estimated SOL transfer cost: ${fees.value} lamports`)
// Estimated SOL transfer cost: 5000 lamports
How to Transfer tokens one to another?
// Send SOL
const transferTransaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: fromKeypair.publicKey,
toPubkey: toKeypair.publicKey,
lamports: lamportsToSend,
}),
)
await sendAndConfirmTransaction(connection, transferTransaction, [fromKeypair])
// Send SPL Token
// Add token transfer instructions to transaction
const transaction = new web3.Transaction().add(
splToken.Token.createTransferInstruction(
splToken.TOKEN_PROGRAM_ID,
fromTokenAccount.address,
toTokenAccount.address,
fromWallet.publicKey,
[],
1,
),
)
// Sign transaction, broadcast, and confirm
await web3.sendAndConfirmTransaction(connection, transaction, [fromWallet])
Build Frontend and deploy contract to Solana devnet
I using rust, anchor to develop smart contract. All I have to do is initializing some struct, and add methods in main program module. Itโs quite easy to understand, and I think itโs simillar with creating and using gRPC.
