Skip to content

Commit aa37185

Browse files
author
Yash Agrawal
committed
feat: minor fixes
1 parent a760984 commit aa37185

File tree

2 files changed

+334
-0
lines changed

2 files changed

+334
-0
lines changed

test/SBTTokenURI.test.ts

Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
/* eslint-disable @typescript-eslint/naming-convention */
3+
4+
import { expect, use } from 'chai'
5+
import { type Contract } from 'ethers'
6+
import { solidity } from 'ethereum-waffle'
7+
8+
import { deploy, getEncodedMetadata, getSigners } from './utils'
9+
10+
use(solidity)
11+
12+
describe('SBT', () => {
13+
const init = async (): Promise<Contract> => {
14+
const signers = await getSigners()
15+
const sbt = await deploy('SBT')
16+
await sbt.initialize(signers.minterUpdater.address, [
17+
signers.minterA.address,
18+
signers.minterB.address,
19+
])
20+
return sbt
21+
}
22+
23+
describe('setTokenURI', () => {
24+
it('The setTokenURI function should function correctly for no attributes', async () => {
25+
const sbt = await init()
26+
const signers = await getSigners()
27+
28+
const metadata = {
29+
name: 'Proof of service NFT',
30+
description:
31+
'This is a proof of service NFT, which indicates your contribution to the project',
32+
tokenURIImage:
33+
'https://i.guim.co.uk/img/media/ef8492feb3715ed4de705727d9f513c168a8b196/37_0_1125_675/master/1125.jpg?width=1200&height=1200&quality=85&auto=format&fit=crop&s=d456a2af571d980d8b2985472c262b31',
34+
stringAttributes: [],
35+
numberAttributes: [],
36+
}
37+
const encodedMetadata = await getEncodedMetadata(sbt, metadata)
38+
await expect(
39+
sbt
40+
.connect(signers.minterA)
41+
.mint(signers.userA.address, encodedMetadata)
42+
)
43+
.to.emit(sbt, 'Minted')
44+
.withArgs(0, signers.userA.address)
45+
46+
const uri = await sbt.tokenURI(0)
47+
const uriInfo: string[] = uri.split(',')
48+
expect(uriInfo[0]).to.equal('data:application/json;base64')
49+
const decodedData = JSON.parse(
50+
Buffer.from(uriInfo[1], 'base64').toString()
51+
) as {
52+
name: string
53+
description: string
54+
image: string
55+
attributes: any[]
56+
}
57+
expect(decodedData.name).to.eq(metadata.name)
58+
expect(decodedData.description).to.eq(metadata.description)
59+
expect(decodedData.image).to.eq(metadata.tokenURIImage)
60+
expect(decodedData.attributes.length).to.eq(0)
61+
})
62+
63+
it('The setTokenURI function should function correctly for only 1 string attribute and 0 number attribute', async () => {
64+
const sbt = await init()
65+
const signers = await getSigners()
66+
67+
const metadata = {
68+
name: 'Proof of service NFT',
69+
description:
70+
'This is a proof of service NFT, which indicates your contribution to the project',
71+
tokenURIImage:
72+
'https://i.guim.co.uk/img/media/ef8492feb3715ed4de705727d9f513c168a8b196/37_0_1125_675/master/1125.jpg?width=1200&height=1200&quality=85&auto=format&fit=crop&s=d456a2af571d980d8b2985472c262b31',
73+
stringAttributes: [{ trait_type: 'Category', value: 'Category A' }],
74+
numberAttributes: [],
75+
}
76+
const encodedMetadata = await getEncodedMetadata(sbt, metadata)
77+
await expect(
78+
sbt
79+
.connect(signers.minterA)
80+
.mint(signers.userA.address, encodedMetadata)
81+
)
82+
.to.emit(sbt, 'Minted')
83+
.withArgs(0, signers.userA.address)
84+
85+
const uri = await sbt.tokenURI(0)
86+
const uriInfo: string[] = uri.split(',')
87+
expect(uriInfo[0]).to.equal('data:application/json;base64')
88+
const decodedData = JSON.parse(
89+
Buffer.from(uriInfo[1], 'base64').toString()
90+
) as {
91+
name: string
92+
description: string
93+
image: string
94+
attributes: any[]
95+
}
96+
expect(decodedData.name).to.eq(metadata.name)
97+
expect(decodedData.description).to.eq(metadata.description)
98+
expect(decodedData.image).to.eq(metadata.tokenURIImage)
99+
expect(decodedData.attributes.length).to.eq(1)
100+
expect(decodedData.attributes[0]).to.deep.equal({
101+
trait_type: 'Category',
102+
value: 'Category A',
103+
})
104+
expect(decodedData.attributes).to.deep.equal([
105+
{ trait_type: 'Category', value: 'Category A' },
106+
])
107+
})
108+
109+
it('The setTokenURI function should function correctly for only 1+ string attribute and 0 number attribute', async () => {
110+
const sbt = await init()
111+
const signers = await getSigners()
112+
113+
const metadata = {
114+
name: 'Proof of service NFT',
115+
description:
116+
'This is a proof of service NFT, which indicates your contribution to the project',
117+
tokenURIImage:
118+
'https://i.guim.co.uk/img/media/ef8492feb3715ed4de705727d9f513c168a8b196/37_0_1125_675/master/1125.jpg?width=1200&height=1200&quality=85&auto=format&fit=crop&s=d456a2af571d980d8b2985472c262b31',
119+
stringAttributes: [
120+
{ trait_type: 'Category', value: 'Category A' },
121+
{ trait_type: 'Location', value: 'Shibuya' },
122+
{ trait_type: 'Entity', value: 'Corporation' },
123+
],
124+
numberAttributes: [],
125+
}
126+
const encodedMetadata = await getEncodedMetadata(sbt, metadata)
127+
await expect(
128+
sbt
129+
.connect(signers.minterA)
130+
.mint(signers.userA.address, encodedMetadata)
131+
)
132+
.to.emit(sbt, 'Minted')
133+
.withArgs(0, signers.userA.address)
134+
135+
const uri = await sbt.tokenURI(0)
136+
const uriInfo: string[] = uri.split(',')
137+
expect(uriInfo[0]).to.equal('data:application/json;base64')
138+
const decodedData = JSON.parse(
139+
Buffer.from(uriInfo[1], 'base64').toString()
140+
) as {
141+
name: string
142+
description: string
143+
image: string
144+
attributes: any[]
145+
}
146+
expect(decodedData.name).to.eq(metadata.name)
147+
expect(decodedData.description).to.eq(metadata.description)
148+
expect(decodedData.image).to.eq(metadata.tokenURIImage)
149+
expect(decodedData.attributes.length).to.eq(3)
150+
expect(decodedData.attributes[0]).to.deep.equal({
151+
trait_type: 'Category',
152+
value: 'Category A',
153+
})
154+
expect(decodedData.attributes[1]).to.deep.equal({
155+
trait_type: 'Location',
156+
value: 'Shibuya',
157+
})
158+
expect(decodedData.attributes[2]).to.deep.equal({
159+
trait_type: 'Entity',
160+
value: 'Corporation',
161+
})
162+
expect(decodedData.attributes).to.deep.equal([
163+
{ trait_type: 'Category', value: 'Category A' },
164+
{ trait_type: 'Location', value: 'Shibuya' },
165+
{ trait_type: 'Entity', value: 'Corporation' },
166+
])
167+
})
168+
169+
it('The setTokenURI function should function correctly for only 0 string attribute and 1 number attribute', async () => {
170+
const sbt = await init()
171+
const signers = await getSigners()
172+
173+
const metadata = {
174+
name: 'Proof of service NFT',
175+
description:
176+
'This is a proof of service NFT, which indicates your contribution to the project',
177+
tokenURIImage:
178+
'https://i.guim.co.uk/img/media/ef8492feb3715ed4de705727d9f513c168a8b196/37_0_1125_675/master/1125.jpg?width=1200&height=1200&quality=85&auto=format&fit=crop&s=d456a2af571d980d8b2985472c262b31',
179+
stringAttributes: [],
180+
numberAttributes: [
181+
{
182+
trait_type: 'No. of contributions',
183+
value: 1,
184+
display_type: 'number',
185+
},
186+
],
187+
}
188+
const encodedMetadata = await getEncodedMetadata(sbt, metadata)
189+
await expect(
190+
sbt
191+
.connect(signers.minterA)
192+
.mint(signers.userA.address, encodedMetadata)
193+
)
194+
.to.emit(sbt, 'Minted')
195+
.withArgs(0, signers.userA.address)
196+
197+
const uri = await sbt.tokenURI(0)
198+
const uriInfo: string[] = uri.split(',')
199+
expect(uriInfo[0]).to.equal('data:application/json;base64')
200+
const decodedData = JSON.parse(
201+
Buffer.from(uriInfo[1], 'base64').toString()
202+
) as {
203+
name: string
204+
description: string
205+
image: string
206+
attributes: any[]
207+
}
208+
expect(decodedData.name).to.eq(metadata.name)
209+
expect(decodedData.description).to.eq(metadata.description)
210+
expect(decodedData.image).to.eq(metadata.tokenURIImage)
211+
expect(decodedData.attributes.length).to.eq(1)
212+
expect(decodedData.attributes[0]).to.deep.equal({
213+
trait_type: 'No. of contributions',
214+
value: '1',
215+
display_type: 'number',
216+
})
217+
expect(decodedData.attributes).to.deep.equal([
218+
{
219+
trait_type: 'No. of contributions',
220+
value: '1',
221+
display_type: 'number',
222+
},
223+
])
224+
})
225+
226+
it('The setTokenURI function should function correctly for only 0 string attribute and 1+ number attribute', async () => {
227+
const sbt = await init()
228+
const signers = await getSigners()
229+
230+
const metadata = {
231+
name: 'Proof of service NFT',
232+
description:
233+
'This is a proof of service NFT, which indicates your contribution to the project',
234+
tokenURIImage:
235+
'https://i.guim.co.uk/img/media/ef8492feb3715ed4de705727d9f513c168a8b196/37_0_1125_675/master/1125.jpg?width=1200&height=1200&quality=85&auto=format&fit=crop&s=d456a2af571d980d8b2985472c262b31',
236+
stringAttributes: [],
237+
numberAttributes: [
238+
{
239+
trait_type: 'No. of contributions',
240+
value: 1,
241+
display_type: 'number',
242+
},
243+
{
244+
trait_type: 'No. of locations',
245+
value: 1000,
246+
display_type: 'number',
247+
},
248+
{ trait_type: 'Gas fee used', value: 12342, display_type: 'number' },
249+
],
250+
}
251+
const encodedMetadata = await getEncodedMetadata(sbt, metadata)
252+
await expect(
253+
sbt
254+
.connect(signers.minterA)
255+
.mint(signers.userA.address, encodedMetadata)
256+
)
257+
.to.emit(sbt, 'Minted')
258+
.withArgs(0, signers.userA.address)
259+
260+
const uri = await sbt.tokenURI(0)
261+
const uriInfo: string[] = uri.split(',')
262+
expect(uriInfo[0]).to.equal('data:application/json;base64')
263+
const decodedData = JSON.parse(
264+
Buffer.from(uriInfo[1], 'base64').toString()
265+
) as {
266+
name: string
267+
description: string
268+
image: string
269+
attributes: any[]
270+
}
271+
expect(decodedData.name).to.eq(metadata.name)
272+
expect(decodedData.description).to.eq(metadata.description)
273+
expect(decodedData.image).to.eq(metadata.tokenURIImage)
274+
expect(decodedData.attributes.length).to.eq(3)
275+
expect(decodedData.attributes[0]).to.deep.equal({
276+
trait_type: 'No. of contributions',
277+
value: '1',
278+
display_type: 'number',
279+
})
280+
expect(decodedData.attributes[1]).to.deep.equal({
281+
trait_type: 'No. of locations',
282+
value: '1000',
283+
display_type: 'number',
284+
})
285+
expect(decodedData.attributes[2]).to.deep.equal({
286+
trait_type: 'Gas fee used',
287+
value: '12342',
288+
display_type: 'number',
289+
})
290+
expect(decodedData.attributes).to.deep.equal([
291+
{
292+
trait_type: 'No. of contributions',
293+
value: '1',
294+
display_type: 'number',
295+
},
296+
{
297+
trait_type: 'No. of locations',
298+
value: '1000',
299+
display_type: 'number',
300+
},
301+
{ trait_type: 'Gas fee used', value: '12342', display_type: 'number' },
302+
])
303+
})
304+
})
305+
})

