Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
Add balance retrieval of NEP5 tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
ixje committed Dec 9, 2017
1 parent 15d6be6 commit f9f78e5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
51 changes: 51 additions & 0 deletions app/api/crypto/nep5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { isValidPublicAddress } from './index'
import bs58 from 'bs58'
import { reverse } from './utils'

/**
* Get the raw script hex string for use in an invokescript rpc call
* @param {String} token hash (hex)
* @param {String} public address of account to check token balance of
* @returns {Buffer} raw script data
*/
export function getTokenBalanceScript(token, address) {
if (isValidPublicAddress(address)) {
let programHashBuffer = bs58.decode(address)
let scriptHash = programHashBuffer.slice(1, 21)

const SCRIPTHASH_LEN = 0x14
const OPCODE_PUSH1 = 0x51
const OPCODE_PACK = 0xc1
const OPERATION_LENGTH = 9
const OPERATION = Buffer.from('balanceOf', 'utf8')
const OPCODE_APPCALL = 0x67
let token_scripthash_buffer = reverse(Buffer.from(token, 'hex'))

// construct script
let offset = 0
let data = Buffer.alloc(54)

data.writeUInt8(SCRIPTHASH_LEN, offset)
offset += 1

data.fill(scriptHash, offset, offset + scriptHash.length)
offset += scriptHash.length

data.writeUInt8(OPCODE_PUSH1, offset)
data.writeUInt8(OPCODE_PACK, offset + 1)
data.writeUInt8(OPERATION_LENGTH, offset + 2)
offset += 3

data.fill(OPERATION, offset, offset + OPERATION.length)
offset += OPERATION.length

data.writeUInt8(OPCODE_APPCALL, offset)
offset += 1

data.fill(token_scripthash_buffer, offset)

return data
} else {
throw new Error('Invalid public address')
}
}
22 changes: 22 additions & 0 deletions app/api/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
buildRawTransaction,
signTransactionData
} from '../crypto'
import { getTokenBalanceScript } from '../crypto/nep5'
import { reverse } from '../crypto/utils'

export function getBalance(address) {
var path = '/v2/address/balance/' + address
Expand Down Expand Up @@ -186,3 +188,23 @@ export function getWalletDataFrom(url) {
}
})
}

/**
* Get the balance of a NEP5 Token
* @param {String} token hash (hex)
* @param {String} public address of account to check token balance of
* @returns {int} token abalance
*/

export function getTokenBalance(token, address) {
const NETWORK_STORAGE_MULTIPLIER = 100000000
return queryRPC('invokescript', [getTokenBalanceScript(token, address).toString('hex')], 2).then(response => {
let valueBuf = Buffer.from(response.result.stack[0].value, 'hex')
let value = parseInt(reverse(valueBuf).toString('hex'), 16) / NETWORK_STORAGE_MULTIPLIER

if (isNaN(value)) {
value = 0
}
return value
})
}

0 comments on commit f9f78e5

Please sign in to comment.