Skip to content

Commit a02cf2c

Browse files
add functions
1 parent b37aa62 commit a02cf2c

15 files changed

Lines changed: 544 additions & 22 deletions

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ $ yarn start
4141
```
4242

4343
run example script (with infura)
44+
4445
```
4546
$ WEB3_PROVIDER_URL=https://mainnet.infura.io/v3/xxxx yarn start
4647
```

examples/get-property-info.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,29 @@ import { contractFactory, addresses } from '@devprotocol/dev-kit'
33

44
// use main net
55
const registryContractAddress = addresses.eth.main.registry
6-
const provider = new ethers.providers.JsonRpcProvider(process.env.WEB3_PROVIDER_URL)
6+
const provider = new ethers.providers.JsonRpcProvider(
7+
process.env.WEB3_PROVIDER_URL
8+
)
79
const contract = contractFactory(provider)
810
const propertyAddress = '0xac1AC9d00314aE7B4a7d6DbEE4860bECedF92309'
9-
const lockupContractAddress = await contract.registry(registryContractAddress).lockup()
11+
const lockupContractAddress = await contract
12+
.registry(registryContractAddress)
13+
.lockup()
1014

11-
const propertyStakingAmount = await contract.lockup(lockupContractAddress).getPropertyValue(propertyAddress)
12-
const stakingAmount = ethers.BigNumber.from(propertyStakingAmount).div(new ethers.BigNumber.from(10).pow(18))
13-
console.log(`${propertyAddress}'s staking amount is ${stakingAmount.toBigInt()} DEV`)
15+
const propertyStakingAmount = await contract
16+
.lockup(lockupContractAddress)
17+
.getPropertyValue(propertyAddress)
18+
const stakingAmount = ethers.BigNumber.from(propertyStakingAmount).div(
19+
new ethers.BigNumber.from(10).pow(18)
20+
)
21+
console.log(
22+
`${propertyAddress}'s staking amount is ${stakingAmount.toBigInt()} DEV`
23+
)
1424

15-
const propertyRewards = await contract.lockup(lockupContractAddress).calculateRewardAmount(propertyAddress)
16-
const reward = ethers.BigNumber.from(propertyRewards[0]).div(new ethers.BigNumber.from(10).pow(36))
25+
const propertyRewards = await contract
26+
.lockup(lockupContractAddress)
27+
.calculateRewardAmount(propertyAddress)
28+
const reward = ethers.BigNumber.from(propertyRewards[0]).div(
29+
new ethers.BigNumber.from(10).pow(36)
30+
)
1731
console.log(`${propertyAddress}'s rewards is ${reward.toBigInt()} DEV`)

examples/get-stoken-info.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,37 @@ import { contractFactory, addresses } from '@devprotocol/dev-kit'
33

44
// use main net
55
const contractAddress = addresses.eth.main.sTokens
6-
const provider = new ethers.providers.JsonRpcProvider(process.env.WEB3_PROVIDER_URL)
6+
const provider = new ethers.providers.JsonRpcProvider(
7+
process.env.WEB3_PROVIDER_URL
8+
)
79
const contract = contractFactory(provider)
810
const propertyAddress = '0xac1AC9d00314aE7B4a7d6DbEE4860bECedF92309'
911

1012
const getSTokenIdsByPropertyAddress = async (contract, propertyAddress) => {
11-
const res = await contract.sTokens(contractAddress).positionsOfProperty(propertyAddress)
12-
return res
13+
const res = await contract
14+
.sTokens(contractAddress)
15+
.positionsOfProperty(propertyAddress)
16+
return res
1317
}
1418

1519
const getSTokenPositions = async (contract, sTokenId) => {
16-
const res = await contract.sTokens(contractAddress).positions(sTokenId)
17-
return res
20+
const res = await contract.sTokens(contractAddress).positions(sTokenId)
21+
return res
1822
}
1923

2024
const getSTokenRewards = (contract, sTokenId) => {
21-
return contract.sTokens(contractAddress).rewards(sTokenId)
25+
return contract.sTokens(contractAddress).rewards(sTokenId)
2226
}
2327

