Skip to content
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

Add easy functions for token transfers #2586

Closed
metaproph3t opened this issue Aug 1, 2023 · 4 comments
Closed

Add easy functions for token transfers #2586

metaproph3t opened this issue Aug 1, 2023 · 4 comments
Labels

Comments

@metaproph3t
Copy link
Contributor

When you want to do a token transfer today within Anchor, it looks something like this (sorry for leading spaces):

		token::transfer(
			CpiContext::new(
				accs.token_program.to_account_info(),
				Transfer {
					from: accs.user_underlying_token_account.to_account_info(),
					to: accs.vault_underlying_token_account.to_account_info(),
					authority: accs.authority.to_account_info(),
				},
			),
			amount,
		)?;

This is pretty verbose and requires 3 levels of nesting. Not ideal, especially when you compare it with Solidity's ERC20.transferFrom(from, to, amount)

It would be nice to replace the function with something like this:

		token::transfer(
			&accs.token_program,
			&accs.user_underlying_token_account,
			&accs.vault_underlying_token_account,
			amount,
			&accs.authority,
		)?;
@devansh-m12
Copy link

I think creating a function might work you can try following code :

pub fn transfer(
    token_program: &AccountInfo,
    user_underlying_token_account: &AccountInfo,
    vault_underlying_token_account: &AccountInfo,
    amount: u64,
    authority: &AccountInfo,
) -> ProgramResult {
    let cpi_ctx = CpiContext::new(
        token_program.clone(),
        Transfer {
            from: user_underlying_token_account.clone(),
            to: vault_underlying_token_account.clone(),
            authority: authority.clone(),
        },
    );
    token::transfer(cpi_ctx, amount)
}

@metaproph3t
Copy link
Contributor Author

Yeah, this is what I've been doing: example. It would just be nice if we could include this in anchor-spl so I don't need to copypasta into each new repo

@devansh-m12
Copy link

If you haven't already, you can make a new pull request for that, and I'm sure they'll integrate it eventually.

metaproph3t added a commit to metaproph3t/anchor that referenced this issue Aug 10, 2023
I was about to work on a PR to address coral-xyz#2586 and was trying to get my
tests set up so that I could test locally. I found it pretty cumbersome
to copy all of the commands from the ci files in `.github` (maybe
there's a better way? in which case, this isn't necessary), so I threw
them in a `justfile` in case it'd be helpful for others.
@acheroncrypto
Copy link
Collaborator

Closing for now: #2596 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants