Exemple #1
0
extern "C" char* GetPeggyByBlock(CBlock *pblock, CBlockIndex *pindex)
{
    cJSON *array, *arrayObj, *header, *peggybase, *peggypayments, *peggytx;
    header = cJSON_CreateObject();
    peggybase = cJSON_CreateObject();
    peggypayments = cJSON_CreateArray();
    peggytx = cJSON_CreateArray();
    array = cJSON_CreateArray();
    arrayObj = cJSON_CreateObject();
    //header
    jaddnum(header, "blocknum", pindex->nHeight);
    jaddnum(header, "blocktimestamp", pblock->nTime);
    jaddstr(header, "blockhash", (char*)pindex->GetBlockHash().ToString().c_str());

    jadd(arrayObj, "header", header);

    if(pindex->nHeight >= nMinPeggyHeight){
        if(!pblock->vtx[2].IsPeggyBase())
            throw runtime_error(
                "Block Does not contain a peggybase transaction!\n"
            );
    }
    else{
        return "Not a peggy block\n";
    }


    int index, vouts;

    //peggypayments
    char bits[4096];
    char hex[4096];

    bool fPeggy = false;
    uint64_t nPeggyPayments = 0;

    for(index=0; index<pblock->vtx.size(); index++){
        fPeggy = false;
        const CTransaction tempTx = pblock->vtx[index];
        if(tempTx.IsCoinBase() || tempTx.IsCoinStake())
            continue;

        if(tempTx.IsPeggyBase()){
            const CTxOut peggyOut = tempTx.vout[0];
            const CScript priceFeed = peggyOut.scriptPubKey;

            char peggybits[4096];
            strcpy(peggybits, (char*)HexStr(priceFeed.begin(), priceFeed.end(), false).c_str()+2);

            jaddstr(peggybase, "txid", (char*)tempTx.GetHash().ToString().c_str());
            jaddstr(peggybase, "peggybase", peggybits);
            jaddnum(peggybase, "time", tempTx.nTime);
            jaddnum(peggybase, "txind", 2);
            jaddnum(peggybase, "voutind", 0);
            jaddstr(peggybase, "address", "peggypayments");
            fPeggy = true;
        }

        for(vouts=0; vouts<tempTx.vout.size(); vouts++){

            const CTxOut tempVout = tempTx.vout[vouts];

            const CScript tempScriptPubKey = tempVout.scriptPubKey;
            CTxDestination destAddress;
            strcpy(hex, (char*)HexStr(tempScriptPubKey.begin(), tempScriptPubKey.end(), false).c_str());
            sprintf(hex, "%s", hex);

            if(fPeggy && (vouts != 0)){
                cJSON *peggyOut = cJSON_CreateObject();


                jaddstr(peggyOut, "txid", (char*)tempTx.GetHash().ToString().c_str());
                jaddnum(peggyOut, "time", tempTx.nTime);
                jaddnum(peggyOut, "txind", index);
                jaddnum(peggyOut, "amount", tempVout.nValue);
                jaddnum(peggyOut, "voutind", vouts);
                jaddstr(peggyOut, "scriptPubKey", hex);

                if(ExtractDestination(tempScriptPubKey, destAddress)){
                    jaddstr(peggyOut, "address", (char*)CBitcoinAddress(destAddress).ToString().c_str());
                }
                else{
                    jaddstr(peggyOut, "address", "null");
                }

                nPeggyPayments += (uint64_t)tempVout.nValue;

                jaddi(peggypayments, peggyOut);
            }

            else if(!fPeggy && (isOpReturn(hex) == 0)){ //peggy lock found
                cJSON *lockVout = cJSON_CreateObject();

                jaddstr(lockVout, "txid", (char*)tempTx.GetHash().ToString().c_str());
                jaddnum(lockVout, "time", tempTx.nTime);
                jaddnum(lockVout, "txind", index);
                jaddnum(lockVout, "voutind", vouts);
                jaddstr(lockVout, "scriptPubKey", hex);

                if(ExtractDestination(tempScriptPubKey, destAddress)){
                    jaddstr(lockVout, "address", (char*)CBitcoinAddress(destAddress).ToString().c_str());
                }
                else{
                    jaddstr(lockVout, "address", "null");
                }

                jaddnum(lockVout, "amount", (uint64_t)tempVout.nValue);

                jaddi(peggytx, lockVout);

                continue; //1 op_return per tx
            }

        }
    }
    jaddnum(peggybase, "amount", nPeggyPayments);

    jadd(arrayObj, "peggybase", peggybase);
    jadd(arrayObj, "peggypayments", peggypayments);
    jadd(arrayObj, "peggytx", peggytx);

    jaddi(array, arrayObj);
    return jprint(array,0);
}