Skip to content

Commit

Permalink
feat(transaction_execute): implement instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
vovacodes committed Jan 8, 2023
1 parent 55bfeb6 commit 9b88ea2
Show file tree
Hide file tree
Showing 17 changed files with 1,526 additions and 39 deletions.
973 changes: 964 additions & 9 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions programs/multisig/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ default = []

[dependencies]
anchor-lang = "=0.26.0"
solana-address-lookup-table-program = "=1.14.11"
12 changes: 8 additions & 4 deletions programs/multisig/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ pub enum MultisigError {
NotAMember,
#[msg("TransactionMessage is malformed.")]
InvalidTransactionMessage,
#[msg("Transaction is stale.")]
#[msg("Transaction is stale")]
StaleTransaction,
#[msg("Invalid transaction status.")]
#[msg("Invalid transaction status")]
InvalidTransactionStatus,
#[msg("Transaction does not belong to the multisig.")]
#[msg("Transaction does not belong to the multisig")]
TransactionNotForMultisig,
#[msg("Member already approved the transaction.")]
#[msg("Member already approved the transaction")]
AlreadyApproved,
#[msg("Wrong number of accounts provided")]
InvalidNumberOfAccounts,
#[msg("Invalid account provided")]
InvalidAccount,
}
17 changes: 13 additions & 4 deletions programs/multisig/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anchor_lang::prelude::*;

