dev
dart 206 lines 10.9 KB
Raw
1 import 'dart:typed_data';
2
3 import 'package:web3dart/web3dart.dart' as web3;
4
5 final ethereumContractAbi = web3.ContractAbi.fromJson(
6 '[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]',
7 'Erc20');
8
9 /// Interface of the ERC20 standard as defined in the EIP.
10 class ERC20 extends web3.GeneratedContract {
11 /// Constructor.
12 ERC20({
13 required web3.EthereumAddress address,
14 required web3.Web3Client client,
15 int? chainId,
16 }) : super(web3.DeployedContract(ethereumContractAbi, address), client, chainId);
17
18 /// Returns the remaining number of tokens that [spender] will be allowed to spend on behalf of [owner] through [transferFrom]. This is zero by default. This value changes when [approve] or [transferFrom] are called.
19 ///
20 /// The optional [atBlock] parameter can be used to view historical data. When
21 /// set, the function will be evaluated in the specified block. By default, the
22 /// latest on-chain block will be used.
23 Future<BigInt> allowance(
24 web3.EthereumAddress owner,
25 web3.EthereumAddress spender, {
26 web3.BlockNum? atBlock,
27 }) async {
28 final function = self.abi.functions[0];
29 assert(checkSignature(function, 'dd62ed3e'));
30 final params = [owner, spender];
31 final response = await read(function, params, atBlock);
32 return (response[0] as BigInt);
33 }
34
35 /// Sets [amount] as the allowance of [spender] over the caller's tokens. Returns a boolean value indicating whether the operation succeeded. IMPORTANT: Beware that changing an allowance with this method brings the risk that someone may use both the old and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards: https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 Emits an [Approval] event.
36 ///
37 /// The optional [transaction] parameter can be used to override parameters
38 /// like the gas price, nonce and max gas. The `data` and `to` fields will be
39 /// set by the contract.
40 Future<Uint8List> approve(
41 web3.EthereumAddress spender,
42 BigInt amount, {
43 required web3.Credentials credentials,
44 web3.Transaction? transaction,
45 }) async {
46 final function = self.abi.functions[1];
47 assert(checkSignature(function, '095ea7b3'));
48 final params = [spender, amount];
49 return writeRaw(credentials, transaction, function, params);
50 }
51
52 /// Returns the amount of tokens owned by [account].
53 ///
54 /// The optional [atBlock] parameter can be used to view historical data. When
55 /// set, the function will be evaluated in the specified block. By default, the
56 /// latest on-chain block will be used.
57 Future<BigInt> balanceOf(
58 web3.EthereumAddress account, {
59 web3.BlockNum? atBlock,
60 }) async {
61 final function = self.abi.functions[2];
62 assert(checkSignature(function, '70a08231'));
63 final params = [account];
64 final response = await read(function, params, atBlock);
65 return (response[0] as BigInt);
66 }
67
68 /// Returns the decimal precision of the token.
69 ///
70 /// The optional [atBlock] parameter can be used to view historical data. When
71 /// set, the function will be evaluated in the specified block. By default, the
72 /// latest on-chain block will be used.
73 Future<BigInt> decimals({web3.BlockNum? atBlock}) async {
74 final function = self.abi.functions[3];
75 assert(checkSignature(function, '313ce567'));
76 final params = [];
77 final response = await read(function, params, atBlock);
78 return (response[0] as BigInt);
79 }
80
81 /// Returns the name of the token.
82 ///
83 /// The optional [atBlock] parameter can be used to view historical data. When
84 /// set, the function will be evaluated in the specified block. By default, the
85 /// latest on-chain block will be used.
86 Future<String> name({web3.BlockNum? atBlock}) async {
87 final function = self.abi.functions[4];
88 assert(checkSignature(function, '06fdde03'));
89 final params = [];
90 final response = await read(function, params, atBlock);
91 return (response[0] as String);
92 }
93
94 /// Returns the symbol of the token.
95 ///
96 /// The optional [atBlock] parameter can be used to view historical data. When
97 /// set, the function will be evaluated in the specified block. By default, the
98 /// latest on-chain block will be used.
99 Future<String> symbol({web3.BlockNum? atBlock}) async {
100 final function = self.abi.functions[5];
101 assert(checkSignature(function, '95d89b41'));
102 final params = [];
103 final response = await read(function, params, atBlock);
104 return (response[0] as String);
105 }
106
107 /// Returns the amount of tokens in existence.
108 ///
109 /// The optional [atBlock] parameter can be used to view historical data. When
110 /// set, the function will be evaluated in the specified block. By default, the
111 /// latest on-chain block will be used.
112 Future<BigInt> totalSupply({web3.BlockNum? atBlock}) async {
113 final function = self.abi.functions[6];
114 assert(checkSignature(function, '18160ddd'));
115 final params = [];
116 final response = await read(function, params, atBlock);
117 return (response[0] as BigInt);
118 }
119
120 /// Moves [amount] tokens from the caller's account to [recipient]. Returns a boolean value indicating whether the operation succeeded. Emits a [Transfer] event.
121 ///
122 /// The optional [transaction] parameter can be used to override parameters
123 /// like the gas price, nonce and max gas. The `data` and `to` fields will be
124 /// set by the contract.
125 Future<Uint8List> transfer(
126 web3.EthereumAddress recipient,
127 BigInt amount, {
128 required web3.Credentials credentials,
129 web3.Transaction? transaction,
130 }) async {
131 final function = self.abi.functions[7];
132 assert(checkSignature(function, 'a9059cbb'));
133 final params = [recipient, amount];
134 return writeRaw(credentials, transaction, function, params);
135 }
136
137 /// Moves [amount] tokens from [sender] to [recipient] using the allowance mechanism. [amount] is then deducted from the caller's allowance. Returns a boolean value indicating whether the operation succeeded. Emits a [Transfer] event.
138 ///
139 /// The optional [transaction] parameter can be used to override parameters
140 /// like the gas price, nonce and max gas. The `data` and `to` fields will be
141 /// set by the contract.
142 Future<Uint8List> transferFrom(
143 web3.EthereumAddress sender, web3.EthereumAddress recipient, BigInt amount,
144 {required web3.Credentials credentials, web3.Transaction? transaction}) async {
145 final function = self.abi.functions[8];
146 assert(checkSignature(function, '23b872dd'));
147 final params = [sender, recipient, amount];
148 return writeRaw(credentials, transaction, function, params);
149 }
150
151 /// Returns a live stream of all Approval events emitted by this contract.
152 Stream<Approval> approvalEvents({web3.BlockNum? fromBlock, web3.BlockNum? toBlock}) {
153 final event = self.event('Approval');
154 final filter = web3.FilterOptions.events(
155 contract: self, event: event, fromBlock: fromBlock, toBlock: toBlock);
156 return client.events(filter).map((web3.FilterEvent result) {
157 final decoded = event.decodeResults(result.topics!, result.data!);
158 return Approval._(decoded);
159 });
160 }
161
162 /// Returns a live stream of all Transfer events emitted by this contract.
163 Stream<Transfer> transferEvents({web3.BlockNum? fromBlock, web3.BlockNum? toBlock}) {
164 final event = self.event('Transfer');
165 final filter = web3.FilterOptions.events(
166 contract: self, event: event, fromBlock: fromBlock, toBlock: toBlock);
167 return client.events(filter).map((web3.FilterEvent result) {
168 final decoded = event.decodeResults(result.topics!, result.data!);
169 return Transfer._(decoded);
170 });
171 }
172 }
173
174 /// Emitted when the allowance of a [spender] for an [owner] is set by a call to [ERC20.approve]. [value] is the new allowance.
175 class Approval {
176 Approval._(List<dynamic> response)
177 : owner = (response[0] as web3.EthereumAddress),
178 spender = (response[1] as web3.EthereumAddress),
179 value = (response[2] as BigInt);
180
181 /// The owner address.
182 final web3.EthereumAddress owner;
183
184 /// The spender address.
185 final web3.EthereumAddress spender;
186
187 /// Value.
188 final BigInt value;
189 }
190
191 /// Emitted when [value] tokens are moved from one account ([from]) to another ([to]). Note that [value] may be zero.
192 class Transfer {
193 Transfer._(List<dynamic> response)
194 : from = (response[0] as web3.EthereumAddress),
195 to = (response[1] as web3.EthereumAddress),
196 value = (response[2] as BigInt);
197
198 /// From address.
199 final web3.EthereumAddress from;
200
201 /// To address.
202 final web3.EthereumAddress to;
203
204 /// Value.
205 final BigInt value;
206 }