All files / api index.ts

75.74% Statements 178/235
100% Branches 2/2
0% Functions 0/16
75.74% Lines 178/235

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 2361x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                               1x 1x 1x 1x 1x 1x 1x     1x 1x     1x 1x       1x 1x     1x 1x     1x 1x               1x     1x 1x     1x                 1x     1x 1x     1x 1x     1x 1x     1x 1x     1x 1x     1x  
import { MetaSV } from './MetaSV'
import { Sensible } from './Sensible'
import { MVC } from './MVC'
 
export enum API_NET {
  MAIN = 'mainnet',
  TEST = 'testnet',
}
 
export enum API_TARGET {
  MVC = 'mvc',
  SENSIBLE = 'sensible',
  METASV = 'metasv',
  SHOW = 'show',
}
 
export type NonFungibleTokenUnspent = {
  txId: string
  outputIndex: number
  tokenAddress: string
  tokenIndex: string
  metaTxId: string
  metaOutputIndex: number
}
 
export type FungibleTokenUnspent = {
  txId: string
  outputIndex: number
  tokenAddress: string
  tokenAmount: string
}
 
export type SA_utxo = {
  txId: string
  outputIndex: number
  satoshis: number
  address: string
}
export type FungibleTokenSummary = {
  codehash: string
  genesis: string
  sensibleId: string
  pendingBalance: string
  balance: string
  symbol: string
  decimal: number
}
 
export type NonFungibleTokenSummary = {
  codehash: string
  genesis: string
  sensibleId: string
  count: string
  pendingCount: string
  metaTxId: string
  metaOutputIndex: number
  supply: string
}
 
export type FungibleTokenBalance = {
  balance: string
  pendingBalance: string
  utxoCount: number
  decimal: number
}
 
export type NftSellUtxo = {
  codehash: string
  genesis: string
  tokenIndex: string
  txId: string
  outputIndex: number
  sellerAddress: string
  satoshisPrice: number
}
export type AuthorizationOption = {
  /**
   * should be provided in MetaSV
   */
  authorization?: string
  /**
   * should be provided in MetaSV
   */
  privateKey?: any
}
 
export type OutpointSpent = {
  spentTxId: string
  spentInputIndex: number
}
export interface ApiBase {
  authorize: (options: AuthorizationOption) => void
  getUnspents: (address: string) => Promise<SA_utxo[]>
  getRawTxData: (txid: string) => Promise<string>
  broadcast: (hex: string) => Promise<string>
  getFungibleTokenUnspents: (
    codehash: string,
    genesis: string,
    address: string,
    size?: number
  ) => Promise<FungibleTokenUnspent[]>
  getFungibleTokenBalance: (
    codehash: string,
    genesis: string,
    address: string
  ) => Promise<FungibleTokenBalance>
  getFungibleTokenSummary(address: string): Promise<FungibleTokenSummary[]>
  getNonFungibleTokenUnspents(
    codehash: string,
    genesis: string,
    address: string,
    cursor?: number,
    size?: number
  ): Promise<NonFungibleTokenUnspent[]>
  getNonFungibleTokenUnspentDetail(
    codehash: string,
    genesis: string,
    tokenIndex: string
  ): Promise<NonFungibleTokenUnspent>
 
  getNonFungibleTokenSummary(address: string): Promise<NonFungibleTokenSummary[]>
 
  getBalance(address: string): Promise<{
    balance: number
    pendingBalance: number
  }>
 
  getNftSellUtxo(codehash: string, genesis: string, tokenIndex: string): Promise<NftSellUtxo>
 
  getNftSellList(
    codehash: string,
    genesis: string,
    cursor?: number,
    size?: number
  ): Promise<NftSellUtxo[]>
 
  getNftSellListByAddress(address: string, cursor?: number, size?: number): Promise<NftSellUtxo[]>
 
  getOutpointSpent(txId: string, index: number): Promise<OutpointSpent>
}
 
export class Api implements ApiBase {
  private apiTarget: API_TARGET
  private apiHandler: ApiBase
  constructor(apiNet: API_NET, apiTarget: API_TARGET = API_TARGET.MVC, serverBase?: string) {
    switch (apiTarget) {
      case API_TARGET.METASV: {
        this.apiHandler = new MetaSV(apiNet, serverBase)
        break
      }
      case API_TARGET.SENSIBLE: {
        this.apiHandler = new Sensible(apiTarget, apiNet, serverBase)
        break
      }
      default: {
        this.apiHandler = new MVC(apiNet, serverBase)
        break
      }
    }
  }
 
  /**
   * Authorization to use MetaSV
   * @param options
   * @returns
   */
  authorize(options: AuthorizationOption) {
    return this.apiHandler.authorize(options)
  }
 
  async getUnspents(address: string) {
    return this.apiHandler.getUnspents(address)
  }
 
  async getBalance(address: string) {
    let _res = await this.apiHandler.getBalance(address)
    return { balance: _res.balance, pendingBalance: _res.pendingBalance }
  }
 
  async getRawTxData(txid: string) {
    return this.apiHandler.getRawTxData(txid)
  }
 
  async broadcast(hex: string) {
    return this.apiHandler.broadcast(hex)
  }
 
  async getFungibleTokenUnspents(
    codehash: string,
    genesis: string,
    address: string,
    size?: number
  ) {
    return this.apiHandler.getFungibleTokenUnspents(codehash, genesis, address, size)
  }
  async getFungibleTokenBalance(codehash: string, genesis: string, address: string) {
    return this.apiHandler.getFungibleTokenBalance(codehash, genesis, address)
  }
 
  async getFungibleTokenSummary(address: string) {
    return this.apiHandler.getFungibleTokenSummary(address)
  }
  async getNonFungibleTokenUnspents(
    codehash: string,
    genesis: string,
    address: string,
    cursor?: number,
    size?: number
  ) {
    return this.apiHandler.getNonFungibleTokenUnspents(codehash, genesis, address, cursor, size)
  }
  async getNonFungibleTokenUnspentDetail(codehash: string, genesis: string, tokenIndex: string) {
    return this.apiHandler.getNonFungibleTokenUnspentDetail(codehash, genesis, tokenIndex)
  }
 
  async getNonFungibleTokenSummary(address: string) {
    return this.apiHandler.getNonFungibleTokenSummary(address)
  }
 
  async getNftSellUtxo(codehash: string, genesis: string, tokenIndex: string) {
    return this.apiHandler.getNftSellUtxo(codehash, genesis, tokenIndex)
  }
 
  async getNftSellList(codehash: string, genesis: string, cursor?: number, size?: number) {
    return this.apiHandler.getNftSellList(codehash, genesis, cursor, size)
  }
 
  async getNftSellListByAddress(address: string, cursor?: number, size?: number) {
    return this.apiHandler.getNftSellListByAddress(address, cursor, size)
  }
 
  async getOutpointSpent(txId: string, index: number) {
    return this.apiHandler.getOutpointSpent(txId, index)
  }
}