-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds the stake delegation functionality to the treasury dashboard. The recorded video from the automated playwright test shows the "happy-path" scenario of staking max available NEAR tokens. https://github.com/user-attachments/assets/35866bc1-315e-40ba-94ba-1528866418f9 --------- Co-authored-by: Peter Salomonsen <[email protected]>
- Loading branch information
1 parent
27d7cdd
commit e221939
Showing
32 changed files
with
2,581 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
instances/treasury-devdao.near/widget/components/StakedNearIframe.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const instance = props.instance; | ||
if (!instance) { | ||
return <></>; | ||
} | ||
|
||
const { treasuryDaoID } = VM.require(`${instance}/widget/config.data`); | ||
|
||
const setNearStakedTokens = props.setNearStakedTokens || (() => {}); | ||
const setPoolWithBalance = props.setPoolWithBalance || (() => {}); | ||
|
||
const code = ` | ||
<!doctype html> | ||
<html> | ||
<body> | ||
<script> | ||
const archiveNodeUrl = "https://archival-rpc.mainnet.near.org"; | ||
const treasuryDaoID = "${treasuryDaoID}" | ||
async function getAccountBalance(stakingpool_id, account_id) { | ||
return await fetch(archiveNodeUrl, { | ||
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: stakingpool_id, | ||
method_name: "get_account_total_balance", | ||
args_base64: btoa( | ||
JSON.stringify({ | ||
account_id: account_id, | ||
}) | ||
), | ||
}, | ||
}), | ||
}) | ||
.then((r) => r.json()) | ||
.then((r) => | ||
parseInt( | ||
r.result.result | ||
.map((c) => String.fromCharCode(c)) | ||
.join("") | ||
.replace(/\"/g, "") | ||
) | ||
); | ||
} | ||
async function getStakingPools() { | ||
return await fetch("https://api.fastnear.com/v1/account/" + treasuryDaoID + "/staking").then(r => r.json()) | ||
} | ||
window.onload = async () => { | ||
const poolResp = await getStakingPools(); | ||
const pools = await Promise.all(poolResp.pools.map(async (i) => { | ||
const balance = await getAccountBalance(i.pool_id, poolResp.account_id); | ||
return {pool: i.pool_id, balance}; | ||
})); | ||
window.parent.postMessage({ handler: "stakedNearPool", pools }, "*"); | ||
}; | ||
</script> | ||
</body> | ||
</html> | ||
`; | ||
|
||
const iframe = ( | ||
<iframe | ||
style={{ | ||
display: "none", | ||
}} | ||
srcDoc={code} | ||
onMessage={(e) => { | ||
switch (e.handler) { | ||
case "stakedNearPool": | ||
const pools = e.pools; | ||
let sum = new Big(0); | ||
pools.forEach((pool) => { | ||
let bigNum = new Big(pool.balance).div(1e24); | ||
sum = sum.plus(bigNum); | ||
}); | ||
setNearStakedTokens(sum.toFixed() ?? "0"); | ||
setPoolWithBalance(pools); | ||
break; | ||
} | ||
}} | ||
/> | ||
); | ||
|
||
return iframe; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.