當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript server.Server類代碼示例

本文整理匯總了TypeScript中duniter/server.Server的典型用法代碼示例。如果您正苦於以下問題:TypeScript Server類的具體用法?TypeScript Server怎麽用?TypeScript Server使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Server類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: async

    pay: async (fromBlock: number, toBlock: number) => {
      let blocks = await duniterServer.dal.getBlocksBetween(fromBlock, toBlock);
      let reallyPaid = payperblock * blocks.length; // Implicitely: amount in the current base
      let statsPerIssuer = getStatsPerIssuer(blocks, payperblock);
      let sources = await duniterServer.dal.getAvailableSourcesByPubkey(remuniterPubkey);
      let current = await duniterServer.dal.getCurrentBlockOrNull() as DBBlock
      let maxBase = current.unitbase;

      const minBase = sources.reduce((min, src) => Math.min(min, src.base), 0);
      const bases: {
        [k: number]: number
      } = {}
      sources.forEach((src) => bases[src.base] = 0);
      sources.forEach((src) => bases[src.base] += src.amount);
      bases[maxBase] = bases[maxBase] || 0;

      // Try to convert 1 base to the other
      let outputsOfRests = [];
      for (let i = minBase; i < maxBase; i++) {
        bases[i] = bases[i] || 0;
        bases[i + 1] = bases[i + 1] || 0;
        const rest = bases[i] % 10;
        bases[i] -= rest;
        bases[i + 1] += bases[i] / 10;
        outputsOfRests.push([rest, i, 'SIG(' + remuniterPubkey + ')'].join(':'));
      }
      const availableMoney = bases[maxBase];

      let outputsToIssuers = Underscore.keys(statsPerIssuer).map((issuer) => [statsPerIssuer[issuer].amount, maxBase, 'SIG(' + issuer + ')'].join(':'));

      if (availableMoney >= reallyPaid) {
        let tx = {
          documentType: 'transaction',
          version: 10,
          currency: duniterServer.conf.currency,
          blockstamp: [current.number, current.hash].join('-'),
          locktime: 0,
          issuers: [remuniterPubkey],
          inputs: sources.map((src) => [src.amount, src.base, src.type, src.identifier, src.noffset].join(':')),
          unlocks: sources.map((src, index) => [index, 'SIG(0)'].join(':')),
          outputs: outputsOfRests.concat(outputsToIssuers).concat([availableMoney - reallyPaid, maxBase, 'SIG(' + remuniterPubkey + ')'].join(':')),
          comment: ['REMU', fromBlock, toBlock].join(':'),
          signatures: [] as string[]
        };
        let rawTX = getRawTransaction(tx);

        // ----- SIGNATURE -----
        tx.signatures = [await duniterServer.sign(rawTX)];

        // ----- SUBMITTING -----
        await duniterServer.writeRawTransaction(getRawTransaction(tx));

        return existsTx(fromBlock, toBlock, 'sending');
      }
      return false;
    },
開發者ID:duniter,項目名稱:remuniter,代碼行數:56,代碼來源:wallet.ts

示例2: constructor

  constructor(
    private port:number,
    private server:Server) {

    ProverDependency.duniter.methods.hookServer(server)

    server.addEndpointsDefinitions(async () => {
      return BmaDependency.duniter.methods.getMainEndpoint(server.conf)
    })
  }
開發者ID:duniter,項目名稱:remuniter,代碼行數:10,代碼來源:test-tools.ts

示例3: listenWebSocket

function listenWebSocket(httpServer: any, duniterServer: Server, payperblock: number) {


  let remuWallet = wallet(duniterServer, payperblock);
  let lastBlocks: DBBlock[] = [];
  let top1 = remuWallet.lastDayTheoretical();
  let top2 = remuWallet.lastWeekTheoretical();
  let topgifts = remuWallet.topGifts();
  let wssBlock = new WebSocketServer({
    server: httpServer,
    path: '/ws/block'
  });

  let getData = () => co(function *() {
    try {
      let current = yield remuWallet.getCurrentBlock();
      let data1 = yield top1;
      let data2 = yield top2;
      let data3 = yield topgifts;
      yield lastBlocks.map((b: any) => co(function *() {
        const idty = yield remuWallet.getUID(b.issuer)
        b.uid = (idty && idty.uid) || b.issuer.substr(0, 10)
        return b;
      }));
      (current as any).uid = (yield remuWallet.getUID(current.issuer)).uid;
      let remains = yield remuWallet.remains();
      return {
        key: remuWallet.pubkey,
        current,
        issuersCount: current.issuersCount,
        issuersWeek: data2.length,
        issuersFrame: current.issuersFrame,
        remains: remains,
        remains_days: (Math.floor(remains / (remuWallet.pay_per_block * Math.pow(10, current.unitbase))) * duniterServer.conf.avgGenTime / (3600 * 24)).toFixed(1),
        pay_per_block: remuWallet.pay_per_block,
        unitbase: current.unitbase,
        blocks: lastBlocks,
        top1: data1,
        top2: data2,
        topgifts: data3,
        unit: PAY_UNIT
      };
    } catch (e) {
      console.error(e);
    }
  });

  wssBlock.on('connection', function connection(ws: any) {
    co(function *() {
      if (lastBlocks.length < 10) {
        let current = yield duniterServer.dal.getCurrentBlockOrNull();
        lastBlocks = yield duniterServer.dal.getBlocksBetween(current.number - 10, current.number);
      }

      ws.on('message', function(message: any) {
        console.log('received: %s', message);
        co(function *() {
          let toSend = yield getData();
          ws.send(JSON.stringify(toSend));
        });
      });
    });
  });

  wssBlock.broadcast = (data: any) => wssBlock.clients.forEach((client: any) => client.send(data));

  // Forward blocks
  duniterServer.on('data', async (data: any) => {

    if (data.bcEvent === 'newHEAD') {
      try {
        // Broadcast block
        lastBlocks.shift();
        lastBlocks.push(data.block);
        top1 = remuWallet.lastDayTheoretical();
        top2 = remuWallet.lastWeekTheoretical();
        topgifts = remuWallet.topGifts();
        co(function* () {
          let toSend = yield getData()
          wssBlock.broadcast(JSON.stringify(toSend));
        });
      } catch (e) {
        console.error(e)
      }
    }
  })
}
開發者ID:duniter,項目名稱:remuniter,代碼行數:87,代碼來源:webserver.ts

示例4: commit

 async commit(options:any = null) {
   const proven = await this.generateNext(options)
   await this.server.writeBlock(proven, true, true) // The resolution is done manually
   const blocksResolved = await this.server.BlockchainService.blockResolution()
   console.log(BlockDTO.fromJSONObject(blocksResolved).getRawSigned())
   if (!blocksResolved) {
     throw Error('BLOCK_WASNT_COMMITTED')
   }
   return blocksResolved
 }
開發者ID:duniter,項目名稱:remuniter,代碼行數:10,代碼來源:test-tools.ts

示例5: createJoinAndSubmit

 async createJoinAndSubmit() {
   const join = await this.makeMembership('IN')
   await this.server.writeRawMembership(join.getRawSigned())
 }
開發者ID:duniter,項目名稱:remuniter,代碼行數:4,代碼來源:test-user.ts

示例6: createCertAndSubmit

 public async createCertAndSubmit(user: TestUser) {
   const cert = await this.createCert(user)
   await this.server.writeRawCertification(cert.getRawSigned())
 }
開發者ID:duniter,項目名稱:remuniter,代碼行數:4,代碼來源:test-user.ts

示例7: createIdentityAndSubmit

 async createIdentityAndSubmit() {
   await this.createIdentity()
   await this.server.writeRawIdentity(this.createdIdentity.getRawSigned())
 }
開發者ID:duniter,項目名稱:remuniter,代碼行數:4,代碼來源:test-user.ts

示例8: createTxAndSubmit

 async createTxAndSubmit(recipient: TestUser, amount: number, comment = '') {
   const raw = await this.prepareITX(amount, recipient, comment)
   await this.server.writeRawTransaction(raw)
 }
開發者ID:duniter,項目名稱:remuniter,代碼行數:4,代碼來源:test-user.ts


注:本文中的duniter/server.Server類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。