Skip to content

Commit e09d325

Browse files
committed
add example for get stoken info
1 parent 9004e4d commit e09d325

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ typings/
6363
# build
6464
dist
6565
*.js
66+
!examples/*.js
6667
*.mjs
6768
*.d.ts
6869
*.ts.map

examples/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
## Usage
3+
4+
`package.json`
5+
6+
```
7+
{
8+
"name": "devkit-examples-sandbox",
9+
"version": "0.0.1",
10+
"type": "module",
11+
"license": "MIT",
12+
"scripts": {
13+
"start": "node index.js"
14+
},
15+
"dependencies": {
16+
"@devprotocol/dev-kit": "^5.8.1",
17+
"ethers": "^5.5.2"
18+
}
19+
}
20+
```
21+
22+
copy the script file you want to run into index.js.
23+
Then `yarn install` to prepare for execution.
24+
25+
```
26+
$ cp xxx.js index.js
27+
$ yarn install
28+
$ tree -L 1
29+
.
30+
├── index.js
31+
├── node_modules
32+
├── package.json
33+
└── yarn.lock
34+
```
35+
36+
run example script (with local ethereum node. like this `http://localhost:8545`):
37+
38+
```
39+
$ yarn start
40+
```
41+
42+
run example script (with infura)
43+
```
44+
$ WEB3_PROVIDER_URL=https://mainnet.infura.io/v3/xxxx yarn start
45+
```

examples/get-stoken-info.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { ethers } from 'ethers'
2+
import { contractFactory, addresses } from '@devprotocol/dev-kit'
3+
4+
// use main net
5+
const contractAddress = addresses.eth.main.sTokens
6+
const provider = new ethers.providers.JsonRpcProvider(process.env.WEB3_PROVIDER_URL)
7+
const contract = contractFactory(provider)
8+
const propertyAddress = '0xac1AC9d00314aE7B4a7d6DbEE4860bECedF92309'
9+
10+
const getSTokenIdsByPropertyAddress = async (contract, propertyAddress) => {
11+
const res = await contract.sTokens(contractAddress).positionsOfProperty(propertyAddress)
12+
return res
13+
}
14+
15+
const getSTokenPositions = async (contract, sTokenId) => {
16+
const res = await contract.sTokens(contractAddress).positions(sTokenId)
17+
return res
18+
}
19+
20+
const getSTokenRewards = (contract, sTokenId) => {
21+
return contract.sTokens(contractAddress).rewards(sTokenId)
22+
}
23+
24+
25+
// get stoken ids
26+
const sTokenIds = await getSTokenIdsByPropertyAddress(contract, propertyAddress)
27+
28+
// 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+
}))
34+
console.log(res)

0 commit comments

Comments
 (0)