Erc20 token and buying - erc20

with my own 0x363.. wallet address
I am generating erc20 token and when I issue this erc20 contract a contract address is generated (0x966D...). that is, I have one wallet address and one coin address.
E.g:
1 mytoken = 1 ethereum
E.g:
If a user buys my token with metamask, the user will pay ethereum.
And where is 1 ethereum. Why is this ethereum not uploaded to my admin account (0x363..). this ethereum goes to erc20 (0x966D ..) as far as I understand. How can I get this ethereum to my admin account?

How can I get this ethereum to my admin account?
Based on the context of the question (the ETH is paid to the token contract - not to a DEX pair contract), I'm assuming you have a custom buy function.
You can expand this buy function to use the transfer() method of address payable to transfer the just received ETH to your admin address.
pragma solidity ^0.8;
contract MyToken {
address admin = address(0x123);
function buy() external payable {
// transfers the whole ETH `value` sent to the `buy()` function
// to the `admin` address
payable(admin).transfer(msg.value);
// ... rest of your code
}
}
Contract bytecode is immutable (with some edge case exceptions), so in order to perform the change, you'll need to deploy a new contract - can't expand the already deployed contract.

Related

Transfer SPL Token using Rust

Is there a way to transfer SPL tokens from on wallet to another.
I have done some research and I found that I have to initialize account first using Create Associated Account and then use Token Transfer?
Is there any code examples where I can transfer tokens or a library which helps with creating account/token transfer. etc
You can directly use the spl-token crate for instructions and types used by the token program: https://docs.rs/spl-token/latest/spl_token/
If you need to create associated token accounts, you can use the spl-associated-token-account crate: https://docs.rs/spl-associated-token-account/latest/spl_associated_token_account/index.html
If you're on-chain, you can create a transfer instruction and pass it in along with the required accounts, ie:
let ix = spl_token::instruction::transfer(
token_program.key,
source.key,
destination.key,
authority.key,
&[],
amount,
)?;
invoke(
&ix,
&[source, destination, authority, token_program],
)
This was adapted from the token-swap transfer code: https://github.com/solana-labs/solana-program-library/blob/b2fad8a0781bddd90c8e9b768184f55306265cef/token-swap/program/src/processor.rs#L138
It's correct that you need to create the source and destination accounts first, and it's preferred for those to be associated token accounts. You can create them on-chain using:
let ix = spl_associated_token_account::instruction::create_associated_token_account(
payer.key,
wallet.key,
token_mint.key,
token_program.key,
);
invoke(
&ix,
&[payer, associated_token_account, wallet, token_mint, system_program, token_program, associated_token_account_program],
)
Note that all of these accounts must be sent to your program in order to perform the cross-program invocations.
Afaik theres no library but token transfer only works from account to account, theres a transfer function inside anchor which you have to invoke and pass the to and from account infos, you can transfer directly to an address beacuse you wud need account info of receiver too which u can get from a publickey but it can be done through escrow, heres a program that i wrote which sends sol to a PDA and the receiver accepts it from PDA
Github link to escrow program

Solana Anchor: how can a program check approved token allowance given by an user?

