Class: ERC20

Inherits:
Contract
  • Object
show all
Defined in:
lib/rubysol/contracts/erc20.rb

Overview

abstract: true

Instance Method Summary collapse

Instance Method Details

#_burn(from:, amount:) ⇒ Object



71
72
73
74
75
76
# File 'lib/rubysol/contracts/erc20.rb', line 71

def _burn( from:, amount: )
  @balanceOf[from] -= amount
  @totalSupply -= amount
  
  log Transfer, from: from, to: address(0), amount: amount
end

#_mint(to:, amount:) ⇒ Object



63
64
65
66
67
68
# File 'lib/rubysol/contracts/erc20.rb', line 63

def _mint( to:, amount: )
  @totalSupply += amount
  @balanceOf[to] += amount
  
  log Transfer, from: address(0), to: to, amount: amount
end

#approve(spender:, amount:) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/rubysol/contracts/erc20.rb', line 25

def approve( spender:, amount: )
  @allowance[msg.sender][spender] = amount
  
  log Approval, owner: msg.sender, spender: spender, amount: amount
  
  true
end

#constructor(name:, symbol:, decimals:) ⇒ Object



18
19
20
21
22
# File 'lib/rubysol/contracts/erc20.rb', line 18

def constructor(name:, symbol:, decimals:)
  @name     = name
  @symbol   = symbol
  @decimals = decimals
end

#transfer(to:, amount:) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/rubysol/contracts/erc20.rb', line 34

def transfer( to:, amount: )
  assert @balanceOf[msg.sender] >= amount, "Insufficient balance"
  
  @balanceOf[msg.sender] -= amount
  @balanceOf[to] += amount

  log Transfer, from: msg.sender, to: to, amount: amount
  
  true
end

#transferFrom(from:, to:, amount:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubysol/contracts/erc20.rb', line 46

def transferFrom( from:, to:, amount: )
  allowed = @allowance[from][msg.sender]
  
  assert @balanceOf[from] >= amount, "Insufficient balance"
  assert allowed >= amount, "Insufficient allowance"
  
  @allowance[from][msg.sender] = allowed - amount
  
  @balanceOf[from] -= amount
  @balanceOf[to] += amount
  
  log Transfer, from: from, to: to, amount: amount
  
  true
end