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 ******************************************************************************/
16 #include "CCauction.h"
17 #include "../txmempool.h"
22 // start of consensus code
24 int64_t IsAuctionvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v)
27 if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 )
29 if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 )
30 return(tx.vout[v].nValue);
35 bool AuctionExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee)
37 static uint256 zerohash;
38 CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; int64_t inputs=0,outputs=0,assetoshis;
39 numvins = tx.vin.size();
40 numvouts = tx.vout.size();
41 for (i=0; i<numvins; i++)
43 //fprintf(stderr,"vini.%d\n",i);
44 if ( (*cp->ismyvin)(tx.vin[i].scriptSig) != 0 )
46 //fprintf(stderr,"vini.%d check mempool\n",i);
47 if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 )
48 return eval->Invalid("cant find vinTx");
51 //fprintf(stderr,"vini.%d check hash and vout\n",i);
52 if ( hashBlock == zerohash )
53 return eval->Invalid("cant Auction from mempool");
54 if ( (assetoshis= IsAuctionvout(cp,vinTx,tx.vin[i].prevout.n)) != 0 )
59 for (i=0; i<numvouts; i++)
61 //fprintf(stderr,"i.%d of numvouts.%d\n",i,numvouts);
62 if ( (assetoshis= IsAuctionvout(cp,tx,i)) != 0 )
63 outputs += assetoshis;
65 if ( inputs != outputs+COIN+txfee )
67 fprintf(stderr,"inputs %llu vs outputs %llu\n",(long long)inputs,(long long)outputs);
68 return eval->Invalid("mismatched inputs != outputs + COIN + txfee");
73 bool AuctionValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn, bool fulfilled)
75 int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; bool retval;
76 return(false); // reject any auction CC for now
77 numvins = tx.vin.size();
78 numvouts = tx.vout.size();
79 preventCCvins = preventCCvouts = -1;
81 return eval->Invalid("no vouts");
84 //fprintf(stderr,"check vins\n");
85 for (i=0; i<numvins; i++)
87 if ( IsCCInput(tx.vin[0].scriptSig) == 0 )
89 fprintf(stderr,"Auctionget invalid vini\n");
90 return eval->Invalid("illegal normal vini");
93 //fprintf(stderr,"check amounts\n");
94 if ( AuctionExactAmounts(cp,eval,tx,1,10000) == false )
96 fprintf(stderr,"Auctionget invalid amount\n");
102 if ( IsAuctionvout(cp,tx,0) != 0 )
107 if ( tx.vout[i].nValue != COIN )
108 return eval->Invalid("invalid Auction output");
109 retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts);
111 fprintf(stderr,"Auctionget validated\n");
112 else fprintf(stderr,"Auctionget invalid\n");
117 // end of consensus code
119 // helper functions for rpc calls in rpcwallet.cpp
121 int64_t AddAuctionInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,int64_t total,int32_t maxinputs)
123 char coinaddr[64]; int64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector<uint8_t> origpubkey; CTransaction vintx; int32_t n = 0;
124 std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs;
125 GetCCaddress(cp,coinaddr,pk);
126 SetCCunspents(unspentOutputs,coinaddr);
127 for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++)
129 txid = it->first.txhash;
131 if ( it->second.satoshis < 1000000 )
133 if ( GetTransaction(txid,vintx,hashBlock,false) != 0 )
135 if ( (nValue= IsAuctionvout(cp,vintx,(int32_t)it->first.index)) > 0 )
137 if ( total != 0 && maxinputs != 0 )
138 mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript()));
139 nValue = it->second.satoshis;
140 totalinputs += nValue;
142 if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) )
150 std::string AuctionBid(uint64_t txfee,uint256 itemhash,int64_t amount)
152 CMutableTransaction mtx; CPubKey mypk,Auctionpk; CScript opret; int64_t inputs,CCchange=0,nValue=COIN; struct CCcontract_info *cp,C;
153 cp = CCinit(&C,EVAL_AUCTION);
156 Auctionpk = GetUnspendable(cp,0);
157 mypk = pubkey2pk(Mypubkey());
158 if ( (inputs= AddAuctionInputs(cp,mtx,Auctionpk,nValue+txfee,60)) > 0 )
160 if ( inputs > nValue )
161 CCchange = (inputs - nValue - txfee);
163 mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,CCchange,Auctionpk));
164 mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG));
165 return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,opret));
166 } else fprintf(stderr,"cant find Auction inputs\n");
170 std::string AuctionDeliver(uint64_t txfee,uint256 itemhash,uint256 bidtxid)
172 CMutableTransaction mtx; CPubKey mypk,Auctionpk; CScript opret; int64_t inputs,CCchange=0,nValue=COIN; struct CCcontract_info *cp,C;
173 cp = CCinit(&C,EVAL_AUCTION);
176 Auctionpk = GetUnspendable(cp,0);
177 mypk = pubkey2pk(Mypubkey());
178 if ( (inputs= AddAuctionInputs(cp,mtx,Auctionpk,nValue+txfee,60)) > 0 )
180 if ( inputs > nValue )
181 CCchange = (inputs - nValue - txfee);
183 mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,CCchange,Auctionpk));
184 mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG));
185 return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,opret));
186 } else fprintf(stderr,"cant find Auction inputs\n");
190 std::string AuctionPost(uint64_t txfee,uint256 itemhash,int64_t minbid,char *title,char *description)
192 CMutableTransaction mtx; CPubKey mypk,Auctionpk; int64_t funds = 0; CScript opret; struct CCcontract_info *cp,C;
193 cp = CCinit(&C,EVAL_AUCTION);
196 mypk = pubkey2pk(Mypubkey());
197 Auctionpk = GetUnspendable(cp,0);
198 if ( AddNormalinputs(mtx,mypk,txfee,64) > 0 )
200 mtx.vout.push_back(MakeCC1vout(EVAL_AUCTION,funds,Auctionpk));
201 return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret));