Skip to content

Commit e609151

Browse files
authored
feat: withdraw and call sol (#74)
1 parent 64579b1 commit e609151

File tree

8 files changed

+442
-62
lines changed

8 files changed

+442
-62
lines changed

Anchor.toml

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

77
[programs.localnet]
8+
connected = "4xEw862A2SEwMjofPkUyd4NEekmVJKJsdHkK3UkAtDrc"
89
gateway = "ZETAjseVjuFsxdRxo6MmTCvqFwb3ZHUx56Co3vCmGis"
910

1011
[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.

programs/connected/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "connected"
3+
version = "0.1.0"
4+
description = "Test program used for testing withdraw and call feature"
5+
edition = "2021"
6+
7+
[lib]
8+
crate-type = ["cdylib", "lib"]
9+
name = "connected"
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/connected/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/connected/src/lib.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use anchor_lang::prelude::*;
2+
use std::mem::size_of;
3+
4+
declare_id!("4xEw862A2SEwMjofPkUyd4NEekmVJKJsdHkK3UkAtDrc");
5+
6+
// NOTE: this is just example contract that can be called from gateway in execute function for testing withdraw and call
7+
#[program]
8+
pub mod connected {
9+
use super::*;
10+
11+
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
12+
Ok(())
13+
}
14+
15+
pub fn on_call(
16+
ctx: Context<OnCall>,
17+
amount: u64,
18+
sender: [u8; 20],
19+
data: Vec<u8>,
20+
) -> Result<()> {
21+
let pda = &mut ctx.accounts.pda;
22+
23+
// Store the sender's public key
24+
pda.last_sender = sender;
25+
26+
// Convert data to a string and store it
27+
let message = String::from_utf8(data).map_err(|_| ErrorCode::InvalidDataFormat)?;
28+
pda.last_message = message;
29+
30+
// Transfer some portion of lamports transferred from gateway to another account
31+
pda.sub_lamports(amount / 2)?;
32+
ctx.accounts.random_wallet.add_lamports(amount / 2)?;
33+
34+
msg!(
35+
"On call executed with amount {}, sender {:?} and message {}",
36+
amount,
37+
pda.last_sender,
38+
pda.last_message
39+
);
40+
41+
Ok(())
42+
}
43+
}
44+
45+
#[derive(Accounts)]
46+
pub struct Initialize<'info> {
47+
#[account(mut)]
48+
pub signer: Signer<'info>,
49+
50+
#[account(init, payer = signer, space = size_of::<Pda>() + 32, seeds = [b"connected"], bump)]
51+
pub pda: Account<'info, Pda>,
52+
53+
pub system_program: Program<'info, System>,
54+
}
55+
56+
#[derive(Accounts)]
57+
pub struct OnCall<'info> {
58+
#[account(mut, seeds = [b"connected"], bump)]
59+
pub pda: Account<'info, Pda>,
60+
61+
pub gateway_pda: UncheckedAccount<'info>,
62+
63+
pub random_wallet: UncheckedAccount<'info>,
64+
65+
pub system_program: Program<'info, System>,
66+
}
67+
68+
#[account]
69+
pub struct Pda {
70+
pub last_sender: [u8; 20],
71+
pub last_message: String,
72+
}
73+
74+
#[error_code]
75+
pub enum ErrorCode {
76+
#[msg("The data provided could not be converted to a valid UTF-8 string.")]
77+
InvalidDataFormat,
78+
}

programs/gateway/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "gateway"
33
version = "0.1.0"
4-
description = "Created with Anchor"
4+
description = "ZetaChain Gateway program on Solana"
55
edition = "2021"
66

77
[lib]

0 commit comments

Comments
 (0)