Cryo Explorer Ethereum Mainnet

Address Contract Partially Verified

Address 0x867092A32bC16816F12Fb326EfF7A2865E1ec138
Balance 0 ETH
Nonce 1
Code Size 17660 bytes
Indexed Transactions 0
External Etherscan · Sourcify

Contract Bytecode

17660 bytes
0x608060405234801561001057600080fd5b50600436106102955760003560e01c8063839df94511610167578063b8eaa980116100ce578063d4876b9f11610087578063d4876b9f14610863578063e83bfb5814610876578063ebbdd2b014610899578063f6a94ecb146108ac578063fc0c546a146108bf578063fe92049d146108d257600080fd5b8063b8eaa980146107ea578063bc525652146107fd578063bf53b1881461081d578063c081d8a314610830578063d09cc57e14610781578063d44e293c1461085057600080fd5b80639f1025c6116101205780639f1025c61461073e578063a1130d041461076e578063a462fb7b14610781578063ab5a4e3514610794578063ac7b2a5f146107a7578063acae8f4e146107ba57600080fd5b8063839df9451461055a57806383bf4609146105b75780638d552d46146105ca578063924532fb146105ed57806395addb90146106135780639e63fa6a146106f957600080fd5b8063484c07141161020b578063590158a7116101c4578063590158a7146104725780636fa42742146104d057806370a08231146104f0578063762c38fd146105105780637f8d429e1461052357806382ffa9f71461053657600080fd5b8063484c0714146103d65780634dc266b4146103f95780634df6ca2a1461040c5780634e60f8831461041f57806351577ea91461043f57806352debac31461045f57600080fd5b80632417395c1161025d5780632417395c1461031e5780632518904c1461034157806326d6c97b1461038557806328828b1e146103a85780632f998a6f146103bb5780633ccfd60b146103ce57600080fd5b806306c3b67a1461029a5780631101a0fd146102c2578063128b7a47146102d757806312a203c3146102ea578063144fa6d71461030b575b600080fd5b6102ad6102a836600461397f565b6108e5565b60405190151581526020015b60405180910390f35b6102d56102d03660046139d0565b6108fa565b005b6102d56102e5366004613bd7565b610c00565b6102fd6102f8366004613b1d565b610d9d565b6040519081526020016102b9565b6102d561031936600461384c565b610f74565b6102fd61032c36600461397f565b60009081526005602052604090206003015490565b61036d61034f36600461397f565b6000908152600560205260409020600101546001600160a01b031690565b6040516001600160a01b0390911681526020016102b9565b6102fd61039336600461397f565b60009081526005602052604090206006015490565b6102d56103b6366004613869565b610ffa565b6102d56103c9366004613ba5565b6112b2565b6102d56114c9565b6102fd6103e436600461397f565b60009081526005602052604090206007015490565b6102d5610407366004613ba5565b6115a6565b6102d561041a36600461397f565b6118ad565b6102fd61042d36600461397f565b60046020526000908152604090205481565b6102fd61044d36600461397f565b60009081526005602052604090205490565b6102fd61046d366004613e42565b6118fb565b6104ab61048036600461397f565b6006602052600090815260409020805460018201546002909201546001600160a01b03909116919083565b604080516001600160a01b0390941684526020840192909252908201526060016102b9565b6102fd6104de36600461384c565b60086020526000908152604090205481565b6102fd6104fe36600461384c565b60016020526000908152604090205481565b6102fd61051e366004613cd4565b611a03565b6102ad61053136600461397f565b611b00565b6102fd61054436600461397f565b6000908152600560208190526040909120015490565b61059561056836600461397f565b6007602052600090815260409020805460019091015463ffffffff821691640100000000900460ff169083565b6040805163ffffffff90941684529115156020840152908201526060016102b9565b6102fd6105c5366004613bf9565b611b5c565b6102fd6105d836600461397f565b60009081526005602052604090206004015490565b6102ad6105fb36600461397f565b60009081526005602052604090206002015460ff1690565b61069261062136600461397f565b60056020819052600091825260409091208054600182015460028301546003840154600485015495850154600686015460079096015494966001600160a01b0385169663ffffffff600160a01b8704811697600160c01b8804821697600160e01b90049091169560ff16949391908b565b604080519b8c526001600160a01b03909a1660208c015263ffffffff988916998b019990995295871660608a015295909316608088015290151560a087015260c086015260e0850152610100840191909152610120830152610140820152610160016102b9565b61072961070736600461397f565b600090815260056020526040902060010154600160a01b900463ffffffff1690565b60405163ffffffff90911681526020016102b9565b61072961074c36600461397f565b600090815260056020526040902060010154600160c01b900463ffffffff1690565b6102fd61077c366004613c35565b611bf8565b6102fd61078f36600461397f565b611c14565b6102fd6107a236600461397f565b611c53565b6102d56107b5366004613b6d565b611d84565b6107296107c836600461397f565b600090815260056020526040902060010154600160e01b900463ffffffff1690565b6102d56107f8366004613b6d565b611ff1565b6102fd61080b36600461397f565b60036020526000908152604090205481565b6102fd61082b366004613da3565b61225f565b6102fd61083e36600461397f565b60096020526000908152604090205481565b6102d561085e366004613abf565b6126a7565b6102fd610871366004613d16565b6127be565b6102ad61088436600461397f565b600a6020526000908152604090205460ff1681565b6102d56108a736600461397f565b6128c7565b6102d56108ba366004613998565b6129cb565b60005461036d906001600160a01b031681565b6102d56108e0366004613a86565b612c4b565b60006001196108f383611c14565b1492915050565b8461090481611b00565b6109295760405162461bcd60e51b8152600401610920906141d6565b60405180910390fd5b60008551116109935760405162461bcd60e51b815260206004820152603060248201527f6174206c65617374206f6e6520686973746f7279206861736820656e7472792060448201526f1b5d5cdd081899481c1c9bdd9a59195960821b6064820152608401610920565b600086815260066020908152604080832080546001820154600290920154600594859052928520938401546004909401546001600160a01b03909116949193915b8a51811015610b42576000610a50848d84815181106109f5576109f5614482565b60200260200101518b8581518110610a0f57610a0f614482565b60200260200101518d8681518110610a2957610a29614482565b60200260200101518f8781518110610a4357610a43614482565b6020026020010151612d9f565b9050610a5c868661435e565b9450610ab98d84878a8f8781518110610a7757610a77614482565b60200260200101518f8881518110610a9157610a91614482565b60200260200101518f8981518110610aab57610aab614482565b602002602001015188612e8f565b8098508196505050898281518110610ad357610ad3614482565b60200260200101519550600560008e8152602001908152602001600020600601548614610b1257610b0560288761439e565b610b0f90876143f4565b95505b8b8281518110610b2457610b24614482565b60200260200101519350508080610b3a9061443b565b9150506109d4565b8215610ba0576001600160a01b03861615610b6757610b628c8786612fd9565b600093505b60008c815260066020526040902080546001600160a01b0319166001600160a01b03881617815560018101869055600201849055610bdc565b610bb48c87610baf888861435e565b612fd9565b60008c815260066020526040812080546001600160a01b031916815560018101829055600201555b50506000998a52600560208190526040909a20909901989098555050505050505050565b6000828152600560205260409020600101548290600160c01b900463ffffffff16610c3d5760405162461bcd60e51b815260040161092090614080565b60008181526005602052604090206002015460ff1615610c6f5760405162461bcd60e51b815260040161092090614273565b600081815260056020526040902060010154600160e01b900463ffffffff16801580610ca657504263ffffffff168163ffffffff16115b610cc25760405162461bcd60e51b815260040161092090614036565b600082815260056020526040902060010154600160a01b900463ffffffff16801580610cfa57504263ffffffff168163ffffffff1611155b610d165760405162461bcd60e51b8152600401610920906140f1565b610d1f8461305b565b600085815260056020526040902060030154610d3c90859061435e565b6000868152600560205260409081902060030182905551339187917f54d68405b79f2aa4fd4e8db7b67844ad254cf8f208aac476c2894134a9deab6691610d8e91899190918252602082015260400190565b60405180910390a35050505050565b600085610da981611b00565b610dc55760405162461bcd60e51b8152600401610920906141d6565b6000878152600560205260409020548614610e225760405162461bcd60e51b815260206004820152601760248201527f636f6e74656e742068617368206d757374206d617463680000000000000000006044820152606401610920565b6000878152600560205260409020600101546001600160a01b03868116911614610e865760405162461bcd60e51b81526020600482015260156024820152740c2e4c4d2e8e4c2e8dee440daeae6e840dac2e8c6d605b1b6044820152606401610920565b60008781526005602052604090206001015463ffffffff600160c01b90910481169085161115610ef85760405162461bcd60e51b815260206004820152601b60248201527f74696d656f7574206d757374206265206c6f6e6720656e6f75676800000000006044820152606401610920565b600087815260056020526040902060060154831115610f595760405162461bcd60e51b815260206004820152601860248201527f626f6e64206d757374206265206869676820656e6f75676800000000000000006044820152606401610920565b50505060009384525050600560205250604090206004015490565b6000546001600160a01b031615610fd85760405162461bcd60e51b815260206004820152602260248201527f546f6b656e2063616e206f6e6c7920626520696e697469616c697a6564206f6e604482015261636560f01b6064820152608401610920565b600080546001600160a01b0319166001600160a01b0392909216919091179055565b6000805b87518210156112a057600088838151811061101b5761101b614482565b60200260200101519050600088848151811061103957611039614482565b602002602001015190506000816001600160401b0381111561105d5761105d614498565b604051908082528060200260200182016040528015611086578160200160208202803683370190505b5090506000826001600160401b038111156110a3576110a3614498565b6040519080825280602002602001820160405280156110cc578160200160208202803683370190505b5090506000836001600160401b038111156110e9576110e9614498565b604051908082528060200260200182016040528015611112578160200160208202803683370190505b5090506000846001600160401b0381111561112f5761112f614498565b604051908082528060200260200182016040528015611158578160200160208202803683370190505b50905060005b85811015611279578c888151811061117857611178614482565b602002602001015185828151811061119257611192614482565b6020026020010181815250508b88815181106111b0576111b0614482565b60200260200101518482815181106111ca576111ca614482565b60200260200101906001600160a01b031690816001600160a01b0316815250508a88815181106111fc576111fc614482565b602002602001015183828151811061121657611216614482565b60200260200101818152505089888151811061123457611234614482565b602002602001015182828151811061124e5761124e614482565b6020908102919091010152876112638161443b565b98505080806112719061443b565b91505061115e565b61128687868686866108fa565b5050505050505081806112989061443b565b925050610ffe565b6112a86114c9565b5050505050505050565b6000848152600560205260409020600101548490600160c01b900463ffffffff166112ef5760405162461bcd60e51b815260040161092090614080565b60008181526005602052604090206002015460ff16156113215760405162461bcd60e51b815260040161092090614273565b600081815260056020526040902060010154600160e01b900463ffffffff1680158061135857504263ffffffff168163ffffffff16115b6113745760405162461bcd60e51b815260040161092090614036565b600082815260056020526040902060010154600160a01b900463ffffffff168015806113ac57504263ffffffff168163ffffffff1611155b6113c85760405162461bcd60e51b8152600401610920906140f1565b8684600081116113ea5760405162461bcd60e51b815260040161092090614244565b60008281526005602052604090206006015480611437576000838152600560205260409020600701548210156114325760405162461bcd60e51b815260040161092090613fff565b611461565b6114428160026143d5565b8210156114615760405162461bcd60e51b81526004016109209061415f565b8988801561149a5760008281526005602052604090206006015481101561149a5760405162461bcd60e51b81526004016109209061420d565b6114a38961305b565b6114b18c8c338c60006131bb565b6114bb8c8c613294565b505050505050505050505050565b336000818152600160205260408082208054908390559154905163a9059cbb60e01b815260048101939093526024830182905290916001600160a01b039091169063a9059cbb90604401602060405180830381600087803b15801561152d57600080fd5b505af1158015611541573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611565919061395d565b61156e57600080fd5b60405181815233907f4ce7033d118120e254016dccf195288400b28fc8936425acd5f17ce2df3ab7089060200160405180910390a250565b6000848152600560205260409020600101548490600160c01b900463ffffffff166115e35760405162461bcd60e51b815260040161092090614080565b600081815260056020526040902060010154600160e01b900463ffffffff1680158061161a57504263ffffffff168163ffffffff16115b6116785760405162461bcd60e51b815260206004820152602960248201527f66696e616c697a6174696f6e206465616c696e65206d757374206e6f742068616044820152681d99481c185cdcd95960ba1b6064820152608401610920565b600082815260056020526040902060010154600160a01b900463ffffffff168015806116b057504263ffffffff168163ffffffff1611155b6116cc5760405162461bcd60e51b8152600401610920906140f1565b604080516020808201899052818301889052825180830384018152606083018452805190820120608083018b905260a0830181905260c08084018990528451808503909101815260e090930184528251928201929092206000818152600790925292902054909190640100000000900460ff161561179f5760405162461bcd60e51b815260206004820152602a60248201527f636f6d6d69746d656e74206d757374206e6f742068617665206265656e2072656044820152691d99585b1959081e595d60b21b6064820152608401610920565b60008181526007602052604090205463ffffffff4281169116116118115760405162461bcd60e51b8152602060048201526024808201527f72657665616c20646561646c696e65206d757374206e6f7420686176652070616044820152631cdcd95960e21b6064820152608401610920565b6000818152600760209081526040808320600181018c9055805464ff0000000019166401000000001790558b8352600590915290206006015486141561185b5761185b8989613294565b6040805189815260208101899052908101879052829033908b907fa7b2d313bc7a062e30b2c3b811aa4c9faf09755a6b4ea3bf42deff920944332f9060600160405180910390a4505050505050505050565b33600081815260086020908152604091829020849055815192835282018390527fdca703d022171824d3d639b33c1525fd2338120b4cfb89507c0b59596893acda910160405180910390a150565b60006119068261305b565b6000898152600360205260409020546119315760405162461bcd60e51b8152600401610920906141a9565b600089868a60405160200161194893929190613fb3565b60405160208183030381529060405280519060200120905060008189898730338b60405160200161197f9796959493929190613f03565b60405160208183030381529060405280519060200120905081336001600160a01b0316827ffe2dac156a3890636ce13f65f4fdf41dcaee11526e4a5374531572d92194796c8e8e8e8e8e8e426040516119de97969594939291906142bb565b60405180910390a46119f581838b8b8b8a8a6132fc565b9a9950505050505050505050565b600086815260036020526040812054611a2e5760405162461bcd60e51b8152600401610920906141a9565b6000878488604051602001611a4593929190613fb3565b60405160208183030381529060405280519060200120905060008187876000303389604051602001611a7d9796959493929190613f03565b60405160208183030381529060405280519060200120905081336001600160a01b0316827ffe2dac156a3890636ce13f65f4fdf41dcaee11526e4a5374531572d92194796c8c8c8c8c8c8c42604051611adc97969594939291906142bb565b60405180910390a4611af481838989896000806132fc565b98975050505050505050565b60008181526005602052604081206001810154600290910154600160e01b90910463ffffffff169060ff16158015611b3d575063ffffffff811615155b8015611b5557504263ffffffff168163ffffffff1611155b9392505050565b600254600081815260036020908152604080832043905551919291611b8391859101613f97565b60408051601f1981840301815282825280516020918201206000858152600490925291902055339082907fb87fb721c0a557bb8dff89a86796466931d82ba530a66a239263eb8735ade2e490611bda908790613fec565b60405180910390a3611bed81600161435e565b60025590505b919050565b600080611c0488611b5c565b9050611af4818888888888611a03565b600081611c2081611b00565b611c3c5760405162461bcd60e51b8152600401610920906141d6565b505060009081526005602052604090206004015490565b600080611c5f83611c14565b9050600119811415611d7e5760008381526009602052604090205480611ced5760405162461bcd60e51b815260206004820152603760248201527f5175657374696f6e2077617320736574746c656420746f6f20736f6f6e20616e60448201527f6420686173206e6f74206265656e2072656f70656e65640000000000000000006064820152608401610920565b611cf681611c14565b9150600119821415611d7c5760405162461bcd60e51b815260206004820152604360248201527f5175657374696f6e207265706c6163656d656e742077617320736574746c656460448201527f20746f6f20736f6f6e20616e6420686173206e6f74206265656e2072656f70656064820152621b995960ea1b608482015260a401610920565b505b92915050565b6000858152600560205260409020600101548590600160c01b900463ffffffff16611dc15760405162461bcd60e51b815260040161092090614080565b60008181526005602052604090206002015460ff1615611df35760405162461bcd60e51b815260040161092090614273565b600081815260056020526040902060010154600160e01b900463ffffffff16801580611e2a57504263ffffffff168163ffffffff16115b611e465760405162461bcd60e51b815260040161092090614036565b600082815260056020526040902060010154600160a01b900463ffffffff16801580611e7e57504263ffffffff168163ffffffff1611155b611e9a5760405162461bcd60e51b8152600401610920906140f1565b878460008111611ebc5760405162461bcd60e51b815260040161092090614244565b60008281526005602052604090206006015480611f0957600083815260056020526040902060070154821015611f045760405162461bcd60e51b815260040161092090613fff565b611f33565b611f148160026143d5565b821015611f335760405162461bcd60e51b81526004016109209061415f565b8a898015611f6c57600082815260056020526040902060060154811015611f6c5760405162461bcd60e51b81526004016109209061420d565b611f758961305b565b60408051602081018f90529081018d9052606081018a905260009060800160408051601f198184030181529190528051602090910120905060006001600160a01b038c1615611fc4578b611fc6565b335b9050611fd28f83613620565b611fe08f83838e60016131bb565b505050505050505050505050505050565b6000858152600560205260409020600101548590600160c01b900463ffffffff1661202e5760405162461bcd60e51b815260040161092090614080565b60008181526005602052604090206002015460ff16156120605760405162461bcd60e51b815260040161092090614273565b600081815260056020526040902060010154600160e01b900463ffffffff1680158061209757504263ffffffff168163ffffffff16115b6120b35760405162461bcd60e51b815260040161092090614036565b600082815260056020526040902060010154600160a01b900463ffffffff168015806120eb57504263ffffffff168163ffffffff1611155b6121075760405162461bcd60e51b8152600401610920906140f1565b8784600081116121295760405162461bcd60e51b815260040161092090614244565b60008281526005602052604090206006015480612176576000838152600560205260409020600701548210156121715760405162461bcd60e51b815260040161092090613fff565b6121a0565b6121818160026143d5565b8210156121a05760405162461bcd60e51b81526004016109209061415f565b8a8980156121d9576000828152600560205260409020600601548110156121d95760405162461bcd60e51b81526004016109209061420d565b6121e28961305b565b6001600160a01b038a166122385760405162461bcd60e51b815260206004820152601960248201527f616e737765726572206d757374206265206e6f6e2d7a65726f000000000000006044820152606401610920565b6122468d8d8c8c60006131bb565b6122508d8d613294565b50505050505050505050505050565b600061226a836108e5565b6122dc5760405162461bcd60e51b815260206004820152603f60248201527f596f752063616e206f6e6c792072656f70656e207175657374696f6e7320746860448201527f6174207265736f6c76656420617320736574746c656420746f6f20736f6f6e006064820152608401610920565b60008a878b6040516020016122f393929190613fb3565b60408051601f1981840301815291815281516020928301206000878152600590935291205490915081146123615760405162461bcd60e51b81526020600482015260156024820152740c6dedce8cadce840d0c2e6d040dad2e6dac2e8c6d605b1b6044820152606401610920565b6000848152600560205260409020600101546001600160a01b038a81169116146123c35760405162461bcd60e51b81526020600482015260136024820152720c2e4c4d2e8e4c2e8dee440dad2e6dac2e8c6d606b1b6044820152606401610920565b60008481526005602052604090206001015463ffffffff898116600160c01b90920416146124265760405162461bcd60e51b815260206004820152601060248201526f0e8d2dacadeeae840dad2e6dac2e8c6d60831b6044820152606401610920565b60008481526005602052604090206001015463ffffffff888116600160a01b909204161461248c5760405162461bcd60e51b81526020600482015260136024820152720dee0cadcd2dccebee8e640dad2e6dac2e8c6d606b1b6044820152606401610920565b60008481526005602052604090206007015485146124e05760405162461bcd60e51b81526020600482015260116024820152700dad2dcbec4dedcc840dad2e6dac2e8c6d607b1b6044820152606401610920565b6000848152600a602052604090205460ff16156125595760405162461bcd60e51b815260206004820152603160248201527f5175657374696f6e20697320616c72656164792072656f70656e696e67206120604482015270383932bb34b7bab99038bab2b9ba34b7b760791b6064820152608401610920565b6000848152600960205260409020548481156125e857612578826108e5565b6125cf5760405162461bcd60e51b815260206004820152602260248201527f5175657374696f6e2068617320616c7265616479206265656e2072656f70656e604482015261195960f21b6064820152608401610920565b506000818152600a60205260409020805460ff19169055805b60006125fa8e8e8e8e8e8e8e8d6118fb565b6000888152600960209081526040808320849055838352600a8252808320805460ff19166001179055600590915280822060039081015486845291909220909101549192506126489161435e565b6000828152600560205260408082206003908101939093558482528082209092018190559051889183917f32e7d5617fb1be6bd0e7c3974d438d4514c4cf349e9330691d8abf6f6fd431219190a39d9c50505050505050505050505050565b60008681526005602081905260408220908101546006909101546126d091908690869086612d9f565b905060008180156126f85750600084815260076020526040902054640100000000900460ff16155b156127905760008481526007602052604090205463ffffffff4281169116106127895760405162461bcd60e51b815260206004820152603760248201527f596f75206d757374207761697420666f72207468652072657665616c2064656160448201527f646c696e65206265666f72652066696e616c697a696e670000000000000000006064820152608401610920565b50846127b3565b60008881526005602052604090206004015487146127ae57856127b0565b825b90505b6112a8888883612c4b565b60006127c98261305b565b6000888152600360205260409020546127f45760405162461bcd60e51b8152600401610920906141a9565b600088858960405160200161280b93929190613fb3565b6040516020818303038152906040528051906020012090506000818888600030338a6040516020016128439796959493929190613f03565b60405160208183030381529060405280519060200120905081336001600160a01b0316827ffe2dac156a3890636ce13f65f4fdf41dcaee11526e4a5374531572d92194796c8d8d8d8d8d8d426040516128a297969594939291906142bb565b60405180910390a46128ba81838a8a8a60008a6132fc565b9998505050505050505050565b60008181526005602052604090206001015481906001600160a01b031633146129025760405162461bcd60e51b815260040161092090614128565b600082815260056020526040902060020154829060ff166129355760405162461bcd60e51b8152600401610920906140ad565b600083815260056020526040902060028101805460ff191690556001015461296a90600160c01b900463ffffffff1642614376565b600084815260056020526040808220600101805463ffffffff94909416600160e01b026001600160e01b0390941693909317909255905184917f71bf7c2b9df0b8818e7eb6746a5bf69699ebbab041f3795f9ed58e469afa9a3a91a2505050565b60008381526005602052604090206001015483906001600160a01b03163314612a065760405162461bcd60e51b815260040161092090614128565b6000848152600560205260409020600101548490600160c01b900463ffffffff16612a435760405162461bcd60e51b815260040161092090614080565b60008181526005602052604090206002015460ff1615612a755760405162461bcd60e51b815260040161092090614273565b600081815260056020526040902060010154600160e01b900463ffffffff16801580612aac57504263ffffffff168163ffffffff16115b612ac85760405162461bcd60e51b815260040161092090614036565b600082815260056020526040902060010154600160a01b900463ffffffff16801580612b0057504263ffffffff168163ffffffff1611155b612b1c5760405162461bcd60e51b8152600401610920906140f1565b86858015612b5557600082815260056020526040902060060154811015612b555760405162461bcd60e51b81526004016109209061420d565b600089815260056020526040902060010154600160e01b900463ffffffff16612bf15760405162461bcd60e51b815260206004820152604260248201527f5175657374696f6e206d75737420616c7265616479206861766520616e20616e60448201527f73776572207768656e206172626974726174696f6e2069732072657175657374606482015261195960f21b608482015260a401610920565b600089815260056020526040808220600201805460ff19166001179055516001600160a01b038a16918b917f75d7939999bc902187c4aed400872883e445145f1983539166f783fa040b47629190a3505050505050505050565b60008381526005602052604090206001015483906001600160a01b03163314612c865760405162461bcd60e51b815260040161092090614128565b600084815260056020526040902060020154849060ff16612cb95760405162461bcd60e51b8152600401610920906140ad565b6001600160a01b038316612d0f5760405162461bcd60e51b815260206004820152601960248201527f616e737765726572206d7573742062652070726f7669646564000000000000006044820152606401610920565b604051849086907f18d760beffe3717270cd90d9d920ec1a48c194e9ad7bba23eb1c92d3eb974f9790600090a36000858152600560205260408120600201805460ff19169055612d6590869086908690806131bb565b60008581526005602052604090206004810185905560010180546001600160e01b0316600160e01b4263ffffffff16021790555050505050565b6000848484846001604051602001612dbb959493929190613f5c565b60405160208183030381529060405280519060200120861415612de057506001612e86565b848484846000604051602001612dfa959493929190613f5c565b60405160208183030381529060405280519060200120861415612e1f57506000612e86565b60405162461bcd60e51b815260206004820152603660248201527f486973746f727920696e7075742070726f766964656420646964206e6f74206d6044820152750c2e8c6d040e8d0ca40caf0e0cac6e8cac840d0c2e6d60531b6064820152608401610920565b95945050505050565b6000808215612f08576000848152600760205260409020548490640100000000900460ff16612ee0576000908152600760205260408120805464ffffffffff19168155600101555086905085612fcc565b6000908152600760205260408120600181018054825464ffffffffff19169092559190915593505b88841415612fc6576001600160a01b038716612f825785965060011960001b8914158015612f46575060008a81526005602052604090206003015415155b15612f7d5760008a815260056020526040902060030154612f6a908b908990612fd9565b60008a8152600560205260408120600301555b612fc6565b866001600160a01b0316866001600160a01b031614612fc657600085891015612fab5788612fad565b855b9050612fbe8b89610baf848d6143f4565b869750809850505b50869050855b9850989650505050505050565b6001600160a01b038216600090815260016020526040902054612ffd90829061435e565b6001600160a01b0383166000818152600160205260409081902092909255905184907f9c121aff33b50c1a53fef034ebec5f83da2d5a5187048f9c76c397ba27c1a1a69061304e9085815260200190565b60405180910390a3505050565b806130635750565b3360009081526001602052604090205480156130bb5781811061309e5761308a82826143f4565b336000908152600160205260409020555050565b6130a881836143f4565b3360009081526001602052604081205591505b6000546040516323b872dd60e01b8152336004820152306024820152604481018490526001600160a01b03909116906323b872dd90606401602060405180830381600087803b15801561310d57600080fd5b505af1158015613121573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190613145919061395d565b6131b75760405162461bcd60e51b815260206004820152603960248201527f5472616e73666572206f6620746f6b656e73206661696c65642c20696e73756660448201527f66696369656e7420617070726f7665642062616c616e63653f000000000000006064820152608401610920565b5050565b60008581526005602081815260408084209092015491516131e59291889187918991889101613f5c565b604051602081830303815290604052805190602001209050600083111561321b5760008681526005602052604090206006018390555b600086815260056020818152604092839020909101839055815187815290810183905290810184905242606082015282151560808201526001600160a01b0385169087907fe47ca4ebbbc2990134d1168821f38c5e177f3d5ee564bffeadeaa351905e62219060a00160405180910390a3505050505050565b600082815260056020526040902060048101829055600101546132c490600160c01b900463ffffffff1642614376565b600092835260056020526040909220600101805463ffffffff93909316600160e01b026001600160e01b039093169290921790915550565b6000878152600560205260409020600101548790600160c01b900463ffffffff161561336a5760405162461bcd60e51b815260206004820152601760248201527f7175657374696f6e206d757374206e6f742065786973740000000000000000006044820152606401610920565b60008563ffffffff16116133c05760405162461bcd60e51b815260206004820152601860248201527f74696d656f7574206d75737420626520706f73697469766500000000000000006044820152606401610920565b6301e133808563ffffffff16106134245760405162461bcd60e51b815260206004820152602260248201527f74696d656f7574206d757374206265206c657373207468616e20333635206461604482015261797360f01b6064820152608401610920565b816001600160a01b038716158015906134465750336001600160a01b03881614155b15613510576001600160a01b038716600090815260086020526040902054808210156134c45760405162461bcd60e51b815260206004820152602760248201527f546f6b656e732070726f7669646564206d75737420636f766572207175657374604482015266696f6e2066656560c81b6064820152608401610920565b6134ce81836143f4565b6001600160a01b0389166000908152600160205260409020549092506134f590829061435e565b6001600160a01b038916600090815260016020526040902055505b6000898152600560205260409020888155600101805463ffffffff888116600160c01b0263ffffffff60c01b19918916600160a01b026001600160c01b03199093166001600160a01b038c1617929092171617905580156135be57600089815260056020908152604091829020600301839055815183815290810183905233918b917f54d68405b79f2aa4fd4e8db7b67844ad254cf8f208aac476c2894134a9deab66910160405180910390a35b831561361557600089815260056020526040908190206007018590555189907f9641ca9d53af3bead658ffcc6c7d8c35e7dae9938367bd8eb45bee35d5c625049061360c9087815260200190565b60405180910390a25b505050505050505050565b60008181526007602052604090205463ffffffff161561368c5760405162461bcd60e51b815260206004820152602160248201527f636f6d6d69746d656e74206d757374206e6f7420616c726561647920657869736044820152601d60fa1b6064820152608401610920565b6000828152600560205260408120600101546136b790600890600160c01b900463ffffffff166143b2565b90506136c38142614376565b600092835260076020526040909220805463ffffffff191663ffffffff909316929092179091555050565b600082601f8301126136ff57600080fd5b8135602061371461370f8361433b565b61430b565b80838252828201915082860187848660051b890101111561373457600080fd5b60005b8581101561375c57813561374a816144ae565b84529284019290840190600101613737565b5090979650505050505050565b600082601f83011261377a57600080fd5b8135602061378a61370f8361433b565b80838252828201915082860187848660051b89010111156137aa57600080fd5b60005b8581101561375c578135845292840192908401906001016137ad565b600082601f8301126137da57600080fd5b81356001600160401b038111156137f3576137f3614498565b613806601f8201601f191660200161430b565b81815284602083860101111561381b57600080fd5b816020850160208301376000918101602001919091529392505050565b803563ffffffff81168114611bf357600080fd5b60006020828403121561385e57600080fd5b8135611b55816144ae565b60008060008060008060c0878903121561388257600080fd5b86356001600160401b038082111561389957600080fd5b6138a58a838b01613769565b975060208901359150808211156138bb57600080fd5b6138c78a838b01613769565b965060408901359150808211156138dd57600080fd5b6138e98a838b01613769565b955060608901359150808211156138ff57600080fd5b61390b8a838b016136ee565b9450608089013591508082111561392157600080fd5b61392d8a838b01613769565b935060a089013591508082111561394357600080fd5b5061395089828a01613769565b9150509295509295509295565b60006020828403121561396f57600080fd5b81518015158114611b5557600080fd5b60006020828403121561399157600080fd5b5035919050565b6000806000606084860312156139ad57600080fd5b8335925060208401356139bf816144ae565b929592945050506040919091013590565b600080600080600060a086880312156139e857600080fd5b8535945060208601356001600160401b0380821115613a0657600080fd5b613a1289838a01613769565b95506040880135915080821115613a2857600080fd5b613a3489838a016136ee565b94506060880135915080821115613a4a57600080fd5b613a5689838a01613769565b93506080880135915080821115613a6c57600080fd5b50613a7988828901613769565b9150509295509295909350565b600080600060608486031215613a9b57600080fd5b83359250602084013591506040840135613ab4816144ae565b809150509250925092565b60008060008060008060c08789031215613ad857600080fd5b86359550602087013594506040870135613af1816144ae565b9350606087013592506080870135915060a0870135613b0f816144ae565b809150509295509295509295565b600080600080600060a08688031215613b3557600080fd5b85359450602086013593506040860135613b4e816144ae565b9250613b5c60608701613838565b949793965091946080013592915050565b600080600080600060a08688031215613b8557600080fd5b8535945060208601359350604086013592506060860135613b5c816144ae565b60008060008060808587031215613bbb57600080fd5b5050823594602084013594506040840135936060013592509050565b60008060408385031215613bea57600080fd5b50508035926020909101359150565b600060208284031215613c0b57600080fd5b81356001600160401b03811115613c2157600080fd5b613c2d848285016137c9565b949350505050565b60008060008060008060c08789031215613c4e57600080fd5b86356001600160401b0380821115613c6557600080fd5b613c718a838b016137c9565b97506020890135915080821115613c8757600080fd5b50613c9489828a016137c9565b9550506040870135613ca5816144ae565b9350613cb360608801613838565b9250613cc160808801613838565b915060a087013590509295509295509295565b60008060008060008060c08789031215613ced57600080fd5b8635955060208701356001600160401b03811115613d0a57600080fd5b613c9489828a016137c9565b600080600080600080600060e0888a031215613d3157600080fd5b8735965060208801356001600160401b03811115613d4e57600080fd5b613d5a8a828b016137c9565b9650506040880135613d6b816144ae565b9450613d7960608901613838565b9350613d8760808901613838565b925060a0880135915060c0880135905092959891949750929550565b60008060008060008060008060006101208a8c031215613dc257600080fd5b8935985060208a01356001600160401b03811115613ddf57600080fd5b613deb8c828d016137c9565b98505060408a0135613dfc816144ae565b9650613e0a60608b01613838565b9550613e1860808b01613838565b989b979a50959894979660a0860135965060c08601359560e0810135955061010001359350915050565b600080600080600080600080610100898b031215613e5f57600080fd5b8835975060208901356001600160401b03811115613e7c57600080fd5b613e888b828c016137c9565b9750506040890135613e99816144ae565b9550613ea760608a01613838565b9450613eb560808a01613838565b979a969950949793969560a0850135955060c08501359460e001359350915050565b60008151808452613eef81602086016020860161440b565b601f01601f19169290920160200192915050565b968752606095861b6bffffffffffffffffffffffff19908116602089015260e09590951b6001600160e01b0319166034880152603887019390935290841b8316605886015290921b16606c830152608082015260a00190565b94855260208501939093526040840191909152606090811b6bffffffffffffffffffffffff191690830152151560f81b607482015260750190565b60008251613fa981846020870161440b565b9190910192915050565b83815263ffffffff60e01b8360e01b16602082015260008251613fdd81602485016020870161440b565b91909101602401949350505050565b602081526000611b556020830184613ed7565b6020808252601c908201527f626f6e64206d7573742065786365656420746865206d696e696d756d00000000604082015260600190565b6020808252602a908201527f66696e616c697a6174696f6e20646561646c696e65206d757374206e6f742068604082015269185d99481c185cdcd95960b21b606082015260800190565b6020808252601390820152721c5d595cdd1a5bdb881b5d5cdd08195e1a5cdd606a1b604082015260600190565b60208082526024908201527f7175657374696f6e206d7573742062652070656e64696e6720617262697472616040820152633a34b7b760e11b606082015260800190565b6020808252601d908201527f6f70656e696e672064617465206d757374206861766520706173736564000000604082015260600190565b6020808252601d908201527f6d73672e73656e646572206d7573742062652061726269747261746f72000000604082015260600190565b6020808252602a908201527f626f6e64206d75737420626520646f75626c65206174206c65617374207072656040820152691d9a5bdd5cc8189bdb9960b21b606082015260800190565b6020808252601390820152721d195b5c1b185d19481b5d5cdd08195e1a5cdd606a1b604082015260600190565b6020808252601a908201527f7175657374696f6e206d7573742062652066696e616c697a6564000000000000604082015260600190565b6020808252601d908201527f626f6e64206d75737420657863656564206d61785f70726576696f7573000000604082015260600190565b602080825260159082015274626f6e64206d75737420626520706f73697469766560581b604082015260600190565b60208082526028908201527f7175657374696f6e206d757374206e6f742062652070656e64696e672061726260408201526734ba3930ba34b7b760c11b606082015260800190565b87815260e0602082015260006142d460e0830189613ed7565b6001600160a01b039790971660408301525063ffffffff948516606082015292909316608083015260a082015260c0015292915050565b604051601f8201601f191681016001600160401b038111828210171561433357614333614498565b604052919050565b60006001600160401b0382111561435457614354614498565b5060051b60200190565b6000821982111561437157614371614456565b500190565b600063ffffffff80831681851680830382111561439557614395614456565b01949350505050565b6000826143ad576143ad61446c565b500490565b600063ffffffff808416806143c9576143c961446c565b92169190910492915050565b60008160001904831182151516156143ef576143ef614456565b500290565b60008282101561440657614406614456565b500390565b60005b8381101561442657818101518382015260200161440e565b83811115614435576000848401525b50505050565b600060001982141561444f5761444f614456565b5060010190565b634e487b7160e01b600052601160045260246000fd5b634e487b7160e01b600052601260045260246000fd5b634e487b7160e01b600052603260045260246000fd5b634e487b7160e01b600052604160045260246000fd5b6001600160a01b03811681146144c357600080fd5b5056fea2646970667358221220c83c901639503cf3347ef83828c2a6188fb8bddd2323c41b9b7d450f1aeabe8f64736f6c63430008060033

