Last active
October 9, 2022 16:40
-
-
Save aeither/132ffe0b21d5bdfcc66d0e66648ddb5e to your computer and use it in GitHub Desktop.
Basic Personal Blog written with Seahorse Lang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# blog | |
# Built with Seahorse v0.1.6 | |
# | |
# On-chain, personal blog! | |
from seahorse.prelude import * | |
# This is your program's public key and it will update | |
# automatically when you build the project. | |
declare_id('1111111111111111111111111111111111111111111'); | |
class AuthorState(Account): | |
name: String | |
avatar: String | |
author: Pubkey | |
post_count: u8 | |
class PostState(Account): | |
title: String | |
content: String | |
author_key: Pubkey | |
author: Pubkey | |
@instruction | |
def signup_user(author: Signer, author_account: Empty[AuthorState], name: String, avatar: String): | |
author_account.init(payer = author, seeds = ["author", author]) | |
print("sign up user") | |
author_account.name = name | |
author_account.avatar = avatar | |
author_account.post_count = 0 | |
author_account.author = author.key() | |
@instruction | |
def update_user(author_account: AuthorState, name: String, avatar: String): | |
author_account.name = name | |
author_account.avatar = avatar | |
@instruction | |
def create_post(author: Signer, author_account: AuthorState, post_account: Empty[PostState], title: String, content: String): | |
post_account.init(payer = author, seeds = ["post", str(author_account.post_count)]) | |
print("create post") | |
assert author.key() == author_account.author, "signer must be user account owner" | |
post_account.title = title | |
post_account.content = content | |
post_account.author = author_account.key() | |
post_account.author_key = author.key() | |
author_account.post_count += 1 | |
# test in ts | |
// No imports needed: web3, anchor, pg and more are globally available | |
describe("Test", async () => { | |
// Generate the user account public key from its seeds | |
const [userAccountAddress] = await web3.PublicKey.findProgramAddress( | |
[Buffer.from("user"), pg.wallet.publicKey.toBuffer()], | |
pg.PROGRAM_ID | |
); | |
it.skip("init user", async () => { | |
// Send transaction | |
const txHash = await pg.program.methods | |
.signupUser("jack", "jack url") | |
.accounts({ | |
userAccount: userAccountAddress, | |
author: pg.wallet.publicKey, | |
systemProgram: web3.SystemProgram.programId, | |
}) | |
.rpc(); | |
console.log(`Use 'solana confirm -v ${txHash}' to see the logs`); | |
// Confirm transaction | |
await pg.connection.confirmTransaction(txHash); | |
// Fetch the created account | |
const userAccount = await pg.program.account.authorState.fetch( | |
userAccountAddress | |
); | |
console.log("name:", userAccount.name); | |
console.log("avatar:", userAccount.avatar); | |
console.log("count:", userAccount.postCount.toString()); | |
}); | |
it("create post", async () => { | |
const userAccount = await pg.program.account.authorState.fetch( | |
userAccountAddress | |
); | |
const [postAccountAddress] = await web3.PublicKey.findProgramAddress( | |
[Buffer.from("post"), Buffer.from(String(userAccount.postCount))], | |
pg.PROGRAM_ID | |
); | |
// Send transaction | |
const txHash = await pg.program.methods | |
.createPost("title 2", "content 2") | |
.accounts({ | |
postAccount: postAccountAddress, | |
author: pg.wallet.publicKey, | |
userAccount: userAccountAddress, | |
systemProgram: web3.SystemProgram.programId, | |
}) | |
.rpc(); | |
// Confirm transaction | |
await pg.connection.confirmTransaction(txHash); | |
// Fetch the post account | |
const postAccount = await pg.program.account.postState.fetch( | |
postAccountAddress | |
); | |
console.log("title:", postAccount.title); | |
assert(postAccount.title); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment