@1inch/solidity-utils 1.2.5 → 1.2.7

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.
package/README.md CHANGED
@@ -40,7 +40,7 @@ This repository contains frequently used smart contracts, libraries and interfac
40
40
  |utils|`signMessage(signer, messageHex) `|signs `messageHex` with `signer` and patchs ganache's signature to geth's version|
41
41
  |utils|`countInstructions(txHash, instruction)`|counts amount of `instruction` in transaction with `txHash` hash|
42
42
  |profileEVM|`profileEVM(txHash, instruction, optionalTraceFile)`|the same as the `countInstructions()` with option of writing all trace to `optionalTraceFile`|
43
- |profileEVM|`gasspectEVM(txHash, optionalTraceFile)`|returns all used operations in `txHash` transaction and their costs with option of writing all trace to `optionalTraceFile`|
43
+ |profileEVM|`gasspectEVM(txHash, options, optionalTraceFile)`|returns all used operations in `txHash` transaction with `options` and their costs with option of writing all trace to `optionalTraceFile`|
44
44
 
45
45
  ### UTILS
46
46
 
package/js/permit.js ADDED
@@ -0,0 +1,110 @@
1
+ const { constants } = require('@openzeppelin/test-helpers');
2
+ const ethSigUtil = require('eth-sig-util');
3
+ const { fromRpcSig } = require('ethereumjs-util');
4
+ const { artifacts } = require('hardhat');
5
+ const ERC20Permit = artifacts.require('@openzeppelin/contracts/token/ERC20/extensions/draft-ERC20Permit.sol:ERC20Permit');
6
+ const ERC20PermitLikeDai = artifacts.require('DaiLikePermitMock');
7
+
8
+ const defaultDeadline = constants.MAX_UINT256;
9
+
10
+ const EIP712Domain = [
11
+ { name: 'name', type: 'string' },
12
+ { name: 'version', type: 'string' },
13
+ { name: 'chainId', type: 'uint256' },
14
+ { name: 'verifyingContract', type: 'address' },
15
+ ];
16
+
17
+ const Permit = [
18
+ { name: 'owner', type: 'address' },
19
+ { name: 'spender', type: 'address' },
20
+ { name: 'value', type: 'uint256' },
21
+ { name: 'nonce', type: 'uint256' },
22
+ { name: 'deadline', type: 'uint256' },
23
+ ];
24
+
25
+ const DaiLikePermit = [
26
+ { name: 'holder', type: 'address' },
27
+ { name: 'spender', type: 'address' },
28
+ { name: 'nonce', type: 'uint256' },
29
+ { name: 'expiry', type: 'uint256' },
30
+ { name: 'allowed', type: 'bool' },
31
+ ];
32
+
33
+ function trim0x (bigNumber) {
34
+ const s = bigNumber.toString();
35
+ if (s.startsWith('0x')) {
36
+ return s.substring(2);
37
+ }
38
+ return s;
39
+ }
40
+
41
+ function cutSelector (data) {
42
+ const hexPrefix = '0x';
43
+ return hexPrefix + data.substr(hexPrefix.length + 8);
44
+ }
45
+
46
+ function domainSeparator (name, version, chainId, verifyingContract) {
47
+ return '0x' + ethSigUtil.TypedDataUtils.hashStruct(
48
+ 'EIP712Domain',
49
+ { name, version, chainId, verifyingContract },
50
+ { EIP712Domain },
51
+ ).toString('hex');
52
+ }
53
+
54
+ function buildData (name, version, chainId, verifyingContract, owner, spender, value, nonce, deadline = defaultDeadline) {
55
+ return {
56
+ primaryType: 'Permit',
57
+ types: { EIP712Domain, Permit },
58
+ domain: { name, version, chainId, verifyingContract },
59
+ message: { owner, spender, value, nonce, deadline },
60
+ };
61
+ }
62
+
63
+ function buildDataLikeDai (name, version, chainId, verifyingContract, holder, spender, nonce, allowed, expiry = defaultDeadline) {
64
+ return {
65
+ primaryType: 'Permit',
66
+ types: { EIP712Domain, Permit: DaiLikePermit },
67
+ domain: { name, version, chainId, verifyingContract },
68
+ message: { holder, spender, nonce, expiry, allowed },
69
+ };
70
+ }
71
+
72
+ async function getPermit (owner, ownerPrivateKey, token, tokenVersion, chainId, spender, value, deadline = defaultDeadline) {
73
+ const permitContract = await ERC20Permit.at(token.address);
74
+ const nonce = await permitContract.nonces(owner);
75
+ const name = await permitContract.name();
76
+ const data = buildData(name, tokenVersion, chainId, token.address, owner, spender, value, nonce, deadline);
77
+ const signature = ethSigUtil.signTypedMessage(Buffer.from(trim0x(ownerPrivateKey), 'hex'), { data });
78
+ const { v, r, s } = fromRpcSig(signature);
79
+ const permitCall = permitContract.contract.methods.permit(owner, spender, value, deadline, v, r, s).encodeABI();
80
+ return cutSelector(permitCall);
81
+ }
82
+
83
+ async function getPermitLikeDai (holder, holderPrivateKey, token, tokenVersion, chainId, spender, allowed, expiry = defaultDeadline) {
84
+ const permitContract = await ERC20PermitLikeDai.at(token.address);
85
+ const nonce = await permitContract.nonces(holder);
86
+ const name = await permitContract.name();
87
+ const data = buildDataLikeDai(name, tokenVersion, chainId, token.address, holder, spender, nonce, allowed, expiry);
88
+ const signature = ethSigUtil.signTypedMessage(Buffer.from(trim0x(holderPrivateKey), 'hex'), { data });
89
+ const { v, r, s } = fromRpcSig(signature);
90
+ const permitCall = permitContract.contract.methods.permit(holder, spender, nonce, expiry, allowed, v, r, s).encodeABI();
91
+ return cutSelector(permitCall);
92
+ }
93
+
94
+ function withTarget (target, data) {
95
+ return target.toString() + trim0x(data);
96
+ }
97
+
98
+ module.exports = {
99
+ defaultDeadline,
100
+ EIP712Domain,
101
+ Permit,
102
+ DaiLikePermit,
103
+ trim0x,
104
+ domainSeparator,
105
+ buildData,
106
+ buildDataLikeDai,
107
+ getPermit,
108
+ getPermitLikeDai,
109
+ withTarget,
110
+ };
package/js/profileEVM.js CHANGED
@@ -1,6 +1,12 @@
1
1
  const { promisify } = require('util');
