Give another account ability to transfer portion of SOL - Solana - solana

In SPL Token there is a function called Approve which gives the delegate account the ability to transfer an amount of tokens. So far I can only see this working for a custom token.
Is it possible to give the delegate account the ability to transfer SOL?
The flow I am after is:
Client grants permission to transfer X SOL
API creates a transaction to perform multiple actions including transferring SOL from the client's account to a system account
Resources:
Approve function: https://docs.rs/spl-token/3.0.1/spl_token/instruction/fn.approve.html

Your best bet is to wrap your SOL into an SPL token account (mint So11111111111111111111111111111111111111112) and use the SOL as an SPL token, e.g.:
$ spl-token wrap 1
Wrapping 1 SOL into GJTxcnA5Sydy8YRhqvHxbQ5QNsPyRKvzguodQEaShJje
Signature: 4f4s5QVMKisLS6ihZcXXPbiBAzjnvkBcp2A7KKER7k9DwJ4qjbVsQBKv2rAyBumXC1gLn8EJQhwWkybE4yJGnw2Y
$ spl-token approve So11111111111111111111111111111111111111112 1 pubkey_to_approve_to
More info about wrapping at https://spl.solana.com/token#wrapping-sol and https://spl.solana.com/token#example-wrapping-sol-in-a-token

Related

Does creating a token account on Solana make it rent free automatically?

I have create a token on the devote and have created token account which I can transfer my token to successfully. I notice that a Solana fee is taken when I do this.
I also understand that I have to deposit enough Solana into an account to pay for rent or have 2 years worth of Solana to make it exempt.
When I create the account (using the code below) is it automatically making the accounts rent exempt or do I have to transfer additional Solana to do so.
How can I do this?
Code:
const tokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
payer,
mint,
payer.publicKey
)
All new accounts now require to be rent exempt cf https://docs.solana.com/developing/programming-model/accounts#rent
So any accounts initialised successfully will be rent exempt. You don't have to worry about anything
Yes, an associated token account created by this call will be rent exempt. You can see it in the associated-token-program source code.

How to transfer (using Program instructions) custom SPL token from my own account to another user's wallet?

This is my situation:
I have created a wallet
solana-keygen new
I have created my own custom SPL Token
spl-token create-token
Then I created an Account for this SPL Token
spl-token create-account
The SPL token is now in my wallet A
In the Solana Program, I would like to programmatically transfer the custom SPL token from Wallet A to Alice(user) wallet when certain conditions are met (for example, when Alice answered a quiz correctly, she will be awarded some custom SPL token).
How do I authorise the Solana Program to deduct from Wallet A (which I had created) and transfer the tokens to Alice wallet?
Please advise me how to go about doing this. Really appreciate this.
To transfer an SPL token within a program, your best option is to have wallet A owned by a program-derived address, and then your program can transfer the tokens from wallet A based on any logic it wants.
So first, transfer the ownership to your program-derived address:
spl-token authorize <WALLET_2_ADDRESS> owner <PROGRAM_DERIVED_ADDRESS>
Then in your program, you can transfer to Alice with something like:
let transfer_instruction = spl_token::instruction::transfer(
&token_program.key,
&wallet_a_token_account.key,
&alice_token_account.key,
&program_derived_account.key,
&[],
transfer_amount,
)?;
let required_accounts_for_transfer = [
wallet_a_token_account.clone(),
alice_token_account.clone(),
program_derived_account.clone(),
];
invoke_signed(
&transfer_instruction,
&required_accounts_for_transfer,
&[
&[b"your", b"seeds", b"here",]
]
)?;
This was adapted from a full example for transferring SPL tokens within a program: https://solanacookbook.com/references/programs.html#how-to-do-cross-program-invocation
More information about program-derived addresses at https://solanacookbook.com/references/programs.html#how-to-create-a-pda, with an example of how to create an account.

What is the alternative of "approve" in evm on Solana?

I have several wallets in evm. I give their X token access to my main wallet by calling Contract(x).approve() function. Thus i can make operations with these wallets. Now i want to do same thing on solana with solana web3 library. However i cannot find any function that gives allowance to my main wallet. The X token on Solana uses solana token program.
Correct, you cannot approve tokens using the normal #solana/web3.js package, so you'll have to use the #solana/spl-token package in one of two ways:
approve does it from the client (https://github.com/solana-labs/solana-program-library/blob/edec44180c3349abd77677acb2270a00121f2936/token/js/client/token.js#L905)
createApproveInstruction creates the instruction which you must include in a Transasction and then send (https://github.com/solana-labs/solana-program-library/blob/edec44180c3349abd77677acb2270a00121f2936/token/js/client/token.js#L1570)

Is there any way to change the authority of a Solana Token?

I created a Solana token with a test wallet/account. Now I want to transfer authority to another account and delete the test one. Is there any way to do that?
You can do that through spl-token authorize on the CLI, or the SetAuthority instruction https://github.com/solana-labs/solana-program-library/blob/2d770628ef0fc05c22b36404cfd9aebd3b59c110/token/program/src/instruction.rs#L977
Note that it is an antipattern to transfer the ownership of an Associated Token Account: https://spl.solana.com/associated-token-account
In that case, the best practice is to create an associated token account for the recipient's wallet, transfer the tokens, and then close the first account.

solana transfer nft to any address web3js

I want to make a transfer from my wallet to another wallet with code. I use web3.js and I made a Solana transfer, but I don't know how to make an NFT transfer.
NFTs on Solana are represented as SPL tokens, which can be transferred in JS using the "#solana/spl-token" package on npm: https://www.npmjs.com/package/#solana/spl-token
There's an example of how to use it at https://github.com/solana-labs/solana-program-library/blob/master/token/js/examples/createMintAndTransferTokens.ts and in the repo tests.
You can find more information on SPL tokens at https://spl.solana.com/token
NFT transfer is same as normal spl-token transfer.
Prior to transfer NFT, you need to know its Token Mint Address or its Associated Token Account of yours.
Also need to know receiver's Associated Token Account of NFT Mint Token Account.
If receiver doesn't have associated token account, you or he need to create it first.
If you are not familiar with the account types, please read my article on medium.
https://medium.com/#blockchainlover2019/how-to-verify-ownership-of-metaplex-nft-programmatically-at-on-chain-1059418c3c6
Transferring token by using web3 is easy and not knowhow knowledge.
This is my code from Solana program (smart contract), which transfers nft from one to another.
let transfer_ix = spl_token::instruction::transfer(
token_program.key,
nft_account_to_send.key,
nft_account_to_receive.key,
&pda,
&[],
1
)?;
invoke_signed(
&transfer_ix,
&[
nft_account_to_send.clone(),
nft_account_to_receive.clone(),
pda_account.clone(),
token_program.clone(),
],
&[&[&b"nft_transfer_is_easy"[..], &[_nonce]]]
)?;
I will add another code for you, which runs on web3.

Resources