Skip to content

Commit 75e54e7

Browse files
authored
feat: execute revert and execute revert spl (#101)
1 parent 55b1c78 commit 75e54e7

File tree

6 files changed

+1342
-167
lines changed

6 files changed

+1342
-167
lines changed

programs/examples/connected/src/lib.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,41 @@ pub mod connected {
4949

5050
Ok(())
5151
}
52+
53+
pub fn on_revert(
54+
ctx: Context<OnRevert>,
55+
amount: u64,
56+
sender: Pubkey,
57+
data: Vec<u8>,
58+
) -> Result<()> {
59+
let pda = &mut ctx.accounts.pda;
60+
61+
// Store the sender's public key
62+
pda.last_revert_sender = sender;
63+
64+
// Convert data to a string and store it
65+
let message = String::from_utf8(data).map_err(|_| ErrorCode::InvalidDataFormat)?;
66+
pda.last_revert_message = message;
67+
68+
// Transfer some portion of lamports transferred from gateway to another account
69+
// Check if the message contains "revert" and return an error if so
70+
if pda.last_revert_message.contains("revert") {
71+
msg!(
72+
"Reverting transaction due to message: '{}'",
73+
pda.last_revert_message
74+
);
75+
return Err(ErrorCode::RevertMessage.into());
76+
}
77+
78+
msg!(
79+
"On revert executed with amount {}, sender {:?} and message {}",
80+
amount,
81+
pda.last_revert_sender,
82+
pda.last_revert_message
83+
);
84+
85+
Ok(())
86+
}
5287
}
5388

5489
#[derive(Accounts)]
@@ -74,10 +109,22 @@ pub struct OnCall<'info> {
74109
pub system_program: Program<'info, System>,
75110
}
76111

112+
#[derive(Accounts)]
113+
pub struct OnRevert<'info> {
114+
#[account(mut, seeds = [b"connected"], bump)]
115+
pub pda: Account<'info, Pda>,
116+
117+
pub gateway_pda: UncheckedAccount<'info>,
118+
119+
pub system_program: Program<'info, System>,
120+
}
121+
77122
#[account]
78123
pub struct Pda {
79124
pub last_sender: [u8; 20],
80125
pub last_message: String,
126+
pub last_revert_sender: Pubkey,
127+
pub last_revert_message: String,
81128
}
82129

83130
#[error_code]

programs/examples/connectedSPL/src/lib.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,41 @@ pub mod connected_spl {
6363

6464
Ok(())
6565
}
66+
67+
pub fn on_revert(
68+
ctx: Context<OnRevert>,
69+
amount: u64,
70+
sender: Pubkey,
71+
data: Vec<u8>,
72+
) -> Result<()> {
73+
let pda = &mut ctx.accounts.pda;
74+
75+
// Store the sender's public key
76+
pda.last_revert_sender = sender;
77+
78+
// Convert data to a string and store it
79+
let message = String::from_utf8(data).map_err(|_| ErrorCode::InvalidDataFormat)?;
80+
pda.last_revert_message = message;
81+
82+
// Transfer some portion of lamports transferred from gateway to another account
83+
// Check if the message contains "revert" and return an error if so
84+
if pda.last_revert_message.contains("revert") {
85+
msg!(
86+
"Reverting transaction due to message: '{}'",
87+
pda.last_revert_message
88+
);
89+
return Err(ErrorCode::RevertMessage.into());
90+
}
91+
92+
msg!(
93+
"On revert executed with amount {}, sender {:?} and message {}",
94+
amount,
95+
pda.last_revert_sender,
96+
pda.last_revert_message
97+
);
98+
99+
Ok(())
100+
}
66101
}
67102

68103
#[derive(Accounts)]
@@ -98,10 +133,29 @@ pub struct OnCall<'info> {
98133
pub system_program: Program<'info, System>,
99134
}
100135

136+
#[derive(Accounts)]
137+
pub struct OnRevert<'info> {
138+
#[account(mut, seeds = [b"connected"], bump)]
139+
pub pda: Account<'info, Pda>,
140+
141+
#[account(mut)]
142+
pub pda_ata: Account<'info, TokenAccount>,
143+
144+
pub mint_account: Account<'info, Mint>,
145+
146+
pub gateway_pda: UncheckedAccount<'info>,
147+
148+
pub token_program: Program<'info, Token>,
149+
150+
pub system_program: Program<'info, System>,
151+
}
152+
101153
#[account]
102154
pub struct Pda {
103155
pub last_sender: [u8; 20],
104156
pub last_message: String,
157+
pub last_revert_sender: Pubkey,
158+
pub last_revert_message: String,
105159
}
106160

107161
#[error_code]

0 commit comments

Comments
 (0)