What is the alternative of "approve" in evm on Solana? - 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)

Related

How to send USDC from solana-cli command line

I am trying to send USDC from the solana-cli command line but I can't find any example in the documentation. Everything is references to how to do it in javascript importing these 2 libraries.
import * as web3 from "#solana/web3.js";
import * as splToken from "#solana/spl-token";
The first to transfer SOL coin and the second for the rest of the TOKENS. Can be done? does anyone know the command?
The SPL docs contain CLI examples for every possible action with spl-token. To perform a transfer, you can do:
$ spl-token transfer EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v <AMOUNT> <RECEIVER>
Here, we specify the USDC mint EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v, which means that the CLI will attempt to send from the default user's associated token account for USDC.
You can see all of the examples for tranfers at https://spl.solana.com/token#example-transferring-tokens-to-another-user

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.

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.

About access control in hyperledger composer to implement a business network

I try to use composer to develop a block-chain web app.
I write the ".acl" file to implement access control, and I also issued different IDs to different participant, then I start the REST server.
The next thing I want to ask is, how can the REST server identify my identity?
Just like, one kind of participant is named "trader", I specify "trader" cannot access function "A" in chaincode, but REST server generate the API of "A", then I write a simple html file and send a POST request to localhost:3000, I can invoke this function directly. I even don't know I access this interface by what kind of identity.
I am confused about this, can some one help me?
Remember how you started the REST server? You had to specify a business network card, and likely it was the card for the admin with all reading and writing rights. Also most likely, you disabled authentication with passport.
With these two elements, of course you can always do anything just by calling any available API function.
You can refer to the passport authentication instructions for composer-rest-server to implement your authentication scheme.
You should code the rules for what is allowed for certain groups (such as "patients", "doctors", "payers") in the ACL permission files. Refer to the section "Granting Network Access Control" in https://hyperledger.github.io/composer/latest/reference/acl_language, which contains some pretty similar examples.
Illicit API calls would then simply fail if attempted by the wrong entity.

Citrus wallet signup api for PHP

I want to make a interface so that user can register and login themselves at citrus using PHP technology and use that wallet in their payment. Does citrus provide any API for this?
Yes, Citrus-Pay does provide APIs for Integrating with Wallet where you can define your own interface.
Alternatively, you may also use the standard UI provided by Citrus. If you want to go forward with APIs,
1) Get a signup token using the API mentioned in this link: https://developers.citruspay.com/documentation/wallettab/identity-profile/oauth-token-api/
2) Call Find Or Create User API as next step. This API will trigger an OTP to the customer's mobile number passed in the request.(A Citrus Wallet is uniquely identified by a mobile number)
API on dev guide: https://developers.citruspay.com/documentation/wallettab/identity-profile/find-or-create-a-wallet/
3) Now this OTP has to be entered in an appropriate API.Which API to be used is told in the response of find or create wallet API called in step 2. (Password-based login is also supported).
It could be sign in with MOTP API ( for an existing citrus wallet customer) or verify mobile and sign API ( for new wallet accounts just being created)enter link description here
4)You will get a full access token (prepaid_pay) in response.Pass this in header of all other APIs which you need to call on behalf of user.
5)For Eg: To make a payment using the customers existing wallet balance , use the following API : https://developers.citruspay.com/documentation/wallettab/pay-using-citrus-wallet/make-payment/
Hope this helps

Resources