Skip to content

Commit

Permalink
show nft owner thumbnail from social.near
Browse files Browse the repository at this point in the history
  • Loading branch information
petersalomonsen committed Dec 25, 2022
1 parent 4668a40 commit fda27de
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
6 changes: 0 additions & 6 deletions examples/nft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ edition = "2021"
[lib]
crate-type = ["cdylib"]

[profile.release]
codegen-units = 1
lto = true
debug = false
opt-level = "z"

[dependencies]
near-sdk = "4.0.0"
lazy_static = "1.4.0"
Expand Down
20 changes: 16 additions & 4 deletions examples/nft/web4/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getNearSocialProfile } from './nearsocial.js';

(async () => {
if ('serviceWorker' in navigator) {
const reg = navigator.serviceWorker.register('serviceworker.js', { scope: './' })
Expand Down Expand Up @@ -88,10 +90,20 @@
const nftowners = await (await fetch(new URL('nftowners.json?t=' + new Date().getTime(), import.meta.url))).json();
const nftownerstablebody = document.getElementById('nftownerstablebody');

nftownerstablebody.innerHTML = nftowners.sort((a,b)=> parseInt(a.token_id) - parseInt(b.token_id)).map(o => `<tr>
<td>${o.token_id}</td>
<td><a href="https://near.social/#/mob.near/widget/ProfilePage?accountId=${o.owner_id}" target="_blank">${o.owner_id}</a></td>
</tr>`).join('');
nftowners.sort((a,b)=> parseInt(a.token_id) - parseInt(b.token_id));

nftownerstablebody.innerHTML = nftowners.map((o, ndx) => `<tr>
<td>${o.token_id}</td>
<td><a href="https://near.social/#/mob.near/widget/ProfilePage?accountId=${o.owner_id}" target="_blank">${o.owner_id}</a></td>
<td><img id="${o.owner_id}_profileimage_${ndx}"></td>
</tr>`).join('');

nftowners.forEach(async (o, ndx) => {
const profile = await getNearSocialProfile(o.owner_id);
if (profile[o.owner_id] && profile[o.owner_id].profile.image.ipfs_cid) {
document.getElementById(o.owner_id+'_profileimage_'+ndx).src= `https://i.near.social/thumbnail/https://ipfs.near.social/ipfs/${profile[o.owner_id].profile.image.ipfs_cid}`;
}
});
} else {
document.documentElement.innerHTML = 'This app requires that your browser supports serviceworkers';
}
Expand Down
21 changes: 21 additions & 0 deletions examples/nft/web4/nearsocial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export async function getNearSocialProfile(accountid) {
const args_base64 = btoa(JSON.stringify({"keys":[`${accountid}/profile/**`]}));
const result = await fetch('https://rpc.mainnet.near.org', {
method: 'post',
headers: {'content-type': 'application/json'},
body: JSON.stringify({
"jsonrpc": "2.0",
"id": "dontcare",
"method": "query",
"params": {
"request_type": "call_function",
"finality": "final",
"account_id": "social.near",
"method_name": "get",
"args_base64": args_base64
}
})
}).then(r => r.json());

return JSON.parse(result.result.result.map(r => String.fromCharCode(r)).join(''));
}
7 changes: 5 additions & 2 deletions examples/nft/web4/nftowners.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
{"token_id":"1","owner_id":"petersalomonsen.near"},
{"token_id":"2","owner_id":"petersalomonsen.near"},
{"token_id":"3","owner_id":"petersalomonsen.near"},
{"token_id":"4","owner_id":"petersalomonsen.near"},
{"token_id":"5","owner_id":"petersalomonsen.near"}
{"token_id":"4","owner_id":"veronikako.near"},
{"token_id":"5","owner_id":"vinkosa.near"},
{"token_id":"6","owner_id":"floriks.near"},
{"token_id":"7","owner_id":"webassemblymusic.near"},
{"token_id":"8","owner_id":"petersalomonsen.near"}
]

0 comments on commit fda27de

Please sign in to comment.