1 /******************************************************************************
2 * Copyright © 2014-2018 The SuperNET Developers. *
4 * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
5 * the top-level directory of this distribution for the individual copyright *
6 * holder information and the developer policies on copyright and licensing. *
8 * Unless otherwise agreed in a custom licensing agreement, no part of the *
9 * SuperNET software, including this file may be copied, modified, propagated *
10 * or distributed except according to the terms contained in the LICENSE file *
12 * Removal or modification of this copyright notice is prohibited. *
14 ******************************************************************************/
19 Pegs CC builds on top of Prices CC and would bind a pricefeed to a token bid/ask automated marketmaking.
21 Funds deposited into CC address for a specific peg would then be used to fund the bid/ask as the pricefeed changes the price. Profits/losses would accumulate in the associated address.
23 In the event funds exceed a specified level, it can be spent into a collection address. The idea is that the collection address can further be used for revshares.
25 int64_t OraclePrice(int32_t height,uint256 reforacletxid,char *markeraddr,char *format);
27 OraclePrice is very useful for pegs.
31 // start of consensus code
33 int64_t IsPegsvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v)
36 if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 )
38 if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 )
39 return(tx.vout[v].nValue);
44 bool PegsExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee)
46 static uint256 zerohash;
47 CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; int64_t inputs=0,outputs=0,assetoshis;
48 numvins = tx.vin.size();
49 numvouts = tx.vout.size();
50 for (i=0; i<numvins; i++)
52 //fprintf(stderr,"vini.%d\n",i);
53 if ( (*cp->ismyvin)(tx.vin[i].scriptSig) != 0 )
55 //fprintf(stderr,"vini.%d check mempool\n",i);
56 if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 )
57 return eval->Invalid("cant find vinTx");
60 //fprintf(stderr,"vini.%d check hash and vout\n",i);
61 if ( hashBlock == zerohash )
62 return eval->Invalid("cant Pegs from mempool");
63 if ( (assetoshis= IsPegsvout(cp,vinTx,tx.vin[i].prevout.n)) != 0 )
68 for (i=0; i<numvouts; i++)
70 //fprintf(stderr,"i.%d of numvouts.%d\n",i,numvouts);
71 if ( (assetoshis= IsPegsvout(cp,tx,i)) != 0 )
72 outputs += assetoshis;
74 if ( inputs != outputs+txfee )
76 fprintf(stderr,"inputs %llu vs outputs %llu\n",(long long)inputs,(long long)outputs);
77 return eval->Invalid("mismatched inputs != outputs + txfee");
82 bool PegsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn, bool fulfilled)
84 int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,numblocks; bool retval; uint256 txid; uint8_t hash[32]; char str[65],destaddr[64];
86 std::vector<std::pair<CAddressIndexKey, CAmount> > txids;
87 numvins = tx.vin.size();
88 numvouts = tx.vout.size();
89 preventCCvins = preventCCvouts = -1;
91 return eval->Invalid("no vouts");
94 for (i=0; i<numvins; i++)
96 if ( IsCCInput(tx.vin[0].scriptSig) == 0 )
98 return eval->Invalid("illegal normal vini");
101 //fprintf(stderr,"check amounts\n");
102 if ( PegsExactAmounts(cp,eval,tx,1,10000) == false )
104 fprintf(stderr,"Pegsget invalid amount\n");
110 memcpy(hash,&txid,sizeof(hash));
111 retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts);
113 fprintf(stderr,"Pegsget validated\n");
114 else fprintf(stderr,"Pegsget invalid\n");
119 // end of consensus code
121 // helper functions for rpc calls in rpcwallet.cpp
123 int64_t AddPegsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,int64_t total,int32_t maxinputs)
125 char coinaddr[64]; int64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector<uint8_t> origpubkey; CTransaction vintx; int32_t vout,n = 0;
126 std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs;
127 GetCCaddress(cp,coinaddr,pk);
128 SetCCunspents(unspentOutputs,coinaddr);
129 for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++)
131 txid = it->first.txhash;
132 vout = (int32_t)it->first.index;
133 // no need to prevent dup
134 if ( GetTransaction(txid,vintx,hashBlock,false) != 0 )
136 if ( (nValue= IsPegsvout(cp,vintx,vout)) > 1000000 && myIsutxo_spentinmempool(txid,vout) == 0 )
138 if ( total != 0 && maxinputs != 0 )
139 mtx.vin.push_back(CTxIn(txid,vout,CScript()));
140 nValue = it->second.satoshis;
141 totalinputs += nValue;
143 if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) )
151 std::string PegsGet(uint64_t txfee,int64_t nValue)
153 CMutableTransaction mtx,tmpmtx; CPubKey mypk,Pegspk; int64_t inputs,CCchange=0; struct CCcontract_info *cp,C; std::string rawhex; uint32_t j; int32_t i,len; uint8_t buf[32768]; bits256 hash;
154 cp = CCinit(&C,EVAL_PEGS);
157 Pegspk = GetUnspendable(cp,0);
158 mypk = pubkey2pk(Mypubkey());
159 if ( (inputs= AddPegsInputs(cp,mtx,Pegspk,nValue+txfee,60)) > 0 )
161 if ( inputs > nValue )
162 CCchange = (inputs - nValue - txfee);
164 mtx.vout.push_back(MakeCC1vout(EVAL_PEGS,CCchange,Pegspk));
165 mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG));
166 fprintf(stderr,"start at %u\n",(uint32_t)time(NULL));
167 j = rand() & 0xfffffff;
168 for (i=0; i<1000000; i++,j++)
171 rawhex = FinalizeCCTx(-1LL,cp,tmpmtx,mypk,txfee,CScript() << OP_RETURN << E_MARSHAL(ss << (uint8_t)EVAL_PEGS << (uint8_t)'G' << j));
172 if ( (len= (int32_t)rawhex.size()) > 0 && len < 65536 )
175 decode_hex(buf,len,(char *)rawhex.c_str());
176 hash = bits256_doublesha256(0,buf,len);
177 if ( (hash.bytes[0] & 0xff) == 0 && (hash.bytes[31] & 0xff) == 0 )
179 fprintf(stderr,"found valid txid after %d iterations %u\n",i,(uint32_t)time(NULL));
182 //fprintf(stderr,"%02x%02x ",hash.bytes[0],hash.bytes[31]);
185 fprintf(stderr,"couldnt generate valid txid %u\n",(uint32_t)time(NULL));
187 } else fprintf(stderr,"cant find Pegs inputs\n");
191 std::string PegsFund(uint64_t txfee,int64_t funds)
193 CMutableTransaction mtx; CPubKey mypk,Pegspk; CScript opret; struct CCcontract_info *cp,C;
194 cp = CCinit(&C,EVAL_PEGS);
197 mypk = pubkey2pk(Mypubkey());
198 Pegspk = GetUnspendable(cp,0);
199 if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 )
201 mtx.vout.push_back(MakeCC1vout(EVAL_PEGS,funds,Pegspk));
202 return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret));
209 UniValue result(UniValue::VOBJ); char numstr[64];
210 CMutableTransaction mtx; CPubKey Pegspk; struct CCcontract_info *cp,C; int64_t funding;
211 result.push_back(Pair("result","success"));
212 result.push_back(Pair("name","Pegs"));
213 cp = CCinit(&C,EVAL_PEGS);
214 Pegspk = GetUnspendable(cp,0);
215 funding = AddPegsInputs(cp,mtx,Pegspk,0,0);
216 sprintf(numstr,"%.8f",(double)funding/COIN);
217 result.push_back(Pair("funding",numstr));