test/utils/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ import { ethers } from 'hardhat'
55
import type { Contract } from 'ethers'
66
import type { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
77

8+
type StringAttributes = Array<{
9+
trait_type: string
10+
value: string
11+
}>
12+
13+
type NumberAttributes = Array<{
14+
trait_type: string
15+
display_type: string
16+
value: number
17+
}>
18+
819
export const deploy = async (name: string): Promise<Contract> => {
920
const factoryStrage = await ethers.getContractFactory(name)
1021
const contract = await factoryStrage.deploy()
@@ -24,6 +35,24 @@ export const getDummyEncodedMetadata = async (
2435
tokenURIImage
2536
)) as string
2637

38+
export const getEncodedMetadata = async (
39+
contract: Contract,
40+
args: {
41+
name: string
42+
description: string
43+
stringAttributes: StringAttributes
44+
numberAttributes: NumberAttributes
45+
tokenURIImage: string
46+
}
47+
): Promise<string> =>
48+
(await contract.encodeMetadata(
49+
args.name,
50+
args.description,
51+
args.stringAttributes,
52+
args.numberAttributes,
53+
args.tokenURIImage
54+
)) as string
55+
2756
export const deployWithArg = async (
2857
name: string,
2958
arg: number | string

0 commit comments

Comments
 (0)