Address Contract Verified
Address
0x91293f7517AcB93C65aE031b7809Be2fBc5FfbeA
Balance
0 ETH
Nonce
2
Code Size
12337 bytes
Creator
0x7833CF98...5C41 at tx 0x0bd105c9...dc305d
Indexed Transactions
0
Contract Bytecode
12337 bytes
0x608060405234801561001057600080fd5b50600436106102485760003560e01c8063962ca4961161013b578063b83d8157116100b8578063dd62ed3e1161007c578063dd62ed3e146104e5578063e0bab4c4146104f8578063e9d54d3914610513578063f2fde38b1461052e578063f887ea401461054157600080fd5b8063b83d81571461048b578063c28f439214610494578063c54e44eb146104a7578063cd377c53146104c2578063cf59d563146104ca57600080fd5b8063a587f4f3116100ff578063a587f4f31461042f578063a9059cbb14610442578063aad51b6614610455578063ad5c46481461045d578063b66dc5c31461047857600080fd5b8063962ca496146103e6578063976572a7146103ee5780639b51fb0d14610401578063a06880c014610414578063a457c2d71461041c57600080fd5b806362469353116101c95780637d7c2a1c1161018d5780637d7c2a1c146103a257806389a30271146103aa5780638da5cb5b146103c557806391892631146103d657806395d89b41146103de57600080fd5b8063624693531461034b578063624736e01461035457806369192b331461035c57806370a0823114610371578063715018a61461039a57600080fd5b80630fd9eeeb116102105780630fd9eeeb146102f357806318160ddd1461030e57806323b872dd14610316578063313ce56714610329578063395093511461033857600080fd5b8063038c33bd1461024d578063054b682e1461028557806306fdde03146102a6578063095ea7b3146102bb5780630e24ecdc146102de575b600080fd5b6102687388e6a0c2ddd26feeb64f039a2c41296fcb3f564081565b6040516001600160a01b0390911681526020015b60405180910390f35b610298610293366004612ba9565b610556565b60405190815260200161027c565b6102ae61056d565b60405161027c9190612c12565b6102ce6102c9366004612c41565b6105ff565b604051901515815260200161027c565b6102f16102ec366004612c41565b610619565b005b610268732ba64efb7a4ec8983e22a49c81fa216ac33f383a81565b600254610298565b6102ce610324366004612c6d565b6107c3565b6040516012815260200161027c565b6102ce610346366004612c41565b6107e7565b61029860125481565b610298610809565b610364610aa7565b60405161027c9190612cae565b61029861037f366004612cdf565b6001600160a01b031660009081526020819052604090205490565b6102f1610ae2565b6102f1610af6565b61026873a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4881565b6005546001600160a01b0316610268565b610298610c9a565b6102ae610d66565b610298610d75565b6102f16103fc366004612ba9565b610e18565b61029861040f366004612ba9565b611122565b6102ce611132565b6102ce61042a366004612c41565b61115a565b6102f161043d366004612c41565b6111d5565b6102ce610450366004612c41565b611cde565b610298611cec565b61026873c02aaa39b223fe8d0a0e5c4f27ead9083c756cc281565b6102f1610486366004612ba9565b611d49565b61029860135481565b600754610268906001600160a01b031681565b61026873dac17f958d2ee523a2206206994597c13d831ec781565b610298611f95565b610268735f98805a4e8be255a32880fdec7f6728c6568ba081565b6102986104f3366004612cfc565b6120fe565b610268736b175474e89094c44da98b954eedeac495271d0f81565b61026873982152a6c7f732ec7c9ea998ddd9ebde00dfa16e81565b6102f161053c366004612cdf565b612129565b610268600080516020612fdc83398151915281565b600d816005811061056657600080fd5b0154905081565b60606003805461057c90612d35565b80601f01602080910402602001604051908101604052809291908181526020018280546105a890612d35565b80156105f55780601f106105ca576101008083540402835291602001916105f5565b820191906000526020600020905b8154815290600101906020018083116105d857829003601f168201915b5050505050905090565b60003361060d81858561219f565b60019150505b92915050565b6106216122c4565b6007546001600160a01b031633146106805760405162461bcd60e51b815260206004820152601760248201527f4f6e6c7920426f6e64546f6b656e2063616e2063616c6c00000000000000000060448201526064015b60405180910390fd5b600081116106d05760405162461bcd60e51b815260206004820152601c60248201527f56616c7565206d7573742062652067726561746572207468616e2030000000006044820152606401610677565b60006106da610c9a565b90506000816106f1670de0b6b3a764000085612d85565b6106fb9190612d9c565b9050610707848261231d565b600760009054906101000a90046001600160a01b03166001600160a01b03166317c5da6a6040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561075757600080fd5b505af115801561076b573d6000803e3d6000fd5b505060408051868152602081018590526001600160a01b03881693507f61df6656777fc276f4bf3b5b10e75813e979220b1c0662899f35fd34241016ac92500160405180910390a250506107bf6001600655565b5050565b6000336107d18582856123dc565b6107dc858585612456565b506001949350505050565b60003361060d8185856107fa83836120fe565b6108049190612dbe565b61219f565b60008073982152a6c7f732ec7c9ea998ddd9ebde00dfa16e6001600160a01b0316633850c7bd6040518163ffffffff1660e01b815260040160e060405180830381865afa15801561085e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906108829190612df3565b505050505050905060007388e6a0c2ddd26feeb64f039a2c41296fcb3f56406001600160a01b0316633850c7bd6040518163ffffffff1660e01b815260040160e060405180830381865afa1580156108de573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906109029190612df3565b50505050505090506000826001600160a01b03161161095c5760405162461bcd60e51b8152602060048201526016602482015275496e76616c6964205742474c2f45544820707269636560501b6044820152606401610677565b6000816001600160a01b0316116109ae5760405162461bcd60e51b8152602060048201526016602482015275496e76616c696420555344432f45544820707269636560501b6044820152606401610677565b60006109be600160601b80612d85565b670de0b6b3a76400006109da6001600160a01b03861680612d85565b6109e49190612d85565b6109ee9190612d9c565b90506000610a056001600160a01b03841680612d85565b670de0b6b3a7640000610a1c600160601b80612d85565b610a269190612d85565b610a309190612d9c565b90506000620f4240610a428385612d85565b610a4c9190612d9c565b905060008111610a9e5760405162461bcd60e51b815260206004820152601860248201527f43616c63756c61746564207072696365206973207a65726f00000000000000006044820152606401610677565b95945050505050565b610aaf612b8b565b6040805160a08101918290529060089060059082845b815481526020019060010190808311610ac5575050505050905090565b610aea6125fa565b610af46000612654565b565b600060125411610b3a5760405162461bcd60e51b815260206004820152600f60248201526e139bc819195c1bdcda5d1cc81e595d608a1b6044820152606401610677565b6301e13380601354610b4c9190612dbe565b421015610b9b5760405162461bcd60e51b815260206004820152601760248201527f546f6f206561726c7920666f7220726562616c616e63650000000000000000006044820152606401610677565b6008546000610bab600383612d9c565b9050600060088101558060086001016000828254610bc99190612dbe565b9091555081905060086002016000828254610be49190612dbe565b9091555081905060086003016000828254610bff9190612dbe565b90915550600090505b6004811015610c57576008610c1e826001612dbe565b60058110610c2e57610c2e612e8d565b015460088260058110610c4357610c43612e8d565b015580610c4f81612ea3565b915050610c08565b506000600c554260138190556040519081527f811d4760f1a92875eb76dbd3dc2359544b2f6a000ba5b78784c0b105b3469bd09060200160405180910390a15050565b6000610ca560025490565b600003610cd757610cb4610d75565b610cbf906064612dbe565b610cd29068056bc75e2d63100000612d9c565b905090565b6000610ce1611f95565b9050600a610cf8670de0b6b3a76400006002612d85565b610d029190612d9c565b811015610d2e57600a610d1e670de0b6b3a76400006002612d85565b610d289190612d9c565b91505090565b600a610d43670de0b6b3a76400006008612d85565b610d4d9190612d9c565b811015610d61576064610d1e826050612d85565b919050565b60606004805461057c90612d35565b600080610d80611cec565b9050610d96670de0b6b3a76400006103e8612d85565b811015610da557603291505090565b610db9670de0b6b3a7640000612710612d85565b811015610dc857602891505090565b610ddd670de0b6b3a7640000620186a0612d85565b811015610dec57601e91505090565b610e01670de0b6b3a7640000620f4240612d85565b811015610e1057601991505090565b601491505090565b610e206122c4565b33600090815260208190526040902054811115610e7f5760405162461bcd60e51b815260206004820152601860248201527f496e73756666696369656e7420534d532062616c616e636500000000000000006044820152606401610677565b6000610e89611f95565b9050600a610ea0670de0b6b3a7640000600c612d85565b610eaa9190612d9c565b811015610ef95760405162461bcd60e51b815260206004820181905260248201527f4352206d757374206265203e3d20312e3220666f7220726564656d7074696f6e6044820152606401610677565b6000610f03610c9a565b90506000670de0b6b3a7640000610f1a8386612d85565b610f249190612d9c565b90506000610f30610809565b610f3b906064612d85565b610f46836050612d85565b610f509190612d9c565b9050806008600001541015610fb15760405162461bcd60e51b815260206004820152602160248201527f496e73756666696369656e74205742474c20696e206669727374206275636b656044820152601d60fa1b6064820152608401610677565b8060086000016000828254610fc69190612ebc565b90915550610fd6905033866126a6565b60405163a9059cbb60e01b815233600482015260248101829052732ba64efb7a4ec8983e22a49c81fa216ac33f383a9063a9059cbb906044016020604051808303816000875af115801561102e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906110529190612ecf565b61106e5760405162461bcd60e51b815260040161067790612eea565b600760009054906101000a90046001600160a01b03166001600160a01b03166317c5da6a6040518163ffffffff1660e01b8152600401600060405180830381600087803b1580156110be57600080fd5b505af11580156110d2573d6000803e3d6000fd5b505060408051888152602081018590523393507fe5b754fb1abb7f01b499791d0b820ae3b6af3424ac1c59768edb53f4ec31a92992500160405180910390a25050505061111f6001600655565b50565b6008816005811061056657600080fd5b600080601254118015610cd257506301e133806013546111529190612dbe565b421015905090565b6000338161116882866120fe565b9050838110156111c85760405162461bcd60e51b815260206004820152602560248201527f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f77604482015264207a65726f60d81b6064820152608401610677565b6107dc828686840361219f565b6111dd6122c4565b6000811161122d5760405162461bcd60e51b815260206004820152601d60248201527f416d6f756e74206d7573742062652067726561746572207468616e20300000006044820152606401610677565b6040516323b872dd60e01b8152336004820152306024820152604481018290526001600160a01b038316906323b872dd906064016020604051808303816000875af1158015611280573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906112a49190612ecf565b6112c05760405162461bcd60e51b815260040161067790612eea565b60006001600160a01b03831673dac17f958d2ee523a2206206994597c13d831ec7148061130957506001600160a01b03831673a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48145b1561167357604051636eb1769f60e11b8152306004820152600080516020612fdc83398151915260248201526000906001600160a01b0385169063dd62ed3e90604401602060405180830381865afa158015611369573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061138d9190612f13565b9050828110156113c9576000196113c7600080516020612fdc8339815191526113b68484612ebc565b6001600160a01b03881691906127d5565b505b6040805160a08101909152600090806113fc87606473c02aaa39b223fe8d0a0e5c4f27ead9083c756cc260c08501612f2c565b60408051601f19818403018152918152908252306020830152428282015260608201879052600060809092018290525163c04b8d5960e01b815291925090600080516020612fdc8339815191529063c04b8d599061145e908590600401612f67565b6020604051808303816000875af115801561147d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906114a19190612f13565b604051636eb1769f60e11b8152306004820152600080516020612fdc833981519152602482015290915073c02aaa39b223fe8d0a0e5c4f27ead9083c756cc29063dd62ed3e90604401602060405180830381865afa158015611507573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061152b9190612f13565b92508483101561157257600019611570600080516020612fdc8339815191526115548684612ebc565b73c02aaa39b223fe8d0a0e5c4f27ead9083c756cc291906127d5565b505b6040805160a08101909152806115b673c02aaa39b223fe8d0a0e5c4f27ead9083c756cc26064732ba64efb7a4ec8983e22a49c81fa216ac33f383a60c08501612f2c565b6040516020818303038152906040528152602001306001600160a01b0316815260200142815260200182815260200160008152509150600080516020612fdc8339815191526001600160a01b031663c04b8d59836040518263ffffffff1660e01b81526004016116269190612f67565b6020604051808303816000875af1158015611645573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906116699190612f13565b9350505050611bda565b6001600160a01b038316735f98805a4e8be255a32880fdec7f6728c6568ba014806116ba57506001600160a01b038316736b175474e89094c44da98b954eedeac495271d0f145b15611bda57604051636eb1769f60e11b8152306004820152600080516020612fdc83398151915260248201526000906001600160a01b0385169063dd62ed3e90604401602060405180830381865afa15801561171a573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061173e9190612f13565b90508281101561176957600019611767600080516020612fdc8339815191526113b68484612ebc565b505b6064735f98805a4e8be255a32880fdec7f6728c6568b9f196001600160a01b0386160161179557506101f45b6040805160a08101909152600090806117c7888573a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4860c08501612f2c565b60408051601f19818403018152918152908252306020830152428282015260608201889052600060809092018290525163c04b8d5960e01b815291925090600080516020612fdc8339815191529063c04b8d5990611829908590600401612f67565b6020604051808303816000875af1158015611848573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061186c9190612f13565b604051636eb1769f60e11b8152306004820152600080516020612fdc833981519152602482015290915073a0b86991c6218b36c1d19d4a2e9eb0ce3606eb489063dd62ed3e90604401602060405180830381865afa1580156118d2573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906118f69190612f13565b93508584101561193d5760001961193b600080516020612fdc83398151915261191f8784612ebc565b73a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4891906127d5565b505b6040805160a081019091528061198173a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48606473c02aaa39b223fe8d0a0e5c4f27ead9083c756cc260c08501612f2c565b60408051601f19818403018152918152908252306020830152428282015260608201849052600060809092018290525163c04b8d5960e01b815291935090600080516020612fdc8339815191529063c04b8d59906119e3908690600401612f67565b6020604051808303816000875af1158015611a02573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611a269190612f13565b604051636eb1769f60e11b8152306004820152600080516020612fdc833981519152602482015290915073c02aaa39b223fe8d0a0e5c4f27ead9083c756cc29063dd62ed3e90604401602060405180830381865afa158015611a8c573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611ab09190612f13565b945086851015611adb57600019611ad9600080516020612fdc8339815191526115548884612ebc565b505b6040805160a0810190915280611b1f73c02aaa39b223fe8d0a0e5c4f27ead9083c756cc26064732ba64efb7a4ec8983e22a49c81fa216ac33f383a60c08501612f2c565b6040516020818303038152906040528152602001306001600160a01b0316815260200142815260200182815260200160008152509250600080516020612fdc8339815191526001600160a01b031663c04b8d59846040518263ffffffff1660e01b8152600401611b8f9190612f67565b6020604051808303816000875af1158015611bae573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611bd29190612f13565b955050505050505b6000611be4610c9a565b9050600081611bfb670de0b6b3a764000085612d85565b611c059190612d9c565b9050611c10836128b4565b601254600003611c24574260128190556013555b611c2e338261231d565b600760009054906101000a90046001600160a01b03166001600160a01b03166317c5da6a6040518163ffffffff1660e01b8152600401600060405180830381600087803b158015611c7e57600080fd5b505af1158015611c92573d6000803e3d6000fd5b505060408051868152602081018590523393507f90890809c654f11d6e72a28fa60149770a0d11ec6c92319d6ceb2bb0a4ea1a1592500160405180910390a25050506107bf6001600655565b60003361060d818585612456565b600080805b6005811015611d2d5760088160058110611d0d57611d0d612e8d565b0154611d199083612dbe565b915080611d2581612ea3565b915050611cf1565b50670de0b6b3a7640000611d3f610809565b610d1e9083612d85565b611d516122c4565b60008111611da15760405162461bcd60e51b815260206004820152601d60248201527f416d6f756e74206d7573742062652067726561746572207468616e20300000006044820152606401610677565b6040516323b872dd60e01b815233600482015230602482015260448101829052732ba64efb7a4ec8983e22a49c81fa216ac33f383a906323b872dd906064016020604051808303816000875af1158015611dff573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611e239190612ecf565b611e3f5760405162461bcd60e51b815260040161067790612eea565b6000611e49610c9a565b9050600081611e56610809565b611e609085612d85565b611e6a9190612d9c565b9050601254600003611e80574260128190556013555b6000611e8a611f95565b9050600a611ea1670de0b6b3a76400006008612d85565b611eab9190612d9c565b811015611ed2578360086000016000828254611ec79190612dbe565b90915550611edb9050565b611edb846128b4565b611ee5338361231d565b600760009054906101000a90046001600160a01b03166001600160a01b031663e3161ddd6040518163ffffffff1660e01b8152600401600060405180830381600087803b158015611f3557600080fd5b505af1158015611f49573d6000803e3d6000fd5b505060408051878152602081018690523393507f90890809c654f11d6e72a28fa60149770a0d11ec6c92319d6ceb2bb0a4ea1a1592500160405180910390a250505061111f6001600655565b600080805b6005811015611fd65760088160058110611fb657611fb6612e8d565b0154611fc29083612dbe565b915080611fce81612ea3565b915050611f9a565b50600254600003611ff057670de0b6b3a764000091505090565b6000612005670de0b6b3a76400006064612d85565b61200d610d75565b612018906064612dbe565b612020610809565b61202a9085612d85565b6120349190612d85565b61203e9190612d9c565b90506000600760009054906101000a90046001600160a01b03166001600160a01b031663964b434e6040518163ffffffff1660e01b8152600401602060405180830381865afa158015612095573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906120b99190612f13565b90508082116120cc576000935050505090565b600254670de0b6b3a76400006120e28385612ebc565b6120ec9190612d85565b6120f69190612d9c565b935050505090565b6001600160a01b03918216600090815260016020908152604080832093909416825291909152205490565b6121316125fa565b6001600160a01b0381166121965760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b6064820152608401610677565b61111f81612654565b6001600160a01b0383166122015760405162461bcd60e51b8152602060048201526024808201527f45524332303a20617070726f76652066726f6d20746865207a65726f206164646044820152637265737360e01b6064820152608401610677565b6001600160a01b0382166122625760405162461bcd60e51b815260206004820152602260248201527f45524332303a20617070726f766520746f20746865207a65726f206164647265604482015261737360f01b6064820152608401610677565b6001600160a01b0383811660008181526001602090815260408083209487168084529482529182902085905590518481527f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92591015b60405180910390a3505050565b6002600654036123165760405162461bcd60e51b815260206004820152601f60248201527f5265656e7472616e637947756172643a207265656e7472616e742063616c6c006044820152606401610677565b6002600655565b6001600160a01b0382166123735760405162461bcd60e51b815260206004820152601f60248201527f45524332303a206d696e7420746f20746865207a65726f2061646472657373006044820152606401610677565b80600260008282546123859190612dbe565b90915550506001600160a01b038216600081815260208181526040808320805486019055518481527fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef910160405180910390a35050565b60006123e884846120fe565b9050600019811461245057818110156124435760405162461bcd60e51b815260206004820152601d60248201527f45524332303a20696e73756666696369656e7420616c6c6f77616e63650000006044820152606401610677565b612450848484840361219f565b50505050565b6001600160a01b0383166124ba5760405162461bcd60e51b815260206004820152602560248201527f45524332303a207472616e736665722066726f6d20746865207a65726f206164604482015264647265737360d81b6064820152608401610677565b6001600160a01b03821661251c5760405162461bcd60e51b815260206004820152602360248201527f45524332303a207472616e7366657220746f20746865207a65726f206164647260448201526265737360e81b6064820152608401610677565b6001600160a01b038316600090815260208190526040902054818110156125945760405162461bcd60e51b815260206004820152602660248201527f45524332303a207472616e7366657220616d6f756e7420657863656564732062604482015265616c616e636560d01b6064820152608401610677565b6001600160a01b03848116600081815260208181526040808320878703905593871680835291849020805487019055925185815290927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef910160405180910390a3612450565b6005546001600160a01b03163314610af45760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e65726044820152606401610677565b600580546001600160a01b038381166001600160a01b0319831681179093556040519116919082907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b6001600160a01b0382166127065760405162461bcd60e51b815260206004820152602160248201527f45524332303a206275726e2066726f6d20746865207a65726f206164647265736044820152607360f81b6064820152608401610677565b6001600160a01b0382166000908152602081905260409020548181101561277a5760405162461bcd60e51b815260206004820152602260248201527f45524332303a206275726e20616d6f756e7420657863656564732062616c616e604482015261636560f01b6064820152608401610677565b6001600160a01b0383166000818152602081815260408083208686039055600280548790039055518581529192917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef91016122b7565b505050565b604051636eb1769f60e11b81523060048201526001600160a01b0383811660248301526000919085169063dd62ed3e90604401602060405180830381865afa158015612825573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906128499190612f13565b90506124508463095ea7b360e01b856128628686612dbe565b6040516001600160a01b039092166024830152604482015260640160408051601f198184030181529190526020810180516001600160e01b03166001600160e01b031990931692909217909152612926565b60005b60058110156107bf576064600d82600581106128d5576128d5612e8d565b01546128e19084612d85565b6128eb9190612d9c565b600882600581106128fe576128fe612e8d565b01600082825461290e9190612dbe565b9091555081905061291e81612ea3565b9150506128b7565b600061297b826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b03166129fb9092919063ffffffff16565b905080516000148061299c57508080602001905181019061299c9190612ecf565b6127d05760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b6064820152608401610677565b6060612a0a8484600085612a12565b949350505050565b606082471015612a735760405162461bcd60e51b815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f6044820152651c8818d85b1b60d21b6064820152608401610677565b600080866001600160a01b03168587604051612a8f9190612fbf565b60006040518083038185875af1925050503d8060008114612acc576040519150601f19603f3d011682016040523d82523d6000602084013e612ad1565b606091505b5091509150612ae287838387612aed565b979650505050505050565b60608315612b5c578251600003612b55576001600160a01b0385163b612b555760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152606401610677565b5081612a0a565b612a0a8383815115612b715781518083602001fd5b8060405162461bcd60e51b81526004016106779190612c12565b6040518060a001604052806005906020820280368337509192915050565b600060208284031215612bbb57600080fd5b5035919050565b60005b83811015612bdd578181015183820152602001612bc5565b50506000910152565b60008151808452612bfe816020860160208601612bc2565b601f01601f19169290920160200192915050565b602081526000612c256020830184612be6565b9392505050565b6001600160a01b038116811461111f57600080fd5b60008060408385031215612c5457600080fd5b8235612c5f81612c2c565b946020939093013593505050565b600080600060608486031215612c8257600080fd5b8335612c8d81612c2c565b92506020840135612c9d81612c2c565b929592945050506040919091013590565b60a08101818360005b6005811015612cd6578151835260209283019290910190600101612cb7565b50505092915050565b600060208284031215612cf157600080fd5b8135612c2581612c2c565b60008060408385031215612d0f57600080fd5b8235612d1a81612c2c565b91506020830135612d2a81612c2c565b809150509250929050565b600181811c90821680612d4957607f821691505b602082108103612d6957634e487b7160e01b600052602260045260246000fd5b50919050565b634e487b7160e01b600052601160045260246000fd5b808202811582820484141761061357610613612d6f565b600082612db957634e487b7160e01b600052601260045260246000fd5b500490565b8082018082111561061357610613612d6f565b805161ffff81168114610d6157600080fd5b80518015158114610d6157600080fd5b600080600080600080600060e0888a031215612e0e57600080fd5b8751612e1981612c2c565b8097505060208801518060020b8114612e3157600080fd5b9550612e3f60408901612dd1565b9450612e4d60608901612dd1565b9350612e5b60808901612dd1565b925060a088015160ff81168114612e7157600080fd5b9150612e7f60c08901612de3565b905092959891949750929550565b634e487b7160e01b600052603260045260246000fd5b600060018201612eb557612eb5612d6f565b5060010190565b8181038181111561061357610613612d6f565b600060208284031215612ee157600080fd5b612c2582612de3565b6020808252600f908201526e151c985b9cd9995c8819985a5b1959608a1b604082015260600190565b600060208284031215612f2557600080fd5b5051919050565b606093841b6bffffffffffffffffffffffff19908116825260e89390931b6001600160e81b0319166014820152921b166017820152602b0190565b602081526000825160a06020840152612f8360c0840182612be6565b905060018060a01b0360208501511660408401526040840151606084015260608401516080840152608084015160a08401528091505092915050565b60008251612fd1818460208701612bc2565b919091019291505056fe000000000000000000000000e592427a0aece92de3edee1f18e0157c05861564a2646970667358221220fc7927ab900741ccf27581ee99ccf132bd06b108faae239f6d4ea8516b48f2f964736f6c63430008140033
Verified Source Code Full Match
Compiler: v0.8.20+commit.a1b79de6
EVM: paris
Optimization: Yes (200 runs)
Ownable.sol 83 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable.sol)
pragma solidity ^0.8.0;
import "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
ReentrancyGuard.sol 77 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (security/ReentrancyGuard.sol)
pragma solidity ^0.8.0;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be _NOT_ENTERED
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == _ENTERED;
}
}
ERC20.sol 365 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/ERC20.sol)
pragma solidity ^0.8.0;
import "./IERC20.sol";
import "./extensions/IERC20Metadata.sol";
import "../../utils/Context.sol";
/**
* @dev Implementation of the {IERC20} interface.
*
* This implementation is agnostic to the way tokens are created. This means
* that a supply mechanism has to be added in a derived contract using {_mint}.
* For a generic mechanism see {ERC20PresetMinterPauser}.
*
* TIP: For a detailed writeup see our guide
* https://forum.openzeppelin.com/t/how-to-implement-erc20-supply-mechanisms/226[How
* to implement supply mechanisms].
*
* The default value of {decimals} is 18. To change this, you should override
* this function so it returns a different value.
*
* We have followed general OpenZeppelin Contracts guidelines: functions revert
* instead returning `false` on failure. This behavior is nonetheless
* conventional and does not conflict with the expectations of ERC20
* applications.
*
* Additionally, an {Approval} event is emitted on calls to {transferFrom}.
* This allows applications to reconstruct the allowance for all accounts just
* by listening to said events. Other implementations of the EIP may not emit
* these events, as it isn't required by the specification.
*
* Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
* functions have been added to mitigate the well-known issues around setting
* allowances. See {IERC20-approve}.
*/
contract ERC20 is Context, IERC20, IERC20Metadata {
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
/**
* @dev Sets the values for {name} and {symbol}.
*
* All two of these values are immutable: they can only be set once during
* construction.
*/
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}
/**
* @dev Returns the name of the token.
*/
function name() public view virtual override returns (string memory) {
return _name;
}
/**
* @dev Returns the symbol of the token, usually a shorter version of the
* name.
*/
function symbol() public view virtual override returns (string memory) {
return _symbol;
}
/**
* @dev Returns the number of decimals used to get its user representation.
* For example, if `decimals` equals `2`, a balance of `505` tokens should
* be displayed to a user as `5.05` (`505 / 10 ** 2`).
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei. This is the default value returned by this function, unless
* it's overridden.
*
* NOTE: This information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* {IERC20-balanceOf} and {IERC20-transfer}.
*/
function decimals() public view virtual override returns (uint8) {
return 18;
}
/**
* @dev See {IERC20-totalSupply}.
*/
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}
/**
* @dev See {IERC20-balanceOf}.
*/
function balanceOf(address account) public view virtual override returns (uint256) {
return _balances[account];
}
/**
* @dev See {IERC20-transfer}.
*
* Requirements:
*
* - `to` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/
function transfer(address to, uint256 amount) public virtual override returns (bool) {
address owner = _msgSender();
_transfer(owner, to, amount);
return true;
}
/**
* @dev See {IERC20-allowance}.
*/
function allowance(address owner, address spender) public view virtual override returns (uint256) {
return _allowances[owner][spender];
}
/**
* @dev See {IERC20-approve}.
*
* NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on
* `transferFrom`. This is semantically equivalent to an infinite approval.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 amount) public virtual override returns (bool) {
address owner = _msgSender();
_approve(owner, spender, amount);
return true;
}
/**
* @dev See {IERC20-transferFrom}.
*
* Emits an {Approval} event indicating the updated allowance. This is not
* required by the EIP. See the note at the beginning of {ERC20}.
*
* NOTE: Does not update the allowance if the current allowance
* is the maximum `uint256`.
*
* Requirements:
*
* - `from` and `to` cannot be the zero address.
* - `from` must have a balance of at least `amount`.
* - the caller must have allowance for ``from``'s tokens of at least
* `amount`.
*/
function transferFrom(address from, address to, uint256 amount) public virtual override returns (bool) {
address spender = _msgSender();
_spendAllowance(from, spender, amount);
_transfer(from, to, amount);
return true;
}
/**
* @dev Atomically increases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
address owner = _msgSender();
_approve(owner, spender, allowance(owner, spender) + addedValue);
return true;
}
/**
* @dev Atomically decreases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `spender` must have allowance for the caller of at least
* `subtractedValue`.
*/
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
address owner = _msgSender();
uint256 currentAllowance = allowance(owner, spender);
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
unchecked {
_approve(owner, spender, currentAllowance - subtractedValue);
}
return true;
}
/**
* @dev Moves `amount` of tokens from `from` to `to`.
*
* This internal function is equivalent to {transfer}, and can be used to
* e.g. implement automatic token fees, slashing mechanisms, etc.
*
* Emits a {Transfer} event.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `from` must have a balance of at least `amount`.
*/
function _transfer(address from, address to, uint256 amount) internal virtual {
require(from != address(0), "ERC20: transfer from the zero address");
require(to != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(from, to, amount);
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
// Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
// decrementing then incrementing.
_balances[to] += amount;
}
emit Transfer(from, to, amount);
_afterTokenTransfer(from, to, amount);
}
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
* the total supply.
*
* Emits a {Transfer} event with `from` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
*/
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply += amount;
unchecked {
// Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above.
_balances[account] += amount;
}
emit Transfer(address(0), account, amount);
_afterTokenTransfer(address(0), account, amount);
}
/**
* @dev Destroys `amount` tokens from `account`, reducing the
* total supply.
*
* Emits a {Transfer} event with `to` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
*/
function _burn(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: burn from the zero address");
_beforeTokenTransfer(account, address(0), amount);
uint256 accountBalance = _balances[account];
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
unchecked {
_balances[account] = accountBalance - amount;
// Overflow not possible: amount <= accountBalance <= totalSupply.
_totalSupply -= amount;
}
emit Transfer(account, address(0), amount);
_afterTokenTransfer(account, address(0), amount);
}
/**
* @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
*
* This internal function is equivalent to `approve`, and can be used to
* e.g. set automatic allowances for certain subsystems, etc.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `owner` cannot be the zero address.
* - `spender` cannot be the zero address.
*/
function _approve(address owner, address spender, uint256 amount) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
/**
* @dev Updates `owner` s allowance for `spender` based on spent `amount`.
*
* Does not update the allowance amount in case of infinite allowance.
* Revert if not enough allowance is available.
*
* Might emit an {Approval} event.
*/
function _spendAllowance(address owner, address spender, uint256 amount) internal virtual {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC20: insufficient allowance");
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
}
}
/**
* @dev Hook that is called before any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* will be transferred to `to`.
* - when `from` is zero, `amount` tokens will be minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens will be burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual {}
/**
* @dev Hook that is called after any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* has been transferred to `to`.
* - when `from` is zero, `amount` tokens have been minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens have been burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _afterTokenTransfer(address from, address to, uint256 amount) internal virtual {}
}
IERC20Metadata.sol 28 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)
pragma solidity ^0.8.0;
import "../IERC20.sol";
/**
* @dev Interface for the optional metadata functions from the ERC20 standard.
*
* _Available since v4.1._
*/
interface IERC20Metadata is IERC20 {
/**
* @dev Returns the name of the token.
*/
function name() external view returns (string memory);
/**
* @dev Returns the symbol of the token.
*/
function symbol() external view returns (string memory);
/**
* @dev Returns the decimals places of the token.
*/
function decimals() external view returns (uint8);
}
IERC20Permit.sol 90 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.4) (token/ERC20/extensions/IERC20Permit.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
* https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
*
* Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
* presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
* need to send a transaction, and thus is not required to hold Ether at all.
*
* ==== Security Considerations
*
* There are two important considerations concerning the use of `permit`. The first is that a valid permit signature
* expresses an allowance, and it should not be assumed to convey additional meaning. In particular, it should not be
* considered as an intention to spend the allowance in any specific way. The second is that because permits have
* built-in replay protection and can be submitted by anyone, they can be frontrun. A protocol that uses permits should
* take this into consideration and allow a `permit` call to fail. Combining these two aspects, a pattern that may be
* generally recommended is:
*
* ```solidity
* function doThingWithPermit(..., uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public {
* try token.permit(msg.sender, address(this), value, deadline, v, r, s) {} catch {}
* doThing(..., value);
* }
*
* function doThing(..., uint256 value) public {
* token.safeTransferFrom(msg.sender, address(this), value);
* ...
* }
* ```
*
* Observe that: 1) `msg.sender` is used as the owner, leaving no ambiguity as to the signer intent, and 2) the use of
* `try/catch` allows the permit to fail and makes the code tolerant to frontrunning. (See also
* {SafeERC20-safeTransferFrom}).
*
* Additionally, note that smart contract wallets (such as Argent or Safe) are not able to produce permit signatures, so
* contracts should have entry points that don't rely on permit.
*/
interface IERC20Permit {
/**
* @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
* given ``owner``'s signed approval.
*
* IMPORTANT: The same issues {IERC20-approve} has related to transaction
* ordering also apply here.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `deadline` must be a timestamp in the future.
* - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
* over the EIP712-formatted function arguments.
* - the signature must use ``owner``'s current nonce (see {nonces}).
*
* For more information on the signature format, see the
* https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
* section].
*
* CAUTION: See Security Considerations above.
*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
/**
* @dev Returns the current nonce for `owner`. This value must be
* included whenever a signature is generated for {permit}.
*
* Every successful call to {permit} increases ``owner``'s nonce by one. This
* prevents a signature from being used multiple times.
*/
function nonces(address owner) external view returns (uint256);
/**
* @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
*/
// solhint-disable-next-line func-name-mixedcase
function DOMAIN_SEPARATOR() external view returns (bytes32);
}
IERC20.sol 78 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 amount) external returns (bool);
}
SafeERC20.sol 143 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.3) (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.0;
import "../IERC20.sol";
import "../extensions/IERC20Permit.sol";
import "../../../utils/Address.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20 {
using Address for address;
/**
* @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
/**
* @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
* calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
*/
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
/**
* @dev Deprecated. This function has issues similar to the ones found in
* {IERC20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/
function safeApprove(IERC20 token, address spender, uint256 value) internal {
// safeApprove should only be called when setting an initial allowance,
// or when resetting it to zero. To increase and decrease it, use
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
require(
(value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
/**
* @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 oldAllowance = token.allowance(address(this), spender);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance + value));
}
/**
* @dev Decrease the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance - value));
}
}
/**
* @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful. Meant to be used with tokens that require the approval
* to be set to zero before setting it to a non-zero value, such as USDT.
*/
function forceApprove(IERC20 token, address spender, uint256 value) internal {
bytes memory approvalCall = abi.encodeWithSelector(token.approve.selector, spender, value);
if (!_callOptionalReturnBool(token, approvalCall)) {
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0));
_callOptionalReturn(token, approvalCall);
}
}
/**
* @dev Use a ERC-2612 signature to set the `owner` approval toward `spender` on `token`.
* Revert on invalid signature.
*/
function safePermit(
IERC20Permit token,
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
uint256 nonceBefore = token.nonces(owner);
token.permit(owner, spender, value, deadline, v, r, s);
uint256 nonceAfter = token.nonces(owner);
require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/
function _callOptionalReturn(IERC20 token, bytes memory data) private {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
require(returndata.length == 0 || abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*
* This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead.
*/
function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We cannot use {Address-functionCall} here since this should return false
// and not revert is the subcall reverts.
(bool success, bytes memory returndata) = address(token).call(data);
return
success && (returndata.length == 0 || abi.decode(returndata, (bool))) && Address.isContract(address(token));
}
}
Address.sol 244 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/Address.sol)
pragma solidity ^0.8.1;
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
*
* Furthermore, `isContract` will also return true if the target contract within
* the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
* which only has an effect at the end of a transaction.
* ====
*
* [IMPORTANT]
* ====
* You shouldn't rely on `isContract` to protect against flash loan attacks!
*
* Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
* like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
* constructor.
* ====
*/
function isContract(address account) internal view returns (bool) {
// This method relies on extcodesize/address.code.length, which returns 0
// for contracts in construction, since the code is only stored at the end
// of the constructor execution.
return account.code.length > 0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.8.0/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
* the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
*
* _Available since v4.8._
*/
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata,
string memory errorMessage
) internal view returns (bytes memory) {
if (success) {
if (returndata.length == 0) {
// only check isContract if the call was successful and the return data is empty
// otherwise we already know that it was a contract
require(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
/**
* @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason or using the provided one.
*
* _Available since v4.3._
*/
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function _revert(bytes memory returndata, string memory errorMessage) private pure {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
/// @solidity memory-safe-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
Context.sol 28 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.4) (utils/Context.sol)
pragma solidity ^0.8.0;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
IUniswapV3SwapCallback.sol 21 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Callback for IUniswapV3PoolActions#swap
/// @notice Any contract that calls IUniswapV3PoolActions#swap must implement this interface
interface IUniswapV3SwapCallback {
/// @notice Called to `msg.sender` after executing a swap via IUniswapV3Pool#swap.
/// @dev In the implementation you must pay the pool tokens owed for the swap.
/// The caller of this method must be checked to be a UniswapV3Pool deployed by the canonical UniswapV3Factory.
/// amount0Delta and amount1Delta can both be 0 if no tokens were swapped.
/// @param amount0Delta The amount of token0 that was sent (negative) or must be received (positive) by the pool by
/// the end of the swap. If positive, the callback must send that amount of token0 to the pool.
/// @param amount1Delta The amount of token1 that was sent (negative) or must be received (positive) by the pool by
/// the end of the swap. If positive, the callback must send that amount of token1 to the pool.
/// @param data Any data passed through by the caller via the IUniswapV3PoolActions#swap call
function uniswapV3SwapCallback(
int256 amount0Delta,
int256 amount1Delta,
bytes calldata data
) external;
}
ISwapRouter.sol 67 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.7.5;
pragma abicoder v2;
import '@uniswap/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol';
/// @title Router token swapping functionality
/// @notice Functions for swapping tokens via Uniswap V3
interface ISwapRouter is IUniswapV3SwapCallback {
struct ExactInputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 deadline;
uint256 amountIn;
uint256 amountOutMinimum;
uint160 sqrtPriceLimitX96;
}
/// @notice Swaps `amountIn` of one token for as much as possible of another token
/// @param params The parameters necessary for the swap, encoded as `ExactInputSingleParams` in calldata
/// @return amountOut The amount of the received token
function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut);
struct ExactInputParams {
bytes path;
address recipient;
uint256 deadline;
uint256 amountIn;
uint256 amountOutMinimum;
}
/// @notice Swaps `amountIn` of one token for as much as possible of another along the specified path
/// @param params The parameters necessary for the multi-hop swap, encoded as `ExactInputParams` in calldata
/// @return amountOut The amount of the received token
function exactInput(ExactInputParams calldata params) external payable returns (uint256 amountOut);
struct ExactOutputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 deadline;
uint256 amountOut;
uint256 amountInMaximum;
uint160 sqrtPriceLimitX96;
}
/// @notice Swaps as little as possible of one token for `amountOut` of another token
/// @param params The parameters necessary for the swap, encoded as `ExactOutputSingleParams` in calldata
/// @return amountIn The amount of the input token
function exactOutputSingle(ExactOutputSingleParams calldata params) external payable returns (uint256 amountIn);
struct ExactOutputParams {
bytes path;
address recipient;
uint256 deadline;
uint256 amountOut;
uint256 amountInMaximum;
}
/// @notice Swaps as little as possible of one token for `amountOut` of another along the specified path (reversed)
/// @param params The parameters necessary for the multi-hop swap, encoded as `ExactOutputParams` in calldata
/// @return amountIn The amount of the input token
function exactOutput(ExactOutputParams calldata params) external payable returns (uint256 amountIn);
}
BondToken.sol 762 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import "./interfaces/IUniswapV3Pool.sol";
interface ISMSFund {
function getWBGLPrice() external view returns (uint256);
function getCurrentAPY() external view returns (uint256);
function getCollateralRatio() external view returns (uint256);
function buckets(uint256 index) external view returns (uint256);
function accumulatedDebt() external view returns (uint256);
function mintSMSFromBond(address user, uint256 USDamount) external;
}
contract BondToken is ERC20, Ownable, ReentrancyGuard {
using SafeERC20 for IERC20;
ISMSFund public immutable smsFund;
// Token addresses from SMSFund
address public constant WBGL = 0x2bA64EFB7A4Ec8983E22A49c81fa216AC33f383A;
address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address public constant LUSD = 0x5f98805A4E8be255a32880FDeC7F6728C6568bA0;
address public constant USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7;
address public constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address public constant DAI = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
ISwapRouter public constant router = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);
struct UserInfo {
uint256 amount;
uint256 rewardDebt;
uint256 lastUpdateTime;
}
struct DepositAuction {
address depositor;
address stablecoin;
uint256 stablecoinAmount;
uint256 auctionDuration;
uint256 startTime;
uint256 endTime;
address bestBidder;
uint256 bestBidAmount; // WBGL amount
bool finished;
bool executed;
mapping(address => uint256) bids;
address[] bidders;
}
struct RedeemAuction {
address redeemer;
address stablecoin;
uint256 bondAmount;
uint256 wbglAmount;
uint256 auctionDuration;
uint256 startTime;
uint256 endTime;
address bestBidder;
uint256 bestBidAmount; // Stablecoin amount
bool finished;
bool executed;
mapping(address => uint256) bids;
address[] bidders;
}
mapping(address => UserInfo) public userInfo;
mapping(uint256 => DepositAuction) public depositAuctions;
mapping(uint256 => RedeemAuction) public redeemAuctions;
uint256 public nextDepositAuctionId;
uint256 public nextRedeemAuctionId;
uint256 public accRewardPerShare;
uint256 public lastRewardTime;
uint256 private constant PRECISION = 1e18;
uint256 private constant YEAR_SECONDS = 365 days;
uint256 private constant MAX_AUCTION_DURATION = 24 hours;
uint256 private constant USDC_PRECISION = 1e6;
event Deposit(address indexed user, uint256 wbglAmount, uint256 bondAmount);
event Withdraw(address indexed user, uint256 bondAmount, uint256 wbglAmount);
event RewardClaimed(address indexed user, uint256 amount);
event DepositAuctionCreated(uint256 indexed auctionId, address indexed depositor, address stablecoin, uint256 amount, uint256 duration);
event DepositAuctionBid(uint256 indexed auctionId, address indexed bidder, uint256 wbglAmount);
event DepositAuctionFinished(uint256 indexed auctionId, address indexed winner, uint256 wbglAmount);
event RedeemAuctionCreated(uint256 indexed auctionId, address indexed redeemer, address stablecoin, uint256 bondAmount, uint256 duration);
event RedeemAuctionBid(uint256 indexed auctionId, address indexed bidder, uint256 stablecoinAmount);
event RedeemAuctionFinished(uint256 indexed auctionId, address indexed winner, uint256 stablecoinAmount);
event BidRetracted(uint256 indexed auctionId, address indexed bidder, uint256 amount, bool isDeposit);
constructor(address _smsFund) ERC20("Bond Token", "BOND") {
smsFund = ISMSFund(_smsFund);
lastRewardTime = block.timestamp;
}
modifier onlySMSFund() {
require(msg.sender == address(smsFund), "Only SMS Fund can call");
_;
}
modifier validStablecoin(address stablecoin) {
require(
stablecoin == USDT ||
stablecoin == USDC ||
stablecoin == DAI ||
stablecoin == LUSD,
"Invalid stablecoin"
);
_;
}
function updatePool() public {
if (block.timestamp <= lastRewardTime) {
return;
}
uint256 bondSupply = totalSupply();
if (bondSupply == 0) {
lastRewardTime = block.timestamp;
return;
}
uint256 timeElapsed = block.timestamp - lastRewardTime;
uint256 apy = smsFund.getCurrentAPY();
uint256 reward = (bondSupply * apy * timeElapsed) / (100 * YEAR_SECONDS);
uint256 increment = (reward * PRECISION);
increment = increment / bondSupply;
accRewardPerShare += increment;
lastRewardTime = block.timestamp;
}
function pendingReward(address _user) public view returns (uint256) {
UserInfo storage user = userInfo[_user];
uint256 _accRewardPerShare = accRewardPerShare;
uint256 bondSupply = totalSupply();
if (block.timestamp > lastRewardTime && bondSupply != 0) {
uint256 timeElapsed = block.timestamp - lastRewardTime;
uint256 apy = smsFund.getCurrentAPY();
uint256 reward = (bondSupply * apy * timeElapsed) / (100 * YEAR_SECONDS);
_accRewardPerShare += (reward * PRECISION) / bondSupply;
}
return (user.amount * _accRewardPerShare) / PRECISION - user.rewardDebt;
}
function pendingTotalRewards() external view returns (uint256) {
uint256 _accRewardPerShare = accRewardPerShare;
if (block.timestamp > lastRewardTime && totalSupply() != 0) {
uint256 timeElapsed = block.timestamp - lastRewardTime;
uint256 apy = smsFund.getCurrentAPY();
uint256 reward = (totalSupply() * apy * timeElapsed) / (100 * YEAR_SECONDS);
_accRewardPerShare += (reward * PRECISION);
}
return (_accRewardPerShare) / PRECISION;
}
function burnBondForSMS(uint256 bondAmount) external nonReentrant {
require(bondAmount > 0, "Bond amount must be greater than 0");
require(balanceOf(msg.sender) >= bondAmount, "Insufficient BOND balance");
updatePool();
UserInfo storage user = userInfo[msg.sender];
require(user.amount >= bondAmount, "Insufficient user bond amount");
// Calculate pending rewards for the amount being burned
uint256 pendingRewardsForBurn = pendingReward(msg.sender);
// Update user info before burning
user.amount -= bondAmount;
user.rewardDebt = (user.amount * accRewardPerShare) / PRECISION;
// Burn the BOND tokens
_burn(msg.sender, bondAmount);
uint256 valueInUSD = (bondAmount + pendingRewardsForBurn);
// Call SMS Fund to mint SMS tokens
smsFund.mintSMSFromBond(msg.sender, valueInUSD);
}
function depositWBGL(uint256 amount) external nonReentrant {
require(amount > 0, "Amount must be greater than 0");
require(IERC20(WBGL).transferFrom(msg.sender, address(smsFund), amount), "Transfer failed");
updatePool();
UserInfo storage user = userInfo[msg.sender];
if (user.amount > 0) {
uint256 pending = (user.amount * accRewardPerShare) / PRECISION - user.rewardDebt;
if (pending > 0) {
user.rewardDebt += pending;
}
}
uint256 wbglPrice = smsFund.getWBGLPrice();
uint256 bondAmount = (amount * wbglPrice) / PRECISION;
user.amount += bondAmount;
user.rewardDebt = (user.amount * accRewardPerShare) / PRECISION;
user.lastUpdateTime = block.timestamp;
_mint(msg.sender, bondAmount);
emit Deposit(msg.sender, amount, bondAmount);
}
function depositStablecoin(address stablecoin, uint256 amount) external validStablecoin(stablecoin) nonReentrant {
depositStablecoinWithAuction(stablecoin, amount, 0);
}
function depositStablecoinWithAuction(address stablecoin, uint256 amount, uint256 auctionDuration) public validStablecoin(stablecoin) nonReentrant {
require(amount > 0, "Amount must be greater than 0");
require(auctionDuration <= MAX_AUCTION_DURATION, "Auction duration too long");
require(IERC20(stablecoin).transferFrom(msg.sender, address(this), amount), "Transfer failed");
uint256 auctionId = nextDepositAuctionId++;
DepositAuction storage auction = depositAuctions[auctionId];
auction.depositor = msg.sender;
auction.stablecoin = stablecoin;
auction.stablecoinAmount = amount;
auction.auctionDuration = auctionDuration;
auction.startTime = block.timestamp;
auction.endTime = block.timestamp + auctionDuration;
emit DepositAuctionCreated(auctionId, msg.sender, stablecoin, amount, auctionDuration);
// If auction duration is 0, finish immediately
if (auctionDuration == 0) {
_finishDepositAuction(auctionId);
}
}
function bidOnDepositAuction(uint256 auctionId, uint256 wbglAmount) external nonReentrant {
DepositAuction storage auction = depositAuctions[auctionId];
require(!auction.finished, "Auction already finished");
require(block.timestamp < auction.endTime, "Auction ended");
require(wbglAmount > auction.bestBidAmount, "Bid too low");
require(IERC20(WBGL).transferFrom(msg.sender, address(this), wbglAmount), "Transfer failed");
// Return previous bid if exists
if (auction.bids[msg.sender] > 0) {
require(IERC20(WBGL).transfer(msg.sender, auction.bids[msg.sender]), "Return failed");
} else {
auction.bidders.push(msg.sender);
}
// Return previous best bid
if (auction.bestBidder != address(0)) {
require(IERC20(WBGL).transfer(auction.bestBidder, auction.bestBidAmount), "Return failed");
}
auction.bids[msg.sender] = wbglAmount;
auction.bestBidder = msg.sender;
auction.bestBidAmount = wbglAmount;
emit DepositAuctionBid(auctionId, msg.sender, wbglAmount);
}
function finishDepositAuction(uint256 auctionId) external nonReentrant {
_finishDepositAuction(auctionId);
}
function _finishDepositAuction(uint256 auctionId) internal {
DepositAuction storage auction = depositAuctions[auctionId];
require(!auction.finished, "Auction already finished");
require(block.timestamp >= auction.endTime, "Auction not ended");
auction.finished = true;
uint256 wbglAmount;
address winner;
if (auction.bestBidder != address(0)) {
// Auction had bids
wbglAmount = auction.bestBidAmount;
winner = auction.bestBidder;
// Transfer stablecoin to winner
require(IERC20(auction.stablecoin).transfer(winner, auction.stablecoinAmount), "Transfer failed");
// Transfer WBGL to SMS Fund
require(IERC20(WBGL).transfer(address(smsFund), wbglAmount), "Transfer failed");
} else {
// No bids, execute Uniswap trade
wbglAmount = _executeStablecoinToWBGLSwap(auction.stablecoin, auction.stablecoinAmount);
winner = address(this); // Contract executed the trade
}
// Mint BOND tokens to depositor
_mintBondTokens(auction.depositor, wbglAmount);
emit DepositAuctionFinished(auctionId, winner, wbglAmount);
}
function redeemBond(uint256 bondAmount) external nonReentrant {
redeemBondWithAuction(bondAmount, USDT, 0);
}
function redeemBondWithAuction(uint256 bondAmount, address stablecoin, uint256 auctionDuration) public validStablecoin(stablecoin) nonReentrant {
require(balanceOf(msg.sender) >= bondAmount, "Insufficient BOND balance");
require(auctionDuration <= MAX_AUCTION_DURATION, "Auction duration too long");
uint256 cr = smsFund.getCollateralRatio();
require(cr >= (11 * PRECISION) / 10, "CR must be >= 1.1 for redemption");
// Calculate WBGL amount to be redeemed
uint256 wbglPrice = smsFund.getWBGLPrice();
uint256 wbglAmount = (bondAmount * PRECISION) / wbglPrice;
// Check if first bucket has enough WBGL
uint256 firstBucketAmount = smsFund.buckets(0);
require(firstBucketAmount >= wbglAmount, "Not enough WBGL in first bucket");
// Burn BOND tokens
_burn(msg.sender, bondAmount);
uint256 auctionId = nextRedeemAuctionId++;
RedeemAuction storage auction = redeemAuctions[auctionId];
auction.redeemer = msg.sender;
auction.stablecoin = stablecoin;
auction.bondAmount = bondAmount;
auction.wbglAmount = wbglAmount;
auction.auctionDuration = auctionDuration;
auction.startTime = block.timestamp;
auction.endTime = block.timestamp + auctionDuration;
emit RedeemAuctionCreated(auctionId, msg.sender, stablecoin, bondAmount, auctionDuration);
// If auction duration is 0, finish immediately
if (auctionDuration == 0) {
_finishRedeemAuction(auctionId);
}
}
function bidOnRedeemAuction(uint256 auctionId, uint256 stablecoinAmount) external nonReentrant {
RedeemAuction storage auction = redeemAuctions[auctionId];
require(!auction.finished, "Auction already finished");
require(block.timestamp < auction.endTime, "Auction ended");
require(stablecoinAmount > auction.bestBidAmount, "Bid too low");
require(IERC20(auction.stablecoin).transferFrom(msg.sender, address(this), stablecoinAmount), "Transfer failed");
// Return previous bid if exists
if (auction.bids[msg.sender] > 0) {
require(IERC20(auction.stablecoin).transfer(msg.sender, auction.bids[msg.sender]), "Return failed");
} else {
auction.bidders.push(msg.sender);
}
// Return previous best bid
if (auction.bestBidder != address(0)) {
require(IERC20(auction.stablecoin).transfer(auction.bestBidder, auction.bestBidAmount), "Return failed");
}
auction.bids[msg.sender] = stablecoinAmount;
auction.bestBidder = msg.sender;
auction.bestBidAmount = stablecoinAmount;
emit RedeemAuctionBid(auctionId, msg.sender, stablecoinAmount);
}
function finishRedeemAuction(uint256 auctionId) external nonReentrant {
_finishRedeemAuction(auctionId);
}
function _finishRedeemAuction(uint256 auctionId) internal {
RedeemAuction storage auction = redeemAuctions[auctionId];
require(!auction.finished, "Auction already finished");
require(block.timestamp >= auction.endTime, "Auction not ended");
auction.finished = true;
uint256 stablecoinAmount;
address winner;
if (auction.bestBidder != address(0)) {
// Auction had bids
stablecoinAmount = auction.bestBidAmount;
winner = auction.bestBidder;
// Transfer WBGL to winner (from SMS Fund first bucket)
require(IERC20(WBGL).transferFrom(address(smsFund), winner, auction.wbglAmount), "Transfer failed");
// Transfer stablecoin to redeemer
require(IERC20(auction.stablecoin).transfer(auction.redeemer, stablecoinAmount), "Transfer failed");
} else {
// No bids, execute Uniswap trade
stablecoinAmount = _executeWBGLToStablecoinSwap(auction.wbglAmount, auction.stablecoin);
winner = address(this); // Contract executed the trade
// Transfer stablecoin to redeemer
require(IERC20(auction.stablecoin).transfer(auction.redeemer, stablecoinAmount), "Transfer failed");
}
emit RedeemAuctionFinished(auctionId, winner, stablecoinAmount);
}
function retractDepositBid(uint256 auctionId) external nonReentrant {
DepositAuction storage auction = depositAuctions[auctionId];
require(auction.finished, "Auction not finished");
require(auction.bids[msg.sender] > 0, "No bid to retract");
require(msg.sender != auction.bestBidder, "Cannot retract winning bid");
uint256 bidAmount = auction.bids[msg.sender];
auction.bids[msg.sender] = 0;
require(IERC20(WBGL).transfer(msg.sender, bidAmount), "Transfer failed");
emit BidRetracted(auctionId, msg.sender, bidAmount, true);
}
function retractRedeemBid(uint256 auctionId) external nonReentrant {
RedeemAuction storage auction = redeemAuctions[auctionId];
require(auction.finished, "Auction not finished");
require(auction.bids[msg.sender] > 0, "No bid to retract");
require(msg.sender != auction.bestBidder, "Cannot retract winning bid");
uint256 bidAmount = auction.bids[msg.sender];
auction.bids[msg.sender] = 0;
require(IERC20(auction.stablecoin).transfer(msg.sender, bidAmount), "Transfer failed");
emit BidRetracted(auctionId, msg.sender, bidAmount, false);
}
function _executeStablecoinToWBGLSwap(address stablecoin, uint256 amount) internal returns (uint256 amountOut) {
// Swap stablecoin to WBGL using the same logic as SMSFund
uint256 allowance = IERC20(stablecoin).allowance(address(this), address(router));
if (allowance < amount) {
uint256 newAllowance = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
IERC20(stablecoin).safeIncreaseAllowance(address(router), newAllowance - allowance);
}
if (stablecoin == USDT || stablecoin == USDC) {
// First swap stablecoin to WETH
ISwapRouter.ExactInputParams memory params = ISwapRouter.ExactInputParams({
path: abi.encodePacked(
stablecoin,
uint24(100), // 0.01% fee
WETH
),
recipient: address(this),
deadline: block.timestamp,
amountIn: amount,
amountOutMinimum: 0
});
uint256 wethOut = ISwapRouter(router).exactInput(params);
allowance = IERC20(WETH).allowance(address(this), address(router));
if (allowance < wethOut) {
uint256 newAllowance = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
IERC20(WETH).safeIncreaseAllowance(address(router), newAllowance - allowance);
}
// swap WETH to WBGL
params = ISwapRouter.ExactInputParams({
path: abi.encodePacked(
WETH,
uint24(100), // 0.01% fee
WBGL
),
recipient: address(smsFund),
deadline: block.timestamp,
amountIn: wethOut,
amountOutMinimum: 0
});
amountOut = ISwapRouter(router).exactInput(params);
} else if (stablecoin == LUSD || stablecoin == DAI) {
// First swap LUSD/DAI to USDC
uint24 poolFee = 100;
if (stablecoin == LUSD) {
poolFee = 500;
}
ISwapRouter.ExactInputParams memory params = ISwapRouter.ExactInputParams({
path: abi.encodePacked(
stablecoin,
poolFee,
USDC
),
recipient: address(this),
deadline: block.timestamp,
amountIn: amount,
amountOutMinimum: 0
});
uint256 usdcOut = ISwapRouter(router).exactInput(params);
allowance = IERC20(USDC).allowance(address(this), address(router));
if (allowance < usdcOut) {
uint256 newAllowance = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
IERC20(USDC).safeIncreaseAllowance(address(router), newAllowance - allowance);
}
// swap USDC to WETH
params = ISwapRouter.ExactInputParams({
path: abi.encodePacked(
USDC,
uint24(100), // 0.01% fee
WETH
),
recipient: address(this),
deadline: block.timestamp,
amountIn: usdcOut,
amountOutMinimum: 0
});
uint256 wethOut = ISwapRouter(router).exactInput(params);
allowance = IERC20(WETH).allowance(address(this), address(router));
if (allowance < wethOut) {
uint256 newAllowance = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
IERC20(WETH).safeIncreaseAllowance(address(router), newAllowance - allowance);
}
// swap WETH to WBGL
params = ISwapRouter.ExactInputParams({
path: abi.encodePacked(
WETH,
uint24(100), // 0.01% fee
WBGL
),
recipient: address(smsFund),
deadline: block.timestamp,
amountIn: wethOut,
amountOutMinimum: 0
});
amountOut = ISwapRouter(router).exactInput(params);
}
}
function _executeWBGLToStablecoinSwap(uint256 wbglAmount, address stablecoin) internal returns (uint256 amountOut) {
// Get WBGL from SMS Fund first bucket
require(IERC20(WBGL).transferFrom(address(smsFund), address(this), wbglAmount), "Transfer failed");
uint256 allowance = IERC20(WBGL).allowance(address(this), address(router));
if (allowance < wbglAmount) {
uint256 newAllowance = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
IERC20(WBGL).safeIncreaseAllowance(address(router), newAllowance - allowance);
}
if (stablecoin == USDT || stablecoin == USDC) {
// First swap WBGL to WETH
ISwapRouter.ExactInputParams memory params = ISwapRouter.ExactInputParams({
path: abi.encodePacked(
WBGL,
uint24(100), // 0.01% fee
WETH
),
recipient: address(this),
deadline: block.timestamp,
amountIn: wbglAmount,
amountOutMinimum: 0
});
uint256 wethOut = ISwapRouter(router).exactInput(params);
allowance = IERC20(WETH).allowance(address(this), address(router));
if (allowance < wethOut) {
uint256 newAllowance = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
IERC20(WETH).safeIncreaseAllowance(address(router), newAllowance - allowance);
}
// swap WETH to stablecoin
params = ISwapRouter.ExactInputParams({
path: abi.encodePacked(
WETH,
uint24(100), // 0.01% fee
stablecoin
),
recipient: address(this),
deadline: block.timestamp,
amountIn: wethOut,
amountOutMinimum: 0
});
amountOut = ISwapRouter(router).exactInput(params);
} else if (stablecoin == LUSD || stablecoin == DAI) {
// First swap WBGL to WETH
ISwapRouter.ExactInputParams memory params = ISwapRouter.ExactInputParams({
path: abi.encodePacked(
WBGL,
uint24(100), // 0.01% fee
WETH
),
recipient: address(this),
deadline: block.timestamp,
amountIn: wbglAmount,
amountOutMinimum: 0
});
uint256 wethOut = ISwapRouter(router).exactInput(params);
allowance = IERC20(WETH).allowance(address(this), address(router));
if (allowance < wethOut) {
uint256 newAllowance = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
IERC20(WETH).safeIncreaseAllowance(address(router), newAllowance - allowance);
}
// swap WETH to USDC
params = ISwapRouter.ExactInputParams({
path: abi.encodePacked(
WETH,
uint24(100), // 0.01% fee
USDC
),
recipient: address(this),
deadline: block.timestamp,
amountIn: wethOut,
amountOutMinimum: 0
});
uint256 usdcOut = ISwapRouter(router).exactInput(params);
allowance = IERC20(USDC).allowance(address(this), address(router));
if (allowance < usdcOut) {
uint256 newAllowance = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
IERC20(USDC).safeIncreaseAllowance(address(router), newAllowance - allowance);
}
// swap USDC to final stablecoin
uint24 poolFee = 100;
if (stablecoin == LUSD) {
poolFee = 500;
}
params = ISwapRouter.ExactInputParams({
path: abi.encodePacked(
USDC,
poolFee,
stablecoin
),
recipient: address(this),
deadline: block.timestamp,
amountIn: usdcOut,
amountOutMinimum: 0
});
amountOut = ISwapRouter(router).exactInput(params);
}
}
function _mintBondTokens(address to, uint256 wbglAmount) internal {
updatePool();
UserInfo storage user = userInfo[to];
if (user.amount > 0) {
uint256 pending = (user.amount * accRewardPerShare) / PRECISION - user.rewardDebt;
if (pending > 0) {
user.rewardDebt += pending;
}
}
uint256 bondAmount = wbglAmount * 1e18 / smsFund.getCollateralRatio(); // 1:1 for stablecoins
user.amount += bondAmount;
user.rewardDebt = (user.amount * accRewardPerShare) / PRECISION;
user.lastUpdateTime = block.timestamp;
_mint(to, bondAmount);
emit Deposit(to, 0, bondAmount);
}
function claimRewards() external nonReentrant {
updatePool();
UserInfo storage user = userInfo[msg.sender];
uint256 pending = (user.amount * accRewardPerShare) / PRECISION - user.rewardDebt;
require(pending > 0, "No rewards to claim");
user.rewardDebt = (user.amount * accRewardPerShare) / PRECISION;
emit RewardClaimed(msg.sender, pending);
}
function updateDebt() external onlySMSFund {
updatePool();
}
function getDepositAuctionInfo(uint256 auctionId) external view returns (
address depositor,
address stablecoin,
uint256 stablecoinAmount,
uint256 startTime,
uint256 endTime,
address bestBidder,
uint256 bestBidAmount,
bool finished
) {
DepositAuction storage auction = depositAuctions[auctionId];
return (
auction.depositor,
auction.stablecoin,
auction.stablecoinAmount,
auction.startTime,
auction.endTime,
auction.bestBidder,
auction.bestBidAmount,
auction.finished
);
}
function getRedeemAuctionInfo(uint256 auctionId) external view returns (
address redeemer,
address stablecoin,
uint256 bondAmount,
uint256 wbglAmount,
uint256 startTime,
uint256 endTime,
address bestBidder,
uint256 bestBidAmount,
bool finished
) {
RedeemAuction storage auction = redeemAuctions[auctionId];
return (
auction.redeemer,
auction.stablecoin,
auction.bondAmount,
auction.wbglAmount,
auction.startTime,
auction.endTime,
auction.bestBidder,
auction.bestBidAmount,
auction.finished
);
}
function getDepositAuctionBid(uint256 auctionId, address bidder) external view returns (uint256) {
return depositAuctions[auctionId].bids[bidder];
}
function getRedeemAuctionBid(uint256 auctionId, address bidder) external view returns (uint256) {
return redeemAuctions[auctionId].bids[bidder];
}
}
IUniswapV3Pool.sol 53 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IUniswapV3Pool {
function factory() external view returns (address);
function token0() external view returns (address);
function token1() external view returns (address);
function fee() external view returns (uint24);
function tickSpacing() external view returns (int24);
function maxLiquidityPerTick() external view returns (uint128);
function slot0()
external
view
returns (
uint160 sqrtPriceX96,
int24 tick,
uint16 observationIndex,
uint16 observationCardinality,
uint16 observationCardinalityNext,
uint8 feeProtocol,
bool unlocked
);
function liquidity() external view returns (uint128);
function observe(uint32[] calldata secondsAgos)
external
view
returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s);
function swap(
address recipient,
bool zeroForOne,
int256 amountSpecified,
uint160 sqrtPriceLimitX96,
bytes calldata data
) external returns (int256 amount0, int256 amount1);
function mint(
address recipient,
int24 tickLower,
int24 tickUpper,
uint128 amount,
bytes calldata data
) external returns (uint256 amount0, uint256 amount1);
function burn(
int24 tickLower,
int24 tickUpper,
uint128 amount
) external returns (uint256 amount0, uint256 amount1);
}
SMSFund.sol 384 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import "./interfaces/IUniswapV3Pool.sol";
import "./BondToken.sol";
contract SMSFund is ERC20, Ownable, ReentrancyGuard {
using SafeERC20 for IERC20;
IUniswapV3Pool public constant wbglEthPool = IUniswapV3Pool(0x982152A6C7f732Ec7C9EA998dDD9Ebde00Dfa16e);
IUniswapV3Pool public constant usdcEthPool = IUniswapV3Pool(0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640);
BondToken public bondToken;
address public constant WBGL = 0x2bA64EFB7A4Ec8983E22A49c81fa216AC33f383A;
address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; // all is swapped through WETH (most liquid)
address public constant LUSD = 0x5f98805A4E8be255a32880FDeC7F6728C6568bA0;
address public constant USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7;
address public constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address public constant DAI = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
ISwapRouter public constant router = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); // Mainnet UNI router
// Buckets for WBGL distribution
uint256[5] public buckets;
uint256[5] public bucketProportions = [24, 22, 20, 18, 16]; // in percentage
uint256 public firstDepositTimestamp;
uint256 public lastRebalanceTimestamp;
//uint256 public accumulatedDebt;
uint256 private constant PRECISION = 1e18;
uint256 private constant YEAR_SECONDS = 365 days;
uint256 private constant USDC_DECIMALS = 6;
uint256 private constant USDC_PRECISION = 1e6;
uint256 private constant Q96 = 2**96;
event Deposit(address indexed user, uint256 wbglAmount, uint256 smsAmount);
event Redeem(address indexed user, uint256 smsAmount, uint256 wbglAmount);
event Rebalance(uint256 timestamp);
event SMSMintedFromBond(address indexed user, uint256 usdAmount, uint256 smsAmount);
constructor() ERC20("Saylor Moon Shares", "SMS") {
bondToken = new BondToken(address(this));
}
function getWBGLPrice() public view returns (uint256) {
// Get WBGL/ETH price
(uint160 sqrtPriceX96_WBGL_ETH,,,,,,) = wbglEthPool.slot0();
// Get USDC/ETH price
(uint160 sqrtPriceX96_USDC_ETH,,,,,,) = usdcEthPool.slot0();
require(sqrtPriceX96_WBGL_ETH > 0, "Invalid WBGL/ETH price");
require(sqrtPriceX96_USDC_ETH > 0, "Invalid USDC/ETH price");
// Calculate WBGL price in ETH
// price = (sqrtPriceX96 / 2^96)^2
// To avoid overflow, we use: price = sqrtPriceX96^2 / 2^192
// But we want to maintain precision, so we multiply by PRECISION first
uint256 wbglPriceInEth = (uint256(sqrtPriceX96_WBGL_ETH) * uint256(sqrtPriceX96_WBGL_ETH) * PRECISION) / (Q96 * Q96);
// Calculate ETH price in USDC
// For USDC/ETH pool, token0 is USDC, token1 is WETH
// sqrtPriceX96 = sqrt(token1/token0) = sqrt(WETH/USDC)
// So ETH price in USDC = (sqrtPriceX96)^2
uint256 ethPriceInUsdc = (Q96 * Q96 * PRECISION) / (uint256(sqrtPriceX96_USDC_ETH) * uint256(sqrtPriceX96_USDC_ETH));
// Calculate WBGL price in USD (18 decimals)
// WBGL/USD = (WBGL/ETH) * (ETH/USDC) * (USDC scale factor)
uint256 wbglPriceInUsd = (wbglPriceInEth * ethPriceInUsdc) / (USDC_PRECISION);
// Ensure we return a reasonable price (not zero)
require(wbglPriceInUsd > 0, "Calculated price is zero");
return wbglPriceInUsd;
}
function getTotalUSDValue() public view returns (uint256) {
uint256 totalWBGL = 0;
for (uint i = 0; i < 5; i++) {
totalWBGL += buckets[i];
}
return (totalWBGL * getWBGLPrice()) / PRECISION;
}
function getCurrentAPY() public view returns (uint256) {
uint256 totalUSD = getTotalUSDValue();
if (totalUSD < 1000 * PRECISION) return 50; // 50%
if (totalUSD < 10000 * PRECISION) return 40; // 40%
if (totalUSD < 100000 * PRECISION) return 30; // 30%
if (totalUSD < 1000000 * PRECISION) return 25; // 25%
return 20; // 20%
}
function getCollateralRatio() public view returns (uint256) {
uint256 totalWBGL = 0;
for (uint i = 0; i < 5; i++) {
totalWBGL += buckets[i];
}
if (totalSupply() == 0) return PRECISION;
uint256 totalValue = (totalWBGL * getWBGLPrice() * (100 + getCurrentAPY())) / (100 * PRECISION);
uint256 accumulatedDebt = bondToken.pendingTotalRewards();
if (totalValue <= accumulatedDebt) return 0;
return ((totalValue - accumulatedDebt) * PRECISION) / totalSupply();
//return totalValue * PRECISION / totalSupply();
}
// price calculation for mint purposes
function getSMSPrice() public view returns (uint256) {
if (totalSupply() == 0) {
// Initial price calculation
return 1e18 * 100 / (100 + getCurrentAPY()); // (PRECISION_USDC / getWBGLPrice() * (100 + getCurrentAPY())) / 100;
}
uint256 cr = getCollateralRatio();
if (cr < (2 * PRECISION) / 10) {
return (2 * PRECISION) / 10; // cannot be less than 20%, floor price protection
}
if (cr < (8 * PRECISION) / 10) {
return (cr * 80) / 100; // 20% discount
}
return cr;
}
function depositWBGL(uint256 amount) external nonReentrant {
require(amount > 0, "Amount must be greater than 0");
require(IERC20(WBGL).transferFrom(msg.sender, address(this), amount), "Transfer failed");
uint256 smsPrice = getSMSPrice();
uint256 smsAmount = (amount * getWBGLPrice()) / smsPrice;
// Handle first deposit
if (firstDepositTimestamp == 0) {
firstDepositTimestamp = block.timestamp;
lastRebalanceTimestamp = block.timestamp;
}
// Handle CR < 0.8 case
uint256 cr = getCollateralRatio();
if (cr < (8 * PRECISION) / 10) {
buckets[0] += amount; // Add 100 WBGL to first bucket
} else {
// Distribute to buckets
_distributeToBuckets(amount);
}
_mint(msg.sender, smsAmount);
bondToken.updatePool();
emit Deposit(msg.sender, amount, smsAmount);
}
function depositStablecoin(address stablecoin, uint256 amount) external nonReentrant {
require(amount > 0, "Amount must be greater than 0");
require(IERC20(stablecoin).transferFrom(msg.sender, address(this), amount), "Transfer failed");
// Swap full amount to WBGL using Uniswap V3
uint256 amountOut;
if (stablecoin == USDT || stablecoin == USDC) {
// First swap stablecoin to WETH
uint256 allowance = IERC20(stablecoin).allowance(address(this), address(router));
if (allowance < amount) {
uint256 newAllowance = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
IERC20(stablecoin).safeIncreaseAllowance(address(router), newAllowance - allowance);
}
ISwapRouter.ExactInputParams memory params = ISwapRouter.ExactInputParams({
path: abi.encodePacked(
stablecoin,
uint24(100), // 0.3% fee - adjust based on your pool's fee tier
WETH
),
recipient: address(this),
deadline: block.timestamp,
amountIn: amount,
amountOutMinimum: 0
});
uint256 wethOut = ISwapRouter(router).exactInput(params);
allowance = IERC20(WETH).allowance(address(this), address(router));
if (allowance < amount) {
uint256 newAllowance = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
IERC20(WETH).safeIncreaseAllowance(address(router), newAllowance - allowance);
}
// swap WETH to WBGL
params = ISwapRouter.ExactInputParams({
path: abi.encodePacked(
WETH,
uint24(100), // 0.01% fee
WBGL
),
recipient: address(this),
deadline: block.timestamp,
amountIn: wethOut,
amountOutMinimum: 0
});
amountOut = ISwapRouter(router).exactInput(params);
} else if (stablecoin == LUSD || stablecoin == DAI) {
// First swap LUSD to USDC
uint256 allowance = IERC20(stablecoin).allowance(address(this), address(router));
if (allowance < amount) {
uint256 newAllowance = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
IERC20(stablecoin).safeIncreaseAllowance(address(router), newAllowance - allowance);
}
uint24 poolFee = 100;
if (stablecoin == LUSD) {
poolFee = 500;
}
ISwapRouter.ExactInputParams memory params = ISwapRouter.ExactInputParams({
path: abi.encodePacked(
stablecoin,
poolFee,
USDC
),
recipient: address(this),
deadline: block.timestamp,
amountIn: amount,
amountOutMinimum: 0
});
uint256 usdcOut = ISwapRouter(router).exactInput(params);
allowance = IERC20(USDC).allowance(address(this), address(router));
if (allowance < amount) {
uint256 newAllowance = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
IERC20(USDC).safeIncreaseAllowance(address(router), newAllowance - allowance);
}
// swap USDC to WETH
params = ISwapRouter.ExactInputParams({
path: abi.encodePacked(
USDC,
uint24(100), // 0.01% fee
WETH
),
recipient: address(this),
deadline: block.timestamp,
amountIn: usdcOut,
amountOutMinimum: 0
});
uint256 wethOut = ISwapRouter(router).exactInput(params);
allowance = IERC20(WETH).allowance(address(this), address(router));
if (allowance < amount) {
uint256 newAllowance = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
IERC20(WETH).safeIncreaseAllowance(address(router), newAllowance - allowance);
}
// swap WETH to WBGL
params = ISwapRouter.ExactInputParams({
path: abi.encodePacked(
WETH,
uint24(100), // 0.01% fee
WBGL
),
recipient: address(this),
deadline: block.timestamp,
amountIn: wethOut,
amountOutMinimum: 0
});
amountOut = ISwapRouter(router).exactInput(params);
}
uint256 smsPrice = getSMSPrice();
uint256 smsAmount = (amountOut * PRECISION) / smsPrice;
// Distribute equivalent WBGL to buckets
_distributeToBuckets(amountOut);
if (firstDepositTimestamp == 0) {
firstDepositTimestamp = block.timestamp;
lastRebalanceTimestamp = block.timestamp;
}
_mint(msg.sender, smsAmount);
bondToken.updateDebt();
emit Deposit(msg.sender, amountOut, smsAmount);
}
function redeemSMS(uint256 smsAmount) external nonReentrant {
require(balanceOf(msg.sender) >= smsAmount, "Insufficient SMS balance");
uint256 cr = getCollateralRatio();
require(cr >= (12 * PRECISION) / 10, "CR must be >= 1.2 for redemption");
uint256 smsPrice = getSMSPrice();
uint256 wbglValue = (smsAmount * smsPrice) / PRECISION;
uint256 wbglAmount = (wbglValue * 80) / (100 * getWBGLPrice()); // 20% penalty
// Check if first bucket has enough WBGL
require(buckets[0] >= wbglAmount, "Insufficient WBGL in first bucket");
// Remove WBGL from first bucket only
buckets[0] -= wbglAmount;
_burn(msg.sender, smsAmount);
require(IERC20(WBGL).transfer(msg.sender, wbglAmount), "Transfer failed");
bondToken.updateDebt();
emit Redeem(msg.sender, smsAmount, wbglAmount);
}
function mintSMSFromBond(address user, uint256 valueInUSD) external nonReentrant {
require(msg.sender == address(bondToken), "Only BondToken can call");
require(valueInUSD > 0, "Value must be greater than 0");
// Convert bond value to SMS tokens
// Bond tokens are valued at 1:1 with USD, so we need to convert to SMS price
uint256 smsPrice = getSMSPrice();
uint256 smsAmount = (valueInUSD * PRECISION) / smsPrice;
// Update accumulated debt to account for the bond value being converted
//accumulatedDebt += totalBondValue;
// Mint SMS tokens to user
_mint(user, smsAmount);
// Update bond token debt tracking
bondToken.updateDebt();
emit SMSMintedFromBond(user, valueInUSD, smsAmount);
}
function rebalance() external {
require(firstDepositTimestamp > 0, "No deposits yet");
require(block.timestamp >= lastRebalanceTimestamp + YEAR_SECONDS, "Too early for rebalance");
uint256 firstBucketAmount = buckets[0];
uint256 redistributeAmount = firstBucketAmount / 3;
// Redistribute first bucket to next three buckets
buckets[0] = 0;
buckets[1] += redistributeAmount;
buckets[2] += redistributeAmount;
buckets[3] += redistributeAmount;
// Shift buckets and add new one at the end
for (uint i = 0; i < 4; i++) {
buckets[i] = buckets[i + 1];
}
buckets[4] = 0;
lastRebalanceTimestamp = block.timestamp;
emit Rebalance(block.timestamp);
}
function _distributeToBuckets(uint256 amount) internal {
for (uint i = 0; i < 5; i++) {
buckets[i] += (amount * bucketProportions[i]) / 100;
}
}
function getBuckets() external view returns (uint256[5] memory) {
return buckets;
}
function canRebalance() external view returns (bool) {
return firstDepositTimestamp > 0 &&
block.timestamp >= lastRebalanceTimestamp + YEAR_SECONDS;
}
}
Read Contract
DAI 0xe0bab4c4 → address
LUSD 0xcf59d563 → address
USDC 0x89a30271 → address
USDT 0xc54e44eb → address
WBGL 0x0fd9eeeb → address
WETH 0xad5c4648 → address
allowance 0xdd62ed3e → uint256
balanceOf 0x70a08231 → uint256
bondToken 0xc28f4392 → address
bucketProportions 0x054b682e → uint256
buckets 0x9b51fb0d → uint256
canRebalance 0xa06880c0 → bool
decimals 0x313ce567 → uint8
firstDepositTimestamp 0x62469353 → uint256
getBuckets 0x69192b33 → uint256[5]
getCollateralRatio 0xcd377c53 → uint256
getCurrentAPY 0x962ca496 → uint256
getSMSPrice 0x91892631 → uint256
getTotalUSDValue 0xaad51b66 → uint256
getWBGLPrice 0x624736e0 → uint256
lastRebalanceTimestamp 0xb83d8157 → uint256
name 0x06fdde03 → string
owner 0x8da5cb5b → address
router 0xf887ea40 → address
symbol 0x95d89b41 → string
totalSupply 0x18160ddd → uint256
usdcEthPool 0x038c33bd → address
wbglEthPool 0xe9d54d39 → address
Write Contract 12 functions
These functions modify contract state and require a wallet transaction to execute.
approve 0x095ea7b3
address spender
uint256 amount
returns: bool
decreaseAllowance 0xa457c2d7
address spender
uint256 subtractedValue
returns: bool
depositStablecoin 0xa587f4f3
address stablecoin
uint256 amount
depositWBGL 0xb66dc5c3
uint256 amount
increaseAllowance 0x39509351
address spender
uint256 addedValue
returns: bool
mintSMSFromBond 0x0e24ecdc
address user
uint256 valueInUSD
rebalance 0x7d7c2a1c
No parameters
redeemSMS 0x976572a7
uint256 smsAmount
renounceOwnership 0x715018a6
No parameters
transfer 0xa9059cbb
address to
uint256 amount
returns: bool
transferFrom 0x23b872dd
address from
address to
uint256 amount
returns: bool
transferOwnership 0xf2fde38b
address newOwner
Recent Transactions
No transactions found for this address