Verified Source Code Partial Match

Compiler: v0.8.6+commit.11564f7e EVM: berlin Optimization: Yes (200 runs)
RealityETH_ERC20_v3_0.sol 1096 lines
// SPDX-License-Identifier: GPL-3.0-only

pragma solidity ^0.8.6;


/**
 * @title ERC20 interface
 * @dev see https://github.com/ethereum/EIPs/issues/20
 */
interface IERC20 {
    function totalSupply() external view returns (uint256);

    function balanceOf(address who) external view returns (uint256);

    function allowance(address owner, address spender) external view returns (uint256);

    function transfer(address to, uint256 value) external returns (bool);

    function approve(address spender, uint256 value) external returns (bool);

    function transferFrom(address from, address to, uint256 value) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint256 value);

    event Approval(address indexed owner, address indexed spender, uint256 value);
}


contract BalanceHolder {

    IERC20 public token;

    mapping(address => uint256) public balanceOf;

    event LogWithdraw(
        address indexed user,
        uint256 amount
    );

    function withdraw() 
    public {
        uint256 bal = balanceOf[msg.sender];
        balanceOf[msg.sender] = 0;
        require(token.transfer(msg.sender, bal));
        emit LogWithdraw(msg.sender, bal);
    }

}


contract RealityETH_ERC20_v3_0 is BalanceHolder {

    address constant NULL_ADDRESS = address(0);

    // History hash when no history is created, or history has been cleared
    bytes32 constant NULL_HASH = bytes32(0);

    // An unitinalized finalize_ts for a question will indicate an unanswered question.
    uint32 constant UNANSWERED = 0;

    // An unanswered reveal_ts for a commitment will indicate that it does not exist.
    uint256 constant COMMITMENT_NON_EXISTENT = 0;

    // Commit->reveal timeout is 1/8 of the question timeout (rounded down).
    uint32 constant COMMITMENT_TIMEOUT_RATIO = 8;

    // Proportion withheld when you claim an earlier bond.
    uint256 constant BOND_CLAIM_FEE_PROPORTION = 40; // One 40th ie 2.5%

    // Special value representing a question that was answered too soon.
    // bytes32(-2). By convention we use bytes32(-1) for "invalid", although the contract does not handle this.
    bytes32 constant UNRESOLVED_ANSWER = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe;

    event LogSetQuestionFee(
        address arbitrator,
        uint256 amount
    );

    event LogNewTemplate(
        uint256 indexed template_id,
        address indexed user, 
        string question_text
    );

    event LogNewQuestion(
        bytes32 indexed question_id,
        address indexed user, 
        uint256 template_id,
        string question,
        bytes32 indexed content_hash,
        address arbitrator, 
        uint32 timeout,
        uint32 opening_ts,
        uint256 nonce,
        uint256 created
    );

    event LogMinimumBond(
        bytes32 indexed question_id,
        uint256 min_bond
    );

    event LogFundAnswerBounty(
        bytes32 indexed question_id,
        uint256 bounty_added,
        uint256 bounty,
        address indexed user 
    );

    event LogNewAnswer(
        bytes32 answer,
        bytes32 indexed question_id,
        bytes32 history_hash,
        address indexed user,
        uint256 bond,
        uint256 ts,
        bool is_commitment
    );

    event LogAnswerReveal(
        bytes32 indexed question_id, 
        address indexed user, 
        bytes32 indexed answer_hash, 
        bytes32 answer, 
        uint256 nonce, 
        uint256 bond
    );

    event LogNotifyOfArbitrationRequest(
        bytes32 indexed question_id,
        address indexed user 
    );

    event LogCancelArbitration(
        bytes32 indexed question_id
    );

    event LogFinalize(
        bytes32 indexed question_id,
        bytes32 indexed answer
    );

    event LogClaim(
        bytes32 indexed question_id,
        address indexed user,
        uint256 amount
    );

    event LogReopenQuestion(
        bytes32 indexed question_id,
        bytes32 indexed reopened_question_id
    );

    struct Question {
        bytes32 content_hash;
        address arbitrator;
        uint32 opening_ts;
        uint32 timeout;
        uint32 finalize_ts;
        bool is_pending_arbitration;
        uint256 bounty;
        bytes32 best_answer;
        bytes32 history_hash;
        uint256 bond;
        uint256 min_bond;
    }

    // Stored in a mapping indexed by commitment_id, a hash of commitment hash, question, bond. 
    struct Commitment {
        uint32 reveal_ts;
        bool is_revealed;
        bytes32 revealed_answer;
    }

    // Only used when claiming more bonds than fits into a transaction
    // Stored in a mapping indexed by question_id.
    struct Claim {
        address payee;
        uint256 last_bond;
        uint256 queued_funds;
    }

    uint256 nextTemplateID = 0;
    mapping(uint256 => uint256) public templates;
    mapping(uint256 => bytes32) public template_hashes;
    mapping(bytes32 => Question) public questions;
    mapping(bytes32 => Claim) public question_claims;
    mapping(bytes32 => Commitment) public commitments;
    mapping(address => uint256) public arbitrator_question_fees; 
    mapping(bytes32 => bytes32) public reopened_questions;
    mapping(bytes32 => bool) public reopener_questions;


    modifier onlyArbitrator(bytes32 question_id) {
        require(msg.sender == questions[question_id].arbitrator, "msg.sender must be arbitrator");
        _;
    }

    modifier stateAny() {
        _;
    }

    modifier stateNotCreated(bytes32 question_id) {
        require(questions[question_id].timeout == 0, "question must not exist");
        _;
    }

    modifier stateOpen(bytes32 question_id) {
        require(questions[question_id].timeout > 0, "question must exist");
        require(!questions[question_id].is_pending_arbitration, "question must not be pending arbitration");
        uint32 finalize_ts = questions[question_id].finalize_ts;
        require(finalize_ts == UNANSWERED || finalize_ts > uint32(block.timestamp), "finalization deadline must not have passed");
        uint32 opening_ts = questions[question_id].opening_ts;
        require(opening_ts == 0 || opening_ts <= uint32(block.timestamp), "opening date must have passed"); 
        _;
    }

    modifier statePendingArbitration(bytes32 question_id) {
        require(questions[question_id].is_pending_arbitration, "question must be pending arbitration");
        _;
    }

    modifier stateOpenOrPendingArbitration(bytes32 question_id) {
        require(questions[question_id].timeout > 0, "question must exist");
        uint32 finalize_ts = questions[question_id].finalize_ts;
        require(finalize_ts == UNANSWERED || finalize_ts > uint32(block.timestamp), "finalization dealine must not have passed");
        uint32 opening_ts = questions[question_id].opening_ts;
        require(opening_ts == 0 || opening_ts <= uint32(block.timestamp), "opening date must have passed"); 
        _;
    }

    modifier stateFinalized(bytes32 question_id) {
        require(isFinalized(question_id), "question must be finalized");
        _;
    }

    modifier bondMustDoubleAndMatchMinimum(bytes32 question_id, uint256 tokens) {
        require(tokens > 0, "bond must be positive"); 
        uint256 current_bond = questions[question_id].bond;
        if (current_bond == 0) {
            require(tokens >= (questions[question_id].min_bond), "bond must exceed the minimum");
        } else {
            require(tokens >= (current_bond * 2), "bond must be double at least previous bond");
        }
        _;
    }

    modifier previousBondMustNotBeatMaxPrevious(bytes32 question_id, uint256 max_previous) {
        if (max_previous > 0) {
            require(questions[question_id].bond <= max_previous, "bond must exceed max_previous");
        }
        _;
    }

    /// @notice Constructor, sets up some initial templates
    /// @dev Creates some generalized templates for different question types used in the DApp.
    constructor() {
        createTemplate('{"title": "%s", "type": "bool", "category": "%s", "lang": "%s"}');
        createTemplate('{"title": "%s", "type": "uint", "decimals": 18, "category": "%s", "lang": "%s"}');
        createTemplate('{"title": "%s", "type": "single-select", "outcomes": [%s], "category": "%s", "lang": "%s"}');
        createTemplate('{"title": "%s", "type": "multiple-select", "outcomes": [%s], "category": "%s", "lang": "%s"}');
        createTemplate('{"title": "%s", "type": "datetime", "category": "%s", "lang": "%s"}');
    }

    /// @notice Set the address of the ERC20 token that will be used for bonds.
    /// @dev Should not be used with ERC20-like token contracts that implement callbacks like ERC777 that could cause re-entrancy issues
    /// @param _token The ERC20 token that will be used for bonds.
    function setToken(IERC20 _token) 
    public
    {
        require(token == IERC20(address(0x0)), "Token can only be initialized once");
        token = _token;
    }

    /// @notice Function for arbitrator to set an optional per-question fee. 
    /// @dev The per-question fee, charged when a question is asked, is intended as an anti-spam measure.
    /// @param fee The fee to be charged by the arbitrator when a question is asked
    function setQuestionFee(uint256 fee) 
        stateAny() 
    external {
        arbitrator_question_fees[msg.sender] = fee;
        emit LogSetQuestionFee(msg.sender, fee);
    }

    /// @notice Create a reusable template, which should be a JSON document.
    /// Placeholders should use gettext() syntax, eg %s.
    /// @dev Template data is only stored in the event logs, but its block number is kept in contract storage.
    /// @param content The template content
    /// @return The ID of the newly-created template, which is created sequentially.
    function createTemplate(string memory content) 
        stateAny()
    public returns (uint256) {
        uint256 id = nextTemplateID;
        templates[id] = block.number;
        template_hashes[id] = keccak256(abi.encodePacked(content));
        emit LogNewTemplate(id, msg.sender, content);
        nextTemplateID = id + 1;
        return id;
    }

    /// @notice Create a new reusable template and use it to ask a question
    /// @dev Template data is only stored in the event logs, but its block number is kept in contract storage.
    /// @param content The template content
    /// @param question A string containing the parameters that will be passed into the template to make the question
    /// @param arbitrator The arbitration contract that will have the final word on the answer if there is a dispute
    /// @param timeout How long the contract should wait after the answer is changed before finalizing on that answer
    /// @param opening_ts If set, the earliest time it should be possible to answer the question.
    /// @param nonce A user-specified nonce used in the question ID. Change it to repeat a question.
    /// @return The ID of the newly-created template, which is created sequentially.
    function createTemplateAndAskQuestion(
        string memory content, 
        string memory question, address arbitrator, uint32 timeout, uint32 opening_ts, uint256 nonce 
    ) 
        // stateNotCreated is enforced by the internal _askQuestion
    public returns (bytes32) {
        uint256 template_id = createTemplate(content);
        return askQuestion(template_id, question, arbitrator, timeout, opening_ts, nonce);
    }

    /// @notice Ask a new question without a bounty and return the ID
    /// @dev Template data is only stored in the event logs, but its block number is kept in contract storage.
    /// @dev Calling without the token param will only work if there is no arbitrator-set question fee.
    /// @dev This has the same function signature as askQuestion() in the non-ERC20 version, which is optionally payable.
    /// @param template_id The ID number of the template the question will use
    /// @param question A string containing the parameters that will be passed into the template to make the question
    /// @param arbitrator The arbitration contract that will have the final word on the answer if there is a dispute
    /// @param timeout How long the contract should wait after the answer is changed before finalizing on that answer
    /// @param opening_ts If set, the earliest time it should be possible to answer the question.
    /// @param nonce A user-specified nonce used in the question ID. Change it to repeat a question.
    /// @return The ID of the newly-created question, created deterministically.
    function askQuestion(uint256 template_id, string memory question, address arbitrator, uint32 timeout, uint32 opening_ts, uint256 nonce) 
        // stateNotCreated is enforced by the internal _askQuestion
    public returns (bytes32) {

        require(templates[template_id] > 0, "template must exist");

        bytes32 content_hash = keccak256(abi.encodePacked(template_id, opening_ts, question));
        bytes32 question_id = keccak256(abi.encodePacked(content_hash, arbitrator, timeout, uint256(0), address(this), msg.sender, nonce));

        // We emit this event here because _askQuestion doesn't need to know the unhashed question. Other events are emitted by _askQuestion.
        emit LogNewQuestion(question_id, msg.sender, template_id, question, content_hash, arbitrator, timeout, opening_ts, nonce, block.timestamp);
        _askQuestion(question_id, content_hash, arbitrator, timeout, opening_ts, 0, 0);

        return question_id;
    }

    /// @notice Ask a new question with a bounty and return the ID
    /// @dev Template data is only stored in the event logs, but its block number is kept in contract storage.
    /// @param template_id The ID number of the template the question will use
    /// @param question A string containing the parameters that will be passed into the template to make the question
    /// @param arbitrator The arbitration contract that will have the final word on the answer if there is a dispute
    /// @param timeout How long the contract should wait after the answer is changed before finalizing on that answer
    /// @param opening_ts If set, the earliest time it should be possible to answer the question.
    /// @param nonce A user-specified nonce used in the question ID. Change it to repeat a question.
    /// @param tokens The combined initial question bounty and question fee
    /// @return The ID of the newly-created question, created deterministically.
    function askQuestionERC20(uint256 template_id, string memory question, address arbitrator, uint32 timeout, uint32 opening_ts, uint256 nonce, uint256 tokens) 
        // stateNotCreated is enforced by the internal _askQuestion
    public returns (bytes32) {

        _deductTokensOrRevert(tokens);

        require(templates[template_id] > 0, "template must exist");

        bytes32 content_hash = keccak256(abi.encodePacked(template_id, opening_ts, question));
        bytes32 question_id = keccak256(abi.encodePacked(content_hash, arbitrator, timeout, uint256(0), address(this), msg.sender, nonce));

        // We emit this event here because _askQuestion doesn't need to know the unhashed question. Other events are emitted by _askQuestion.
        emit LogNewQuestion(question_id, msg.sender, template_id, question, content_hash, arbitrator, timeout, opening_ts, nonce, block.timestamp);
        _askQuestion(question_id, content_hash, arbitrator, timeout, opening_ts, 0, tokens);

        return question_id;
    }

    /// @notice Ask a new question and return the ID
    /// @dev Template data is only stored in the event logs, but its block number is kept in contract storage.
    /// @param template_id The ID number of the template the question will use
    /// @param question A string containing the parameters that will be passed into the template to make the question
    /// @param arbitrator The arbitration contract that will have the final word on the answer if there is a dispute
    /// @param timeout How long the contract should wait after the answer is changed before finalizing on that answer
    /// @param opening_ts If set, the earliest time it should be possible to answer the question.
    /// @param nonce A user-specified nonce used in the question ID. Change it to repeat a question.
    /// @param min_bond The minimum bond that may be used for an answer.
    /// @param tokens Number of tokens sent
    /// @return The ID of the newly-created question, created deterministically.
    function askQuestionWithMinBondERC20(uint256 template_id, string memory question, address arbitrator, uint32 timeout, uint32 opening_ts, uint256 nonce, uint256 min_bond, uint256 tokens) 
        // stateNotCreated is enforced by the internal _askQuestion
    public returns (bytes32) {

        _deductTokensOrRevert(tokens);

        require(templates[template_id] > 0, "template must exist");

        bytes32 content_hash = keccak256(abi.encodePacked(template_id, opening_ts, question));
        bytes32 question_id = keccak256(abi.encodePacked(content_hash, arbitrator, timeout, min_bond, address(this), msg.sender, nonce));

        // We emit this event here because _askQuestion doesn't need to know the unhashed question.
        // Other events are emitted by _askQuestion.
        emit LogNewQuestion(question_id, msg.sender, template_id, question, content_hash, arbitrator, timeout, opening_ts, nonce, block.timestamp);
        _askQuestion(question_id, content_hash, arbitrator, timeout, opening_ts, min_bond, tokens);

        return question_id;
    }

    function _deductTokensOrRevert(uint256 tokens) 
    internal {
 
        if (tokens == 0) {
            return;
        }

        uint256 bal = balanceOf[msg.sender];

        // Deduct any tokens you have in your internal balance first
        if (bal > 0) {
            if (bal >= tokens) {
                balanceOf[msg.sender] = bal - tokens;
                return;
            } else {
                tokens = tokens - bal;
                balanceOf[msg.sender] = 0;
            }
        }
        // Now we need to charge the rest from 
        require(token.transferFrom(msg.sender, address(this), tokens), "Transfer of tokens failed, insufficient approved balance?");
        return;

    }

    function _askQuestion(bytes32 question_id, bytes32 content_hash, address arbitrator, uint32 timeout, uint32 opening_ts, uint256 min_bond, uint256 tokens) 
        stateNotCreated(question_id)
    internal {

        // A timeout of 0 makes no sense, and we will use this to check existence
        require(timeout > 0, "timeout must be positive"); 
        require(timeout < 365 days, "timeout must be less than 365 days"); 

        uint256 bounty = tokens;

        // The arbitrator can set a fee for asking a question. 
        // This is intended as an anti-spam defence.
        // The fee is waived if the arbitrator is asking the question.
        // This allows them to set an impossibly high fee and make users proxy the question through them.
        // This would allow more sophisticated pricing, question whitelisting etc.
        if (arbitrator != NULL_ADDRESS && msg.sender != arbitrator) {
            uint256 question_fee = arbitrator_question_fees[arbitrator];
            require(bounty >= question_fee, "Tokens provided must cover question fee"); 
            bounty = bounty - question_fee;
            balanceOf[arbitrator] = balanceOf[arbitrator] + question_fee;
        }

        questions[question_id].content_hash = content_hash;
        questions[question_id].arbitrator = arbitrator;
        questions[question_id].opening_ts = opening_ts;
        questions[question_id].timeout = timeout;

        if (bounty > 0) {
            questions[question_id].bounty = bounty;
            emit LogFundAnswerBounty(question_id, bounty, bounty, msg.sender);
        }

        if (min_bond > 0) {
            questions[question_id].min_bond = min_bond;
            emit LogMinimumBond(question_id, min_bond);
        }

    }

    /// @notice Add funds to the bounty for a question
    /// @dev Add bounty funds after the initial question creation. Can be done any time until the question is finalized.
    /// @param question_id The ID of the question you wish to fund
    /// @param tokens The number of tokens to fund
    function fundAnswerBountyERC20(bytes32 question_id, uint256 tokens) 
        stateOpen(question_id)
    external {
        _deductTokensOrRevert(tokens);
        questions[question_id].bounty = questions[question_id].bounty + tokens;
        emit LogFundAnswerBounty(question_id, tokens, questions[question_id].bounty, msg.sender);
    }

    /// @notice Submit an answer for a question.
    /// @dev Adds the answer to the history and updates the current "best" answer.
    /// May be subject to front-running attacks; Substitute submitAnswerCommitment()->submitAnswerReveal() to prevent them.
    /// @param question_id The ID of the question
    /// @param answer The answer, encoded into bytes32
    /// @param max_previous If specified, reverts if a bond higher than this was submitted after you sent your transaction.
    /// @param tokens The amount of tokens to submit
    function submitAnswerERC20(bytes32 question_id, bytes32 answer, uint256 max_previous, uint256 tokens) 
        stateOpen(question_id)
        bondMustDoubleAndMatchMinimum(question_id, tokens)
        previousBondMustNotBeatMaxPrevious(question_id, max_previous)
    external {
        _deductTokensOrRevert(tokens);
        _addAnswerToHistory(question_id, answer, msg.sender, tokens, false);
        _updateCurrentAnswer(question_id, answer);
    }

    /// @notice Submit an answer for a question, crediting it to the specified account.
    /// @dev Adds the answer to the history and updates the current "best" answer.
    /// May be subject to front-running attacks; Substitute submitAnswerCommitment()->submitAnswerReveal() to prevent them.
    /// @param question_id The ID of the question
    /// @param answer The answer, encoded into bytes32
    /// @param max_previous If specified, reverts if a bond higher than this was submitted after you sent your transaction.
    /// @param answerer The account to which the answer should be credited
    /// @param tokens Number of tokens sent
    function submitAnswerForERC20(bytes32 question_id, bytes32 answer, uint256 max_previous, address answerer, uint256 tokens)
        stateOpen(question_id)
        bondMustDoubleAndMatchMinimum(question_id, tokens)
        previousBondMustNotBeatMaxPrevious(question_id, max_previous)
    external {
        _deductTokensOrRevert(tokens);
        require(answerer != NULL_ADDRESS, "answerer must be non-zero");
        _addAnswerToHistory(question_id, answer, answerer, tokens, false);
        _updateCurrentAnswer(question_id, answer);
    }

    // @notice Verify and store a commitment, including an appropriate timeout
    // @param question_id The ID of the question to store
    // @param commitment The ID of the commitment
    function _storeCommitment(bytes32 question_id, bytes32 commitment_id) 
    internal
    {
        require(commitments[commitment_id].reveal_ts == COMMITMENT_NON_EXISTENT, "commitment must not already exist");

        uint32 commitment_timeout = questions[question_id].timeout / COMMITMENT_TIMEOUT_RATIO;
        commitments[commitment_id].reveal_ts = uint32(block.timestamp) + commitment_timeout;
    }

    /// @notice Submit the hash of an answer, laying your claim to that answer if you reveal it in a subsequent transaction.
    /// @dev Creates a hash, commitment_id, uniquely identifying this answer, to this question, with this bond.
    /// The commitment_id is stored in the answer history where the answer would normally go.
    /// Does not update the current best answer - this is left to the later submitAnswerReveal() transaction.
    /// @param question_id The ID of the question
    /// @param answer_hash The hash of your answer, plus a nonce that you will later reveal
    /// @param max_previous If specified, reverts if a bond higher than this was submitted after you sent your transaction.
    /// @param _answerer If specified, the address to be given as the question answerer. Defaults to the sender.
    /// @param tokens Number of tokens sent
    /// @dev Specifying the answerer is useful if you want to delegate the commit-and-reveal to a third-party.
    function submitAnswerCommitmentERC20(bytes32 question_id, bytes32 answer_hash, uint256 max_previous, address _answerer, uint256 tokens) 
        stateOpen(question_id)
        bondMustDoubleAndMatchMinimum(question_id, tokens)
        previousBondMustNotBeatMaxPrevious(question_id, max_previous)
    external {

        _deductTokensOrRevert(tokens);

        bytes32 commitment_id = keccak256(abi.encodePacked(question_id, answer_hash, tokens));
        address answerer = (_answerer == NULL_ADDRESS) ? msg.sender : _answerer;

        _storeCommitment(question_id, commitment_id);
        _addAnswerToHistory(question_id, commitment_id, answerer, tokens, true);

    }

    /// @notice Submit the answer whose hash you sent in a previous submitAnswerCommitment() transaction
    /// @dev Checks the parameters supplied recreate an existing commitment, and stores the revealed answer
    /// Updates the current answer unless someone has since supplied a new answer with a higher bond
    /// msg.sender is intentionally not restricted to the user who originally sent the commitment; 
    /// For example, the user may want to provide the answer+nonce to a third-party service and let them send the tx
    /// NB If we are pending arbitration, it will be up to the arbitrator to wait and see any outstanding reveal is sent
    /// @param question_id The ID of the question
    /// @param answer The answer, encoded as bytes32
    /// @param nonce The nonce that, combined with the answer, recreates the answer_hash you gave in submitAnswerCommitment()
    /// @param bond The bond that you paid in your submitAnswerCommitment() transaction
    function submitAnswerReveal(bytes32 question_id, bytes32 answer, uint256 nonce, uint256 bond) 
        stateOpenOrPendingArbitration(question_id)
    external {

        bytes32 answer_hash = keccak256(abi.encodePacked(answer, nonce));
        bytes32 commitment_id = keccak256(abi.encodePacked(question_id, answer_hash, bond));

        require(!commitments[commitment_id].is_revealed, "commitment must not have been revealed yet");
        require(commitments[commitment_id].reveal_ts > uint32(block.timestamp), "reveal deadline must not have passed");

        commitments[commitment_id].revealed_answer = answer;
        commitments[commitment_id].is_revealed = true;

        if (bond == questions[question_id].bond) {
            _updateCurrentAnswer(question_id, answer);
        }

        emit LogAnswerReveal(question_id, msg.sender, answer_hash, answer, nonce, bond);

    }

    function _addAnswerToHistory(bytes32 question_id, bytes32 answer_or_commitment_id, address answerer, uint256 bond, bool is_commitment) 
    internal 
    {
        bytes32 new_history_hash = keccak256(abi.encodePacked(questions[question_id].history_hash, answer_or_commitment_id, bond, answerer, is_commitment));

        // Update the current bond level, if there's a bond (ie anything except arbitration)
        if (bond > 0) {
            questions[question_id].bond = bond;
        }
        questions[question_id].history_hash = new_history_hash;

        emit LogNewAnswer(answer_or_commitment_id, question_id, new_history_hash, answerer, bond, block.timestamp, is_commitment);
    }

    function _updateCurrentAnswer(bytes32 question_id, bytes32 answer)
    internal {
        questions[question_id].best_answer = answer;
        questions[question_id].finalize_ts = uint32(block.timestamp) + questions[question_id].timeout;
    }

    // Like _updateCurrentAnswer but without advancing the timeout
    function _updateCurrentAnswerByArbitrator(bytes32 question_id, bytes32 answer)
    internal {
        questions[question_id].best_answer = answer;
        questions[question_id].finalize_ts = uint32(block.timestamp);
    }

    /// @notice Notify the contract that the arbitrator has been paid for a question, freezing it pending their decision.
    /// @dev The arbitrator contract is trusted to only call this if they've been paid, and tell us who paid them.
    /// @param question_id The ID of the question
    /// @param requester The account that requested arbitration
    /// @param max_previous If specified, reverts if a bond higher than this was submitted after you sent your transaction.
    function notifyOfArbitrationRequest(bytes32 question_id, address requester, uint256 max_previous) 
        onlyArbitrator(question_id)
        stateOpen(question_id)
        previousBondMustNotBeatMaxPrevious(question_id, max_previous)
    external {
        require(questions[question_id].finalize_ts > UNANSWERED, "Question must already have an answer when arbitration is requested");
        questions[question_id].is_pending_arbitration = true;
        emit LogNotifyOfArbitrationRequest(question_id, requester);
    }

    /// @notice Cancel a previously-requested arbitration and extend the timeout
    /// @dev Useful when doing arbitration across chains that can't be requested atomically
    /// @param question_id The ID of the question
    function cancelArbitration(bytes32 question_id) 
        onlyArbitrator(question_id)
        statePendingArbitration(question_id)
    external {
        questions[question_id].is_pending_arbitration = false;
        questions[question_id].finalize_ts = uint32(block.timestamp) + questions[question_id].timeout;
        emit LogCancelArbitration(question_id);
    }

    /// @notice Submit the answer for a question, for use by the arbitrator.
    /// @dev Doesn't require (or allow) a bond.
    /// If the current final answer is correct, the account should be whoever submitted it.
    /// If the current final answer is wrong, the account should be whoever paid for arbitration.
    /// However, the answerer stipulations are not enforced by the contract.
    /// @param question_id The ID of the question
    /// @param answer The answer, encoded into bytes32
    /// @param answerer The account credited with this answer for the purpose of bond claims
    function submitAnswerByArbitrator(bytes32 question_id, bytes32 answer, address answerer) 
        onlyArbitrator(question_id)
        statePendingArbitration(question_id)
    public {

        require(answerer != NULL_ADDRESS, "answerer must be provided");
        emit LogFinalize(question_id, answer);

        questions[question_id].is_pending_arbitration = false;
        _addAnswerToHistory(question_id, answer, answerer, 0, false);
        _updateCurrentAnswerByArbitrator(question_id, answer);
    }

    /// @notice Submit the answer for a question, for use by the arbitrator, working out the appropriate winner based on the last answer details.
    /// @dev Doesn't require (or allow) a bond.
    /// @param question_id The ID of the question
    /// @param answer The answer, encoded into bytes32
    /// @param payee_if_wrong The account to by credited as winner if the last answer given is wrong, usually the account that paid the arbitrator
    /// @param last_history_hash The history hash before the final one
    /// @param last_answer_or_commitment_id The last answer given, or the commitment ID if it was a commitment.
    /// @param last_answerer The address that supplied the last answer
    function assignWinnerAndSubmitAnswerByArbitrator(bytes32 question_id, bytes32 answer, address payee_if_wrong, bytes32 last_history_hash, bytes32 last_answer_or_commitment_id, address last_answerer) 
    external {
        bool is_commitment = _verifyHistoryInputOrRevert(questions[question_id].history_hash, last_history_hash, last_answer_or_commitment_id, questions[question_id].bond, last_answerer);

        address payee;
        // If the last answer is an unrevealed commit, it's always wrong.
        // For anything else, the last answer was set as the "best answer" in submitAnswer or submitAnswerReveal.
        if (is_commitment && !commitments[last_answer_or_commitment_id].is_revealed) {
            require(commitments[last_answer_or_commitment_id].reveal_ts < uint32(block.timestamp), "You must wait for the reveal deadline before finalizing");
            payee = payee_if_wrong;
        } else {
            payee = (questions[question_id].best_answer == answer) ? last_answerer : payee_if_wrong;
        }
        submitAnswerByArbitrator(question_id, answer, payee);
    }


    /// @notice Report whether the answer to the specified question is finalized
    /// @param question_id The ID of the question
    /// @return Return true if finalized
    function isFinalized(bytes32 question_id) 
    view public returns (bool) {
        uint32 finalize_ts = questions[question_id].finalize_ts;
        return ( !questions[question_id].is_pending_arbitration && (finalize_ts > UNANSWERED) && (finalize_ts <= uint32(block.timestamp)) );
    }

    /// @notice (Deprecated) Return the final answer to the specified question, or revert if there isn't one
    /// @param question_id The ID of the question
    /// @return The answer formatted as a bytes32
    function getFinalAnswer(bytes32 question_id) 
        stateFinalized(question_id)
    external view returns (bytes32) {
        return questions[question_id].best_answer;
    }

    /// @notice Return the final answer to the specified question, or revert if there isn't one
    /// @param question_id The ID of the question
    /// @return The answer formatted as a bytes32
    function resultFor(bytes32 question_id) 
        stateFinalized(question_id)
    public view returns (bytes32) {
        return questions[question_id].best_answer;
    }

    /// @notice Returns whether the question was answered before it had an answer, ie resolved to UNRESOLVED_ANSWER
    /// @param question_id The ID of the question 
    function isSettledTooSoon(bytes32 question_id)
    public view returns(bool) {
        return (resultFor(question_id) == UNRESOLVED_ANSWER);
    }

    /// @notice Like resultFor(), but errors out if settled too soon, or returns the result of a replacement if it was reopened at the right time and settled
    /// @param question_id The ID of the question 
    function resultForOnceSettled(bytes32 question_id)
    external view returns(bytes32) {
        bytes32 result = resultFor(question_id);
        if (result == UNRESOLVED_ANSWER) {
            // Try the replacement
            bytes32 replacement_id = reopened_questions[question_id];
            require(replacement_id != bytes32(0x0), "Question was settled too soon and has not been reopened");
            // We only try one layer down rather than recursing to keep the gas costs predictable
            result = resultFor(replacement_id);
            require(result != UNRESOLVED_ANSWER, "Question replacement was settled too soon and has not been reopened");
        }
        return result;
    }

    /// @notice Asks a new question reopening a previously-asked question that was settled too soon
    /// @dev A special version of askQuestion() that replaces a previous question that was settled too soon
    /// @param template_id The ID number of the template the question will use
    /// @param question A string containing the parameters that will be passed into the template to make the question
    /// @param arbitrator The arbitration contract that will have the final word on the answer if there is a dispute
    /// @param timeout How long the contract should wait after the answer is changed before finalizing on that answer
    /// @param opening_ts If set, the earliest time it should be possible to answer the question.
    /// @param nonce A user-specified nonce used in the question ID. Change it to repeat a question.
    /// @param min_bond The minimum bond that can be used to provide the first answer.
    /// @param reopens_question_id The ID of the question this reopens
    /// @param tokens The number of tokens you want to use as an additional question reward for the reopened question.
    /// @return The ID of the newly-created question, created deterministically.
    function reopenQuestionERC20(uint256 template_id, string memory question, address arbitrator, uint32 timeout, uint32 opening_ts, uint256 nonce, uint256 min_bond, bytes32 reopens_question_id, uint256 tokens)
        // stateNotCreated is enforced by the internal _askQuestion
    public returns (bytes32) {

        // _deductTokensOrRevert will be called when we call askQuestionWithMinBondERC20

        require(isSettledTooSoon(reopens_question_id), "You can only reopen questions that resolved as settled too soon");

        bytes32 content_hash = keccak256(abi.encodePacked(template_id, opening_ts, question));

        // A reopening must exactly match the original question, except for the nonce and the creator
        require(content_hash == questions[reopens_question_id].content_hash, "content hash mismatch");
        require(arbitrator == questions[reopens_question_id].arbitrator, "arbitrator mismatch");
        require(timeout == questions[reopens_question_id].timeout, "timeout mismatch");
        require(opening_ts == questions[reopens_question_id].opening_ts , "opening_ts mismatch");
        require(min_bond == questions[reopens_question_id].min_bond, "min_bond mismatch");

        // If the the question was itself reopening some previous question, you'll have to re-reopen the previous question first.
        // This ensures the bounty can be passed on to the next attempt of the original question.
        require(!reopener_questions[reopens_question_id], "Question is already reopening a previous question");

        // A question can only be reopened once, unless the reopening was also settled too soon in which case it can be replaced
        bytes32 existing_reopen_question_id = reopened_questions[reopens_question_id];

        // Normally when we reopen a question we will take its bounty and pass it on to the reopened version.
        bytes32 take_bounty_from_question_id = reopens_question_id;
        // If the question has already been reopened but was again settled too soon, we can transfer its bounty to the next attempt.
        if (existing_reopen_question_id != bytes32(0)) {
            require(isSettledTooSoon(existing_reopen_question_id), "Question has already been reopened");
            // We'll overwrite the reopening with our new question and move the bounty.
            // Once that's done we'll detach the failed reopener and you'll be able to reopen that too if you really want, but without the bounty.
            reopener_questions[existing_reopen_question_id] = false;
            take_bounty_from_question_id = existing_reopen_question_id;
        }

        bytes32 question_id = askQuestionWithMinBondERC20(template_id, question, arbitrator, timeout, opening_ts, nonce, min_bond, tokens);

        reopened_questions[reopens_question_id] = question_id;
        reopener_questions[question_id] = true;

        questions[question_id].bounty = questions[take_bounty_from_question_id].bounty + questions[question_id].bounty;
        questions[take_bounty_from_question_id].bounty = 0;

        emit LogReopenQuestion(question_id, reopens_question_id);

        return question_id;
    }

    /// @notice Return the final answer to the specified question, provided it matches the specified criteria.
    /// @dev Reverts if the question is not finalized, or if it does not match the specified criteria.
    /// @param question_id The ID of the question
    /// @param content_hash The hash of the question content (template ID + opening time + question parameter string)
    /// @param arbitrator The arbitrator chosen for the question (regardless of whether they are asked to arbitrate)
    /// @param min_timeout The timeout set in the initial question settings must be this high or higher
    /// @param min_bond The bond sent with the final answer must be this high or higher
    /// @return The answer formatted as a bytes32
    function getFinalAnswerIfMatches(
        bytes32 question_id, 
        bytes32 content_hash, address arbitrator, uint32 min_timeout, uint256 min_bond
    ) 
        stateFinalized(question_id)
    external view returns (bytes32) {
        require(content_hash == questions[question_id].content_hash, "content hash must match");
        require(arbitrator == questions[question_id].arbitrator, "arbitrator must match");
        require(min_timeout <= questions[question_id].timeout, "timeout must be long enough");
        require(min_bond <= questions[question_id].bond, "bond must be high enough");
        return questions[question_id].best_answer;
    }

    /// @notice Assigns the winnings (bounty and bonds) to everyone who gave the accepted answer
    /// Caller must provide the answer history, in reverse order
    /// @dev Works up the chain and assign bonds to the person who gave the right answer
    /// If someone gave the winning answer earlier, they must get paid from the higher bond
    /// That means we can't pay out the bond added at n until we have looked at n-1
    /// The first answer is authenticated by checking against the stored history_hash.
    /// One of the inputs to history_hash is the history_hash before it, so we use that to authenticate the next entry, etc
    /// Once we get to a null hash we'll know we're done and there are no more answers.
    /// Usually you would call the whole thing in a single transaction, but if not then the data is persisted to pick up later.
    /// @param question_id The ID of the question
    /// @param history_hashes Second-last-to-first, the hash of each history entry. (Final one should be empty).
    /// @param addrs Last-to-first, the address of each answerer or commitment sender
    /// @param bonds Last-to-first, the bond supplied with each answer or commitment
    /// @param answers Last-to-first, each answer supplied, or commitment ID if the answer was supplied with commit->reveal
    function claimWinnings(
        bytes32 question_id, 
        bytes32[] memory history_hashes, address[] memory addrs, uint256[] memory bonds, bytes32[] memory answers
    ) 
        stateFinalized(question_id)
    public {

        require(history_hashes.length > 0, "at least one history hash entry must be provided");

        // These are only set if we split our claim over multiple transactions.
        address payee = question_claims[question_id].payee; 
        uint256 last_bond = question_claims[question_id].last_bond; 
        uint256 queued_funds = question_claims[question_id].queued_funds; 

        // Starts as the hash of the final answer submitted. It'll be cleared when we're done.
        // If we're splitting the claim over multiple transactions, it'll be the hash where we left off last time
        bytes32 last_history_hash = questions[question_id].history_hash;

        bytes32 best_answer = questions[question_id].best_answer;

        uint256 i;
        for (i = 0; i < history_hashes.length; i++) {
        
            // Check input against the history hash, and see which of 2 possible values of is_commitment fits.
            bool is_commitment = _verifyHistoryInputOrRevert(last_history_hash, history_hashes[i], answers[i], bonds[i], addrs[i]);
            
            queued_funds = queued_funds + last_bond; 
            (queued_funds, payee) = _processHistoryItem(
                question_id, best_answer, queued_funds, payee, 
                addrs[i], bonds[i], answers[i], is_commitment);
 
            // Line the bond up for next time, when it will be added to somebody's queued_funds
            last_bond = bonds[i];

            // Burn (just leave in contract balance) a fraction of all bonds except the final one.
            // This creates a cost to increasing your own bond, which could be used to delay resolution maliciously
            if (last_bond != questions[question_id].bond) {
                last_bond = last_bond - last_bond / BOND_CLAIM_FEE_PROPORTION;
            }

            last_history_hash = history_hashes[i];

        }
 
        if (last_history_hash != NULL_HASH) {
            // We haven't yet got to the null hash (1st answer), ie the caller didn't supply the full answer chain.
            // Persist the details so we can pick up later where we left off later.

            // If we know who to pay we can go ahead and pay them out, only keeping back last_bond
            // (We always know who to pay unless all we saw were unrevealed commits)
            if (payee != NULL_ADDRESS) {
                _payPayee(question_id, payee, queued_funds);
                queued_funds = 0;
            }

            question_claims[question_id].payee = payee;
            question_claims[question_id].last_bond = last_bond;
            question_claims[question_id].queued_funds = queued_funds;
        } else {
            // There is nothing left below us so the payee can keep what remains
            _payPayee(question_id, payee, queued_funds + last_bond);
            delete question_claims[question_id];
        }

        questions[question_id].history_hash = last_history_hash;

    }

    function _payPayee(bytes32 question_id, address payee, uint256 value) 
    internal {
        balanceOf[payee] = balanceOf[payee] + value;
        emit LogClaim(question_id, payee, value);
    }

    function _verifyHistoryInputOrRevert(
        bytes32 last_history_hash,
        bytes32 history_hash, bytes32 answer, uint256 bond, address addr
    )
    internal pure returns (bool) {
        if (last_history_hash == keccak256(abi.encodePacked(history_hash, answer, bond, addr, true)) ) {
            return true;
        }
        if (last_history_hash == keccak256(abi.encodePacked(history_hash, answer, bond, addr, false)) ) {
            return false;
        } 
        revert("History input provided did not match the expected hash");
    }

    function _processHistoryItem(
        bytes32 question_id, bytes32 best_answer, 
        uint256 queued_funds, address payee, 
        address addr, uint256 bond, bytes32 answer, bool is_commitment
    )
    internal returns (uint256, address) {

        // For commit-and-reveal, the answer history holds the commitment ID instead of the answer.
        // We look at the referenced commitment ID and switch in the actual answer.
        if (is_commitment) {
            bytes32 commitment_id = answer;
            // If it's a commit but it hasn't been revealed, it will always be considered wrong.
            if (!commitments[commitment_id].is_revealed) {
                delete commitments[commitment_id];
                return (queued_funds, payee);
            } else {
                answer = commitments[commitment_id].revealed_answer;
                delete commitments[commitment_id];
            }
        }

        if (answer == best_answer) {

            if (payee == NULL_ADDRESS) {

                // The entry is for the first payee we come to, ie the winner.
                // They get the question bounty.
                payee = addr;

                if (best_answer != UNRESOLVED_ANSWER && questions[question_id].bounty > 0) {
                    _payPayee(question_id, payee, questions[question_id].bounty);
                    questions[question_id].bounty = 0;
                }

            } else if (addr != payee) {

                // Answerer has changed, ie we found someone lower down who needs to be paid

                // The lower answerer will take over receiving bonds from higher answerer.
                // They should also be paid the takeover fee, which is set at a rate equivalent to their bond. 
                // (This is our arbitrary rule, to give consistent right-answerers a defence against high-rollers.)

                // There should be enough for the fee, but if not, take what we have.
                // There's an edge case involving weird arbitrator behaviour where we may be short.
                uint256 answer_takeover_fee = (queued_funds >= bond) ? bond : queued_funds;
                // Settle up with the old (higher-bonded) payee
                _payPayee(question_id, payee, queued_funds - answer_takeover_fee);

                // Now start queued_funds again for the new (lower-bonded) payee
                payee = addr;
                queued_funds = answer_takeover_fee;

            }

        }

        return (queued_funds, payee);

    }

    /// @notice Convenience function to assign bounties/bonds for multiple questions in one go, then withdraw all your funds.
    /// Caller must provide the answer history for each question, in reverse order
    /// @dev Can be called by anyone to assign bonds/bounties, but funds ar...

// [truncated — 54807 bytes total]

Read Contract

arbitrator_question_fees 0x6fa42742 → uint256
balanceOf 0x70a08231 → uint256
commitments 0x839df945 → uint32, bool, bytes32
getArbitrator 0x2518904c → address
getBestAnswer 0x8d552d46 → bytes32
getBond 0x26d6c97b → uint256
getBounty 0x2417395c → uint256
getContentHash 0x51577ea9 → bytes32
getFinalAnswer 0xa462fb7b → bytes32
getFinalAnswerIfMatches 0x12a203c3 → bytes32
getFinalizeTS 0xacae8f4e → uint32
getHistoryHash 0x82ffa9f7 → bytes32
getMinBond 0x484c0714 → uint256
getOpeningTS 0x9e63fa6a → uint32
getTimeout 0x9f1025c6 → uint32
isFinalized 0x7f8d429e → bool
isPendingArbitration 0x924532fb → bool
isSettledTooSoon 0x06c3b67a → bool
question_claims 0x590158a7 → address, uint256, uint256
questions 0x95addb90 → bytes32, address, uint32, uint32, uint32, bool, uint256, bytes32, bytes32, uint256, uint256
reopened_questions 0xc081d8a3 → bytes32
reopener_questions 0xe83bfb58 → bool
resultFor 0xd09cc57e → bytes32
resultForOnceSettled 0xab5a4e35 → bytes32
template_hashes 0x4e60f883 → bytes32
templates 0xbc525652 → uint256
token 0xfc0c546a → address

Write Contract 20 functions

These functions modify contract state and require a wallet transaction to execute.

askQuestion 0x762c38fd
uint256 template_id
string question
address arbitrator
uint32 timeout
uint32 opening_ts
uint256 nonce
returns: bytes32
askQuestionERC20 0xd4876b9f
uint256 template_id
string question
address arbitrator
uint32 timeout
uint32 opening_ts
uint256 nonce
uint256 tokens
returns: bytes32
askQuestionWithMinBondERC20 0x52debac3
uint256 template_id
string question
address arbitrator
uint32 timeout
uint32 opening_ts
uint256 nonce
uint256 min_bond
uint256 tokens
returns: bytes32
assignWinnerAndSubmitAnswerByArbitrator 0xd44e293c
bytes32 question_id
bytes32 answer
address payee_if_wrong
bytes32 last_history_hash
bytes32 last_answer_or_commitment_id
address last_answerer
cancelArbitration 0xebbdd2b0
bytes32 question_id
claimMultipleAndWithdrawBalance 0x28828b1e
bytes32[] question_ids
uint256[] lengths
bytes32[] hist_hashes
address[] addrs
uint256[] bonds
bytes32[] answers
claimWinnings 0x1101a0fd
bytes32 question_id
bytes32[] history_hashes
address[] addrs
uint256[] bonds
bytes32[] answers
createTemplate 0x83bf4609
string content
returns: uint256
createTemplateAndAskQuestion 0xa1130d04
string content
string question
address arbitrator
uint32 timeout
uint32 opening_ts
uint256 nonce
returns: bytes32
fundAnswerBountyERC20 0x128b7a47
bytes32 question_id
uint256 tokens
notifyOfArbitrationRequest 0xf6a94ecb
bytes32 question_id
address requester
uint256 max_previous
reopenQuestionERC20 0xbf53b188
uint256 template_id
string question
address arbitrator
uint32 timeout
uint32 opening_ts
uint256 nonce
uint256 min_bond
bytes32 reopens_question_id
uint256 tokens
returns: bytes32
setQuestionFee 0x4df6ca2a
uint256 fee
setToken 0x144fa6d7
address _token
submitAnswerByArbitrator 0xfe92049d
bytes32 question_id
bytes32 answer
address answerer
submitAnswerCommitmentERC20 0xac7b2a5f
bytes32 question_id
bytes32 answer_hash
uint256 max_previous
address _answerer
uint256 tokens
submitAnswerERC20 0x2f998a6f
bytes32 question_id
bytes32 answer
uint256 max_previous
uint256 tokens
submitAnswerForERC20 0xb8eaa980
bytes32 question_id
bytes32 answer
uint256 max_previous
address answerer
uint256 tokens
submitAnswerReveal 0x4dc266b4
bytes32 question_id
bytes32 answer
uint256 nonce
uint256 bond
withdraw 0x3ccfd60b
No parameters

Recent Transactions

No transactions found for this address