Skip to content

Commit ffea17c

Browse files
authored
feat: withdraw and call spl (#77)
1 parent e609151 commit ffea17c

File tree

9 files changed

+1414
-84
lines changed

9 files changed

+1414
-84
lines changed

Anchor.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ skip-lint = false
66

77
[programs.localnet]
88
connected = "4xEw862A2SEwMjofPkUyd4NEekmVJKJsdHkK3UkAtDrc"
9+
connected_spl = "8iUjRRhUCn8BjrvsWPfj8mguTe9L81ES4oAUApiF8JFC"
910
gateway = "ZETAjseVjuFsxdRxo6MmTCvqFwb3ZHUx56Co3vCmGis"
1011

1112
[registry]

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go-idl/generated/gateway.go

Lines changed: 99 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

programs/connected/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ pub mod connected {
3131
pda.sub_lamports(amount / 2)?;
3232
ctx.accounts.random_wallet.add_lamports(amount / 2)?;
3333

34+
// Check if the message is "revert" and return an error if so
35+
if pda.last_message == "revert" {
36+
msg!("Reverting transaction due to 'revert' message.");
37+
return Err(ErrorCode::RevertMessage.into());
38+
}
39+
3440
msg!(
3541
"On call executed with amount {}, sender {:?} and message {}",
3642
amount,
@@ -75,4 +81,7 @@ pub struct Pda {
7581
pub enum ErrorCode {
7682
#[msg("The data provided could not be converted to a valid UTF-8 string.")]
7783
InvalidDataFormat,
84+
85+
#[msg("Revert message detected. Transaction execution halted.")]
86+
RevertMessage,
7887
}

programs/connectedSPL/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "connectedSPL"
3+
version = "0.1.0"
4+
description = "Test program used for testing withdraw and call SPL feature"
5+
edition = "2021"
6+
7+
[lib]
8+
crate-type = ["cdylib", "lib"]
9+
name = "connected_spl"
10+
11+
[features]
12+
default = []
13+
cpi = ["no-entrypoint"]
14+
no-entrypoint = []
15+
no-idl = []
16+
no-log-ix-name = []
17+
idl-build = ["anchor-lang/idl-build"]
18+
19+
[dependencies]
20+
anchor-lang = { version = "=0.30.0" }
21+
anchor-spl = { version = "=0.30.0", features = ["idl-build"] }
22+
spl-associated-token-account = "3.0.2"
23+
solana-program = "=1.18.15"

programs/connectedSPL/Xargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.bpfel-unknown-unknown.dependencies.std]
2+
features = []

programs/connectedSPL/src/lib.rs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
use anchor_lang::prelude::*;
2+
use anchor_spl::token::{transfer_checked, Mint, Token, TokenAccount};
3+
use std::mem::size_of;
4+
5+
declare_id!("8iUjRRhUCn8BjrvsWPfj8mguTe9L81ES4oAUApiF8JFC");
6+
7+
// NOTE: this is just example contract that can be called from gateway in execute_spl_token function for testing withdraw and call spl
8+
#[program]
9+
pub mod connected_spl {
10+
use super::*;
11+
12+
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
13+
Ok(())
14+
}
15+
16+
pub fn on_call(
17+
ctx: Context<OnCall>,
18+
amount: u64,
19+
sender: [u8; 20],
20+
data: Vec<u8>,
21+
) -> Result<()> {
22+
let pda = &mut ctx.accounts.pda;
23+
24+
// Store the sender's public key
25+
pda.last_sender = sender;
26+
27+
// Convert data to a string and store it
28+
let message = String::from_utf8(data).map_err(|_| ErrorCode::InvalidDataFormat)?;
29+
pda.last_message = message;
30+
31+
// Transfer some portion of tokens transferred from gateway to another account
32+
let token = &ctx.accounts.token_program;
33+
let signer_seeds: &[&[&[u8]]] = &[&[b"connectedSPL", &[ctx.bumps.pda]]];
34+
35+
let xfer_ctx = CpiContext::new_with_signer(
36+
token.to_account_info(),
37+
anchor_spl::token::TransferChecked {
38+
from: ctx.accounts.pda_ata.to_account_info(),
39+
mint: ctx.accounts.mint_account.to_account_info(),
40+
to: ctx.accounts.random_wallet_ata.to_account_info(),
41+
authority: pda.to_account_info(),
42+
},
43+
signer_seeds,
44+
);
45+
46+
transfer_checked(xfer_ctx, amount / 2, 6)?;
47+
48+
// Check if the message is "revert" and return an error if so
49+
if pda.last_message == "revert" {
50+
msg!("Reverting transaction due to 'revert' message.");
51+
return Err(ErrorCode::RevertMessage.into());
52+
}
53+
54+
msg!(
55+
"On call executed with amount {}, sender {:?} and message {}",
56+
amount,
57+
pda.last_sender,
58+
pda.last_message
59+
);
60+
61+
Ok(())
62+
}
63+
}
64+
65+
#[derive(Accounts)]
66+
pub struct Initialize<'info> {
67+
#[account(mut)]
68+
pub signer: Signer<'info>,
69+
70+
#[account(init, payer = signer, space = size_of::<Pda>() + 32, seeds = [b"connectedSPL"], bump)]
71+
pub pda: Account<'info, Pda>,
72+
73+
pub system_program: Program<'info, System>,
74+
}
75+
76+
#[derive(Accounts)]
77+
pub struct OnCall<'info> {
78+
#[account(mut, seeds = [b"connectedSPL"], bump)]
79+
pub pda: Account<'info, Pda>,
80+
81+
#[account(mut)]
82+
pub pda_ata: Account<'info, TokenAccount>,
83+
84+
pub mint_account: Account<'info, Mint>,
85+
86+
pub gateway_pda: UncheckedAccount<'info>,
87+
88+
pub random_wallet: UncheckedAccount<'info>,
89+
90+
#[account(mut)]
91+
pub random_wallet_ata: AccountInfo<'info>,
92+
93+
pub token_program: Program<'info, Token>,
94+
95+
pub system_program: Program<'info, System>,
96+
}
97+
98+
#[account]
99+
pub struct Pda {
100+
pub last_sender: [u8; 20],
101+
pub last_message: String,
102+
}
103+
104+
#[error_code]
105+
pub enum ErrorCode {
106+
#[msg("The data provided could not be converted to a valid UTF-8 string.")]
107+
InvalidDataFormat,
108+
109+
#[msg("Revert message detected. Transaction execution halted.")]
110+
RevertMessage,
111+
}

0 commit comments

Comments
 (0)