pragma solidity ^0.5.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP. Does not include
* the optional functions; to access them see `ERC20Detailed`.
*/
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
uint256 c = a - b;
return c;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// Solidity only automatically asserts when dividing by 0
require(b > 0, "SafeMath: division by zero");
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, "SafeMath: modulo by zero");
return a % b;
}
}
/**
* @dev Implementation of the `IERC20` interface.
*
* This implementation is agnostic to the way tokens are created. This means
* that a supply mechanism has to be added in a derived contract using `_mint`.
* For a generic mechanism see `ERC20Mintable`.
*
* *For a detailed writeup see our guide [How to implement supply
* mechanisms](https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226).*
*
* We have followed general OpenZeppelin guidelines: functions revert instead
* of returning `false` on failure. This behavior is nonetheless conventional
* and does not conflict with the expectations of ERC20 applications.
*
* Additionally, an `Approval` event is emitted on calls to `transferFrom`.
* This allows applications to reconstruct the allowance for all accounts just
* by listening to said events. Other implementations of the EIP may not emit
* these events, as it isn't required by the specification.
*
* Finally, the non-standard `decreaseAllowance` and `increaseAllowance`
* functions have been added to mitigate the well-known issues around setting
* allowances. See `IERC20.approve`.
*/
contract MyERC20 is IERC20 {
using SafeMath for uint256;
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) private _allowances;
// NOTE Start of https://github.com/OpenZeppelin/openzeppelin-solidity/blob/v2.3.0/contracts/token/ERC20/ERC20Detailed.sol
string private _name;
string private _symbol;
uint8 private _decimals;
constructor (string memory name, string memory symbol, uint8 decimals) public {
_name = name;
_symbol = symbol;
_decimals = decimals;
_mint(msg.sender, 100000 * 10 ** uint256(decimals)); // CAUTION!
}
/**
* @dev Returns the name of the token.
*/
function name() public view returns (string memory) {
return _name;
}
/**
* @dev Returns the symbol of the token, usually a shorter version of the
* name.
*/
function symbol() public view returns (string memory) {
return _symbol;
}
/**
* @dev Returns the number of decimals used to get its user representation.
* For example, if `decimals` equals `2`, a balance of `505` tokens should
* be displayed to a user as `5,05` (`505 / 10 ** 2`).
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei.
*
* > Note that this information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* `IERC20.balanceOf` and `IERC20.transfer`.
*/
function decimals() public view returns (uint8) {
return _decimals;
}
// NOTE End of https://github.com/OpenZeppelin/openzeppelin-solidity/blob/v2.3.0/contracts/token/ERC20/ERC20Detailed.sol
uint256 private _totalSupply;
/**
* @dev See `IERC20.totalSupply`.
*/
function totalSupply() public view returns (uint256) {
return _totalSupply;
}
/**
* @dev See `IERC20.balanceOf`.
*/
function balanceOf(address account) public view returns (uint256) {
return _balances[account];
}
/**
* @dev See `IERC20.transfer`.
*
* Requirements:
*
* - `recipient` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/
function transfer(address recipient, uint256 amount) public returns (bool) {
_transfer(msg.sender, recipient, amount);
return true;
}
/**
* @dev See `IERC20.allowance`.
*/
function allowance(address owner, address spender) public view returns (uint256) {
return _allowances[owner][spender];
}
/**
* @dev See `IERC20.approve`.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 value) public returns (bool) {
_approve(msg.sender, spender, value);
return true;
}
/**
* @dev See `IERC20.transferFrom`.
*
* Emits an `Approval` event indicating the updated allowance. This is not
* required by the EIP. See the note at the beginning of `ERC20`;
*
* Requirements:
* - `sender` and `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `value`.
* - the caller must have allowance for `sender`'s tokens of at least
* `amount`.
*/
function transferFrom(address sender, address recipient, uint256 amount) public returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, msg.sender, _allowances[sender][msg.sender].sub(amount));
return true;
}
/**
* @dev Atomically increases the allowance granted to `spender` by the caller.
*
* This is an alternative to `approve` that can be used as a mitigation for
* problems described in `IERC20.approve`.
*
* Emits an `Approval` event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
_approve(msg.sender, spender, _allowances[msg.sender][spender].add(addedValue));
return true;
}
/**
* @dev Atomically decreases the allowance granted to `spender` by the caller.
*
* This is an alternative to `approve` that can be used as a mitigation for
* problems described in `IERC20.approve`.
*
* Emits an `Approval` event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `spender` must have allowance for the caller of at least
* `subtractedValue`.
*/
function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
_approve(msg.sender, spender, _allowances[msg.sender][spender].sub(subtractedValue));
return true;
}
/**
* @dev Moves tokens `amount` from `sender` to `recipient`.
*
* This is internal function is equivalent to `transfer`, and can be used to
* e.g. implement automatic token fees, slashing mechanisms, etc.
*
* Emits a `Transfer` event.
*
* Requirements:
*
* - `sender` cannot be the zero address.
* - `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
*/
function _transfer(address sender, address recipient, uint256 amount) internal {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
_balances[sender] = _balances[sender].sub(amount);
_balances[recipient] = _balances[recipient].add(amount);
emit Transfer(sender, recipient, amount);
}
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
* the total supply.
*
* Emits a `Transfer` event with `from` set to the zero address.
*
* Requirements
*
* - `to` cannot be the zero address.
*/
function _mint(address account, uint256 amount) internal {
require(account != address(0), "ERC20: mint to the zero address");
_totalSupply = _totalSupply.add(amount);
_balances[account] = _balances[account].add(amount);
emit Transfer(address(0), account, amount);
}
/**
* @dev Destroys `amount` tokens from `account`, reducing the
* total supply.
*
* Emits a `Transfer` event with `to` set to the zero address.
*
* Requirements
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
*/
function _burn(address account, uint256 value) internal {
require(account != address(0), "ERC20: burn from the zero address");
_balances[account] = _balances[account].sub(value);
_totalSupply = _totalSupply.sub(value);
emit Transfer(account, address(0), value);
}
/**
* @dev Sets `amount` as the allowance of `spender` over the `owner`s tokens.
*
* This is internal function is equivalent to `approve`, and can be used to
* e.g. set automatic allowances for certain subsystems, etc.
*
* Emits an `Approval` event.
*
* Requirements:
*
* - `owner` cannot be the zero address.
* - `spender` cannot be the zero address.
*/
function _approve(address owner, address spender, uint256 value) internal {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = value;
emit Approval(owner, spender, value);
}
/**
* @dev Destoys `amount` tokens from `account`.`amount` is then deducted
* from the caller's allowance.
*
* See `_burn` and `_approve`.
*/
function _burnFrom(address account, uint256 amount) internal {
_burn(account, amount);
_approve(account, msg.sender, _allowances[account][msg.sender].sub(amount));
}
}
FAQs
How to write ERC20 smart contract? ›
- Step 1: Decide what you want your Token to be.
- Step 2: Code the Contract.
- Step 3: Test the Token on the TestNet.
- Watch the Custom Token.
- Verify the Source Code.
Creating a Smart Contract With Solidity
The first line in a solidity contract sets the source code version. This is to ensure that the contract doesn't suddenly behave differently with a new compiler version. In this example, we declared two states variables: creator and message.
- Step 1: Connect to the Ethereum network. ...
- Step 2: Create your app (and API key) ...
- Step 3: Create an Ethereum account (address) ...
- Step 4: Add ether from a Faucet. ...
- Step 5: Check your Balance. ...
- Step 6: Initialize our project. ...
- Step 7: Download Hardhat. ...
- Step 8: Create Hardhat project.
- TotalSupply: provides information about the total token supply.
- BalanceOf: provides account balance of the owner's account.
- Transfer: executes transfers of a specified number of tokens to a specified address.
- Step 1: Find an open source Solidity contract as a starting point. ...
- Step 2: Define the abstract token contract. ...
- Step 3: Define the abstract store contract. ...
- Step 4: Write test cases for use with TDD. ...
- Step 5: Implement the smart contract code.
Users can start the token creation process by connecting their Ethereum wallet (selecting between Wallet Connect or MetaMask) or create one by selecting the “Get wallet” button. They will then need to add enough funds to pay for contract deployment and set up their tokens.
Is it hard to write smart contracts? ›It isn't technically more challenging that most coding languages. To develop basic smart contracts, or decentralized applications (dApps), doesn't required you to have a background in cryptography, game theory, protocol design, distributed computer networks, or anything of the like.
What four 4 things are required for the creation of a contract? ›Essential elements of a contract
an offer. an acceptance. an intention to create a legal relationship. a consideration (usually money).
- Offer - One of the parties made a promise to do or refrain from doing some specified action in the future.
- Consideration - Something of value was promised in exchange for the specified action or nonaction. ...
- Acceptance - The offer was accepted unambiguously.
In general it takes anywhere from two weeks (one sprint) to two months to complete Discovery.
What is a simple example of smart contract? ›
A smart contract is a self-executing program based on if-then logic. For example, vending machines are a ubiquitous presence in everyday life. It's also a simple model of a smart contract: If someone inserts $2 and then presses B4, then the machine dispenses the package of cookies held in the B4 slot.
How do I write my own contract? ›- The date the contract begins and when it expires.
- The names of all parties involved in the transaction.
- Any key terms and definitions.
- The products and services included in the transaction.
- Any payment amounts, project schedules, terms, and billing dates.
The ERC-20 introduces a standard for Fungible Tokens, in other words, they have a property that makes each Token be exactly the same (in type and value) as another Token. For example, an ERC-20 Token acts just like the ETH, meaning that 1 Token is and will always be equal to all the other Tokens.
What is an example of an ERC20 token? ›...
Popular ERC20 tokens list
- Chainlink (LINK)
- Tether (USDT)
- Shiba Inu (SHIB)
- Wrapped Bitcoin (WBTC)
- OmiseGO (OMG)
- 0x (ZRX)
Moreover, ERC standards are designed to allow ERC tokens to interact seamlessly. ERC-20, ERC-721, and ERC-1155 appear as the three popular ERC token standards or protocols that have their applications across major industries.
How many lines of code is a smart contract? ›The Essence of a Smart Contract
Back to Ethereum, you actually need some eight lines of code to create a basic a very basic token contract. A full application stack, however, may extend anywhere between a few thousands lines of code and, say, 50,000 lines of programming code.
- Get it in writing. ...
- Keep it simple. ...
- Deal with the right person. ...
- Identify each party correctly. ...
- Spell out all of the details. ...
- Specify payment obligations. ...
- Agree on circumstances that terminate the contract. ...
- Agree on a way to resolve disputes.
- Add a Pragma Line and Import an OpenZeppelin Package.
- Create the ERC-20 Token Contract.
- Test the Token Contract.
- Deploy the ERC-20 Token.
- Set the events of the token.
- Set the name, symbol, and decimal of the token.
- Declare the total supply.
- Set the amount of the total supply and the balances.
- Get the balance of an owner.
- Transfer tokens to an account.
- Approve a token transfer.
Open Metamask and click on the Add Token button, select the Custom Token option and paste the contract's address in the first field. Metamask will fetch the Token Symbol and decimals automatically. Click on next and your token will be added to the wallet, it will be available under the assets section in Metamask.
How much do smart contract Devs make? ›
Trend in Smart Contract Developer salary
How Smart Contract Developer salary has changed in Dec 2021 - Feb 2023: Dec 2021: $180k. Jan 2022: $144k. Feb 2022: $216k.
₹4L - ₹16L (Employer Est.) Good understanding of smart contracts and exposure to developing smart contracts (Node/ Java / JavaScript).
What language are smart contracts written in? ›The smart contracts are written in C# and then wrapped with a web-based front end.
What are the five 5 main conditions included in a building contract? ›...
The five clauses you should be aware of include:
- indemnities;
- liquidated damages;
- consequential loss;
- time bars; and.
- warranties.
The five requirements for creating a valid contract are an offer, acceptance, consideration, competency and legal intent.
What are the three 3 proper objects of a contract? ›Contracts are made up of three basic parts – an offer, an acceptance and consideration. The offer and acceptance are what the purpose of the agreement is between the parties.
What are the 7 requirements of a contract? ›For a contract to be valid and recognized by the common law, it must include certain elements— offer, acceptance, consideration, intention to create legal relations, authority and capacity, and certainty. Without these elements, a contract is not legally binding and may not be enforced by the courts.
What are the six 6 key requirements for forming a contract? ›...
- 1 Offer and acceptance. ...
- 2 Intention to create legal relations. ...
- 3 Consideration. ...
- 4 Legal capacity. ...
- 5 Consent. ...
- 6 Illegal and void contracts.
- Offer.
- Acceptance.
- Awareness.
- Consideration.
- Capacity.
- Legality.
Freelance Solidity developers, on average, charge $81-100 per hourly. For Solidity jobs, a developer may charge differently based on the expected duration of your job.
How much should I pay for a smart contract? ›
The cost of deploying and launching a smart contract on Ethereum is likely to vary based on the complexity of the contract. However, the average cost per transaction is $0.0015 – $0.0025 depending on the gas price. This means that for every 10,000 transactions, you can expect to spend around $150 – $300 in fees.
Can a smart contract fail? ›Since smart contracts are the cornerstone of blockchain applications, when they fail, the applications built with those contracts fail, and the entire ecosystem suffers. Blockchains are a fundamentally different programming environment, which leads to new problems.
Can anyone write a smart contract? ›Permissionless. Anyone can write a smart contract and deploy it to the network. You just need to learn how to code in a smart contract language, and have enough ETH to deploy your contract.
What is a smart contract for dummies? ›Smart contracts are simply programs stored on a blockchain that run when predetermined conditions are met. They typically are used to automate the execution of an agreement so that all participants can be immediately certain of the outcome, without any intermediary's involvement or time loss.
What is the most popular smart contract? ›The first smart contract platform in the world was Ethereum, which is still the most widely used by programmers today. Since going live in 2015, the platform has made it possible to deploy apps ranging from ICOs to smart-contract-based insurance. Since its 2015 launch, Ethereum has ruled the field of smart contracts.
Can just anyone write a contract? ›If you're asking whether you need a lawyer to draft a contract, legally, the answer is no. Anyone can draft a contract on their own and as long as the elements above are included and both parties are legally competent and consent to the agreement, it is generally lawful.
How much should I charge to write a contract? ›Contract drafting costs range between $200 and $800 for a simple contract and $1,000 and $5,000 for a complex contract. Contract attorneys can offer hourly or flat fee contract drafting services.
Can you hand write a contract? ›Handwritten contracts are legally binding if they meet the necessary conditions that apply to all contracts: mutual agreement, capacity, consideration, and legal validity. There are no legal differences between typed and handwritten agreements when it comes to enforceability.
How can I send my ERC20 token to smart contract? ›Sending ERC20 tokens is very easy with MetaMask. You just need to paste the ERC20 address, ensure that you have enough ETH to cover the transaction fee, and click the “Send” button.
How do you write an ERC20 token? ›- Set the events of the token.
- Set the name, symbol, and decimal of the token.
- Declare the total supply.
- Set the amount of the total supply and the balances.
- Get the balance of an owner.
- Transfer tokens to an account.
- Approve a token transfer.
How much does it cost to deploy an ERC20 smart contract? ›
However, the average cost per transaction is $0.0015 – $0.0025 depending on the gas price. This means that for every 10,000 transactions, you can expect to spend around $150 – $300 in fees. The cost of deploying a smart contract will also depend on how quickly you want to get it live.
How long does it take to send ERC20? ›CoinList waits for 30 confirmations to consider an ETH or ERC-20 transaction final. Although typically this should only take about ~5 minutes,this can take anywhere from five minutes to four hours. You can see the number of confirmations in your CoinList wallet.
How much does it cost to create ERC20 token? ›On average if you are looking to start a decent ICO project, it will cost you between $500-$2000 for creating millions of ERC20 tokens.
Do I need ETH to send ERC20 tokens? ›Yes. All ERC20 transactions require Ethereum (ETH) to pay for transactions on the Ethereum network. This means to send an ERC20 token from your Exodus wallet, you will need ETH to pay for the Ethereum network fee, which is also known as gas.
How many decimals are in an ERC20 token? ›When minting tokens or transferring them around, you will be actually sending the number num GLD * 10^decimals . By default, ERC20 uses a value of 18 for decimals .
How much is ERC20 in usd? ›Conversion tables
The current value of 1 ERC20 is $0.01 USD.
Anyone can write a smart contract and deploy it to the network. You just need to learn how to code in a smart contract language, and have enough ETH to deploy your contract. Deploying a smart contract is technically a transaction, so you need to pay Gas in the same way you need to pay gas for a simple ETH transfer.
How long does it take to make a smart contract? ›In general it takes anywhere from two weeks (one sprint) to two months to complete Discovery.
Why is ERC20 fees so high? ›The main reason for the high fees of Bitcoin miners is the organic market. Bitcoin block size is 1MB, which means that miners can only confirm 1MB of exchange value for each square (like clockwork). This is why the excavator fees on ERC-20 are so high.
How much gas does an ERC20 take? ›If you're sending ERC20 to your friend, you'll need around 65,000 gas (and 21000 for ETH) for the transaction at the moment. But if you want to seal the deal on Uniswap, your estimated gas limit would go up to 200,000. The gas limit refers to the maximum amount of gas users would use for a transaction.
Who pays for smart contract execution? ›
With every transaction client pays the fee, in gas, and this gas will be used to execute called smart contract function. the one who send the transaction will have to pay the gas cost of executing that particular function in the contract.