I am trying to port Ethereum's allowance function into Solana program,
token.allowance(msg.sender, address(this))
it seems there is no such allowance function existing in Solana SPL, or Anchor SPL... is there?
Solana SPL: https://spl.solana.com/token#authority-delegation ...
Quote "Authority delegation#
Account owners may delegate authority over some or all of their token balance using the Approve instruction. Delegated authorities may transfer or burn up to the amount they've been delegated. Authority delegation may be revoked by the Account's owner via the Revoke instruction."
...
this does not say clearly how to use such a function
https://github.com/solana-labs/solana-program-library/blob/master/token/program/src/instruction.rs#L919 ... the approve function in is Rust, but it is difficult to be used
Anchor SPL
https://docs.rs/anchor-spl/0.18.2/anchor_spl/token/struct.Approve.html
I see Anchor makes calling Solana's approve function easier. but I could not find the allowance function.
https://docs.rs/anchor-spl/0.19.0/anchor_spl/token/index.html
This is used to check token amounts on certain account. not allowance.
It seems in Solana, we do not need to check the allowance given from an user to another address... because I found this in Anchor's cashiers check test example:
// Transfer funds to the check.
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info().clone(),
to: ctx.accounts.vault.to_account_info().clone(),
authority: ctx.accounts.owner.clone(),
};
let cpi_program = ctx.accounts.token_program.clone();
let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
token::transfer(cpi_ctx, amount)?;
That example above does not check for user's given allowance on the program.
Does that mean any Solana program can transfer any user's tokens without their consent?
Why does the approve function exist if we cannot check the allowance?
To answer your questions...
Does that mean any Solana program can transfer any user's tokens without their consent?
That would be quite a security flaw! The concept is that, when you transfer tokens, some account must sign to validate the transfer. That account may be the owner, or some pre-approved delegate. In the exsample that you've shown, authority is given as ctx.accounts.owner.clone(), which means that the owner has likely already signed the transaction. If the owner approves some amount to a delegate, and then signs with that delegate, the token program figures out that the delegate has signed, and allows the transfer.
Why does the approve function exist if we cannot check the allowance?
To check the allowance, you'll have to deserialize the account into a token Account https://docs.rs/anchor-spl/0.19.0/anchor_spl/token/struct.TokenAccount.html. You can use one of the helpers, like try_deserialize, which gives you a type that wraps spl_token::state::Account.
With your spl_token::state::Account, you can check the delegated_amount field to see how much has been approved by the user: https://github.com/solana-labs/solana-program-library/blob/8eb2c3ce60bfe943e277eb172ba8e9ce9b6bdae6/token/program/src/state.rs#L103

Recover BNB sent to self deployed contract

I was trying out creating BOT, so created and deployed a contract in BSC from meatamask. I sent some BNB to that contract to check but it didnt work. How can I get the BNB back? Help is appreciated as I am very new to this
Unless you have a custom function in the contract that allows you to withdraw the funds, they are forever locked in the contract address.
pragma solidity ^0.8;
contract MyContract {
receive() external payable {}
function withdraw() external {
// transfer this contract's whole BNB balance to the `0x123` address
payable(address(0x123)).transfer(address(this).balance);
}
}

How to send transaction from metamask wallet to backend node js

i am building simple dapp application where i want to verify something and then only make contract interaction but right now i am struggling to put a middleware which will act like user will make txn through metamask and then this txn or something will go to backend server on any language probably node js , and i will do some checking and if all good then Send it to block chain.
Any suggestion?
Right now its all in react Frontend and metamask browser extension.. and i can not make client side code restricted
And i can not ask for private key even.
Not possible by design. A transaction needs to be signed by the sender's private key. So unless the users are willing to give you their private key (so that you could sign the transaction for them on the backend), you'll need to change your approach.
If you need to allow interaction with the contract only to users authorized by your app, the contract needs to hold the list of authorized addresses. And the list can be updated by your app (that holds the private key to the owner address). Example:
pragma solidity ^0.8;
contract MyContract {
address public owner = address(0x123);
mapping(address => bool) public isAuthorized;
function setAuthorized(address _address, bool _isAuthorized) external {
require(msg.sender == owner, 'Only the contract owner can set authorized addresses');
isAuthorized[_address] = _isAuthorized;
}
function foo() external {
require(isAuthorized[msg.sender], 'Only authorized addresses can execute this function');
// ...
}
}

Generate new Ethereum Wallet in NodeJS

Are you familiar with ethereum and web3js-api in node js?
I used the framework sails, and i am a little bit confused how to generate a new account like (https://www.myetherwallet.com/). For now i used web3js-api v.1.0.0. I can get current account and balance. i try to create new account, but it return error, says create is not a function, etc.
i used testnet, how can i connect it to metamask (Rinkeby Network)? So if i generate new account, the account will appear in metamask account list also.
If you know, please share.
Thanks.
You can use this package :
https://www.npmjs.com/package/node-ethereum-wallet
let myWallet = new EthereumWallet() // using MyEtherAPI.com web3 HTTP provider
In its simplest form, Ethereum wallet is just a single private key.
Generate a random 256-bit integer - this is your private key
web3.js functions can import any private key with privateKeyToAccount - This will derive an Ethereum public key and Ethereum address, which is just trimmed public key, for you
Now web3.js can use your private key to sign transactions and messages

Resources