mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-22 09:12:05 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
35bc3025d6 | ||
|
|
6f6531428a | ||
|
|
cb5e962167 | ||
|
|
33da626f78 | ||
|
|
e9766630ed | ||
|
|
9b6adbf4ec | ||
|
|
5f09447da4 |
@@ -16,9 +16,8 @@ of [BITBOX SDK](https://github.com/Bitcoin-com/bitbox-sdk).
|
||||
Both bch-api and bch-js are part of the
|
||||
[full stack of BCH software](https://troutsblog.com/research/bitcoin-cash/how-to-bch-full-stack-developer).
|
||||
|
||||
- [API Documentation](https://api.bchjs.cash/docs/)
|
||||
- [BCH Example Applications](https://github.com/Permissionless-Software-Foundation/bch-js-examples)
|
||||
- [slp-cli-wallet](https://github.com/christroutner/slp-cli-wallet): a hacker-friendly, command-line BCH and SLP token wallet using this REST API.
|
||||
- [API Documentation](https://fullstack.cash/documentation)
|
||||
- [Example Code](https://fullstack.cash/examples)
|
||||
|
||||
Have questions? Need help? Join our community support
|
||||
[Telegram channel](https://t.me/bch_js_toolkit)
|
||||
|
||||
@@ -11,5 +11,7 @@ export DATA="{\"ref\":\"$DEPLOY_SECRET\"}"
|
||||
|
||||
curl -X POST http://fullstack.cash:9000/hooks/bch-api-mainnet -H "Content-Type: application/json" -d $DATA
|
||||
curl -X POST http://fullstack.cash:9000/hooks/bch-api-testnet -H "Content-Type: application/json" -d $DATA
|
||||
curl -X POST http://$FREE_SERVER:9000/hooks/bch-api-testnet -H "Content-Type: application/json" -d $DATA
|
||||
curl -X POST http://$FREE_SERVER:9000/hooks/bch-api-mainnet -H "Content-Type: application/json" -d $DATA
|
||||
|
||||
echo "...Finished deploying to production."
|
||||
|
||||
+14
-14
@@ -23,24 +23,24 @@
|
||||
target="_blank"
|
||||
>REST API</a
|
||||
>, allowing modern applications to interact with the Bitcoin Cash (BCH)
|
||||
blockchain. New to the BCH blockchain? Start by reading more about
|
||||
blockchain. New to the BCH blockchain? Check out these
|
||||
<a
|
||||
href="https://troutsblog.com/research/bitcoin-cash/how-to-bch-full-stack-developer"
|
||||
href="https://fullstack.cash/documentation"
|
||||
target="_blank"
|
||||
>building full stack applications on BCH</a
|
||||
>educational resources</a
|
||||
>.
|
||||
</p><br />
|
||||
<p>
|
||||
A REST API can work with any programming language, but bch-api is intended
|
||||
to be paired with the
|
||||
<a
|
||||
href="https://www.npmjs.com/package/@chris.troutner/bch-js"
|
||||
href="https://www.npmjs.com/package/@psf/bch-js"
|
||||
target="_blank"
|
||||
>npm bch-js</a
|
||||
>
|
||||
JavaScript library.
|
||||
<a
|
||||
href="https://github.com/Permissionless-Software-Foundation/bch-js-examples"
|
||||
href="https://fullstack.cash/examples"
|
||||
target="_blank"
|
||||
>Here is example code</a
|
||||
>
|
||||
@@ -53,7 +53,7 @@
|
||||
</p><br />
|
||||
<p>
|
||||
While you'll be able to run through the examples without one, you should
|
||||
<a href="https://account.bchjs.cash/" target="_blank"
|
||||
<a href="https://fullstack.cash/login" target="_blank"
|
||||
>sign-up for a free API key</a
|
||||
>
|
||||
in order to unlock higher rate limits.
|
||||
@@ -66,7 +66,7 @@
|
||||
<h3>bch-api</h3>
|
||||
<ul>
|
||||
<li>
|
||||
<a href="https://account.bchjs.cash" target="_blank">
|
||||
<a href="https://fullstack.cash/login" target="_blank">
|
||||
Get an API key to increase rate limits
|
||||
</a>
|
||||
</li>
|
||||
@@ -80,14 +80,14 @@
|
||||
<br />
|
||||
|
||||
<li>
|
||||
<a href="https://tapi.bchjs.cash">
|
||||
<a href="https://tapi.fullstack.cash">
|
||||
testnet REST API
|
||||
</a>
|
||||
</li>
|
||||
<br />
|
||||
|
||||
<li>
|
||||
<a href="https://github.com/christroutner/bch-api" target="_blank">
|
||||
<a href="https://github.com/Permissionless-Software-Foundation/bch-api" target="_blank">
|
||||
bch-api GitHub repository
|
||||
</a>
|
||||
</li>
|
||||
@@ -114,14 +114,14 @@
|
||||
</li>
|
||||
<br />
|
||||
<li>
|
||||
<a href="https://bchjs.cash/" target="_blank">
|
||||
<a href="https://fullstack.cash/cashstrap" target="_blank">
|
||||
Run your own infrastructure
|
||||
</a>
|
||||
</li>
|
||||
<br />
|
||||
|
||||
<li>
|
||||
<a href="https://github.com/bitcoinbook/bitcoinbook#chapters" target="_blank">
|
||||
<a href="https://fullstack.cash/documentation" target="_blank">
|
||||
Mastering Bitcoin (great free reference)
|
||||
</a>
|
||||
</li>
|
||||
@@ -135,7 +135,7 @@
|
||||
<br />
|
||||
|
||||
<li>
|
||||
<a href="https://developer.bitcoin.com/faucets/bch/" target="_blank">
|
||||
<a href="https://fullstack.cash/faucet/" target="_blank">
|
||||
Testnet Faucet
|
||||
</a>
|
||||
</li>
|
||||
@@ -148,7 +148,7 @@
|
||||
<h3>bch-js</h3>
|
||||
<ul>
|
||||
<li>
|
||||
<a href="https://www.npmjs.com/package/@chris.troutner/bch-js"
|
||||
<a href="https://www.npmjs.com/package/@psf/bch-js"
|
||||
target="_blank">
|
||||
npm library
|
||||
</a>
|
||||
@@ -163,7 +163,7 @@
|
||||
<br />
|
||||
|
||||
<li>
|
||||
<a href="https://github.com/christroutner/bch-js" target="_blank">
|
||||
<a href="https://github.com/Permissionless-Software-Foundation/bch-js" target="_blank">
|
||||
GitHub code repository
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@@ -64,10 +64,6 @@ class Slp {
|
||||
_this.router.get('/balancesForAddress/:address', _this.balancesForAddress)
|
||||
_this.router.post('/balancesForAddress', _this.balancesForAddressBulk)
|
||||
_this.router.get('/balancesForToken/:tokenId', _this.balancesForTokenSingle)
|
||||
_this.router.get(
|
||||
'/balance/:address/:tokenId',
|
||||
_this.balancesForAddressByTokenID
|
||||
)
|
||||
_this.router.get('/convert/:address', _this.convertAddressSingle)
|
||||
_this.router.post('/convert', _this.convertAddressBulk)
|
||||
_this.router.post('/validateTxid', _this.validateBulk)
|
||||
@@ -830,170 +826,6 @@ class Slp {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {get} /slp/balance/{address}/{TokenId} List single slp token balance for address.
|
||||
* @apiName List single slp token balance for address.
|
||||
* @apiGroup SLP
|
||||
* @apiDescription Returns List single slp token balance for address.
|
||||
*
|
||||
*
|
||||
* @apiExample Example usage:
|
||||
* curl -X GET "https://api.fullstack.cash/v3/slp/balance/simpleledger:qz9tzs6d5097ejpg279rg0rnlhz546q4fsnck9wh5m/1cda254d0a995c713b7955298ed246822bee487458cd9747a91d9e81d9d28125" -H "accept:application/json"
|
||||
*
|
||||
*
|
||||
*/
|
||||
// Retrieve token balances for a single token class, for a single address.
|
||||
async balancesForAddressByTokenID (req, res, next) {
|
||||
try {
|
||||
// Validate input data.
|
||||
const address = req.params.address
|
||||
if (!address || address === '') {
|
||||
res.status(400)
|
||||
return res.json({ error: 'address can not be empty' })
|
||||
}
|
||||
|
||||
const tokenId = req.params.tokenId
|
||||
if (!tokenId || tokenId === '') {
|
||||
res.status(400)
|
||||
return res.json({ error: 'tokenId can not be empty' })
|
||||
}
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
_this.bchjs.SLP.Address.toCashAddress(address)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
error: `Invalid BCH address. Double check your address is valid: ${address}`
|
||||
})
|
||||
}
|
||||
|
||||
// Prevent a common user error. Ensure they are using the correct network address.
|
||||
const cashAddr = _this.bchjs.SLP.Address.toCashAddress(address)
|
||||
const networkIsValid = _this.routeUtils.validateNetwork(cashAddr)
|
||||
if (!networkIsValid) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
error:
|
||||
'Invalid network. Trying to use a testnet address on mainnet, or vice versa.'
|
||||
})
|
||||
}
|
||||
|
||||
// Convert input to an simpleledger: address.
|
||||
const slpAddr = _this.bchjs.SLP.Address.toSlpAddress(req.params.address)
|
||||
|
||||
const query = {
|
||||
v: 3,
|
||||
q: {
|
||||
db: ['g'],
|
||||
aggregate: [
|
||||
{
|
||||
$match: {
|
||||
'graphTxn.outputs': {
|
||||
$elemMatch: {
|
||||
address: slpAddr,
|
||||
status: 'UNSPENT',
|
||||
slpAmount: { $gte: 0 }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: '$graphTxn.outputs'
|
||||
},
|
||||
{
|
||||
$match: {
|
||||
'graphTxn.outputs.address': slpAddr,
|
||||
'graphTxn.outputs.status': 'UNSPENT',
|
||||
'graphTxn.outputs.slpAmount': { $gte: 0 }
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
amount: '$graphTxn.outputs.slpAmount',
|
||||
address: '$graphTxn.outputs.address',
|
||||
txid: '$graphTxn.txid',
|
||||
vout: '$graphTxn.outputs.vout',
|
||||
tokenId: '$tokenDetails.tokenIdHex'
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '$tokenId',
|
||||
balanceString: {
|
||||
$sum: '$amount'
|
||||
},
|
||||
slpAddress: {
|
||||
$first: '$address'
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
limit: 10000
|
||||
}
|
||||
}
|
||||
|
||||
const options = _this.generateCredentials()
|
||||
|
||||
const s = JSON.stringify(query)
|
||||
const b64 = Buffer.from(s).toString('base64')
|
||||
const url = `${process.env.SLPDB_URL}q/${b64}`
|
||||
|
||||
// Get data from SLPDB.
|
||||
const opt = {
|
||||
method: 'get',
|
||||
baseURL: url,
|
||||
headers: options.headers,
|
||||
timeout: options.timeout
|
||||
}
|
||||
const tokenRes = await _this.axios.request(opt)
|
||||
console.log(`tokenRes.data: ${JSON.stringify(tokenRes.data, null, 2)}`)
|
||||
|
||||
let resVal = {
|
||||
cashAddress: _this.bchjs.SLP.Address.toCashAddress(slpAddr),
|
||||
legacyAddress: _this.bchjs.SLP.Address.toLegacyAddress(slpAddr),
|
||||
slpAddress: slpAddr,
|
||||
tokenId: tokenId,
|
||||
balance: 0,
|
||||
balanceString: '0'
|
||||
}
|
||||
|
||||
if (tokenRes.data.g.length > 0) {
|
||||
tokenRes.data.g.forEach(async (token) => {
|
||||
if (token._id === tokenId) {
|
||||
resVal = {
|
||||
cashAddress: _this.bchjs.SLP.Address.toCashAddress(slpAddr),
|
||||
legacyAddress: _this.bchjs.SLP.Address.toLegacyAddress(slpAddr),
|
||||
slpAddress: slpAddr,
|
||||
tokenId: token._id,
|
||||
balance: parseFloat(token.balanceString),
|
||||
balanceString: token.balanceString
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
resVal = {
|
||||
cashAddress: _this.bchjs.SLP.Address.toCashAddress(slpAddr),
|
||||
legacyAddress: _this.bchjs.SLP.Address.toLegacyAddress(slpAddr),
|
||||
slpAddress: slpAddr,
|
||||
tokenId: tokenId,
|
||||
balance: 0,
|
||||
balanceString: '0'
|
||||
}
|
||||
}
|
||||
|
||||
res.status(200)
|
||||
return res.json(resVal)
|
||||
} catch (err) {
|
||||
wlogger.error('Error in slp.ts/balancesForAddressByTokenID().', err)
|
||||
|
||||
return _this.errorHandler(err, res)
|
||||
// return res.json({
|
||||
// error: `Error in /balance/:address/:tokenId: ${err.message}`
|
||||
// })
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {get} /slp/convert/{address} Convert address to slpAddr, cashAddr and legacy.
|
||||
* @apiName Convert address to slpAddr, cashAddr and legacy.
|
||||
|
||||
Reference in New Issue
Block a user