스마트 컨트랙트 코드 balanceof - seumateu keonteulaegteu kodeu balanceof

ERC-20 defines a common list of rules that all fungible Ethereum tokens should adhere to. Consequently, this token standard empowers developers of all types to accurately predict how new tokens will function within the larger Ethereum system. This simplifies and eases developers’ tasks, because they can proceed with their work, knowing that each and every new project won’t need to be redone every time a new token is released, as long as the token follows the rules.

Here is, presented as an interface, the functions an ERC-20 must implement. If you’re not sure about what is an interface: check our article about OOP programming in Solidity.

1pragma solidity ^0.6.0;

2

3interface IERC20 {

4

5 function totalSupply() external view returns (uint256);

6 function balanceOf(address account) external view returns (uint256);

7 function allowance(address owner, address spender) external view returns (uint256);

8

9 function transfer(address recipient, uint256 amount) external returns (bool);

10 function approve(address spender, uint256 amount) external returns (bool);

11 function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

12

13

14 event Transfer(address indexed from, address indexed to, uint256 value);

15 event Approval(address indexed owner, address indexed spender, uint256 value);

16}

17

Show all

스마트 컨트랙트 코드 balanceof - seumateu keonteulaegteu kodeu balanceof
Copy

Here is a line-by-line explainer of what every function is for. After this we’ll present a simple implementation of the ERC-20 token.

Getters

1function totalSupply() external view returns (uint256);

2

Copy

Returns the amount of tokens in existence. This function is a getter and does not modify the state of the contract. Keep in mind that there are no floats in Solidity. Therefore most tokens adopt 18 decimals and will return the total supply and other results as followed 1000000000000000000 for 1 token. Not every token has 18 decimals and this is something you really need to watch for when dealing with tokens.

1function balanceOf(address account) external view returns (uint256);

2

Copy

Returns the amount of tokens owned by an address (

1function totalSupply() external view returns (uint256);

2

Copy

0). This function is a getter and does not modify the state of the contract.

1function allowance(address owner, address spender) external view returns (uint256);

2

Copy

The ERC-20 standard allows an address to give an allowance to another address to be able to retrieve tokens from it. This getter returns the remaining number of tokens that the

1function totalSupply() external view returns (uint256);

2

Copy

1 will be allowed to spend on behalf of

1function totalSupply() external view returns (uint256);

2

Copy

2. This function is a getter and does not modify the state of the contract and should return 0 by default.

Functions

1function transfer(address recipient, uint256 amount) external returns (bool);

2

Copy

Moves the

1function totalSupply() external view returns (uint256);

2

Copy

3 of tokens from the function caller address (

1function totalSupply() external view returns (uint256);

2

Copy

4) to the recipient address. This function emits the

1function totalSupply() external view returns (uint256);

2

Copy

5 event defined later. It returns true if the transfer was possible.

1function approve(address spender, uint256 amount) external returns (bool);

2

Copy

Set the amount of

1function totalSupply() external view returns (uint256);

2

Copy

6 the

1function totalSupply() external view returns (uint256);

2

Copy

1 is allowed to transfer from the function caller (

1function totalSupply() external view returns (uint256);

2

Copy

4) balance. This function emits the Approval event. The function returns whether the allowance was successfully set.

1function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

2

Copy

Moves the

1function totalSupply() external view returns (uint256);

2

Copy

3 of tokens from

1function balanceOf(address account) external view returns (uint256);

2

Copy

0 to

1function balanceOf(address account) external view returns (uint256);

2

Copy

1 using the allowance mechanism. amount is then deducted from the caller’s allowance. This function emits the

1function totalSupply() external view returns (uint256);

2

Copy

5 event.

Events

1event Transfer(address indexed from, address indexed to, uint256 value);

2

Copy

This event is emitted when the amount of tokens (value) is sent from the

1function balanceOf(address account) external view returns (uint256);

2

Copy

3 address to the

1function balanceOf(address account) external view returns (uint256);

2

Copy

4 address.

In the case of minting new tokens, the transfer is usually

1function balanceOf(address account) external view returns (uint256);

2

Copy

3 the 0x00..0000 address while in the case of burning tokens the transfer is

1function balanceOf(address account) external view returns (uint256);

2

Copy

4 0x00..0000.

1event Approval(address indexed owner, address indexed spender, uint256 value);

2

Copy

This event is emitted when the amount of tokens (

1function balanceOf(address account) external view returns (uint256);

2

Copy

7) is approved by the

1function totalSupply() external view returns (uint256);

2

Copy

2 to be used by the

1function totalSupply() external view returns (uint256);

2

Copy

1.

A basic implementation of ERC-20 tokens

Here is the most simple code to base your ERC-20 token from:

1pragma solidity ^0.8.0;

2

3interface IERC20 {

4

5 function totalSupply() external view returns (uint256);

6 function balanceOf(address account) external view returns (uint256);

7 function allowance(address owner, address spender) external view returns (uint256);

8

9 function transfer(address recipient, uint256 amount) external returns (bool);

10 function approve(address spender, uint256 amount) external returns (bool);

11 function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

12

13

14 event Transfer(address indexed from, address indexed to, uint256 value);

15 event Approval(address indexed owner, address indexed spender, uint256 value);

16}

17

18

19contract ERC20Basic is IERC20 {

20

21 string public constant name = "ERC20Basic";

22 string public constant symbol = "ERC";

23 uint8 public constant decimals = 18;

24

25

26 mapping(address => uint256) balances;

27

28 mapping(address => mapping (address => uint256)) allowed;

29

30 uint256 totalSupply_ = 10 ether;

31

32

33 constructor() {

34 balances[msg.sender] = totalSupply_;

35 }

36

37 function totalSupply() public override view returns (uint256) {

38 return totalSupply_;

39 }

40

41 function balanceOf(address tokenOwner) public override view returns (uint256) {

42 return balances[tokenOwner];

43 }

44

45 function transfer(address receiver, uint256 numTokens) public override returns (bool) {

46 require(numTokens <= balances[msg.sender]);

47 balances[msg.sender] = balances[msg.sender]-numTokens;

48 balances[receiver] = balances[receiver]+numTokens;

49 emit Transfer(msg.sender, receiver, numTokens);

50 return true;

51 }

52

53 function approve(address delegate, uint256 numTokens) public override returns (bool) {

54 allowed[msg.sender][delegate] = numTokens;

55 emit Approval(msg.sender, delegate, numTokens);

56 return true;

57 }

58

59 function allowance(address owner, address delegate) public override view returns (uint) {

60 return allowed[owner][delegate];

61 }

62

63 function transferFrom(address owner, address buyer, uint256 numTokens) public override returns (bool) {

64 require(numTokens <= balances[owner]);

65 require(numTokens <= allowed[owner][msg.sender]);

66

67 balances[owner] = balances[owner]-numTokens;

68 allowed[owner][msg.sender] = allowed[owner][msg.sender]-numTokens;

69 balances[buyer] = balances[buyer]+numTokens;

70 emit Transfer(owner, buyer, numTokens);

71 return true;

72 }

73}

74

Show all

Copy

Another excellent implementation of the ERC-20 token standard is the OpenZeppelin ERC-20 implementation.