Skip to content

Commit

Permalink
feat: integrate Stacking STX into database and provide through accoun…
Browse files Browse the repository at this point in the history
…t API details, upgrade to nodejs v14
  • Loading branch information
zone117x committed Nov 11, 2020
1 parent 726f1c6 commit 5d84254
Show file tree
Hide file tree
Showing 22 changed files with 372 additions and 222 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
FROM node:13-alpine
FROM node:14-alpine

WORKDIR /app
COPY . .

RUN apk add --no-cache --virtual .build-deps alpine-sdk python git openjdk8-jre
RUN echo "GIT_TAG=$(git tag --points-at HEAD)" >> .env
RUN npm config set unsafe-perm true && npm install --unsafe-perm && npm run build --unsafe-perm && npm prune --production
RUN npm config set unsafe-perm true && npm install && npm run build && npm prune --production
RUN apk del .build-deps

CMD npm run start
2 changes: 2 additions & 0 deletions docs/api/address/get-address-balances.example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"stx": {
"balance": "1000000",
"locked": "50000",
"unlock_height": "256",
"total_sent": "0",
"total_received": "1000000"
},
Expand Down
4 changes: 2 additions & 2 deletions docs/api/address/get-address-balances.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"required": ["stx", "fungible_tokens", "non_fungible_tokens"],
"properties": {
"stx": {
"$ref": "../../entities/balance/balance.schema.json"
"$ref": "../../entities/balance/stx-balance.schema.json"
},
"fungible_tokens": {
"type": "object",
"patternProperties": {
"*": {
"$ref": "../../entities/balance/balance.schema.json"
"$ref": "../../entities/balance/ft-balance.schema.json"
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions docs/api/address/get-address-stx-balance.example.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"balance": "1000000",
"locked": "50000",
"unlock_height": "256",
"total_sent": "0",
"total_received": "1000000"
}
2 changes: 1 addition & 1 deletion docs/api/address/get-address-stx-balance.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"description": "GET request that returns address balances",
"title": "AddressStxBalanceResponse",
"type": "object",
"$ref": "../../entities/balance/balance.schema.json"
"$ref": "../../entities/balance/stx-balance.schema.json"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Balance",
"description": "FtBalance",
"type": "object",
"additionalProperties": false,
"required": ["balance", "total_sent", "total_received"],
Expand Down
24 changes: 24 additions & 0 deletions docs/entities/balance/stx-balance.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "StxBalance",
"type": "object",
"additionalProperties": false,
"required": ["balance", "locked", "unlock_height", "total_sent", "total_received"],
"properties": {
"balance": {
"type": "string"
},
"locked": {
"type": "string"
},
"unlock_height": {
"type": "string"
},
"total_sent": {
"type": "string"
},
"total_received": {
"type": "string"
}
}
}
8 changes: 6 additions & 2 deletions docs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ export interface AddressAssetsListResponse {
*/
export interface AddressBalanceResponse {
/**
* Balance
* StxBalance
*/
stx: {
balance: string;
locked: string;
unlock_height: string;
total_sent: string;
total_received: string;
};
fungible_tokens: {
/**
* Balance
* FtBalance
*
* This interface was referenced by `undefined`'s JSON-Schema definition
* via the `patternProperty` "*".
Expand Down Expand Up @@ -59,6 +61,8 @@ export interface AddressBalanceResponse {
*/
export interface AddressStxBalanceResponse {
balance: string;
locked: string;
unlock_height: string;
total_sent: string;
total_received: string;
}
Expand Down
22 changes: 8 additions & 14 deletions follower.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
### Build blockstack-core-sidecar API
FROM node:13.14.0-buster as build
FROM node:14-alpine as build
WORKDIR /app
COPY . .
RUN apt-get update && apt-get install -y openjdk-11-jre-headless
RUN apk add --no-cache --virtual .build-deps alpine-sdk python git openjdk8-jre
RUN echo "GIT_TAG=$(git tag --points-at HEAD)" >> .env
RUN npm install
RUN npm run build
RUN npm prune --production
# RUN npm config set unsafe-perm true && npm install --unsafe-perm && npm run build --unsafe-perm && npm prune --production
RUN npm config set unsafe-perm true && npm install && npm run build && npm prune --production

### Fetch stacks-node binary
FROM blockstack/stacks-blockchain:v23.0.0.10-krypton-stretch as stacks-node-build
Expand All @@ -20,6 +19,10 @@ SHELL ["/bin/bash", "-c"]
RUN apt-get update
RUN apt-get install -y sudo curl pslist

### Install nodejs
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get install -y nodejs

### Set noninteractive apt-get
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections

Expand All @@ -34,15 +37,6 @@ USER stacky
RUN sudo chown -R stacky:stacky $HOME
RUN mkdir /home/stacky/.bashrc.d

### Node.js
ENV NODE_VERSION=13.14.0
RUN curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash \
&& bash -c ". .nvm/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION"
ENV PATH=$PATH:/home/stacky/.nvm/versions/node/v${NODE_VERSION}/bin
RUN node -e 'console.log("Node.js runs")'

### Setup stacks-node
COPY --from=stacks-node-build /bin/stacks-node stacks-node/
ENV PATH="$PATH:$HOME/stacks-node"
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@
"homepage": "https://github.com/blockstack/stacks-blockchain-api#readme",
"prettier": "@blockstack/prettier-config",
"engines": {
"node": ">=13.11"
"node": ">=14"
},
"engineStrict" : true,
"release": {
"plugins": [
"@semantic-release/commit-analyzer",
Expand Down
20 changes: 12 additions & 8 deletions src/api/routes/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ export function createAddressRouter(db: DataStore): RouterWithAsync {
return res.status(400).json({ error: `invalid STX address "${stxAddress}"` });
}
// Get balance info for STX token
const { balance, totalSent, totalReceived } = await db.getStxBalance(stxAddress);
const stxBalanceResult = await db.getStxBalance(stxAddress);
const result: AddressStxBalanceResponse = {
balance: balance.toString(),
total_sent: totalSent.toString(),
total_received: totalReceived.toString(),
balance: stxBalanceResult.balance.toString(),
locked: stxBalanceResult.locked.toString(),
unlock_height: stxBalanceResult.unlockHeight.toString(),
total_sent: stxBalanceResult.totalSent.toString(),
total_received: stxBalanceResult.totalReceived.toString(),
};
res.json(result);
});
Expand All @@ -57,7 +59,7 @@ export function createAddressRouter(db: DataStore): RouterWithAsync {
return res.status(400).json({ error: `invalid STX address "${stxAddress}"` });
}
// Get balance info for STX token
const { balance, totalSent, totalReceived } = await db.getStxBalance(stxAddress);
const stxBalanceResult = await db.getStxBalance(stxAddress);

// Get balances for fungible tokens
const ftBalancesResult = await db.getFungibleTokenBalances(stxAddress);
Expand All @@ -81,9 +83,11 @@ export function createAddressRouter(db: DataStore): RouterWithAsync {

const result: AddressBalanceResponse = {
stx: {
balance: balance.toString(),
total_sent: totalSent.toString(),
total_received: totalReceived.toString(),
balance: stxBalanceResult.balance.toString(),
locked: stxBalanceResult.locked.toString(),
unlock_height: stxBalanceResult.unlockHeight.toString(),
total_sent: stxBalanceResult.totalSent.toString(),
total_received: stxBalanceResult.totalReceived.toString(),
},
fungible_tokens: ftBalances,
non_fungible_tokens: nftBalances,
Expand Down
10 changes: 9 additions & 1 deletion src/datastore/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,16 @@ export interface DbSearchResult {
entity_data?: DbBlock | DbMempoolTx | DbTx;
}

export interface DbFtBalance {
balance: bigint;
totalSent: bigint;
totalReceived: bigint;
}

export interface DbStxBalance {
balance: bigint;
locked: bigint;
unlockHeight: bigint;
totalSent: bigint;
totalReceived: bigint;
}
Expand Down Expand Up @@ -285,7 +293,7 @@ export interface DataStore extends DataStoreEventEmitter {

getStxBalance(stxAddress: string): Promise<DbStxBalance>;
getStxBalanceAtBlock(stxAddress: string, blockHeight: number): Promise<DbStxBalance>;
getFungibleTokenBalances(stxAddress: string): Promise<Map<string, DbStxBalance>>;
getFungibleTokenBalances(stxAddress: string): Promise<Map<string, DbFtBalance>>;
getNonFungibleTokenCounts(
stxAddress: string
): Promise<Map<string, { count: bigint; totalSent: bigint; totalReceived: bigint }>>;
Expand Down
Loading

0 comments on commit 5d84254

Please sign in to comment.