Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit ab4b9fb

Browse files
author
Tyera Eulberg
authored
Add rust token client support for memo (#2901)
* Remove unnecessary fee-payer repeat signing * Add with_memo api
1 parent 7330b17 commit ab4b9fb

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

Cargo.lock

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

token/rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ solana-sdk = "=1.9.5"
1515
# We never want the entrypoint for ATA, but we want the entrypoint for token when
1616
# testing token
1717
spl-associated-token-account = { version = "1.0.5", path = "../../associated-token-account/program", features = ["no-entrypoint"] }
18+
spl-memo = { version = "3.0.1", path = "../../memo/program", features = ["no-entrypoint"] }
1819
spl-token-2022 = { version = "0.2", path="../program-2022" }
1920
thiserror = "1.0"

token/rust/src/token.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use spl_token_2022::{
2222
};
2323
use std::{
2424
fmt, io,
25-
sync::Arc,
25+
sync::{Arc, RwLock},
2626
time::{Duration, Instant},
2727
};
2828
use thiserror::Error;
@@ -124,6 +124,7 @@ pub struct Token<T, S> {
124124
pubkey: Pubkey,
125125
payer: S,
126126
program_id: Pubkey,
127+
memo: Arc<RwLock<Option<String>>>,
127128
}
128129

129130
impl<T, S> fmt::Debug for Token<T, S>
@@ -134,6 +135,7 @@ where
134135
f.debug_struct("Token")
135136
.field("pubkey", &self.pubkey)
136137
.field("payer", &self.payer.pubkey())
138+
.field("memo", &self.memo.read().unwrap())
137139
.finish()
138140
}
139141
}
@@ -154,6 +156,7 @@ where
154156
pubkey: *address,
155157
payer,
156158
program_id: *program_id,
159+
memo: Arc::new(RwLock::new(None)),
157160
}
158161
}
159162

@@ -168,9 +171,16 @@ where
168171
pubkey: self.pubkey,
169172
payer,
170173
program_id: self.program_id,
174+
memo: Arc::new(RwLock::new(None)),
171175
}
172176
}
173177

178+
pub fn with_memo<M: AsRef<str>>(&self, memo: M) -> &Self {
179+
let mut w_memo = self.memo.write().unwrap();
180+
*w_memo = Some(memo.as_ref().to_string());
181+
self
182+
}
183+
174184
pub async fn get_new_latest_blockhash(&self) -> TokenResult<Hash> {
175185
let blockhash = self
176186
.client
@@ -206,16 +216,22 @@ where
206216

207217
pub async fn process_ixs<S2: Signers>(
208218
&self,
209-
instructions: &[Instruction],
219+
token_instructions: &[Instruction],
210220
signing_keypairs: &S2,
211221
) -> TokenResult<T::Output> {
222+
let mut instructions = vec![];
223+
let mut w_memo = self.memo.write().unwrap();
224+
if let Some(memo) = w_memo.take() {
225+
instructions.push(spl_memo::build_memo(memo.as_bytes(), &[]));
226+
}
227+
instructions.extend_from_slice(token_instructions);
212228
let latest_blockhash = self
213229
.client
214230
.get_latest_blockhash()
215231
.await
216232
.map_err(TokenError::Client)?;
217233

218-
let mut tx = Transaction::new_with_payer(instructions, Some(&self.payer.pubkey()));
234+
let mut tx = Transaction::new_with_payer(&instructions, Some(&self.payer.pubkey()));
219235
tx.try_partial_sign(&[&self.payer], latest_blockhash)
220236
.map_err(|error| TokenError::Client(error.into()))?;
221237
tx.try_sign(signing_keypairs, latest_blockhash)
@@ -280,12 +296,12 @@ where
280296
) -> TokenResult<Self> {
281297
let token = Self::new(client, program_id, &native_mint::id(), payer);
282298
token
283-
.process_ixs(
299+
.process_ixs::<[&dyn Signer; 0]>(
284300
&[instruction::create_native_mint(
285301
program_id,
286302
&token.payer.pubkey(),
287303
)?],
288-
&[&token.payer],
304+
&[],
289305
)
290306
.await?;
291307

@@ -299,14 +315,14 @@ where
299315

300316
/// Create and initialize the associated account.
301317
pub async fn create_associated_token_account(&self, owner: &Pubkey) -> TokenResult<Pubkey> {
302-
self.process_ixs(
318+
self.process_ixs::<[&dyn Signer; 0]>(
303319
&[create_associated_token_account(
304320
&self.payer.pubkey(),
305321
owner,
306322
&self.pubkey,
307323
&self.program_id,
308324
)],
309-
&[&self.payer],
325+
&[],
310326
)
311327
.await
312328
.map(|_| self.get_associated_token_address(owner))
@@ -359,7 +375,7 @@ where
359375
owner,
360376
)?,
361377
],
362-
&[&self.payer, account],
378+
&[account],
363379
)
364380
.await
365381
.map(|_| account.pubkey())
@@ -753,13 +769,13 @@ where
753769
&self,
754770
sources: &[&Pubkey],
755771
) -> TokenResult<T::Output> {
756-
self.process_ixs(
772+
self.process_ixs::<[&dyn Signer; 0]>(
757773
&[transfer_fee::instruction::harvest_withheld_tokens_to_mint(
758774
&self.program_id,
759775
&self.pubkey,
760776
sources,
761777
)?],
762-
&[&self.payer],
778+
&[],
763779
)
764780
.await
765781
}

0 commit comments

Comments
 (0)