Skip to content

Commit

Permalink
add transfer tokens method to neon-js (#85)
Browse files Browse the repository at this point in the history
merging into master right away, as opposed to dev first, as this is needed for next wallet release (i believe nothing else has changed on dev since it was last pushed to master?)
  • Loading branch information
dvdschwrtz authored and Ejhfast committed Nov 25, 2017
1 parent 26e13ff commit 4b2708c
Show file tree
Hide file tree
Showing 8 changed files with 77,918 additions and 42 deletions.
42,048 changes: 42,030 additions & 18 deletions lib/browser.js

Large diffs are not rendered by default.

Binary file removed lib/browser.js.gz
Binary file not shown.
1 change: 1 addition & 0 deletions lib/browser.js.map

Large diffs are not rendered by default.

35,829 changes: 35,812 additions & 17 deletions lib/index.js

Large diffs are not rendered by default.

Binary file removed lib/index.js.gz
Binary file not shown.
1 change: 1 addition & 0 deletions lib/index.js.map

Large diffs are not rendered by default.

51 changes: 50 additions & 1 deletion src/api/nep5.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { ScriptBuilder } from '../sc'
import { getScriptHashFromAddress } from '../wallet'
import { getScriptHashFromAddress, Account } from '../wallet'
import { Query, VMExtractor } from '../rpc'
import { ab2str, hexstring2ab, reverseHex, fixed82num } from '../utils'
import { getRPCEndpoint, getBalance } from './neonDB'
import { Transaction } from '../transactions'
import { ASSET_ID } from '../consts'

/**
* Queries for NEP5 Token information.
Expand Down Expand Up @@ -44,3 +47,49 @@ export const getTokenBalance = (net, scriptHash, address) => {
return fixed82num(res.result.stack[0].value)
})
}

/**
* Transfers NEP5 Tokens.
* @param {string} net
* @param {string} scriptHash
* @param {string} fromWif
* @param {string} toAddress
* @param {number} transferAmount
* @param {number} gasCost
* @param {function} signingFunction
* @return {Promise<Response>} RPC response
*/
export const doTransferToken = (net, scriptHash, fromWif, toAddress, transferAmount, gasCost = 0, signingFunction = null) => {
const account = new Account(fromWif)
const rpcEndpointPromise = getRPCEndpoint(net)
const balancePromise = getBalance(net, account.address)
let signedTx
let endpt
return Promise.all([rpcEndpointPromise, balancePromise])
.then((values) => {
endpt = values[0]
const balances = values[1]
const fromAddrScriptHash = reverseHex(getScriptHashFromAddress(account.address))
const intents = [
{ assetId: ASSET_ID.GAS, value: 0.00000001, scriptHash: fromAddrScriptHash }
]
const toAddrScriptHash = reverseHex(getScriptHashFromAddress(toAddress))
const invoke = { scriptHash, operation: 'transfer', args: [fromAddrScriptHash, toAddrScriptHash, transferAmount] }
const unsignedTx = Transaction.createInvocationTx(balances, intents, invoke, gasCost, { version: 1 })
if (signingFunction) {
return signingFunction(unsignedTx, account.publicKey)
} else {
return unsignedTx.sign(account.privateKey)
}
})
.then((signedResult) => {
signedTx = signedResult
return Query.sendRawTransaction(signedTx).execute(endpt)
})
.then((res) => {
if (res.result === true) {
res.txid = signedTx
}
return res
})
}
30 changes: 24 additions & 6 deletions tests/api/nep5.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import * as NEP5 from '../../src/api/nep5'
import * as NEP5 from '../../src/api/nep5'
import testKeys from '../testKeys.json'

describe('NEP5', function () {
this.timeout(10000)
const net = 'http://seed3.neo.org:20332'
const scriptHash = 'd7678dd97c000be3f33e9362e673101bac4ca654'

it('get basic info', () => {
return NEP5.getTokenInfo('http://test1.cityofzion.io:8880', '5b7074e873973a6ed3708862f219a6fbf4d1c411')
return NEP5.getTokenInfo(net, scriptHash)
.then(result => {
result.name.should.equal('Red Pulse Token 3.1.4')
result.symbol.should.equal('RPX')
result.name.should.equal('LOCALTOKEN')
result.symbol.should.equal('LWTF')
result.decimals.should.equal(8)
result.totalSupply.should.be.above(1000)
result.totalSupply.should.equal(1969000)
})
.catch((e) => {
console.log(e)
throw e
})
})
it('get balance', () => {
return NEP5.getTokenBalance('http://test1.cityofzion.io:8880', '5b7074e873973a6ed3708862f219a6fbf4d1c411', 'AVf4UGKevVrMR1j3UkPsuoYKSC4ocoAkKx')
return NEP5.getTokenBalance(net, scriptHash, testKeys.c.address)
.then(result => {
result.should.be.above(0)
})
Expand All @@ -25,4 +29,18 @@ describe('NEP5', function () {
throw e
})
})
it('transfers tokens', () => {
const testNet = 'TestNet'
const fromWif = 'L5FzBMGSG2d7HVJL5vWuXfxUKsrkX5irFhtw1L5zU4NAvNuXzd8a'
const transferAmount = 1
const gasCost = 0
return NEP5.doTransferToken(testNet, scriptHash, fromWif, testKeys.c.address, transferAmount, gasCost)
.then(({ result }) => {
result.should.equal(true)
})
.catch((e) => {
console.log(e)
throw e
})
})
})

0 comments on commit 4b2708c

Please sign in to comment.