24-
2528
// get stoken ids
2629
const sTokenIds = await getSTokenIdsByPropertyAddress(contract, propertyAddress)
2730

2831
// get postions data by stoken id
29-
const res = await Promise.all(sTokenIds.map(async (sTokenId) => {
30-
const positions = await getSTokenPositions(contract, sTokenId)
31-
const rewards = await getSTokenRewards(contract, sTokenId)
32-
return { sTokenId, positions, rewards }
33-
}))
32+
const res = await Promise.all(
33+
sTokenIds.map(async (sTokenId) => {
34+
const positions = await getSTokenPositions(contract, sTokenId)
35+
const rewards = await getSTokenRewards(contract, sTokenId)
36+
return { sTokenId, positions, rewards }
37+
})
38+
)
3439
console.log(res)

lib/ethereum/s-tokens/abi.ts

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,44 @@ export const sTokensAbi = [
4949
name: 'ApprovalForAll',
5050
type: 'event',
5151
},
52+
{
53+
anonymous: false,
54+
inputs: [
55+
{
56+
indexed: false,
57+
internalType: 'uint256',
58+
name: 'tokenId',
59+
type: 'uint256',
60+
},
61+
{
62+
indexed: false,
63+
internalType: 'address',
64+
name: 'freezingUser',
65+
type: 'address',
66+
},
67+
],
68+
name: 'Freezed',
69+
type: 'event',
70+
},
71+
{
72+
anonymous: false,
73+
inputs: [
74+
{
75+
indexed: false,
76+
internalType: 'uint256',
77+
name: 'tokenId',
78+
type: 'uint256',
79+
},
80+
{
81+
indexed: false,
82+
internalType: 'address',
83+
name: 'meltingUser',
84+
type: 'address',
85+
},
86+
],
87+
name: 'Melted',
88+
type: 'event',
89+
},
5290
{
5391
anonymous: false,
5492
inputs: [
@@ -86,6 +124,31 @@ export const sTokensAbi = [
86124
name: 'Minted',
87125
type: 'event',
88126
},
127+
{
128+
anonymous: false,
129+
inputs: [
130+
{
131+
indexed: false,
132+
internalType: 'uint256',
133+
name: 'tokenId',
134+
type: 'uint256',
135+
},
136+
{
137+
indexed: false,
138+
internalType: 'address',
139+
name: 'author',
140+
type: 'address',
141+
},
142+
{
143+
indexed: false,
144+
internalType: 'string',
145+
name: 'data',
146+
type: 'string',
147+
},
148+
],
149+
name: 'SetTokenUri',
150+
type: 'event',
151+
},
89152
{
90153
anonymous: false,
91154
inputs: [
@@ -198,6 +261,61 @@ export const sTokensAbi = [
198261
stateMutability: 'view',
199262
type: 'function',
200263
},
264+
{
265+
inputs: [],
266+
name: 'descriptorAddress',
267+
outputs: [
268+
{
269+
internalType: 'address',
270+
name: '',
271+
type: 'address',
272+
},
273+
],
274+
stateMutability: 'view',
275+
type: 'function',
276+
},
277+
{
278+
inputs: [
279+
{
280+
internalType: 'uint256',
281+
name: '_tokenId',
282+
type: 'uint256',
283+
},
284+
],
285+
name: 'descriptors',
286+
outputs: [
287+
{
288+
internalType: 'bool',
289+
name: '',
290+
type: 'bool',
291+
},
292+
{
293+
internalType: 'address',
294+
name: '',
295+
type: 'address',
296+
},
297+
{
298+
internalType: 'string',
299+
name: '',
300+
type: 'string',
301+
},
302+
],
303+
stateMutability: 'view',
304+
type: 'function',
305+
},
306+
{
307+
inputs: [
308+
{
309+
internalType: 'uint256',
310+
name: '_tokenId',
311+
type: 'uint256',
312+
},
313+
],
314+
name: 'freezeTokenURI',
315+
outputs: [],
316+
stateMutability: 'nonpayable',
317+
type: 'function',
318+
},
201319
{
202320
inputs: [
203321
{
@@ -254,6 +372,19 @@ export const sTokensAbi = [
254372
stateMutability: 'view',
255373
type: 'function',
256374
},
375+
{
376+
inputs: [
377+
{
378+
internalType: 'uint256',
379+
name: '_tokenId',
380+
type: 'uint256',
381+
},
382+
],
383+
name: 'meltTokenURI',
384+
outputs: [],
385+
stateMutability: 'nonpayable',
386+
type: 'function',
387+
},
257388
{
258389
inputs: [
259390
{
@@ -495,6 +626,37 @@ export const sTokensAbi = [
495626
stateMutability: 'nonpayable',
496627
type: 'function',
497628
},
629+
{
630+
inputs: [
631+
{
632+
internalType: 'address',
633+
name: '_descriptor',
634+
type: 'address',
635+
},
636+
],
637+
name: 'setDescriptor',
638+
outputs: [],
639+
stateMutability: 'nonpayable',
640+
type: 'function',
641+
},
642+
{
643+
inputs: [
644+
{
645+
internalType: 'uint256',
646+
name: '_tokenId',
647+
type: 'uint256',
648+
},
649+
{
650+
internalType: 'string',
651+
name: '_data',
652+
type: 'string',
653+
},
654+
],
655+
name: 'setTokenURIImage',
656+
outputs: [],
657+
stateMutability: 'nonpayable',
658+
type: 'function',
659+
},
498660
{
499661
inputs: [
500662
{
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { createDescriptorsCaller } from './descriptors'
2+
3+
describe('descriptors.spec.ts', () => {
4+
describe('createDescriptorsCaller', () => {
5+
it('call success', async () => {
6+
const value = {
7+
0: 'false',
8+
1: '0x12345',
9+
2: 'https://hogehoge',
10+
isFreezed_: 'false',
11+
amount_: '0x12345',
12+
price_: 'https://hogehoge',
13+
}
14+
15+
const contract = {
16+
descriptors: jest
17+
.fn()
18+
.mockImplementation(async () => Promise.resolve(value)),
19+
}
20+
21+
const expected = {
22+
isFreezed: false,
23+
freezingUser: '0x12345',
24+
descriptor: 'https://hogehoge',
25+
}
26+
27+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
28+
const caller = createDescriptorsCaller(contract as any)
29+
30+
const result = await caller(1)
31+
32+
expect(result).toEqual(expected)
33+
})
34+
35+
it('call failure', async () => {
36+
const error = 'error'
37+
38+
const contract = {
39+
descriptors: jest
40+
.fn()
41+
.mockImplementation(async () => Promise.reject(error)),
42+
}
43+
44+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
45+
const caller = createDescriptorsCaller(contract as any)
46+
47+
const result = await caller(1).catch((err) => err)
48+
49+
expect(result).toEqual(error)
50+
})
51+
})
52+
})
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
2+
import { ethers } from 'ethers'
3+
import { execute, QueryOption } from '../../common/utils/execute'
4+
import { arrayify } from '../../common/utils/arrayify'
5+
6+
export type Descriptors = {
7+
readonly isFreezed: boolean
8+
readonly freezingUser: string
9+
readonly descriptor: string
10+
}
11+
12+
export type CreateDescriptorsCaller = (
13+
contract: ethers.Contract
14+
) => (tokenId: number) => Promise<Descriptors>
15+
16+
export const createDescriptorsCaller: CreateDescriptorsCaller =
17+
(contract: ethers.Contract) => async (tokenId: number) => {
18+
const res = await execute<
19+
QueryOption,
20+
{
21+
readonly isFreezed_: string
22+
readonly freezingUser_: string
23+
readonly descriptor_: string
24+
}
25+
>({
26+
contract,
27+
method: 'descriptors',
28+
args: [String(tokenId)],
29+
mutation: false,
30+
})
31+
const arrayified = arrayify(res)
32+
return {
33+
isFreezed: arrayified[0].toLowerCase() === 'true',
34+
freezingUser: arrayified[1],
35+
descriptor: arrayified[2],
36+
}
37+
}

0 commit comments

Comments
 (0)