KimSeogyu.github.io

Solana (SOL)

μš”μ•½

  1. 쀑앙화λ₯Ό λ²—μ–΄λ‚˜μ§€ λͺ»ν•¨. μž¬λ‹¨μ—μ„œ λ„€νŠΈμ›Œν¬λ₯Ό 껐닀 μΌ°λ‹€ ν•  수 있음
  2. λ…Έλ“œκ°€ μ—„μ²­ 잘 죽음 (μ˜¬ν•΄λ§Œ λͺ‡ 번 μ€‘μ§€λ˜μ—ˆλŠ”μ§€..)
  3. μ†”λΌλ‚˜μ™€ μ†”λΌλ‚˜ ν”„λ‘œκ·Έλž¨(μ»¨νŠΈλž™νŠΈ) κ°œλ°œμ–Έμ–΄κ°€ ν†΅μΌλ˜μ–΄ μžˆλ‹€λŠ”κ²Œ 맀우 λ§€λ ₯포인트
  4. μž¬λ‹¨μ—μ„œ μ œκ³΅ν•˜λŠ” νˆ΄λ“€μ΄ λ„ˆλ¬΄ ν™˜μƒμ μž„. μ•„μ΄λ””μ–΄λ§Œ μžˆλ‹€λ©΄ λŒ€μΆ© μ‹œμž‘ν•  수 μžˆμ„ 정도
  5. 이것도 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.

Screenshot of Demo project (thx to https://buildspace.so)