Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: chrismaree <[email protected]>
  • Loading branch information
chrismaree committed Sep 23, 2024
2 parents 916542d + 2d66aba commit 92a6262
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 103 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"lint-solidity": "yarn solhint ./contracts/**/*.sol",
"lint-js": "yarn prettier --list-different && yarn lint-rust",
"lint-rust": "cargo fmt --all -- --check && cargo clippy",
"lint-fix": "yarn prettier --write",
"lint-fix": "yarn prettier --write && cargo fmt --all",
"prettier": "prettier .",
"clean-fast": "for dir in node_modules cache cache-zk artifacts artifacts-zk dist typechain; do mv \"${dir}\" \"_${dir}\"; rm -rf \"_${dir}\" &; done",
"clean": "rm -rf node_modules cache cache-zk artifacts artifacts-zk dist typechain",
Expand Down
107 changes: 107 additions & 0 deletions programs/svm-spoke/src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use anchor_lang::prelude::*;

// Admin events
#[event]
pub struct SetXDomainAdmin {
pub new_admin: Pubkey,
}

#[event]
pub struct PausedDeposits {
pub is_paused: bool,
}

#[event]
pub struct PausedFills {
pub is_paused: bool,
}

#[event]
pub struct EnabledDepositRoute {
pub origin_token: Pubkey,
pub destination_chain_id: u64,
pub enabled: bool,
}

// Deposit events
#[event]
pub struct V3FundsDeposited {
pub input_token: Pubkey,
pub output_token: Pubkey,
pub input_amount: u64,
pub output_amount: u64,
pub destination_chain_id: u64,
pub deposit_id: u32,
pub quote_timestamp: u32,
pub fill_deadline: u32,
pub exclusivity_deadline: u32,
pub depositor: Pubkey,
pub recipient: Pubkey,
pub exclusive_relayer: Pubkey,
pub message: Vec<u8>,
}

// Fill events
#[derive(AnchorSerialize, AnchorDeserialize, Clone, PartialEq)]
pub enum FillType {
FastFill,
ReplacedSlowFill,
SlowFill,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct V3RelayExecutionEventInfo {
pub updated_recipient: Pubkey,
pub updated_message: Vec<u8>,
pub updated_output_amount: u64,
pub fill_type: FillType,
}

#[event]
pub struct FilledV3Relay {
pub input_token: Pubkey,
pub output_token: Pubkey,
pub input_amount: u64,
pub output_amount: u64,
pub repayment_chain_id: u64,
pub origin_chain_id: u64,
pub deposit_id: u32,
pub fill_deadline: u32,
pub exclusivity_deadline: u32,
pub exclusive_relayer: Pubkey,
pub relayer: Pubkey,
pub depositor: Pubkey,
pub recipient: Pubkey,
pub message: Vec<u8>,
pub relay_execution_info: V3RelayExecutionEventInfo,
}

// Slow fill events
#[event]
pub struct RequestedV3SlowFill {
pub input_token: Pubkey,
pub output_token: Pubkey,
pub input_amount: u64,
pub output_amount: u64,
pub origin_chain_id: u64,
pub deposit_id: u32,
pub fill_deadline: u32,
pub exclusivity_deadline: u32,
pub exclusive_relayer: Pubkey,
pub depositor: Pubkey,
pub recipient: Pubkey,
pub message: Vec<u8>,
}

// Relayer refund events
#[event]
pub struct ExecutedRelayerRefundRoot {
pub amount_to_return: u64,
pub chain_id: u64,
pub refund_amounts: Vec<u64>,
pub root_bundle_id: u32,
pub leaf_id: u32,
pub l2_token_address: Pubkey,
pub refund_addresses: Vec<Pubkey>,
pub caller: Pubkey,
}
24 changes: 2 additions & 22 deletions programs/svm-spoke/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::constraints::is_local_or_remote_owner;

use crate::{
error::CustomError,
event::{EnabledDepositRoute, PausedDeposits, PausedFills, SetXDomainAdmin},
state::{RootBundle, Route, State},
};

Expand All @@ -32,7 +33,7 @@ pub struct Initialize<'info> {
pub fn initialize(
ctx: Context<Initialize>,
seed: u64,
initial_number_of_deposits: u64,
initial_number_of_deposits: u32,
chain_id: u64, // Across definition of chainId for Solana.
remote_domain: u32, // CCTP domain for Mainnet Ethereum.
cross_domain_admin: Pubkey, // HubPool on Mainnet Ethereum.
Expand Down Expand Up @@ -243,24 +244,3 @@ pub fn relay_root_bundle(
state.root_bundle_id += 1;
Ok(())
}
#[event]
pub struct SetXDomainAdmin {
pub new_admin: Pubkey,
}

#[event]
pub struct PausedDeposits {
pub is_paused: bool,
}

#[event]
pub struct PausedFills {
pub is_paused: bool,
}

#[event]
pub struct EnabledDepositRoute {
pub origin_token: Pubkey,
pub destination_chain_id: u64,
pub enabled: bool,
}
14 changes: 14 additions & 0 deletions programs/svm-spoke/src/instructions/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anchor_lang::solana_program::keccak;
use crate::{
constants::DISCRIMINATOR_SIZE,
error::CustomError,
event::ExecutedRelayerRefundRoot,
state::{RootBundle, State, TransferLiability},
utils::{is_claimed, set_claimed, verify_merkle_proof},
};
Expand All @@ -12,6 +13,7 @@ use anchor_spl::token_interface::{
transfer_checked, Mint, TokenAccount, TokenInterface, TransferChecked,
};

#[event_cpi]
#[derive(Accounts)]
#[instruction(root_bundle_id: u32, relayer_refund_leaf: RelayerRefundLeaf)]
pub struct ExecuteRelayerRefundLeaf<'info> {
Expand Down Expand Up @@ -162,5 +164,17 @@ pub fn execute_relayer_refund_leaf<'info>(
ctx.accounts.transfer_liability.pending_to_hub_pool += relayer_refund_leaf.amount_to_return;
}

// Emit the ExecutedRelayerRefundRoot event
emit_cpi!(ExecutedRelayerRefundRoot {
amount_to_return: relayer_refund_leaf.amount_to_return,
chain_id: relayer_refund_leaf.chain_id,
refund_amounts: relayer_refund_leaf.refund_amounts,
root_bundle_id,
leaf_id: relayer_refund_leaf.leaf_id,
l2_token_address: ctx.accounts.mint.key(),
refund_addresses: relayer_refund_leaf.refund_accounts,
caller: ctx.accounts.signer.key(),
});

Ok(())
}
18 changes: 1 addition & 17 deletions programs/svm-spoke/src/instructions/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anchor_lang::prelude::*;

