what can you do with the gas that is sent to a fall back function - solidity

am am learning about transfer and send in solidity, and the fallback function. To my understanding, when you send ether to a smart contract, you also send some gas to the fallback function as well. Why is this mechanism in place? I thought gas was used to pay validators, how come there is gas now stored in the contract + what is this gas in the contract used for now? Thanks

Each operation costs some predefined amount of gas. The combined amount - no matter how many internal transactions are performed within the main transaction - is always paid by the main transaction sender.
The transfer() function (in the current Solidity version 0.8) allows the called address to spend only 2300 gas, preventing the reentrancy attack.
This amount is enough to emit an event but it's not sufficient to perform another call - for example back to the caller contract, that would allow for the reentrancy attack.
So to answer your original question:
what can you do with the gas that is sent to a fall back function
From the called contract perspective: You can only spend it to perform few basic operations such as to emit an event. But you cannot sell it back to ETH and store it in the called contract, nor call another contract (including the sender).
contract Called {
event Received(uint256 amount);
fallback() external payable {
// ok
emit Received(msg.value);
// fail - costs more than the 2300 limit
msg.sender.call("");
}
}

Related

Dedicated mint event vs transfer event in erc721

For NFT minting - Standard says to emit transfer event with value of from as zero address. But i was wondering a dedicated event let's say mint sound more better and clear.
event Mint(address to, string tokenId)
Also it gives us advantage of one more variable to be indexed in event as max three value can be indexed.
Can anyone please clear this ? What is best way?
Pasted from a comment below my other answer answering a question
why not to use dedicated mint event ?
I can't speak for the authors and reviewers of the ERC-721 standard, why they chose this specific way. But from my understanding, it was already a common practice to emit Transfer event log with zero sender address when minting ERC-20 tokens, when they were creating the 721 standard. So one of the reasons might have been reusability of code for offchain apps such as blockchain explorers, to be able to handle token minting in a more generalized way.
To add context to your more specific question about the advantage of being able to pass more values:
Apart from Transfer, you can also emit other event logs, including this arbitrary Mint as well, when you're minting new tokens.
Since this Mint event is not standardized, it will not be recognized by most offchain apps (such as Etherscan) as token mint. They will only show it on the transaction detail page as "some event named Mint that we don't recognize", but their internal aggregated database of "who owns which tokens" and "these tokens were minted during this transaction" will still reflect only the values passed to the Transfer event.
However, you'll be able to handle this arbitrary event from your own offchain apps.

Is it possible to transfer money without Internal TXNs?

Exactly what I want is this:
I'm transferring money to a contract. The money coming into this contract will be forwarded to the destination address without creating any TXN (that is, anonymously).
I created a contract and the money sent to this contract is sent to the target wallet. In this way, information does not appear in Transaction transactions of the target wallet.
the code i use
receive() external payable {
payable(0x8162Ac860EF729d60C0f3683bfaA0334A3499956).send(msg.value);
}
As pictured (see Transactions)
https://i.stack.imgur.com/OPxH5.png
So far everything is ok. But there is one problem. Transaction appears in Internal TXNs. I want to hide this.
https://i.stack.imgur.com/39nCh.png
What should happen: Money will be transferred to the contract and it will transfer money anonymously without generating any TX belonging to the target wallet.
REQUIRE --->> When the money sent to the contract is sent to the counter wallet, no information will appear in the Transactions and Internal TXNs tab of the counter wallet.
Is it possible to do this? Please tell me what should I do if
possible.
No, this is not possible. Any transfer of a native chain token (BNB in your case of Binance Smart Chain) will appear on an explorer like Etherscan (Bscscan) as an "internal transaction".

Smart Contract's state variables on the blockchain

I am new to blockchain, please help me understand.
How the smart contract's state variables are stored on the blockchain if a smart contract is immutable (because it was deployed as a transaction = byte-code is stored in a transaction)?
Ok, maybe every new state of a state variable is stored with a new method-update call (set) in the transaction, but how then does the smart contract know how to address them if it was created earlier?
And here I found a mention of a state storage on every EVM. "Technically you don’t need to store this on disk, you could just play back all transactions when you boot up the node" - again, how it is possible to play back all transactions related to a contract, how are they connected to a contract?
Immutability applies only to data placed directly in the blockchain, that is, to transaction data. In Ethereum, the values of smart contract variables are determined specifically by each node when processing a transaction on its EVM instance.
As for, for example, Hyperledger Fabric, the final results of calculations are also transmitted along with the transaction, and the node simply records them in its state database. But at the same time, he himself determines whether to accept or not to accept this transaction.

Asking about self-destruct in solidity

i have the code in solidity, i'm wondering something: I deploy code for the 0x583... first, i send 1 eth to another account. Before i click withdrawAllMoney, i click destroySmartContract, 1 eth 's automatically send to receiver (this is okie!). But after that, i keep going send the 1 eth from 0x583.. to another account, it still take out eth from 0x583.. but the orther can't receive it.
I'm wondering: when i call the selfdistruct, is the contract real deleted (can't send or recieved...)
Thank you!
when i call the selfdistruct, is the contract real deleted (can't send or recieved...)
selfdestruct() effectively removes the deployed bytecode from the contract address.
In the next block, this (former contract) address will act as a regular address without a smart contract, so it is able to receive tokens and ETH.
But, since it doesn't hold any bytecode anymore, you won't be able to interact with the contract (it's not there anymore).

How do I modify the behavior of specific payable function calls with my token in Solidity?

Say I want to make it so certain wallets can never, ever receive my token, or maybe so that interactions with a specific, known malicious contract, or broken functions on an otherwise working contract on chain always revert. How do I go about doing this?
In my specific case, I would like to make it so that for a period after deployment, but before a hardcoded unix timestamp, people can add or remove liquidity to a uniswap pool, but no swaps can occur, so that the price stays constant while everyone adds liquidity, until the timestamp passes and swaps are then able to occur, and so that they can all safely remove their liquidity from the pool if something goes wrong before the timestamp passes with no impermanent loss.
For the "blacklist" functionality, you could maintain a mapping containing the blacklisted accounts. Then whenever a transfer of your token occurs, you can require that the address not be in the blacklist, otherwise the transfer reverts. You could also add a function to "un-blacklist" an account, if you so wished.
As for the second point - i'm curious as to how you're defining the 'price' of your token if no swaps can occur, and what incentivises anyone to add liquidity under that arrangement?