Skip to content

Commit 4fd6e58

Browse files
jolycaojolycao
jolycao
authored and
jolycao
committed
fix test code for 23-erc865-and-erc875
1 parent aa27645 commit 4fd6e58

File tree

10 files changed

+336
-390
lines changed

10 files changed

+336
-390
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@
7575
19. [brownie](basic/19-brownie/README.md)
7676
20. [scaffold-flash-loan & dex arbitrage](basic/20-scaffold-flash-loan/readme.md)
7777
21. [scaffold-Lender](basic/21-scaffold-lender/README.md)
78-
22. [CryptoKitties](https://zhuanlan.zhihu.com/p/34690916)
78+
22. [scaffold-zk](basic/22-scaffold-zk/readme.md)
7979
23. [ERC865 & ERC875](basic/23-erc865-and-erc875/README.md)
80-
24. [scaffold-zk](basic/22-scaffold-zk/readme.md)
80+
24. [CryptoKitties](https://zhuanlan.zhihu.com/p/34690916)
8181
25. [multi-sig-wallet](basic/25-multi-sig-wallet/readme.md)
8282
26. [snapshot](https://www.chainnews.com/articles/038258049958.htm)
8383
27. [Quadratic vote](basic/27-quadratic-vote/README.md)
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PRIVATE_KEY=xxxxxxxxxxxxxxxx

basic/23-erc865-and-erc875/README.md

+24-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
## ERC865
1+
## 概要
2+
### ERC865
3+
通过 test_DToken.js 介绍了如何使用“无Gas”交易,阐明了“无Gas”实际上意味着将Gas成本转移给其他人。
4+
使用此模式有很多好处,因此,它被广泛使用. 签名允许将交易 gas 成本从用户转移到服务提供商, 从而在许多情况下消除了相当大的障碍. 它还允许实现更高级的委派模式, 通常会对UX进行相当大的改进
25

3-
代理合约,转账无需gas费,离线发送代理方上链
4-
5-
## ERC875
6-
支持原子交易的token
7-
8-
## EIP712
9-
签名协议
6+
### ERC875
7+
支持原子交易的token
8+
9+
### EIP712
10+
通过签名功能函数的好处是用户可以免费完成委托或投票交易, 同时会有可信的第三方花费gas费用将投票结果写到区块链中. 在本次教程中,我们重点展示这类函数的例子.
11+
12+
## 测试步骤
13+
- 安装依赖
14+
```
15+
yarn
16+
```
17+
18+
- 测试代理合约
19+
```
20+
npx hardhat test test/test_DToken.js
21+
```
1022

1123
## 参考链接
12-
https://github.com/propsproject/props-token-distribution
13-
EIP712 :https://learnblockchain.cn/article/1357
14-
https://learnblockchain.cn/2019/04/24/token-EIP712
15-
https://learnblockchain.cn/article/1496
24+
https://github.com/propsproject/props-token-distribution
25+
https://learnblockchain.cn/article/1357
26+
https://learnblockchain.cn/2019/04/24/token-EIP712
27+
https://learnblockchain.cn/article/1496

basic/23-erc865-and-erc875/contracts/ERC865Token.sol

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ contract ERC865Token is Initializable, ERC20, IERC865 {
7373
require(_spender != address(0),"Invalid _spender address");
7474

7575
bytes32 hashedParams = getApprovePreSignedHash(address(this), _spender, _value, _fee, _nonce);
76-
address from = ECDSA.recover(hashedParams, _signature);
76+
address from = ECDSA.recover(ECDSA.toEthSignedMessageHash(hashedParams), _signature);
7777
require(from != address(0),"Invalid from address recovered");
7878
bytes32 hashedTx = keccak256(abi.encodePacked(from, hashedParams));
7979
require(hashedTxs[hashedTx] == false,"Transaction hash was already used");
@@ -107,7 +107,7 @@ contract ERC865Token is Initializable, ERC20, IERC865 {
107107
require(_spender != address(0),"Invalid _spender address");
108108

109109
bytes32 hashedParams = getIncreaseAllowancePreSignedHash(address(this), _spender, _addedValue, _fee, _nonce);
110-
address from = ECDSA.recover(hashedParams, _signature);
110+
address from = ECDSA.recover(ECDSA.toEthSignedMessageHash(hashedParams), _signature);
111111
require(from != address(0),"Invalid from address recovered");
112112
bytes32 hashedTx = keccak256(abi.encodePacked(from, hashedParams));
113113
require(hashedTxs[hashedTx] == false,"Transaction hash was already used");
@@ -141,7 +141,7 @@ contract ERC865Token is Initializable, ERC20, IERC865 {
141141
require(_spender != address(0),"Invalid _spender address");
142142

143143
bytes32 hashedParams = getDecreaseAllowancePreSignedHash(address(this), _spender, _subtractedValue, _fee, _nonce);
144-
address from = ECDSA.recover(hashedParams, _signature);
144+
address from = ECDSA.recover(ECDSA.toEthSignedMessageHash(hashedParams), _signature);
145145
require(from != address(0),"Invalid from address recovered");
146146
bytes32 hashedTx = keccak256(abi.encodePacked(from, hashedParams));
147147
require(hashedTxs[hashedTx] == false,"Transaction hash was already used");
@@ -179,7 +179,7 @@ contract ERC865Token is Initializable, ERC20, IERC865 {
179179

180180
bytes32 hashedParams = getTransferFromPreSignedHash(address(this), _from, _to, _value, _fee, _nonce);
181181

182-
address spender = ECDSA.recover(hashedParams, _signature);
182+
address spender = ECDSA.recover(ECDSA.toEthSignedMessageHash(hashedParams), _signature);
183183
require(spender != address(0),"Invalid spender address recovered");
184184
bytes32 hashedTx = keccak256(abi.encodePacked(spender, hashedParams));
185185
require(hashedTxs[hashedTx] == false,"Transaction hash was already used");
+43-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
require("@nomiclabs/hardhat-waffle");
2+
const fs = require("fs");
3+
require('dotenv').config()
4+
25

36
// This is a sample Hardhat task. To learn how to create your own go to
47
// https://hardhat.org/guides/create-task.html
@@ -10,13 +13,49 @@ task("accounts", "Prints the list of accounts", async () => {
1013
}
1114
});
1215

13-
// You need to export an object to set up your config
14-
// Go to https://hardhat.org/config/ to learn more
16+
function mnemonic() {
17+
18+
return process.env.PRIVATE_KEY;
19+
20+
}
1521

1622
/**
1723
* @type import('hardhat/config').HardhatUserConfig
1824
*/
1925
module.exports = {
2026
solidity: "0.4.25",
21-
};
22-
27+
networks: {
28+
localhost: {
29+
url: "http://localhost:8545",
30+
//gasPrice: 125000000000,//you can adjust gasPrice locally to see how much it will cost on production
31+
/*
32+
notice no mnemonic here? it will just use account 0 of the hardhat node to deploy
33+
(you can put in a mnemonic here to set the deployer locally)
34+
*/
35+
},
36+
rinkeby: {
37+
url: "https://rinkeby.infura.io/v3/0aae8358bfe04803b8e75bb4755eaf07", //<---- YOUR INFURA ID! (or it won't work)
38+
accounts: [
39+
mnemonic()
40+
],
41+
},
42+
kovan: {
43+
url: "https://kovan.infura.io/v3/0aae8358bfe04803b8e75bb4755eaf07", //<---- YOUR INFURA ID! (or it won't work)
44+
accounts: [
45+
mnemonic()
46+
],
47+
},
48+
mainnet: {
49+
url: "https://mainnet.infura.io/v3/0aae8358bfe04803b8e75bb4755eaf07", //<---- YOUR INFURA ID! (or it won't work)
50+
accounts: [
51+
mnemonic()
52+
],
53+
},
54+
ropsten: {
55+
url: "https://ropsten.infura.io/v3/0aae8358bfe04803b8e75bb4755eaf07", //<---- YOUR INFURA ID! (or it won't work)
56+
accounts: [
57+
mnemonic()
58+
],
59+
},
60+
}
61+
};

basic/23-erc865-and-erc875/package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
"devDependencies": {
44
"@nomiclabs/hardhat-ethers": "^2.0.2",
55
"@nomiclabs/hardhat-waffle": "^2.0.1",
6+
"bignumber.js": "^8.0.2",
67
"chai": "^4.3.4",
78
"ethereum-waffle": "^3.3.0",
9+
"ethereumjs-util": "^5.1.3",
810
"ethers": "^5.3.1",
9-
"hardhat": "^2.3.3",
10-
"bignumber.js": "^8.0.2",
11-
"ethereumjs-util": "^5.1.3"
11+
"hardhat": "^2.3.3"
12+
},
13+
"dependencies": {
14+
"dotenv": "^10.0.0"
1215
}
1316
}

0 commit comments

Comments
 (0)