use crate::{
error::CustomError,
event::V3FundsDeposited,
state::{Route, State},
};

Expand Down Expand Up @@ -136,20 +137,3 @@ pub fn deposit_v3(

Ok(())
}

#[event]
pub struct V3FundsDeposited {
pub input_token: Pubkey,
pub output_token: Pubkey,
pub input_amount: u64,
pub output_amount: u64,
pub destination_chain_id: u64,
pub deposit_id: u64,
pub quote_timestamp: u32,
pub fill_deadline: u32,
pub exclusivity_deadline: u32,
pub depositor: Pubkey,
pub recipient: Pubkey,
pub exclusive_relayer: Pubkey,
pub message: Vec<u8>,
}
34 changes: 1 addition & 33 deletions programs/svm-spoke/src/instructions/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
constants::DISCRIMINATOR_SIZE,
constraints::is_relay_hash_valid,
error::CustomError,
event::{FillType, FilledV3Relay, V3RelayExecutionEventInfo},
state::{FillStatus, FillStatusAccount, State},
};

Expand Down Expand Up @@ -91,13 +92,6 @@ pub struct V3RelayData {
pub message: Vec<u8>,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, PartialEq)]
pub enum FillType {
FastFill,
ReplacedSlowFill,
SlowFill,
}

pub fn fill_v3_relay(
ctx: Context<FillV3Relay>,
relay_hash: [u8; 32], // include in props, while not using it, to enable us to access it from the #Instruction Attribute within the accounts. This enables us to pass in the relay_hash PDA.
Expand Down Expand Up @@ -238,29 +232,3 @@ pub fn close_fill_pda(
}

// Events.
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct V3RelayExecutionEventInfo {
pub updated_recipient: Pubkey,
pub updated_message: Vec<u8>,
pub updated_output_amount: u64,
pub fill_type: FillType,
}

#[event]
pub struct FilledV3Relay {
pub input_token: Pubkey,
pub output_token: Pubkey,
pub input_amount: u64,
pub output_amount: u64,
pub repayment_chain_id: u64,
pub origin_chain_id: u64,
pub deposit_id: u32,
pub fill_deadline: u32,
pub exclusivity_deadline: u32,
pub exclusive_relayer: Pubkey,
pub relayer: Pubkey,
pub depositor: Pubkey,
pub recipient: Pubkey,
pub message: Vec<u8>,
pub relay_execution_info: V3RelayExecutionEventInfo,
}
20 changes: 2 additions & 18 deletions programs/svm-spoke/src/instructions/slow_fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use crate::{
};

// TODO: We can likely move some of the common exports to better locations. we are pulling a lot of these from fill.rs
use crate::{FillType, FilledV3Relay, V3RelayData, V3RelayExecutionEventInfo};
use crate::event::{FillType, FilledV3Relay, RequestedV3SlowFill, V3RelayExecutionEventInfo};
use crate::V3RelayData; // Pulled type definition from fill.rs.

#[event_cpi]
#[derive(Accounts)]
Expand Down Expand Up @@ -273,20 +274,3 @@ pub fn execute_v3_slow_relay_leaf(

Ok(())
}

// Events.
#[event]
pub struct RequestedV3SlowFill {
pub input_token: Pubkey,
pub output_token: Pubkey,
pub input_amount: u64,
pub output_amount: u64,
pub origin_chain_id: u64,
pub deposit_id: u32,
pub fill_deadline: u32,
pub exclusivity_deadline: u32,
pub exclusive_relayer: Pubkey,
pub depositor: Pubkey,
pub recipient: Pubkey,
pub message: Vec<u8>,
}
3 changes: 2 additions & 1 deletion programs/svm-spoke/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare_program!(token_messenger_minter);
pub mod constants;
mod constraints;
pub mod error;
pub mod event;
mod instructions;
mod state;
pub mod utils;
Expand All @@ -24,7 +25,7 @@ pub mod svm_spoke {
pub fn initialize(
ctx: Context<Initialize>,
seed: u64,
initial_number_of_deposits: u64,
initial_number_of_deposits: u32,
chain_id: u64,
remote_domain: u32,
cross_domain_admin: Pubkey,
Expand Down
2 changes: 1 addition & 1 deletion programs/svm-spoke/src/state/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct State {
pub paused_fills: bool,
pub owner: Pubkey,
pub seed: u64, // Add a seed to the state to enable multiple deployments.
pub number_of_deposits: u64,
pub number_of_deposits: u32,
pub chain_id: u64, // Across definition of chainId for Solana.
pub current_time: u32, // Only used in testable mode, else set to 0 on mainnet.
pub remote_domain: u32, // CCTP domain for Mainnet Ethereum.
Expand Down
22 changes: 20 additions & 2 deletions test/svm/SvmSpoke.Bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import {
RelayerRefundLeaf,
RelayerRefundLeafSolana,
RelayerRefundLeafType,
readProgramEvents,
} from "./utils";

const { provider, program, owner, initializeState, connection, chainId } = common;
const { provider, program, owner, initializeState, connection, chainId, assertSE } = common;

describe("svm_spoke.bundle", () => {
anchor.setProvider(provider);
Expand Down Expand Up @@ -139,7 +140,7 @@ describe("svm_spoke.bundle", () => {
isSolana: true,
leafId: new BN(0),
chainId: chainId,
amountToReturn: new BN(0),
amountToReturn: new BN(69420),
mintPublicKey: mint,
refundAccounts: [relayerTA, relayerTB],
refundAmounts: [relayerARefund, relayerBRefund],
Expand Down Expand Up @@ -191,6 +192,23 @@ describe("svm_spoke.bundle", () => {
.remainingAccounts(remainingAccounts)
.rpc();

// Verify the ExecutedRelayerRefundRoot event
await new Promise((resolve) => setTimeout(resolve, 500)); // Wait for event processing
let events = await readProgramEvents(connection, program);
let event = events.find((event) => event.name === "executedRelayerRefundRoot").data;

// Remove the expectedValues object and use direct assertions
assertSE(event.amountToReturn, relayerRefundLeaves[0].amountToReturn, "amountToReturn should match");
assertSE(event.chainId, chainId, "chainId should match");
assertSE(event.refundAmounts[0], relayerARefund, "Relayer A refund amount should match");
assertSE(event.refundAmounts[1], relayerBRefund, "Relayer B refund amount should match");
assertSE(event.rootBundleId, stateAccountData.rootBundleId, "rootBundleId should match");
assertSE(event.leafId, leaf.leafId, "leafId should match");
assertSE(event.l2TokenAddress, mint, "l2TokenAddress should match");
assertSE(event.refundAddresses[0], relayerTA, "Relayer A address should match");
assertSE(event.refundAddresses[1], relayerTB, "Relayer B address should match");
assertSE(event.caller, owner, "caller should match");

const fVaultBal = (await connection.getTokenAccountBalance(vault)).value.amount;
const fRelayerABal = (await connection.getTokenAccountBalance(relayerTA)).value.amount;
const fRelayerBBal = (await connection.getTokenAccountBalance(relayerTB)).value.amount;
Expand Down
2 changes: 1 addition & 1 deletion test/svm/SvmSpoke.Deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
getAccount,
} from "@solana/spl-token";
import { PublicKey, Keypair } from "@solana/web3.js";
import { readProgramEvents } from "../../src/SvmUtils";
import { common } from "./SvmSpoke.common";
import { readProgramEvents } from "./utils";
const { provider, connection, program, owner, seedBalance, initializeState, depositData } = common;
const { createRoutePda, getVaultAta, assertSE, assert, getCurrentTime, depositQuoteTimeBuffer, fillDeadlineBuffer } =
common;
Expand Down
2 changes: 1 addition & 1 deletion test/svm/SvmSpoke.Fill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe("svm_spoke.fill", () => {
});

it("Allows fill by non-exclusive relayer after exclusivity deadline", async () => {
updateRelayData({ ...relayData, exclusivityDeadline: new BN(Math.floor(Date.now() / 1000) - 30) });
updateRelayData({ ...relayData, exclusivityDeadline: new BN(Math.floor(Date.now() / 1000) - 100) });

accounts.signer = otherRelayer.publicKey;
accounts.relayer = otherRelayer.publicKey;
Expand Down
Loading

0 comments on commit 92a6262

Please sign in to comment.