[NodeJS] API 사용 법 12편 Steem enjin API 사용해보기 (자동 파워업!)

안녕하세요 ayogom 입니다.
어제에 이어서 오늘도 steem enjin api에 대한 이야기입니다. 어제까지 설치를 진행했고, 간단한 api를 사용하여 특정 토큰의 잔고를 확인했습니다. 확인 했다면 이제 파워업을 해야죠!

어제의 코드를 이어서 한번 보겠습니다.

const SSC = require('sscjs');
const ssc = new SSC('https://api.steem-engine.net/rpc/');

var steem = require('steem')

ssc.find(
    'tokens',  //contract name
    'balances', //table name
    {
        'symbol':'SCT', 
        'account': 'ayogom'
    }, //query to perform on the table
    0, //limit the number of records to retrieve
    0,  //offset applied to the records set
    [],  //array of index definitions { index: string, descending: boolean }
    (err, results) => { //callback
     console.log(err, results);
});

파워업을 해야 하니깐 제 계정으로 변경합니다.

바로 여기에서 balance가 현재 보유량입니다. 추출 해보도록 하겠습니다.

const SSC = require('sscjs');
const ssc = new SSC('https://api.steem-engine.net/rpc/');

var steem = require('steem')

ssc.find(
    'tokens',  //contract name
    'balances', //table name
    {
        'symbol':'SCT', 
        'account': 'ayogom'
    }, //query to perform on the table
    0, //limit the number of records to retrieve
    0,  //offset applied to the records set
    [],  //array of index definitions { index: string, descending: boolean }
    (err, results) => { //callback
     console.log(err, results);
     let arr = results;
     console.log(arr[0].balance);
     if (arr[0].balance > 0){
        console.log("SCT가 0개 이상 가지고 있습니다.")
      }
});

눈치 채셨나요? 일단 결과값을 배열(arr)에 집어 넣습니다. 배열에 넣어야 코드짜기가 편해지거든요
그리고 해당 배열에서 balance를 추출 합니다. 그리고 해당 값이 0 이상이면 comment 가 출력 되도록 하였습니다.

현재 저는 SCT를 약 38.265개를 가지고 있기 때문에 0 이상으로 comment 가 출력이 되었습니다.
그럼 이제 저 코멘트 대신 파워업을 하면 되겠네요! 한번 해볼까요?

if (arr[0].balance > 0){
        console.log("SCT가 0개 이상 가지고 있습니다.")
        amount = Math.floor(arr[0].balance)
        var wif2 = "액티브키"
        var json = JSON.stringify({"contractName":"tokens","contractAction":"stake","contractPayload":{"symbol":""+"SCT"+"","to":""+"ayogom"+"","quantity":""+amount+""}});

        steem.broadcast.customJson(wif2, ['ayogom'],[], 'ssc-mainnet1', json, function(err, result) {   
            console.log(err,result);
        });
 }

해당 토큰을 0개 이상 가지고 있다면 그것을 steem.broadcast.customJson 를 통해서 스테이킹 했습니다. engin 의 api 의 경우 steem api의 customJson을 이용해서 트랜젝션이 만들어지기 때문에 실제 수량이 부족하더라도 api 포맷에 문제가 없다면 정상적으로 기록이 됩니다.

한번 코드를 풀로 봐볼까요?

const SSC = require('sscjs');
const ssc = new SSC('https://api.steem-engine.net/rpc/');

var steem = require('steem')

ssc.find(
    'tokens',  //contract name
    'balances', //table name
    {
        'symbol':'SCT', 
        'account': 'ayogom'
    }, //query to perform on the table
    0, //limit the number of records to retrieve
    0,  //offset applied to the records set
    [],  //array of index definitions { index: string, descending: boolean }
    (err, results) => { //callback
     console.log(err, results);
     let arr = results;
     console.log(arr[0].balance);
     if (arr[0].balance > 0){
        console.log("SCT가 0개 이상 가지고 있습니다.")
        amount = Math.floor(arr[0].balance)
        var wif2 = "액티브키"
        var json = JSON.stringify({"contractName":"tokens","contractAction":"stake","contractPayload":{"symbol":""+"SCT"+"","to":""+"ayogom"+"","quantity":""+amount+""}});

        steem.broadcast.customJson(wif2, ['ayogom'],[], 'ssc-mainnet1', json, function(err, result) {   
            console.log(err,result);
        });
 }
});

이 코드에 지난 시간에 [NodeJS] API 사용 법 6편 예약 포스팅 해보기 -3편 (매일매일 글쓰기) 에서 소개시켜드렸던 매일매일 글쓰는 비법을 쓴다면 매일매일 자동으로 스테이킹을 할 수 있게 됩니다.

어떠신가요? 쉽죠? 밥 아저씨와 다르게 제 설명은 쉬운것 같습니다...

작성 예정인 글 List

  • Nodejs 를 이용한 스팀 API 사용해보기 (계속)

누군가에게는 도움이 될 수 있는 글

스팀잇 기본 이야기

니트로스 (SCT, ZZAN 등 ) 이야기

스팀잇 심화 이야기

스팀잇 보상과 연결되는 이야기

해킹과 관련된 이야기

개발과 관련된 이야기
Sort:  

[US$80.50](▼16%)[SONGJING]SONGJING SJ-125E 제습기 / 송징 SJ-125E 제습기 / 36평적용 / 165W 고출력 / 대용량 물탱크 / 관부가

WWW.QOO10.COM

start success go! go! go!

@ayogom transfered 9 KRWP to @krwp.burn. voting percent : 8.42%, voting power : 60.19%, steem power : 1784487.27, STU KRW : 1200.
@ayogom staking status : 825 KRWP
@ayogom limit for KRWP voting service : 1.65 KRWP (rate : 0.002)
What you sent : 9 KRWP
Refund balance : 7.35 KRWP [54444888 - c52b66e2d6da3ce53cf5454209bd2822096ca7e7]

언제나 코딩 대단하십니다.

감사합니다. 코드 잘 사용할께요.
나중에 가능하면 텔레그램 댓글 알림봇 만드는 방법을... ㅋㅋ

Coin Marketplace

STEEM 0.36
TRX 0.12
JST 0.039
BTC 69735.97
ETH 3533.64
USDT 1.00
SBD 4.72