@yaswap/evm-contracts 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,242 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity 0.8.11;
3
+
4
+ /// @title Multicall3
5
+ /// @notice Aggregate results from multiple function calls
6
+ /// @dev Multicall & Multicall2 backwards-compatible
7
+ /// @dev Aggregate methods are marked `payable` to save 24 gas per call
8
+ /// @author Michael Elliot <mike@makerdao.com>
9
+ /// @author Joshua Levine <joshua@makerdao.com>
10
+ /// @author Nick Johnson <arachnid@notdot.net>
11
+ /// @author Andreas Bigger <andreas@nascent.xyz>
12
+ /// @author Matt Solomon <matt@mattsolomon.dev>
13
+ contract Multicall3 {
14
+ struct Call {
15
+ address target;
16
+ bytes callData;
17
+ }
18
+
19
+ struct Call3 {
20
+ address target;
21
+ bool allowFailure;
22
+ bytes callData;
23
+ }
24
+
25
+ struct Call3Value {
26
+ address target;
27
+ bool allowFailure;
28
+ uint256 value;
29
+ bytes callData;
30
+ }
31
+
32
+ struct Result {
33
+ bool success;
34
+ bytes returnData;
35
+ }
36
+
37
+ /// @notice Backwards-compatible call aggregation with Multicall
38
+ /// @param calls An array of Call structs
39
+ /// @return blockNumber The block number where the calls were executed
40
+ /// @return returnData An array of bytes containing the responses
41
+ function aggregate(Call[] calldata calls) public payable returns (uint256 blockNumber, bytes[] memory returnData) {
42
+ blockNumber = block.number;
43
+ uint256 length = calls.length;
44
+ returnData = new bytes[](length);
45
+ Call calldata call;
46
+ for (uint256 i = 0; i < length; ) {
47
+ bool success;
48
+ call = calls[i];
49
+ (success, returnData[i]) = call.target.call(call.callData);
50
+ require(success, 'Multicall3: call failed');
51
+ unchecked {
52
+ ++i;
53
+ }
54
+ }
55
+ }
56
+
57
+ /// @notice Backwards-compatible with Multicall2
58
+ /// @notice Aggregate calls without requiring success
59
+ /// @param requireSuccess If true, require all calls to succeed
60
+ /// @param calls An array of Call structs
61
+ /// @return returnData An array of Result structs
62
+ function tryAggregate(bool requireSuccess, Call[] calldata calls) public payable returns (Result[] memory returnData) {
63
+ uint256 length = calls.length;
64
+ returnData = new Result[](length);
65
+ Call calldata call;
66
+ for (uint256 i = 0; i < length; ) {
67
+ Result memory result = returnData[i];
68
+ call = calls[i];
69
+ (result.success, result.returnData) = call.target.call(call.callData);
70
+ if (requireSuccess) require(result.success, 'Multicall3: call failed');
71
+ unchecked {
72
+ ++i;
73
+ }
74
+ }
75
+ }
76
+
77
+ /// @notice Backwards-compatible with Multicall2
78
+ /// @notice Aggregate calls and allow failures using tryAggregate
79
+ /// @param calls An array of Call structs
80
+ /// @return blockNumber The block number where the calls were executed
81
+ /// @return blockHash The hash of the block where the calls were executed
82
+ /// @return returnData An array of Result structs
83
+ function tryBlockAndAggregate(bool requireSuccess, Call[] calldata calls)
84
+ public
85
+ payable
86
+ returns (
87
+ uint256 blockNumber,
88
+ bytes32 blockHash,
89
+ Result[] memory returnData
90
+ )
91
+ {
92
+ blockNumber = block.number;
93
+ blockHash = blockhash(block.number);
94
+ returnData = tryAggregate(requireSuccess, calls);
95
+ }
96
+
97
+ /// @notice Backwards-compatible with Multicall2
98
+ /// @notice Aggregate calls and allow failures using tryAggregate
99
+ /// @param calls An array of Call structs
100
+ /// @return blockNumber The block number where the calls were executed
101
+ /// @return blockHash The hash of the block where the calls were executed
102
+ /// @return returnData An array of Result structs
103
+ function blockAndAggregate(Call[] calldata calls)
104
+ public
105
+ payable
106
+ returns (
107
+ uint256 blockNumber,
108
+ bytes32 blockHash,
109
+ Result[] memory returnData
110
+ )
111
+ {
112
+ (blockNumber, blockHash, returnData) = tryBlockAndAggregate(true, calls);
113
+ }
114
+
115
+ /// @notice Aggregate calls, ensuring each returns success if required
116
+ /// @param calls An array of Call3 structs
117
+ /// @return returnData An array of Result structs
118
+ function aggregate3(Call3[] calldata calls) public payable returns (Result[] memory returnData) {
119
+ uint256 length = calls.length;
120
+ returnData = new Result[](length);
121
+ Call3 calldata calli;
122
+ for (uint256 i = 0; i < length; ) {
123
+ Result memory result = returnData[i];
124
+ calli = calls[i];
125
+ (result.success, result.returnData) = calli.target.call(calli.callData);
126
+ assembly {
127
+ // Revert if the call fails and failure is not allowed
128
+ // `allowFailure := calldataload(add(calli, 0x20))` and `success := mload(result)`
129
+ if iszero(or(calldataload(add(calli, 0x20)), mload(result))) {
130
+ // set "Error(string)" signature: bytes32(bytes4(keccak256("Error(string)")))
131
+ mstore(0x00, 0x08c379a000000000000000000000000000000000000000000000000000000000)
132
+ // set data offset
133
+ mstore(0x04, 0x0000000000000000000000000000000000000000000000000000000000000020)
134
+ // set length of revert string
135
+ mstore(0x24, 0x0000000000000000000000000000000000000000000000000000000000000017)
136
+ // set revert string: bytes32(abi.encodePacked("Multicall3: call failed"))
137
+ mstore(0x44, 0x4d756c746963616c6c333a2063616c6c206661696c6564000000000000000000)
138
+ revert(0x00, 0x64)
139
+ }
140
+ }
141
+ unchecked {
142
+ ++i;
143
+ }
144
+ }
145
+ }
146
+
147
+ /// @notice Aggregate calls with a msg value
148
+ /// @notice Reverts if msg.value is less than the sum of the call values
149
+ /// @param calls An array of Call3Value structs
150
+ /// @return returnData An array of Result structs
151
+ function aggregate3Value(Call3Value[] calldata calls) public payable returns (Result[] memory returnData) {
152
+ uint256 valAccumulator;
153
+ uint256 length = calls.length;
154
+ returnData = new Result[](length);
155
+ Call3Value calldata calli;
156
+ for (uint256 i = 0; i < length; ) {
157
+ Result memory result = returnData[i];
158
+ calli = calls[i];
159
+ uint256 val = calli.value;
160
+ // Humanity will be a Type V Kardashev Civilization before this overflows - andreas
161
+ // ~ 10^25 Wei in existence << ~ 10^76 size uint fits in a uint256
162
+ unchecked {
163
+ valAccumulator += val;
164
+ }
165
+ (result.success, result.returnData) = calli.target.call{ value: val }(calli.callData);
166
+ assembly {
167
+ // Revert if the call fails and failure is not allowed
168
+ // `allowFailure := calldataload(add(calli, 0x20))` and `success := mload(result)`
169
+ if iszero(or(calldataload(add(calli, 0x20)), mload(result))) {
170
+ // set "Error(string)" signature: bytes32(bytes4(keccak256("Error(string)")))
171
+ mstore(0x00, 0x08c379a000000000000000000000000000000000000000000000000000000000)
172
+ // set data offset
173
+ mstore(0x04, 0x0000000000000000000000000000000000000000000000000000000000000020)
174
+ // set length of revert string
175
+ mstore(0x24, 0x0000000000000000000000000000000000000000000000000000000000000017)
176
+ // set revert string: bytes32(abi.encodePacked("Multicall3: call failed"))
177
+ mstore(0x44, 0x4d756c746963616c6c333a2063616c6c206661696c6564000000000000000000)
178
+ revert(0x00, 0x84)
179
+ }
180
+ }
181
+ unchecked {
182
+ ++i;
183
+ }
184
+ }
185
+ // Finally, make sure the msg.value = SUM(call[0...i].value)
186
+ require(msg.value == valAccumulator, 'Multicall3: value mismatch');
187
+ }
188
+
189
+ /// @notice Returns the block hash for the given block number
190
+ /// @param blockNumber The block number
191
+ function getBlockHash(uint256 blockNumber) public view returns (bytes32 blockHash) {
192
+ blockHash = blockhash(blockNumber);
193
+ }
194
+
195
+ /// @notice Returns the block number
196
+ function getBlockNumber() public view returns (uint256 blockNumber) {
197
+ blockNumber = block.number;
198
+ }
199
+
200
+ /// @notice Returns the block coinbase
201
+ function getCurrentBlockCoinbase() public view returns (address coinbase) {
202
+ coinbase = block.coinbase;
203
+ }
204
+
205
+ /// @notice Returns the block difficulty
206
+ function getCurrentBlockDifficulty() public view returns (uint256 difficulty) {
207
+ difficulty = block.difficulty;
208
+ }
209
+
210
+ /// @notice Returns the block gas limit
211
+ function getCurrentBlockGasLimit() public view returns (uint256 gaslimit) {
212
+ gaslimit = block.gaslimit;
213
+ }
214
+
215
+ /// @notice Returns the block timestamp
216
+ function getCurrentBlockTimestamp() public view returns (uint256 timestamp) {
217
+ timestamp = block.timestamp;
218
+ }
219
+
220
+ /// @notice Returns the (ETH) balance of a given address
221
+ function getEthBalance(address addr) public view returns (uint256 balance) {
222
+ balance = addr.balance;
223
+ }
224
+
225
+ /// @notice Returns the block hash of the last block
226
+ function getLastBlockHash() public view returns (bytes32 blockHash) {
227
+ unchecked {
228
+ blockHash = blockhash(block.number - 1);
229
+ }
230
+ }
231
+
232
+ /// @notice Gets the base fee of the given block
233
+ /// @notice Can revert if the BASEFEE opcode is not implemented by the given chain
234
+ function getBasefee() public view returns (uint256 basefee) {
235
+ basefee = block.basefee;
236
+ }
237
+
238
+ /// @notice Returns the chain id
239
+ function getChainId() public view returns (uint256 chainid) {
240
+ chainid = block.chainid;
241
+ }
242
+ }
package/package.json ADDED
@@ -0,0 +1,94 @@
1
+ {
2
+ "name": "@yaswap/evm-contracts",
3
+ "version": "2.0.0",
4
+ "description": "",
5
+ "author": "yaswap",
6
+ "homepage": "",
7
+ "license": "ISC",
8
+ "dependencies": {
9
+ "@openzeppelin/contracts": "^4.4.2"
10
+ },
11
+ "devDependencies": {
12
+ "@codechecks/client": "^0.1.12",
13
+ "@commitlint/cli": "^16.1.0",
14
+ "@commitlint/config-conventional": "^16.0.0",
15
+ "@ethersproject/abi": "5.7.0",
16
+ "@ethersproject/abstract-signer": "5.7.0",
17
+ "@ethersproject/bignumber": "5.7.0",
18
+ "@ethersproject/bytes": "5.7.0",
19
+ "@ethersproject/providers": "5.7.0",
20
+ "@nomiclabs/hardhat-ethers": "^2.0.4",
21
+ "@nomiclabs/hardhat-etherscan": "^3.0.3",
22
+ "@nomiclabs/hardhat-waffle": "^2.0.2",
23
+ "@typechain/ethers-v5": "^9.0.0",
24
+ "@typechain/hardhat": "^4.0.0",
25
+ "@types/chai": "^4.3.0",
26
+ "@types/fs-extra": "^9.0.13",
27
+ "@types/mocha": "^9.1.0",
28
+ "@types/node": "^17.0.14",
29
+ "@typescript-eslint/eslint-plugin": "^5.10.2",
30
+ "@typescript-eslint/parser": "^5.10.2",
31
+ "chai": "^4.3.6",
32
+ "commitizen": "^4.2.4",
33
+ "cross-env": "^7.0.3",
34
+ "cz-conventional-changelog": "^3.3.0",
35
+ "dotenv": "^15.0.0",
36
+ "eslint": "^8.8.0",
37
+ "eslint-config-prettier": "^8.3.0",
38
+ "ethereum-waffle": "^3.4.0",
39
+ "ethers": "5.7.0",
40
+ "fs-extra": "^10.0.0",
41
+ "hardhat": "^2.8.3",
42
+ "hardhat-gas-reporter": "^1.0.7",
43
+ "husky": "^7.0.4",
44
+ "lint-staged": "^12.3.3",
45
+ "lodash": "4.17.21",
46
+ "mocha": "^9.2.0",
47
+ "pinst": "^2.1.6",
48
+ "prettier": "^2.5.1",
49
+ "prettier-plugin-solidity": "^1.0.0-beta.19",
50
+ "shelljs": "^0.8.5",
51
+ "shx": "^0.3.4",
52
+ "solhint": "^3.3.6",
53
+ "solhint-plugin-prettier": "^0.0.5",
54
+ "solidity-coverage": "^0.7.18",
55
+ "ts-generator": "^0.1.1",
56
+ "ts-node": "^10.4.0",
57
+ "typechain": "^7.0.0",
58
+ "typescript": "^4.5.5"
59
+ },
60
+ "files": [
61
+ "/contracts",
62
+ "artifacts/contracts/**/*.json",
63
+ "!artifacts/contracts/**/*.dbg.json"
64
+ ],
65
+ "keywords": [
66
+ "blockchain",
67
+ "ethereum",
68
+ "hardhat",
69
+ "smart-contracts",
70
+ "solidity"
71
+ ],
72
+ "publishConfig": {
73
+ "access": "public"
74
+ },
75
+ "scripts": {
76
+ "build": "yarn compile",
77
+ "clean": "shx rm -rf ./artifacts ./cache ./coverage ./src/types ./coverage.json",
78
+ "commit": "git-cz",
79
+ "compile": "cross-env TS_NODE_TRANSPILE_ONLY=true hardhat compile",
80
+ "coverage": "yarn typechain && hardhat coverage --solcoverjs ./.solcover.js --temp artifacts --testfiles \"test/**/*.ts\"",
81
+ "deploy": "hardhat deploy:HTLC",
82
+ "deploy-multicall": "hardhat deploy:Multicall",
83
+ "lint": "yarn lint:sol && yarn lint:ts && yarn prettier:check",
84
+ "lint:sol": "solhint --config ./.solhint.json --max-warnings 0 \"contracts/**/*.sol\"",
85
+ "lint:ts": "eslint --config ./.eslintrc.yaml --ignore-path ./.eslintignore --ext .js,.ts .",
86
+ "postpublish": "pinst --enable",
87
+ "prepublishOnly": "pinst --disable",
88
+ "prettier": "prettier --config ./.prettierrc.yaml --write \"**/*.{js,json,md,sol,ts}\"",
89
+ "prettier:check": "prettier --check --config ./.prettierrc.yaml \"**/*.{js,json,md,sol,ts}\"",
90
+ "test": "hardhat test",
91
+ "verify": "hardhat verify 0x133713376F69C1A67d7f3594583349DFB53d8166",
92
+ "typechain": "cross-env TS_NODE_TRANSPILE_ONLY=true hardhat typechain"
93
+ }
94
+ }