Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ported memo to anchor #3024

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions memo/anchor_program/memo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "memo"
version = "0.1.0"
description = "Created with Anchor"
edition = "2018"

[lib]
crate-type = ["cdylib", "lib"]
name = "memo"

[features]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
default = []

[dependencies]
anchor-lang = "0.22.0"
2 changes: 2 additions & 0 deletions memo/anchor_program/memo/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
34 changes: 34 additions & 0 deletions memo/anchor_program/memo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use anchor_lang::prelude::*;

declare_id!("4ARKJpztrkSJXJQ8ii9pr6NjJdwKrC6M3jA8LG21tvjG");

#[program]
pub mod memo {
use super::*;
use std::str;
pub fn log_memo(ctx: Context<BuildMemo>, input: Vec<u8>) -> Result<()> {
let accounts = ctx.remaining_accounts;

let mut missing_required_signature = false;
for account_info in accounts.iter() {
if let Some(address) = account_info.signer_key() {
msg!("Signed by: {:?}", address);
} else {
missing_required_signature = true;
}
}
if missing_required_signature {
return Err(ProgramError::MissingRequiredSignature.into());
}

let memo = str::from_utf8(&input).map_err(|err| {
msg!("Invalid UTF-8, from bytes: {:?}", err);
ProgramError::InvalidInstructionData
})?;
msg!("Memo (len {}): {:?}", memo.len(), memo);
Ok(())
}
}

#[derive(Accounts)]
pub struct BuildMemo {}
98 changes: 98 additions & 0 deletions memo/anchor_program/tests/memo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const anchor = require("@project-serum/anchor");
const assert = require("assert");

describe("memo", () => {
// Configure the client to use the local cluster.
anchor.setProvider(anchor.Provider.env());
const program = anchor.workspace.Memo;

it("Test 1 => Sending Buffers", async () => {
const string = "letters";
var codedString = Buffer.from(string);

const tx = await program.rpc.logMemo(codedString);
console.log("Transaction Signature:", tx);
});

it("Test 2 => Asserting Emojis and Bytes", async () => {
let emoji = Uint8Array.from(Buffer.from("🐆"));
let bytes = Uint8Array.from([0xf0, 0x9f, 0x90, 0x86]);
assert.equal(emoji.toString(), bytes.toString());

const tx1 = await program.rpc.logMemo(Buffer.from("🐆"));

const tx2 = await program.rpc.logMemo(Buffer.from(bytes));
console.log("Transaction Signature One:", tx1);
console.log("Transaction Signature Two:", tx2);
});

it("Test 3 => Sending Signed Transaction", async () => {
const account1 = anchor.web3.Keypair.generate();
const account2 = anchor.web3.Keypair.generate();
const account3 = anchor.web3.Keypair.generate();

const tx = await program.rpc.logMemo(Buffer.from("🐆"), {
accounts: [],
remainingAccounts: [
{ pubkey: account1.publicKey, isWritable: false, isSigner: true },
{ pubkey: account2.publicKey, isWritable: false, isSigner: true },
{ pubkey: account3.publicKey, isWritable: false, isSigner: true },
],
signers: [account1, account2, account3],
});
console.log("Transaction Signature:", tx);
});

it("Test 4 => Sending Unsigned Transaction with a Memo", async () => {
// This test should fail because the transaction is not signed.
const account1 = anchor.web3.Keypair.generate();
const account2 = anchor.web3.Keypair.generate();
const account3 = anchor.web3.Keypair.generate();

assert.rejects(() => {
program.rpc.logMemo(Buffer.from("🐆"), {
accounts: [],
remainingAccounts: [
{ pubkey: account1.publicKey, isWritable: false, isSigner: false },
{ pubkey: account2.publicKey, isWritable: false, isSigner: false },
{ pubkey: account3.publicKey, isWritable: false, isSigner: false },
],
signers: [account1, account2, account3],
});
}, new Error("unknown signer"));

console.log("Test failed successfully :)");
});

it("Test 5 => Sending a transaction with missing signers", async () => {
// This test should fail because the transaction is not signed completely.
const account1 = anchor.web3.Keypair.generate();
const account2 = anchor.web3.Keypair.generate();
const account3 = anchor.web3.Keypair.generate();

assert.rejects(async () => {
await program.rpc.logMemo(Buffer.from("🐆"), {
accounts: [],
remainingAccounts: [
{ pubkey: account1.publicKey, isWritable: false, isSigner: true },
{ pubkey: account2.publicKey, isWritable: false, isSigner: false },
{ pubkey: account3.publicKey, isWritable: false, isSigner: true },
],
signers: [account1, account2, account3],
});
}, new Error("unknown signer"));
console.log("Test failed successfully :)");
});

it("Test 6 => Testing invalid input", async () => {
// This test should fail because the input is invalid.
let invalid_utf8 = Uint8Array.from([
0xf0, 0x9f, 0x90, 0x86, 0xf0, 0x9f, 0xff, 0x86,
]);

assert.rejects(() => {
program.rpc.logMemo(Buffer.from(invalid_utf8));
}, new Error());
console.log("Test failed successfully :)");
});
});