[Steemit javascript api 사용하기 레슨7] 업보팅 계산하기3(나의보팅파워와 스팀달라넣기+ 임대받은 스팀파워)
안녕하세요 @fullmi입니다.
오늘은 저번과 같이 업보팅 계산을 할건데요.
저번시간에는 임대받은 스팀파워를 뺴먹었죠.
저도 잘 몰라서 데이터 분석하는데 시간이 좀 걸렸네요.
결국은 알아냈네요. account조회후 받은 데이터에서
received_vesting_shares
위의 값이 임대받은 값이네요. 힘드네요 정확한 api문서가 있으면 좋으련만
예측과 추측이 난무하는 개발세상이네요.
아무튼 그리하여 완료된 소스는 아래와 같습니다.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Voteing Power</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.steemjs.com/lib/latest/steem.min.js"></script>
<script>
$(function () {
var a, n, r, i, o, p = 1e4;
//입력한 값으로 보팅 달러 계산하기
function init() {
//보팅파워계산해서 엘러먼트에 반인딩
$.ajax({
url: https://api.steemit.com,
type: "POST",
data: '{"id":0,"jsonrpc":"2.0","method":"database_api.find_accounts","params":{"accounts":["fullmi"]}}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
console.log(result);
var account = result.result.accounts[0];
var votingPower = account.voting_power;
//최근보팅이 얼마전에 있었는지 계산이 필요함
var sec = (new Date - new Date(account.last_vote_time + "Z")) / 1000;
//다음으로는 마지막으로 업보드한 보팅파워에 새롭게 계산된 파팅파워를 더한다.
var reVotingPower = votingPower + (10000 * sec / 432000);
//소수점 2자리까지 계산한 보팅파워 단 100%를 넘지 않는다.
reVotingPower = Math.min(reVotingPower / 100, 100).toFixed(2);
$("input[name='frm_votingpower']").val(reVotingPower);
}
});
//스팀파워계산해서 엘러먼트에 바인딩
$.ajax({
url: https://api.steemit.com,
type: "POST",
data: '{"id": 0,"jsonrpc":"2.0","method":"database_api.find_accounts","params":{"accounts": ["fullmi"]}}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
console.log(r);
var response = r.result.accounts[0];
//나의 영향력.
var vestingShares = response.vesting_shares.amount / Math.pow(10, response.vesting_shares.precision);
//임대받은값
var receivedVestingShares = response.received_vesting_shares.amount / Math.pow(10, response.received_vesting_shares.precision);
console.log(vestingShares);
console.log(receivedVestingShares);
//글로벌 프로퍼티값 가져오기
$.ajax({
url: '
https://api.steemit.com / https://api.steemit.com
', type: "POST",
data: '{"id": 1,"jsonrpc":"2.0","method":"database_api.get_dynamic_global_properties"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (g) {
console.log(g);
var gr = g.result;
//토탈기여스팀 기여도
var totalVestingFundSteem = gr.total_vesting_fund_steem.amount / Math.pow(10, gr.total_vesting_fund_steem.precision);
//토탈기여도
var totalVests = gr.total_vesting_shares.amount / Math.pow(10, gr.total_vesting_shares.precision);;
a = totalVestingFundSteem / totalVests;
//나의스팀파워
var steemPowerMyOwn = (totalVestingFundSteem * (vestingShares / totalVests)).toFixed(2);
//임대받은 나의 스팀파워
var steemPowerDelegated = (totalVestingFundSteem * (receivedVestingShares / totalVests)).toFixed(2);
//토탈스팀파워
var steemPower= parseFloat(steemPowerMyOwn)+parseFloat(steemPowerDelegated);
$("input[name='frm_steempower']").val(steemPower);
}
})
}
})
}
function e() {
var e = $("input[name='frm_steempower']").val()
, t = $("input[name='frm_votingpower']").val()
, n = $("input[name='frm_voteweight']").val()
, r = e / a
, m = parseInt(100 * t * (100 * n) / p);
m = parseInt((m + 49) / 50);
var l = parseInt(r * m * 100) * i * o;
$("#vote_value").text(l.toFixed(2)),
$("#vote_value").hide().fadeIn("fast")
}
function t() {
//리워드값 가져오기
steem.api.getRewardFund("post", function (e, t) {
n = t.reward_balance,
r = t.recent_claims,
i = n.replace(" STEEM", "") / r,
$("#reward_balance").text(parseInt(n).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")),
$("#recent_claims").text(parseInt(r / 1e9).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + "B")
}),
steem.api.getCurrentMedianHistoryPrice(function (e, t) {
o = t.base.replace(" SBD", "") / t.quote.replace(" STEEM", ""),
$("#steem_price_sbd").text("$" + o.toFixed(2))
}),
setTimeout(t, 1e4);//10*10*10*10이니까 10초마다 호출해서 가져오는거네요
}
steem.api.setOptions({
url: "
https://api.steemit.com / https://api.steemit.com
" }),
t();
$("#form_calc").on("submit", function (t) {
e(),
t.preventDefault()
});
init();
});
</script>
</head>
<body>
<div id="mainwrapper">
<br>
<div class="topbox">Steem Price
<br>
<span class="value" id="steem_price_sbd">$0.00</span>
</div>
<div class="topbox">Reward balance
<br>
<span class="value" id="reward_balance">0</span>
</div>
<div class="topbox">Recent claims
<br>
<span class="value" id="recent_claims">0</span>
</div>
<div style="clear:both;height:40px"></div>
<section class="box">
<div class="header">
<span>Upvote Calculator</span>
</div>
<div style="float:right;padding-top:36px;font-size:44px;width:40%">
<b>$
<span id="vote_value">0.00</span>
</b>
</div>
<form id="form_calc" method="get" style="padding:16px;line-height:21px">
<label for="frm_steempower">
<span style="width:130px">Steem Power</span>
<input name="frm_steempower" size="6" value="10000">
</label>
<br>
<label for="frm_votingpower">
<span style="width:130px">Voting Power</span>
<input name="frm_votingpower" size="6" value="100"> %</label>
<br>
<label for="frm_voteweight">
<span style="width:130px">Vote Weight</span>
<input name="frm_voteweight" size="6" value="100"> %</label>
<br>
<label style="display:block;margin-top:14px">
<span> </span>
<input type="submit" value="Calculate">
</label>
</form>
</section>
</div>
</div>
</body>
</html>
kr-event태그하셔서 왔습니다^^
보팅, 팔로우하고 갑니다~
저도 SBD 100% BACK 이벤트 진행중인데 오셔서 참여부탁드려요~
이벤트참여
오늘도 좋은하루 되세요~ :)
수고하셨습니다. :)
감사해요