[node.js 스팀봇 강좌] #1. steem.js 라이브러리 이용하기

in #kr7 years ago (edited)

steem.js 라이브러리를 이용하면 스팀잇에서 포스팅과 보팅을 포함한 대부분의 기능은 할 수 있습니다.

깃허브(https://github.com/steemit/steem-js/)에 steem.js의 소스코드와 문서 및 예제가 있으나 설명이 친절하지는 않습니다.

더군다나 node.js는 npm이라는 패키지 관리자를 통해 간단한 명령으로 라이브러리를 자신의 PC에 설치할 수 있기 때문에 깃허브에서 steem.js를 다운로드 받아 저장할 일은 없습니다.

첫번째 강좌는 node.js (설치파일: https://nodejs.org/ko/)를 설치한 커맨드 창에서 'npm install steem'를 실행시킨 상태를 전제로 가장 간단한 예제를 실행해 보겠습니다.

프로그램 소스코드를 작성/수정하는 편집기는 메모장에서부터 전용 프로그램까지 다양하지만 저는 visual studio code라는 MS의 무료 소프트웨어를 사용합니다.

1단계

빈 파일(test.js)을 생성합니다.

2단계

다음과 같이 steem.js 라이브러리를 로드 한 후, steem 패키지에 있는 getAccounts 함수로 스팀잇 유저 Dan과 Ned의 계정정보를 조회하는 코드를 작성합니다.

1.PNG

getAccounts 함수의 마지막 인자는 아래의 세줄은 getAccounts함수가 실행된 후 그 결과를 처리하는 소위 callback함수로서 javascript 특유의 프로그래밍 기법입니다.

, function(err, response){
console.log(err, response);
}

이 콜백함수의 기능은 err (에러)결과나 response(응답)결과를 콘솔(모니터 등)에 출력하는 역할을 합니다.

3단계

위 코드를 test.js 파일에 저장한 후 javascript를 지원하는 편집기에서 '실행' 메뉴를 선택하거나 윈도우 명령프롬프트에서 'node test.js'를 타이핑하여 실행합니다.

111.PNG

위 과정을 모두 수행하고 나면 다음과 같이 스팀잇 서버에서 두 사람의 계정정보를 javascript에서 자주 사용하는 JSON 형식으로 응답을 줍니다.

결과확인

steeemd.com에서 조회되는 계정정보과 거의 같은 것을 보실 수 있습니다.

Debugging with legacy protocol because Node.js v6.11.0 was detected.
'C:\Program Files\nodejs\node.exe' test.js 
null [ { id: 497,
    name: 'ned',
    owner: { weight_threshold: 1, account_auths: [], key_auths: [Object] },
    active: { weight_threshold: 1, account_auths: [], key_auths: [Object] },
    posting: { weight_threshold: 1, account_auths: [], key_auths: [Object] },
    memo_key: 'STM6sUWU9dtss1N8m7tQfUt7PacNHACrt3jBZNtkYkMMBv3e4CH3p',
    json_metadata: '{"profile":{"profile_image":"https://s32.postimg.org/oix1wvfp1/ned_steem_steemit.jpg","location":"A vision in  a dream.","name":"ned"}}',
    proxy: '',
    last_owner_update: '2017-03-02T00:20:39',
    last_account_update: '2017-03-16T17:47:57',
    created: '2016-03-31T14:21:45',
    mined: false,
    owner_challenged: false,
    active_challenged: false,
    last_owner_proved: '1970-01-01T00:00:00',
    last_active_proved: '1970-01-01T00:00:00',
    recovery_account: 'steem',
    last_account_recovery: '1970-01-01T00:00:00',
    reset_account: 'null',
    comment_count: 0,
    lifetime_vote_count: 0,
    post_count: 634,
    can_vote: true,
    voting_power: 9800,
    last_vote_time: '2017-08-16T16:40:33',
    balance: '581951.410 STEEM',
    savings_balance: '0.000 STEEM',
    sbd_balance: '5716.744 SBD',
    sbd_seconds: '14383766366907',
    sbd_seconds_last_update: '2017-08-22T16:42:30',
    sbd_last_interest_payment: '2017-07-24T13:14:15',
    savings_sbd_balance: '1249.949 SBD',
    savings_sbd_seconds: '0',
    savings_sbd_seconds_last_update: '2017-01-01T19:19:27',
    savings_sbd_last_interest_payment: '2017-01-01T19:19:27',
    savings_withdraw_requests: 0,
    reward_sbd_balance: '74.059 SBD',
    reward_steem_balance: '0.000 STEEM',
    reward_vesting_balance: '595795.837531 VESTS',
    reward_vesting_steem: '288.109 STEEM',
    vesting_shares: '7344140982.676874 VESTS',
    delegated_vesting_shares: '7203414925.000000 VESTS',
    received_vesting_shares: '0.000000 VESTS',
    vesting_withdraw_rate: '0.000000 VESTS',
    next_vesting_withdrawal: '1969-12-31T23:59:59',
    withdrawn: 0,
    to_withdraw: 0,
    withdraw_routes: 0,
    curation_rewards: 734347064,
    posting_rewards: 8175663,
    proxied_vsf_votes: [ '912262787320591', 0, 0, 0 ],
    witnesses_voted_for: 0,
    average_bandwidth: '6235789132',
    lifetime_bandwidth: '3588037000000',
    last_bandwidth_update: '2017-08-23T06:40:33',
    average_market_bandwidth: 1060000000,
    lifetime_market_bandwidth: '112620000000',
    last_market_bandwidth_update: '2017-08-11T13:52:15',
    last_post: '2017-08-23T06:40:33',
    last_root_post: '2017-08-05T01:48:12',
    vesting_balance: '0.000 STEEM',
    reputation: '41382423390591',
    transfer_history: [],
    market_history: [],
    post_history: [],
    vote_history: [],
    other_history: [],
    witness_votes: [],
    tags_usage: [],
    guest_bloggers: [] },
  { id: 496,
    name: 'dan',
    owner: { weight_threshold: 1, account_auths: [], key_auths: [Object] },
    active: { weight_threshold: 1, account_auths: [], key_auths: [Object] },
    posting: { weight_threshold: 1, account_auths: [], key_auths: [Object] },
    memo_key: 'STM7qtpUZfaHwyJk9ZwvXH3EsAxJcYWY4913Vj6xVWkskM8SRV2Ub',
    json_metadata: '',
    proxy: '',
    last_owner_update: '2016-07-15T19:25:21',
    last_account_update: '2016-07-15T19:27:51',
    created: '2016-03-31T14:21:39',
    mined: false,
    owner_challenged: false,
    active_challenged: false,
    last_owner_proved: '1970-01-01T00:00:00',
    last_active_proved: '1970-01-01T00:00:00',
    recovery_account: 'steem',
    last_account_recovery: '1970-01-01T00:00:00',
    reset_account: 'null',
    comment_count: 0,
    lifetime_vote_count: 0,
    post_count: 411,
    can_vote: true,
    voting_power: 8153,
    last_vote_time: '2017-08-21T01:10:57',
    balance: '869424.190 STEEM',
    savings_balance: '0.000 STEEM',
    sbd_balance: '782.896 SBD',
    sbd_seconds: '334524748026',
    sbd_seconds_last_update: '2017-08-23T02:38:12',
    sbd_last_interest_payment: '2017-08-18T03:50:03',
    savings_sbd_balance: '0.000 SBD',
    savings_sbd_seconds: '0',
    savings_sbd_seconds_last_update: '1970-01-01T00:00:00',
    savings_sbd_last_interest_payment: '1970-01-01T00:00:00',
    savings_withdraw_requests: 0,
    reward_sbd_balance: '2.234 SBD',
    reward_steem_balance: '0.000 STEEM',
    reward_vesting_balance: '428470.282436 VESTS',
    reward_vesting_steem: '207.600 STEEM',
    vesting_shares: '7287739184.310816 VESTS',
    delegated_vesting_shares: '0.000000 VESTS',
    received_vesting_shares: '0.000000 VESTS',
    vesting_withdraw_rate: '662521744.028256 VESTS',
    next_vesting_withdrawal: '2017-08-30T01:41:15',
    withdrawn: '1325043488056512',
    to_withdraw: '8612782672367328',
    withdraw_routes: 0,
    curation_rewards: 91504500,
    posting_rewards: 46019625,
    proxied_vsf_votes: [ 0, 0, 0, 0 ],
    witnesses_voted_for: 23,
    average_bandwidth: '7054762922',
    lifetime_bandwidth: '624143000000',
    last_bandwidth_update: '2017-08-22T21:38:48',
    average_market_bandwidth: 1550000000,
    lifetime_market_bandwidth: '40610000000',
    last_market_bandwidth_update: '2017-05-30T14:50:57',
    last_post: '2017-08-22T21:38:48',
    last_root_post: '2017-08-01T15:27:24',
    vesting_balance: '0.000 STEEM',
    reputation: '84569983600326',
    transfer_history: [],
    market_history: [],
    post_history: [],
    vote_history: [],
    other_history: [],
    witness_votes: 
     [ 'arhag',
       'au1nethyb1',
       'bhuz',
       'boatymcboatface',
       'chitty',
       'clayop',
       'complexring',
       'datasecuritynode',
       'delegate.lafona',
       'gtg',
       'ihashfury',
       'joseph',
       'liondani',
       'masteryoda',
       'pfunk',
       'pharesim',
       'riverhead',
       'roadscape',
       'steempty',
       'steemychicken1',
       'theprophet0',
       'wackou',
       'xeldal' ],
    tags_usage: [],
    guest_bloggers: [] } ]

첫번째 강좌는 여기까지입니다~
Sort:  

오오 대박이네요. 마침 이걸 쓸날이 와서 이런 강좌가 꼭 필요했습니다.
다음 강좌도 기대되네요!!

그런데 steem.js 라이브러리는 서버에서만 사용 가능한가요? 웹브라우저에서 일반 JavaScript 처럼 사용은 못하는건가요? -> 바로 찾아봤는데 둘다 가능하군요. 감사합니다 ㅋㅋ

html에 < script > 태그로 사용하라고 만든게 아닐까 할만큼, 깃허브의 예제도 웹페이지에서 실행되는 형태로 되어 있습니다. 말씀하시는 기능이 모두 가능할꺼에요. ^^

웹에서 동작하는 예제를 찾아보니 스팀잇 유저 @geusgod님이 아래와 같은 사이트를 뚝딱 만드셨다고 포스팅도 하셨네요.(https://steemit.com/kr/@geusgod/steem-js )
포스팅에도 나와있는 steem.js로 만들어진 사이트 주소는 http://beststeem.com/ 입니다.
세상에 잘도 만들었어요. 이 분한테 배우고 싶군요^^

이렇게 직접 찾아봐주시고 정말 감사합니다. 도움이 엄청 될거 같아요.
이분 정말 깔끔하게 잘 만드셨네요^^

👌👌👍👍

제가 배워보고 싶은데 글만으로는 어렵군요. 동영상강좌라면 유익할거 같습니다.

제가 동영상을 찍어서 웹에 올리고 하는 것은 해본적이 없는데, 평일에는 힘들것 같고 주말쯤 올려볼께요. 프로그래밍이라는게 실제 언어와도 비슷하게 어떤 과제에 대해 정답도 없고 하면 할수록 늘기도 하는 부분이라 관심을 계속 가지시기만 해도 밋업때 말씀하셨던 마이크로파이낸스나 환전소를 직접 또는 개발자를 통해 간접적으로 충분히 만들어내실 수 있을꺼에요. 사실 문제는 금융규제인데 못잡아도 1년 이상은 걸릴 것 같으니 아직은 여유가 있는 것 같습니다^^ 그리고 금감원에서 핀테크 상담센터를 운영중에 있으니 구체적인 규제에 대한 상담도 받으실 수 있어요.

@minari 안녕하세요.
차근차근 한번 시도해보려고 하는 중입니다,
알려주신 node.js 설치하면 아래와 같이 두개가 생기는 걸 확인 할 수 있었습니다.
2017-09-13 18;37;11_0.png

그 중 강좌에서 사용되는 명령어는 node.js command promt에 입력하면 된다는 것까지 확인했습니다.
궁금한 건 그 위에 생성된 node.js의 용도는 무엇인지 궁금합니다^^;
배경 정보가 없어 그런지 검색해도 찾아볼 수가 없어서 질문드려봅니다^^;

저도 처음 실행해보네요. interactive한 노드 프로그래밍 인터페이스 같습니다. 한줄한줄 코드를 짜면서 실행을 할 수 있는 대화식 node.js 사용방법인데, 제일 편한건 node.js를 지원하는 편집기를 하나 사용하는거에요. command창을 일일히 열어서 타이핑하는 것보다 '실행'버튼 한번 누르는게 훨씬 편하고 결과 확인이나 저장도 더 편하거든요. 약간 노력을 들인다면 ipython 또는 jupyter라는 웹브라우저를 활용하는 ide도 있는데 이게 사실 제일 편했습니다. 일단 추천드리는 것은 MS VScode 프로그램이에요 ^^

답변 감사드립니다.
암호화폐에 대해서 관심이 생겨서 알아보러 왔다가 프로그램 공부를 하고 있네요^^;

강좌 감사합니다.
따라하려는데 다음 오류가 발생하네요.
Unhandled rejection Error: getaddrinfo ENOTFOUND steemd.steemit.com steemd.steemit.com:443
at errnoException (dns.js:55:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:97:26)

다음 문장 추가하니 되는군요.
steem.api.setOptions({ url: 'wss://ws.golos.io' });

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 64513.75
ETH 3146.11
USDT 1.00
SBD 3.95