/// New multisig account is created.
#[event]
pub struct MultisigCreatedEvent {
pub struct MultisigCreated {
/// The multisig account.
pub multisig: Pubkey,
#[index]
Expand All @@ -22,7 +22,7 @@ pub enum ConfigUpdateType {

/// Multisig config is updated.
#[event]
pub struct MultisigConfigUpdatedEvent {
pub struct MultisigConfigUpdated {
/// The multisig account.
pub multisig: Pubkey,
#[index]
Expand All @@ -35,7 +35,7 @@ pub struct MultisigConfigUpdatedEvent {

/// New multisig transaction account is created.
#[event]
pub struct TransactionCreatedEvent {
pub struct TransactionCreated {
/// The multisig account.
pub multisig: Pubkey,
/// The transaction account.
Expand All @@ -47,7 +47,7 @@ pub struct TransactionCreatedEvent {

/// New multisig transaction account is created.
#[event]
pub struct TransactionApprovedEvent {
pub struct TransactionApproved {
/// The multisig account.
pub multisig: Pubkey,
/// The transaction account.
Expand All @@ -56,3 +56,12 @@ pub struct TransactionApprovedEvent {
/// Memo that was added by the creator.
pub memo: Option<String>,
}

/// New multisig transaction account is created.
#[event]
pub struct TransactionExecuted {
/// The multisig account.
pub multisig: Pubkey,
/// The transaction account.
pub transaction: Pubkey,
}
2 changes: 2 additions & 0 deletions programs/multisig/src/instructions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
pub use multisig_config::*;
pub use multisig_create::*;
pub use transaction_create::*;
pub use transaction_execute::*;
pub use transaction_vote::*;

mod multisig_config;
mod multisig_create;
mod transaction_create;
mod transaction_execute;
mod transaction_vote;
2 changes: 1 addition & 1 deletion programs/multisig/src/instructions/multisig_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl MultisigCreate<'_> {
multisig.create_key = args.create_key;
multisig.bump = *ctx.bumps.get("multisig").unwrap();

emit!(MultisigCreatedEvent {
emit!(MultisigCreated {
multisig: ctx.accounts.multisig.to_account_info().key.clone(),
memo: args.memo,
});
Expand Down
2 changes: 1 addition & 1 deletion programs/multisig/src/instructions/transaction_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl TransactionCreate<'_> {
// Updated last transaction index in the multisig account.
multisig.transaction_index = transaction_index;

emit!(TransactionCreatedEvent {
emit!(TransactionCreated {
multisig: multisig_key,
transaction: transaction.key(),
memo: args.memo,
Expand Down
96 changes: 96 additions & 0 deletions programs/multisig/src/instructions/transaction_execute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use anchor_lang::prelude::*;
use anchor_lang::solana_program::program::invoke_signed;

use crate::errors::*;
use crate::events::*;
use crate::state::*;
use crate::utils::*;

#[derive(Accounts)]
pub struct TransactionExecute<'info> {
#[account(
mut,
seeds = [SEED_PREFIX, multisig.create_key.as_ref(), SEED_MULTISIG],
bump = multisig.bump,
)]
pub multisig: Box<Account<'info, Multisig>>,

#[account(
mut,
seeds = [
SEED_PREFIX,
multisig.key().as_ref(),
&transaction.transaction_index.to_le_bytes(),
SEED_TRANSACTION
],
bump = transaction.bump,
constraint = transaction.status == TransactionStatus::ExecuteReady @ MultisigError::InvalidTransactionStatus,
constraint = transaction.multisig == multisig.key() @ MultisigError::TransactionNotForMultisig
)]
pub transaction: Account<'info, MultisigTransaction>,

#[account(
mut,
constraint = multisig.is_member(member.key()).is_some() || multisig.allow_external_execute @ MultisigError::NotAMember,
constraint = multisig.member_has_permission(member.key(), Permission::Execute) || multisig.allow_external_execute @ MultisigError::Unauthorized,
)]
pub member: Signer<'info>,
// `remaining_accounts` must include the following accounts in the exact order:
// 1. AddressLookupTable accounts in the order they appear in `message.address_table_lookups`.
// 2. Accounts in the order they appear in `message.account_keys`.
// 3. Accounts in the order they appear in `message.address_table_lookups`.
}

impl TransactionExecute<'_> {
/// Execute the multisig transaction.
/// The transaction must be `ExecuteReady`.
pub fn transaction_execute(ctx: Context<Self>) -> Result<()> {
let multisig = &mut ctx.accounts.multisig;
let transaction = &mut ctx.accounts.transaction;

let multisig_key = multisig.key();
let authority_seeds = &[
SEED_PREFIX,
multisig_key.as_ref(),
&transaction.authority_index.to_le_bytes(),
SEED_AUTHORITY,
&[transaction.authority_bump],
];

let authority_pubkey =
Pubkey::create_program_address(authority_seeds, ctx.program_id).unwrap();

let transaction_message = &transaction.message;
let num_lookups = transaction_message.address_table_lookups.len();
let message_account_infos = ctx
.remaining_accounts
.get(num_lookups..)
.ok_or(MultisigError::InvalidNumberOfAccounts)?;
let address_lookup_table_account_infos = ctx
.remaining_accounts
.get(..num_lookups)
.ok_or(MultisigError::InvalidNumberOfAccounts)?;

let executable_message = ExecutableTransactionMessage::new_validated(
transaction_message,
message_account_infos,
address_lookup_table_account_infos,
&authority_pubkey,
)?;

// Execute the transaction instructions one-by-one.
for (ix, account_infos) in executable_message.to_instructions_and_accounts().iter() {
invoke_signed(ix, account_infos, &[authority_seeds])?;
}

// Mark it as executed
transaction.status = TransactionStatus::Executed;

emit!(TransactionExecuted {
multisig: multisig_key,
transaction: transaction.key(),
});

Ok(())
}
}
6 changes: 3 additions & 3 deletions programs/multisig/src/instructions/transaction_vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct TransactionVote<'info> {
SEED_PREFIX,
multisig.key().as_ref(),
&transaction.transaction_index.to_le_bytes(),
b"transaction"
SEED_TRANSACTION
],
bump = transaction.bump,
constraint = transaction.status == TransactionStatus::Active @ MultisigError::InvalidTransactionStatus,
Expand All @@ -35,7 +35,7 @@ pub struct TransactionVote<'info> {
#[account(
mut,
constraint = multisig.is_member(member.key()).is_some() @ MultisigError::NotAMember,
constraint = multisig.member_has_permission(member.key(), Permission::Vote) @MultisigError::Unauthorized,
constraint = multisig.member_has_permission(member.key(), Permission::Vote) @ MultisigError::Unauthorized,
)]
pub member: Signer<'info>,
}
Expand Down Expand Up @@ -65,7 +65,7 @@ impl TransactionVote<'_> {
transaction.status = TransactionStatus::ExecuteReady;
}

emit!(TransactionApprovedEvent {
emit!(TransactionApproved {
multisig: multisig.key(),
transaction: transaction.key(),
memo: args.memo,
Expand Down
6 changes: 6 additions & 0 deletions programs/multisig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ pub mod multisig {
TransactionVote::transaction_approve(ctx, args)
}

/// Execute the multisig transaction.
/// The transaction must be `ExecuteReady`.
pub fn transaction_execute(ctx: Context<TransactionExecute>) -> Result<()> {
TransactionExecute::transaction_execute(ctx)
}

// // instruction to remove a member/key from the multisig
// pub fn remove_member(ctx: Context<MsAuth>, old_member: Pubkey) -> Result<()> {
// // if there is only one key in this multisig, reject the removal
Expand Down
2 changes: 1 addition & 1 deletion programs/multisig/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Multisig {
) {
self.stale_transaction_index = self.transaction_index;

emit!(MultisigConfigUpdatedEvent {
emit!(MultisigConfigUpdated {
multisig: multisig_address,
update,
memo
Expand Down
Loading

0 comments on commit 9b88ea2

Please sign in to comment.