Solidity İle Ethereum Dao Geliştir
-
pragma solidity ^0.4.11; /* ETHLottery v1.1 author: Justin Spinner - 6/22/17 versions: v1.0: 0x450c5b37f21eb3B729b73327743ba1AC941ac627 A lottery based on integer values of hashes. - Every purchased ticket is a distinct hash calculated This is converted to a uint which is the ticket number. - When conditions are met (all tickets are purchased or time is up), the winning lottery ticket is produced and compared against all purchased tickets to find the closest. - It is not probable to have a matching ticket number unless we run into a hash collision. The ticket number closest to the winning lotto number is the winner. - In the unlikely event of a tie, the first purchaser wins. */ contract ETHLottery { address public owner; LottoInfo public info; uint nonce; bool override; bool alive; // general information for current lottery struct LottoInfo { uint version; uint jackpot; uint priceOf; uint max_tickets; uint avail_tickets; uint endBlock; } // store winners for each round (version) struct LottoWinners { address winner; uint lottoNumber; uint winningNumber; uint difference; } // store purchased tickets struct Tickets { address ticketOwner; uint ticketNumber; } // mappings mapping(uint => Tickets) public tickets; mapping(uint => LottoWinners) public winners; // events event LottoWinner(address winner, uint ticketNumber, uint amount); event BoughtTickets(address buyer, uint ntickets); event HashCollision(bytes32 ticketHash, bytes32 parm1, uint parm2, uint parm3); // only owner modifier onlyOwner { require(msg.sender == owner); _; } // input validation modifier goodParms(uint a, uint b, uint c) { require(a > 0); require(b > 0); require(c > 10 finney); _; } /* Constructor @parm - Ntickets - max number of purchasable tickets @parm - ticketPrice - price of 1 ticket in wei (1 ether = 1e18 wei) @parm - blockInterval - number of blocks from current in which ticket purchases are possible. */ function ETHLottery(uint Ntickets, uint ticketPrice, uint blockInterval) goodParms(Ntickets, blockInterval, ticketPrice) payable { owner = msg.sender; info.version = 1; info.max_tickets = Ntickets; info.avail_tickets = Ntickets; info.priceOf = ticketPrice; info.endBlock = block.number + blockInterval; if (msg.value > 0) info.jackpot = msg.value; } /* BuyTicket: @parm - num_tickets - number of tickets to purchase. Note: There is a defined limit to number of tickets someone can purchase at a time which is based on amount gas used for function execution and the gas limit for the block. In general, each ticket created takes an amount of gas, G where (Ntickets*G < gaslimit) must hold true. */ function BuyTicket(uint8 num_tickets) payable { require(block.number < info.endBlock); require(num_tickets > 0); require(info.avail_tickets >= num_tickets); require(msg.value >= (num_tickets*info.priceOf)); uint extraETH = msg.value - num_tickets*info.priceOf; // send any extra ETH back if (extraETH > 0) require(msg.sender.send(extraETH)); info.avail_tickets -= num_tickets; info.jackpot += msg.value-extraETH; for (uint i = 0; i < num_tickets; i++) { tickets[nonce].ticketOwner = msg.sender; tickets[nonce].ticketNumber = uint(sha256(block.blockhash(block.number-1), nonce)); nonce++; } BoughtTickets(msg.sender, num_tickets); // once the last ticket is purchased, should we make last purchaser pay // for PickWinningNumber() execution? It would be cheaper overall // but may make sense to have the owner initiate, or maybe a valid user // can initiate it after all tickets are purchased if they wish. if (info.avail_tickets == 0) PickWinningNumber(); } /* PickWinningNumber - generate lotto number and get winner */ function PickWinningNumber() { if ((block.number > info.endBlock) || (info.avail_tickets == 0) || (override)) { if (override) override = false; if (calcWinner(uint(sha256(block.blockhash(block.number-1), now, nonce)))) { nonce = 0; info.jackpot = 0; info.avail_tickets = 0; } } else throw; } /* calcWinner - find the ticket number closest to lotto number @parm lottoNumber - generated lotto number - This presents a problem where the amount of tickets could leave us stuck with the gas limit problem. */ function calcWinner(uint lottoNumber) internal returns(bool) { uint diff1 = 0; uint diff2 = 0; uint num = 0; LottoWinners winner = winners[info.version-1]; winner.lottoNumber = lottoNumber; address currentWinner = this; uint closestTicket = 0; for (uint i = 0; i < nonce; i++) { num = tickets[i].ticketNumber; // get difference from winningNumber if (num == lottoNumber) { closestTicket = num; currentWinner = tickets[i].ticketOwner; // record to verify if this is hash collision HashCollision(bytes32(num), block.blockhash(block.number-1), now, nonce); break; } else if (num > lottoNumber) { diff2 = num - lottoNumber; } else diff2 = lottoNumber - num; // set first ticket as the current closest ticket, else, check // the current closest ticket to lottoNumber. if (diff1 == 0) { diff1 = diff2; closestTicket = num; currentWinner = tickets[i].ticketOwner; } else { if (diff2 < diff1) { diff1 = diff2; closestTicket = num; currentWinner = tickets[i].ticketOwner; } else if (diff2 == diff1) break; // tie goes to first purchaser } } require(uint(currentWinner) != uint(this)); // make sure we can payout require(currentWinner.send(this.balance)); // re-assign ownership of contract owner = currentWinner; winner.winner = currentWinner; winner.winningNumber = closestTicket; winner.difference = diff1; // announce winner LottoWinner(currentWinner, closestTicket, info.jackpot); return true; } // allow owner to re-init new lottery after current round finishes function restartLottery(uint Ntickets, uint ticketPrice, uint blockInterval) goodParms(Ntickets, blockInterval, ticketPrice) onlyOwner payable { info.version++; info.max_tickets = Ntickets; info.avail_tickets = Ntickets; info.priceOf = ticketPrice; info.endBlock = block.number + blockInterval; if (msg.value > 0) info.jackpot = msg.value; } // clean up function kill() onlyOwner { // If in the middle of a round, call PickWinningNumber(). // The winner will be the next owner and selfdestruct in the context // of the new owner address. if ((info.avail_tickets != info.max_tickets) && (info.avail_tickets > 0)) { override = true; PickWinningNumber(); } selfdestruct(owner); } // throwback function() payable { info.jackpot += msg.value; } }
ran tarafından 30/Haz/17 20:18 tarihinde düzenlenmiştir -
JavaScript yakın bir dil
https://ethereum.gitbooks.io/frontier-guide/content/writing_contract.html
http://solidity.readthedocs.io/en/develop/layout-of-source-files.html
----------
örnek bir yarı ademi merkeziyetci uygulama -
https://ropsten.etherscan.io/address/0xa4c9019d2b480b5ca1b8309ab2aa36a21e37d10f#code
yukarıdaki kodun çalışan halli, bu adresten kopyalayarak derleyin.
tüm kodu ETH ağı yürütür. tüm ödeme dahil.
ran tarafından 30/Haz/17 20:19 tarihinde düzenlenmiştir -
Burdan
https://github.com/ethereum/mist/releases
cüzdanı indirip kurun , testnetE geçin - ve
Cüzdanı nekroze yani tüm bulokları indirmeniz gerekir ve sonra konturat bölümünden kodları yapıştırarak kontratı derlersiniz.
yanlız eth ihiyacınız olacak
metamask.io kurun açın "ropsten teste" geçim buy diyin altaki butona tıklayın yeni vir sayfa açılır ordan yeşil butona ardı ardına tıklayın ve bir kaç ETH alın.
Mist v0.8.10
cüzdanından adresini kopyala metamas aç ve SEND de ve mist cüdanındaki adresi yapıştır ve miktarı yaz ve gönder.
ran tarafından 30/Haz/17 20:13 tarihinde düzenlenmiştir -
bu adresten sözleşme ile etkileşime geçebilirsin:
bu adresi aç
https://www.myetherwallet.com/#contracts
üst sağ açılan menüden "ropsten" seç
ve bu adresteki
https://ropsten.etherscan.io/address/0xa4c9019d2b480b5ca1b8309ab2aa36a21e37d10f#code
Contract ABI
kopyala Contract ABI yapıştır
Adres bölümüne kontrat adresini:
0xa4c9019D2B480b5CA1B8309aB2aa36a21E37d10f
ve ERİŞİM de
alta bir meü açılır konturatın yapacağı üçretli ve üçretsiz tüm işler.
https://www.myetherwallet.com/#contracts da yine metamask kulanılır.
Konturatın altındaki küçük menüden BuyTicket seç
sayfanın altına özel anahtar'ı tıkladğında kutucuk aşılır ve metamask aç sağ üsteki anahtarı tıkla çüzdanı ilk çalıştırdığın şireni gir sumbit de gizli anahtar gözükür kopyala ve MY deki küçük kutuçuğa yapıştır. Aç de ve
" num_tickets uint8" çift haneli bir sayı gir
Gönderilecek miktar : 1
Gas Limiti : 21465
değerleri gir, Generate TRAnsaction tıkla ve EVET Eminim butonuu tıkla, bu kadar.
ran tarafından 30/Haz/17 20:46 tarihinde düzenlenmiştir
