-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFurryTrack.js
143 lines (116 loc) · 6.16 KB
/
FurryTrack.js
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
// test/FurryTrack-test.js
const { expect } = require("chai");
const helpers = require("@nomicfoundation/hardhat-network-helpers");
describe("FurryTrack contract", function () {
let FurryTrack;
let token721;
let _name='FurryTrack';
let _symbol='FURRY';
let dogOwner,vet,otheraccounts;
beforeEach(async function () {
FurryTrack = await ethers.getContractFactory("FurryTrack");
[owner, dogOwner, vet, ...otheraccounts] = await ethers.getSigners();
token721 = await FurryTrack.deploy();
});
// You can nest describe calls to create subsections.
describe("Deployment", function () {
it("Should has the correct name and symbol ", async function () {
expect(await token721.name()).to.equal(_name);
expect(await token721.symbol()).to.equal(_symbol);
});
});
describe("safeMint()", function() {
context("mint a new token in FurryTrack by contract owner (breeders or animal shelters)", function () {
it("can initialize the metadata for this token", async function () {
await token721.safeMint(true, "baby dog", 0, 0, "ipfs://cid/1.jpg", "good")
expect(await token721.ownerOf(0)).to.equal(owner.address);
dataURI = await token721.tokenURI(0)
expect(dataURI).to.equal(
"data:application/json;base64,eyJuYW1lIjoiYmFieSBkb2ciLCAiaW1hZ2UiOiAiaXBmczovL2NpZC8xLmpwZyIsICJhdHRyaWJ1dGVzIjpbeyJ0cmFpdF90eXBlIjoiZmF0aGVyIiwidmFsdWUiOjB9LHsidHJhaXRfdHlwZSI6Im1vbnRoZXIiLCJ2YWx1ZSI6MH0seyJ0cmFpdF90eXBlIjoiaGVhbHRoIiwidmFsdWUiOiJnb29kIn0seyJ0cmFpdF90eXBlIjoidHlwZSIsICJ2YWx1ZSI6IkRvZyJ9XX0="
);
const metadata = atob(dataURI.substring(29));
const result = JSON.parse(metadata);
expect(result.name).to.equal("baby dog")
expect(result.attributes.length).to.equal(4)
});
});
});
describe("grantWritePermission()", function() {
beforeEach(async function () {
await token721.safeMint(true, "baby dog", 0, 0, "ipfs://cid/1.jpg", "good")
expect(await token721.ownerOf(0)).to.equal(owner.address);
await token721.transferFrom(owner.address, dogOwner.address, 0)
expect(await token721.ownerOf(0)).to.equal(dogOwner.address);
expect(await token721.isAddressCanWrite(owner.address, 0)).to.equal(false);
expect(await token721.isAddressCanWrite(dogOwner.address, 0)).to.equal(false);
expect(await token721.isAddressCanWrite(vet.address, 0)).to.equal(false);
await helpers.mine(100);
});
context("grant write permission to the dog owner", function () {
it("can not grant permission to dog owner", async function () {
let messageHash = ethers.utils.id(dogOwner.address);
let messageBytes = ethers.utils.arrayify(messageHash);
let signature = await dogOwner.signMessage(messageBytes);
await expect(
token721.connect(dogOwner).grantWritePermission(0, dogOwner.address, messageBytes, signature)
).to.be.revertedWith("not allow to grant permission to the token owner")
});
});
context("grant write permission to the vet by dog owner", function () {
it("the vet's wallet can write health status", async function () {
let messageHash = ethers.utils.id(vet.address);
let messageBytes = ethers.utils.arrayify(messageHash);
let signature = await vet.signMessage(messageBytes);
grant = await token721.connect(dogOwner).grantWritePermission(0, vet.address, messageBytes, signature)
receipt = await grant.wait()
expect(receipt.events[0]["event"]).to.equal("GrantWritePermission")
expect(await token721.isAddressCanWrite(owner.address, 0)).to.equal(false);
expect(await token721.isAddressCanWrite(dogOwner.address, 0)).to.equal(false);
expect(await token721.isAddressCanWrite(vet.address, 0)).to.equal(true);
});
it("the write permission will be revoked after 128 blocks", async function () {
let messageHash = ethers.utils.id(vet.address);
let messageBytes = ethers.utils.arrayify(messageHash);
let signature = await vet.signMessage(messageBytes);
grant = await token721.connect(dogOwner).grantWritePermission(0, vet.address, messageBytes, signature)
receipt = await grant.wait()
expect(receipt.events[0]["event"]).to.equal("GrantWritePermission")
await helpers.mine(129);
expect(await token721.isAddressCanWrite(owner.address, 0)).to.equal(false);
expect(await token721.isAddressCanWrite(dogOwner.address, 0)).to.equal(false);
expect(await token721.isAddressCanWrite(vet.address, 0)).to.equal(false);
});
});
});
describe("updateHealthStatus()", function() {
context("with vet address", function () {
beforeEach(async function () {
await token721.safeMint(true, "baby dog", 0, 0, "ipfs://cid/1.jpg", "good")
expect(await token721.ownerOf(0)).to.equal(owner.address);
await token721.transferFrom(owner.address, dogOwner.address, 0)
expect(await token721.ownerOf(0)).to.equal(dogOwner.address);
await helpers.mine(100);
let messageHash = ethers.utils.id(vet.address);
let messageBytes = ethers.utils.arrayify(messageHash);
let signature = await vet.signMessage(messageBytes);
grant = await token721.connect(dogOwner).grantWritePermission(0, vet.address, messageBytes, signature)
receipt = await grant.wait()
expect(receipt.events[0]["event"]).to.equal("GrantWritePermission")
expect(await token721.isAddressCanWrite(vet.address, 0)).to.equal(true);
});
it("update health status with the result of the diagnosis", async function () {
dataURI = await token721.tokenURI(0)
metadata = atob(dataURI.substring(29));
result = JSON.parse(metadata);
expect(result.attributes[2]["value"]).to.equal("good")
write = await token721.connect(vet).updateHealthStatus(0, "not well")
receipt = await write.wait()
expect(receipt.events[0]["event"]).to.equal("StatusChanged")
dataURI = await token721.tokenURI(0)
metadata = atob(dataURI.substring(29));
result = JSON.parse(metadata);
expect(result.attributes[2]["value"]).to.equal("not well")
});
});
});
});