form my understandings, after the first execution of a smart contract, the network will return the remaining gas to the contract sender. if so, how will the contract obtain gas for future execution??
A contract can only be executed because of either 1 of 2 reasons:
- It receives a transaction
- It is called by another contract, which, in turn, is executing also due to these 2 reasons.
Thus, for a contract (or a chain of called contracts) to execute, there must be a transaction in the network to start them. This transaction necessarily provides all gas for the whole execution, including the contract it calls directly and contracts called by it, so on and so forth. The provided gas will be forwarded around along with contract calling, so subsequently called contracts have gas for their execution.
The contract deploy transaction is essentially the same, providing gas for execution of contract initialization (in addition to writing contract's code to blockchain).
Side note: By contract call I mean the CALL, DELEGATECALL and similar EVM instructions carried out during contract execution, not the (confusingly named) call that just executes a constant/view contract function in your local friendly node.
Gas is not a currency. It is a unit of work.
Gas is calculated based on the exact operations performed during the execution of a transaction. For smart contracts, this means it is based on what code is executed.
The gas used is then multiplied against the gas price to arrive at the final fee for the transaction, which is deducted from the ether balance of the account that initiated the transaction.
After you deploy a smart contract, anyone who makes a transaction to it will be responsible for paying the transaction fees.