forked from Boilertalk/Web3.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathERC20.swift
156 lines (130 loc) · 5.9 KB
/
ERC20.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// ERC20.swift
// Web3
//
// Created by Josh Pyles on 6/19/18.
//
import Foundation
import BigInt
#if !Web3CocoaPods
import Web3
#endif
/// Base protocol for ERC20
public protocol ERC20Contract: EthereumContract {
static var Transfer: SolidityEvent { get }
static var Approval: SolidityEvent { get }
func totalSupply() -> SolidityInvocation
func balanceOf(address: EthereumAddress) -> SolidityInvocation
func approve(spender: EthereumAddress, value: BigUInt) -> SolidityInvocation
func allowance(owner: EthereumAddress, spender: EthereumAddress) -> SolidityInvocation
func transferFrom(from: EthereumAddress, to: EthereumAddress, value: BigUInt) -> SolidityInvocation
func transfer(to: EthereumAddress, value: BigUInt) -> SolidityInvocation
}
public protocol AnnotatedERC20: EthereumContract {
func name() -> SolidityInvocation
func symbol() -> SolidityInvocation
func decimals() -> SolidityInvocation
}
/// Generic implementation class. Use directly, or subclass to conveniently add your contract's events or methods.
open class GenericERC20Contract: StaticContract, ERC20Contract, AnnotatedERC20 {
public var address: EthereumAddress?
public let eth: Web3.Eth
open var constructor: SolidityConstructor?
open var events: [SolidityEvent] {
return [GenericERC20Contract.Transfer, GenericERC20Contract.Approval]
}
public required init(address: EthereumAddress?, eth: Web3.Eth) {
self.address = address
self.eth = eth
}
}
// MARK: - Implementation of ERC721 standard methods and events
public extension ERC20Contract {
static var Transfer: SolidityEvent {
let inputs: [SolidityEvent.Parameter] = [
SolidityEvent.Parameter(name: "_from", type: .address, indexed: true),
SolidityEvent.Parameter(name: "_to", type: .address, indexed: true),
SolidityEvent.Parameter(name: "_value", type: .uint256, indexed: false)
]
return SolidityEvent(name: "Transfer", anonymous: false, inputs: inputs)
}
static var Approval: SolidityEvent {
let inputs: [SolidityEvent.Parameter] = [
SolidityEvent.Parameter(name: "_owner", type: .address, indexed: true),
SolidityEvent.Parameter(name: "_spender", type: .address, indexed: true),
SolidityEvent.Parameter(name: "_value", type: .uint256, indexed: false)
]
return SolidityEvent(name: "Approval", anonymous: false, inputs: inputs)
}
func totalSupply() -> SolidityInvocation {
let outputs = [SolidityFunctionParameter(name: "_totalSupply", type: .uint256)]
let method = SolidityConstantFunction(name: "totalSupply", outputs: outputs, handler: self)
return method.invoke()
}
func balanceOf(address: EthereumAddress) -> SolidityInvocation {
let inputs = [SolidityFunctionParameter(name: "_owner", type: .address)]
let outputs = [SolidityFunctionParameter(name: "_balance", type: .uint256)]
let method = SolidityConstantFunction(name: "balanceOf", inputs: inputs, outputs: outputs, handler: self)
return method.invoke(address)
}
func approve(spender: EthereumAddress, value: BigUInt) -> SolidityInvocation {
let inputs = [
SolidityFunctionParameter(name: "_spender", type: .address),
SolidityFunctionParameter(name: "_value", type: .uint256)
]
let method = SolidityNonPayableFunction(name: "approve", inputs: inputs, handler: self)
return method.invoke(spender, value)
}
func allowance(owner: EthereumAddress, spender: EthereumAddress) -> SolidityInvocation {
let inputs = [
SolidityFunctionParameter(name: "_owner", type: .address),
SolidityFunctionParameter(name: "_spender", type: .address)
]
let outputs = [
SolidityFunctionParameter(name: "_remaining", type: .uint256)
]
let method = SolidityConstantFunction(name: "allowance", inputs: inputs, outputs: outputs, handler: self)
return method.invoke(owner, spender)
}
func transferFrom(from: EthereumAddress, to: EthereumAddress, value: BigUInt) -> SolidityInvocation {
let inputs = [
SolidityFunctionParameter(name: "_from", type: .address),
SolidityFunctionParameter(name: "_to", type: .address),
SolidityFunctionParameter(name: "_value", type: .uint256)
]
let method = SolidityNonPayableFunction(name: "transferFrom", inputs: inputs, handler: self)
return method.invoke(from, to, value)
}
func transfer(to: EthereumAddress, value: BigUInt) -> SolidityInvocation {
let inputs = [
SolidityFunctionParameter(name: "_to", type: .address),
SolidityFunctionParameter(name: "_value", type: .uint256)
]
let method = SolidityNonPayableFunction(name: "transfer", inputs: inputs, handler: self)
return method.invoke(to, value)
}
}
// MARK: - Implementation of ERC20 Metadata
public extension AnnotatedERC20 {
func name() -> SolidityInvocation {
let outputs = [
SolidityFunctionParameter(name: "_name", type: .string)
]
let method = SolidityConstantFunction(name: "name", inputs: [], outputs: outputs, handler: self)
return method.invoke()
}
func symbol() -> SolidityInvocation {
let outputs = [
SolidityFunctionParameter(name: "_symbol", type: .string)
]
let method = SolidityConstantFunction(name: "symbol", inputs: [], outputs: outputs, handler: self)
return method.invoke()
}
func decimals() -> SolidityInvocation {
let outputs = [
SolidityFunctionParameter(name: "_decimals", type: .uint8)
]
let method = SolidityConstantFunction(name: "decimals", inputs: [], outputs: outputs, handler: self)
return method.invoke()
}
}