Skip to content

Qa/test receiver #889

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions chains/solana/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ eslint-report.json
override*.toml
# go work files
go.work*

devnet.config.yaml
5 changes: 4 additions & 1 deletion chains/solana/contracts/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ test_token_pool = "JuCcZ4smxAYv9QHJ36jshA7pA3FuQ3vQeWLUeAtZduJ"
timelock = "DoajfR5tK24xVw51fWcawUZWhAXD8yrBJVacc13neVQA"
ping_pong_demo = "PPbZmYFf5SPAM9Jhm9mNmYoCwT7icPYVKAfJoMCQovU"

[programs.devnet]
example_ccip_sender = "2zzXhZX96Z6VtowsdH9chgmZehN31b3AQ6448oWtWqQB"

[registry]
url = "https://anchor.projectserum.com"

[provider]
cluster = "Localnet"
cluster = "devnet"
wallet = "id.json"
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use solana_program::{

use ccip_router::messages::{GetFeeResult, SVM2AnyMessage, SVMTokenAmount};

declare_id!("4LfBQWYaU6zQZbDyYjX8pbY4qjzrhoumUFYZEZEqMNhJ");
declare_id!("9tuddMwvfb2U1j7HmQVA5mqYYofgoWNYb2eNf4FTtUQD");

#[cfg(target_os = "solana")]
#[global_allocator]
Expand Down Expand Up @@ -77,6 +77,7 @@ pub mod example_ccip_sender {
acc.mint,
acc.from_ata,
acc.self_ata,
&ctx.accounts.authority.to_account_info(),
&ctx.accounts.ccip_sender.to_account_info(),
&acc.ccip_router_pool_signer.to_account_info(),
seeds,
Expand Down Expand Up @@ -142,6 +143,7 @@ pub mod example_ccip_sender {
&ctx.accounts.ccip_fee_token_mint.to_account_info(),
&ctx.accounts.authority_fee_token_ata.to_account_info(),
&ctx.accounts.ccip_fee_token_user_ata.to_account_info(),
&ctx.accounts.authority.to_account_info(),
&ctx.accounts.ccip_sender.to_account_info(),
&ctx.accounts.ccip_fee_billing_signer.to_account_info(),
seeds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ use anchor_spl::{
token_interface,
};
use ccip_router::messages::SVMTokenAmount;
use solana_program::{address_lookup_table::state::AddressLookupTable, program::invoke_signed};
use solana_program::{
address_lookup_table::state::AddressLookupTable,
program::{invoke, invoke_signed},
};

#[allow(clippy::too_many_arguments)]
pub fn transfer_to_self_and_approve<'a>(
token_program: &AccountInfo<'a>,
token_mint: &AccountInfo<'a>,
from_ata: &AccountInfo<'a>,
self_ata: &AccountInfo<'a>,
user_signer: &AccountInfo<'a>,
self_signer: &AccountInfo<'a>,
to_signer: &AccountInfo<'a>,
seeds: &[&[u8]],
Expand All @@ -23,27 +27,27 @@ pub fn transfer_to_self_and_approve<'a>(
) -> Result<()> {
// transfer to this program
{
// build transfer from token_2022 SDK, but set expected token program ID
let mut ix = transfer_checked(
&spl_token_2022::ID,
&from_ata.key(),
&token_mint.key(),
&self_ata.key(),
&self_signer.key(),
&user_signer.key(),
&[],
amount,
decimals,
)?;
ix.program_id = token_program.key(); // allow any custom token program
invoke_signed(
ix.program_id = token_program.key();

invoke(
&ix,
&[
from_ata.clone(),
token_mint.clone(),
self_ata.clone(),
self_signer.clone(),
user_signer.clone(),
token_program.clone(),
],
&[seeds],
)?;
}

Expand All @@ -59,14 +63,16 @@ pub fn transfer_to_self_and_approve<'a>(
amount,
decimals,
)?;
ix.program_id = token_program.key(); // allow any custom token program
ix.program_id = token_program.key();

invoke_signed(
&ix,
&[
self_ata.clone(),
token_mint.clone(),
to_signer.clone(),
self_signer.clone(),
token_program.clone(),
],
&[seeds],
)?;
Expand Down
56 changes: 49 additions & 7 deletions chains/solana/contracts/programs/test-ccip-receiver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,37 @@ declare_id!("EvhgrPhTDt4LcSPS2kfJgH6T6XWZ6wT3X9ncDGLT1vui");
#[program]
pub mod test_ccip_receiver {
use solana_program::instruction::Instruction;
use solana_program::keccak::hash;
use solana_program::program::invoke_signed;

use super::*;

/// The initialization is responsibility of the External User, CCIP is not handling initialization of Accounts
pub fn initialize(ctx: Context<Initialize>, router: Pubkey) -> Result<()> {
ctx.accounts.counter.value = 0;
ctx.accounts.counter.reject_all = false;
ctx.accounts.counter.behavior = Behavior::Normal;
ctx.accounts
.counter
.state
.init(ctx.accounts.authority.key(), router)
}

pub fn set_reject_all(ctx: Context<SetRejectAll>, reject_all: bool) -> Result<()> {
ctx.accounts.counter.reject_all = reject_all;
pub fn set_behavior(ctx: Context<SetConfig>, behavior: Behavior) -> Result<()> {
ctx.accounts.counter.behavior = behavior;
Ok(())
}

pub fn transfer_ownership(ctx: Context<SetConfig>, new_owner: Pubkey) -> Result<()> {
msg!("Transferring ownership to {:?}", new_owner);
ctx.accounts.counter.state.owner = new_owner;
Ok(())
}

pub fn echo(_ctx: Context<Empty>, msg: String) -> Result<String> {
msg!("Called `echo` with message {:?}", msg);
Ok(msg)
}

/// This function is called by the CCIP Router to execute the CCIP message.
/// The method name needs to be ccip_receive with Anchor encoding,
/// if not using Anchor the discriminator needs to be [0x0b, 0xf4, 0x09, 0xf9, 0x2c, 0x53, 0x2f, 0xf5]
Expand All @@ -39,11 +51,20 @@ pub mod test_ccip_receiver {
pub fn ccip_receive(ctx: Context<CcipReceive>, message: Any2SVMMessage) -> Result<()> {
msg!("Called `ccip_receive` with message {:?}", message);

require!(
!ctx.accounts.counter.reject_all,
require_neq!(
ctx.accounts.counter.behavior,
Behavior::RejectAll,
CcipTestReceiverError::RejectAll
);

if ctx.accounts.counter.behavior == Behavior::ExtraCUs {
// Extra CUs for testing
let mut h = hash(b"something!");
for _ in 1..5000 {
h = hash(&h.0);
}
}

let counter = &mut ctx.accounts.counter;
let (incremented, wrapped_around) = counter.value.overflowing_add(1);
counter.value = incremented;
Expand Down Expand Up @@ -138,7 +159,7 @@ pub struct Initialize<'info> {
}

#[derive(Accounts)]
pub struct SetRejectAll<'info> {
pub struct SetConfig<'info> {
#[account(
mut,
seeds = [b"counter"],
Expand Down Expand Up @@ -212,11 +233,32 @@ pub struct TokenPool<'info> {
// [...] extra passed accounts
}

#[derive(Accounts)]
pub struct Empty {}

#[repr(u8)]
#[derive(InitSpace, AnchorSerialize, AnchorDeserialize, Clone, Copy, PartialEq, Debug)]
pub enum Behavior {
Normal = 0,
RejectAll = 1,
ExtraCUs = 2,
}

impl std::fmt::Display for Behavior {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Behavior::Normal => write!(f, "Normal"),
Behavior::RejectAll => write!(f, "RejectAll"),
Behavior::ExtraCUs => write!(f, "ExtraCUs"),
}
}
}

#[account]
#[derive(InitSpace, Debug)]
pub struct Counter {
pub value: u64,
pub reject_all: bool,
pub behavior: Behavior,
pub state: BaseState,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,5 +472,8 @@
"name": "InvalidProposedOwner",
"msg": "Proposed owner is invalid"
}
]
],
"metadata": {
"address": "9tuddMwvfb2U1j7HmQVA5mqYYofgoWNYb2eNf4FTtUQD"
}
}
63 changes: 58 additions & 5 deletions chains/solana/contracts/target/idl/test_ccip_receiver.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
]
},
{
"name": "setRejectAll",
"name": "setBehavior",
"accounts": [
{
"name": "counter",
Expand All @@ -56,11 +56,45 @@
],
"args": [
{
"name": "rejectAll",
"type": "bool"
"name": "behavior",
"type": {
"defined": "Behavior"
}
}
]
},
{
"name": "transferOwnership",
"accounts": [
{
"name": "counter",
"isMut": true,
"isSigner": false
},
{
"name": "authority",
"isMut": false,
"isSigner": true
}
],
"args": [
{
"name": "newOwner",
"type": "publicKey"
}
]
},
{
"name": "echo",
"accounts": [],
"args": [
{
"name": "msg",
"type": "string"
}
],
"returns": "string"
},
{
"name": "ccipReceive",
"docs": [
Expand Down Expand Up @@ -198,8 +232,10 @@
"type": "u64"
},
{
"name": "rejectAll",
"type": "bool"
"name": "behavior",
"type": {
"defined": "Behavior"
}
},
{
"name": "state",
Expand Down Expand Up @@ -298,6 +334,23 @@
}
]
}
},
{
"name": "Behavior",
"type": {
"kind": "enum",
"variants": [
{
"name": "Normal"
},
{
"name": "RejectAll"
},
{
"name": "ExtraCUs"
}
]
}
}
],
"errors": [
Expand Down
Loading