Skip to content

Commit

Permalink
ci: introduce formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Aug 7, 2024
1 parent 9ec1a63 commit b09affd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# assets
C_GREEN=\033[0;32m
C_RED=\033[0;31m
C_BLUE=\033[0;34m
C_END=\033[0m

.PHONY: fmt
fmt:
@echo "$(C_GREEN)# Formatting rust code$(C_END)"
@./scripts/fmt.sh
46 changes: 34 additions & 12 deletions programs/protocol-contracts-solana/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use anchor_lang::system_program;
use anchor_spl::token::{transfer, Token, TokenAccount};
use solana_program::keccak::hash;
use solana_program::secp256k1_recover::secp256k1_recover;
use std::mem::size_of;
use spl_associated_token_account;
use std::mem::size_of;

#[error_code]
pub enum Errors {
Expand All @@ -28,12 +28,15 @@ pub enum Errors {

declare_id!("94U5AHQMKkV5txNJ17QPXWoh474PheGou6cNP2FEuL1d");

Check warning on line 29 in programs/protocol-contracts-solana/src/lib.rs

View workflow job for this annotation

GitHub Actions / ubuntu / stable / typos

"Woh" should be "Who".


#[program]
pub mod gateway {
use super::*;

pub fn initialize(ctx: Context<Initialize>, tss_address:[u8; 20], chain_id: u64) -> Result<()> {
pub fn initialize(
ctx: Context<Initialize>,
tss_address: [u8; 20],
chain_id: u64,
) -> Result<()> {
let initialized_pda = &mut ctx.accounts.pda;
initialized_pda.nonce = 0;
initialized_pda.tss_address = tss_address;
Expand All @@ -45,7 +48,10 @@ pub mod gateway {

pub fn update_tss(ctx: Context<UpdateTss>, tss_address: [u8; 20]) -> Result<()> {
let pda = &mut ctx.accounts.pda;
require!(ctx.accounts.signer.key() == pda.authority, Errors::SignerIsNotAuthority);
require!(
ctx.accounts.signer.key() == pda.authority,
Errors::SignerIsNotAuthority
);
pda.tss_address = tss_address;
Ok(())
}
Expand All @@ -61,12 +67,20 @@ pub mod gateway {
},
);
system_program::transfer(cpi_context, amount)?;
msg!("{:?} deposits {:?} lamports to PDA", ctx.accounts.signer.key(), amount);
msg!(
"{:?} deposits {:?} lamports to PDA",
ctx.accounts.signer.key(),
amount
);

Ok(())
}

pub fn deposit_spl_token(ctx: Context<DepositSplToken>, amount: u64, memo: Vec<u8>) -> Result<()> {
pub fn deposit_spl_token(
ctx: Context<DepositSplToken>,
amount: u64,
memo: Vec<u8>,
) -> Result<()> {
require!(memo.len() >= 20, Errors::MemoLengthTooShort);
require!(memo.len() <= 512, Errors::MemoLengthExceeded);
let token = &ctx.accounts.token_program;
Expand All @@ -77,7 +91,10 @@ pub mod gateway {
&from.mint,
);
// must deposit to the ATA from PDA in order to receive credit
require!(pda_ata == ctx.accounts.to.to_account_info().key(), Errors::DepositToAddressMismatch);
require!(
pda_ata == ctx.accounts.to.to_account_info().key(),
Errors::DepositToAddressMismatch
);

let xfer_ctx = CpiContext::new(
token.to_account_info(),
Expand Down Expand Up @@ -113,7 +130,10 @@ pub mod gateway {
concatenated_buffer.extend_from_slice(&nonce.to_be_bytes());
concatenated_buffer.extend_from_slice(&amount.to_be_bytes());
concatenated_buffer.extend_from_slice(&ctx.accounts.to.key().to_bytes());
require!(message_hash == hash(&concatenated_buffer[..]).to_bytes(), Errors::MessageHashMismatch);
require!(
message_hash == hash(&concatenated_buffer[..]).to_bytes(),
Errors::MessageHashMismatch
);

let address = recover_eth_address(&message_hash, recovery_id, &signature)?; // ethereum address is the last 20 Bytes of the hashed pubkey
msg!("recovered address {:?}", address);
Expand Down Expand Up @@ -150,7 +170,10 @@ pub mod gateway {
concatenated_buffer.extend_from_slice(&nonce.to_be_bytes());
concatenated_buffer.extend_from_slice(&amount.to_be_bytes());
concatenated_buffer.extend_from_slice(&ctx.accounts.to.key().to_bytes());
require!(message_hash == hash(&concatenated_buffer[..]).to_bytes(), Errors::MessageHashMismatch);
require!(
message_hash == hash(&concatenated_buffer[..]).to_bytes(),
Errors::MessageHashMismatch
);

let address = recover_eth_address(&message_hash, recovery_id, &signature)?; // ethereum address is the last 20 Bytes of the hashed pubkey
msg!("recovered address {:?}", address);
Expand Down Expand Up @@ -231,7 +254,7 @@ pub struct DepositSplToken<'info> {
#[account(mut)]
pub from: Account<'info, TokenAccount>, // this must be owned by signer; normally the ATA of signer
#[account(mut)]
pub to: Account<'info, TokenAccount>, // this must be ATA of PDA
pub to: Account<'info, TokenAccount>, // this must be ATA of PDA
}

#[derive(Accounts)]
Expand Down Expand Up @@ -278,7 +301,6 @@ pub struct Pda {
chain_id: u64,
}


#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -295,4 +317,4 @@ mod tests {
let message_hash = hash(&concatenated_buffer[..]).to_bytes();
println!("message_hash: {:?}", message_hash);
}
}
}
23 changes: 23 additions & 0 deletions scripts/fmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

# Exit on any error
set -e

if ! command -v brew &> /dev/null
then
echo "brew is required to run the script."
exit 1
fi

if ! command -v rustfmt &> /dev/null
then
echo "rustfmt could not be found, installing..."
brew install rustfmt
fi

cargo fmt
if [[ $? == 0 ]] ; then
echo "Code is formatted!"
else
echo "An error ocurred during formatting"

Check warning on line 22 in scripts/fmt.sh

View workflow job for this annotation

GitHub Actions / ubuntu / stable / typos

"ocurred" should be "occurred".
fi

0 comments on commit b09affd

Please sign in to comment.