2
2
  const fs = require('fs').promises;
3
3
 
4
+ const gasspectOptionsDefault = {
5
+ minOpGasCost: 300, // minimal gas cost of returned operations
6
+ args: false, // return operations arguments
7
+ res: false, // return operations results
8
+ };
9
+
4
10
  function _normalizeOp (ops, i) {
5
11
  if (ops[i].op === 'STATICCALL') {
6
12
  ops[i].gasCost = ops[i].gasCost - ops[i + 1].gas;
@@ -53,7 +59,10 @@ function _normalizeOp (ops, i) {
53
59
  if (ops[i].gasCost === 20000) {
54
60
  ops[i].op += '_I';
55
61
  }
56
- ops[i].res = ops[i + 1].stack[ops[i + 1].stack.length - 1];
62
+
63
+ if (ops[i].op.startsWith('SLOAD')) {
64
+ ops[i].res = ops[i + 1].stack[ops[i + 1].stack.length - 1];
65
+ }
57
66
  }
58
67
  if (ops[i].op === 'EXTCODESIZE') {
59
68
  ops[i].args = [
@@ -86,7 +95,9 @@ async function profileEVM (txHash, instruction, optionalTraceFile) {
86
95
  return str.split('"' + instruction.toUpperCase() + '"').length - 1;
87
96
  }
88
97
 
89
- async function gasspectEVM (txHash, options = {}, optionalTraceFile) {
98
+ async function gasspectEVM (txHash, options = gasspectOptionsDefault, optionalTraceFile) {
99
+ options = { ...gasspectOptionsDefault, ...options };
100
+
90
101
  const trace = await promisify(web3.currentProvider.send.bind(web3.currentProvider))({
91
102
  jsonrpc: '2.0',
92
103
  method: 'debug_traceTransaction',
@@ -111,8 +122,7 @@ async function gasspectEVM (txHash, options = {}, optionalTraceFile) {
111
122
  }
112
123
  }
113
124
 
114
- const minOpGasCost = options.minOpGasCost ? options.minOpGasCost : 300;
115
- const result = ops.filter(op => op.gasCost > minOpGasCost).map(op => op.traceAddress.join('-') + '-' + op.op +
125
+ const result = ops.filter(op => op.gasCost > options.minOpGasCost).map(op => op.traceAddress.join('-') + '-' + op.op +
116
126
  (options.args ? '(' + (op.args || []).join(',') + ')' : '') +
117
127
  (options.res ? (op.res ? ':0x' + op.res : '') : '') +
118
128
  ' = ' + op.gasCost);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1inch/solidity-utils",
3
- "version": "1.2.5",
3
+ "version": "1.2.7",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",