Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# STDLIB_VERSION 3 #-}
- {-# CONTENT_TYPE DAPP #-}
- {-# SCRIPT_TYPE ACCOUNT #-}
- #
- # Smart Contract Implementing Staking and Reward Withdrawal by Users of Neutrino Protocol
- # (RPD is Reward Payouts Distribution)
- #
- #-------------------Base functions----------------------
- func getNumberByKey(key: String) = {
- match getInteger(this, key) {
- case a:Int => a
- case _ => 0
- }
- }
- func getStringByKey(key: String) = {
- match getString(this, key) {
- case a:String => a
- case _ => ""
- }
- }
- func getBoolByKey(key: String) = {
- match getBoolean(this, key) {
- case a:Boolean => a
- case _ => false
- }
- }
- func getNumberByAddressAndKey(address: String, key: String) = {
- match getInteger(addressFromStringValue(address), key) {
- case a:Int => a
- case _ => 0
- }
- }
- func getStringByAddressAndKey(address: Address, key: String) = {
- match getString(address, key) {
- case a:String => a
- case _ => ""
- }
- }
- #-------------------Constructor-------------------------
- let NeutrinoAssetIdKey = "neutrino_asset_id"
- let NeutrinoContractKey = "neutrino_contract"
- #-------------------Keys---------------------------------
- let BalanceKey = "rpd_balance"
- let ControlContractKey = "control_contract"
- let AdminsKey = "admins"
- func getUserBalanceKey(owner: String, assetId: String) = BalanceKey + "_" + assetId + "_" + owner
- func getContractBalanceKey(assetId: String) = BalanceKey + "_" + assetId
- func getExpireProposalKey(hash: String) = "proposal_expire" + "_" + hash
- func getOwnerProposalKey(hash: String) = "proposal_owner" + "_" + hash
- func getArgumentsProposalKey(hash: String) = "proposal_arguments" + "_" + hash
- func getVoteKey(owner: String, hash: String) = "proposal_vote" + "_" + owner + "_" + hash
- #-------------------Convert functions-------------------
- func convertJsonArrayToList(jsonArray: String) = {
- jsonArray.split(",") #jsonArray.take(size(jsonArray)-1).drop(1).split(",")
- }
- #-------------------Global vars-------------------------
- let neutrinoContract = addressFromStringValue(getStringByKey(NeutrinoContractKey))
- let neutrinoAssetId = fromBase58String(getStringByAddressAndKey(neutrinoContract, NeutrinoAssetIdKey))
- #-------------------Get functions----------------------
- func getContractBalance(assetId: String) = getNumberByKey(getContractBalanceKey(assetId))
- func getUserBalance(owner : String, assetId: String) = getNumberByKey(getUserBalanceKey(owner, assetId))
- func getExpireProposal(hash: String) = getNumberByKey(getExpireProposalKey(hash))
- func getOwnerProposal(hash: String) = getStringByKey(getOwnerProposalKey(hash))
- func getArgumentsProposal(hash: String) = getStringByKey(getArgumentsProposalKey(hash))
- func getVote(owner: String, hash: String) = getStringByKey(getVoteKey(owner, hash))
- #-------------------Callable----------------------
- # start neutrino staking
- # [called by user]
- @Callable(i)
- func lockNeutrino() = {
- let pmt = extract(i.payment)
- if (pmt.assetId != neutrinoAssetId)
- then throw("can use neutrino")
- else {
- let account = toString(i.caller)
- let assetIdString = toBase58String(valueOrErrorMessage(pmt.assetId, "parse value error"))
- WriteSet([
- DataEntry(getContractBalanceKey(assetIdString), getContractBalance(assetIdString) + pmt.amount),
- DataEntry(getUserBalanceKey(account, assetIdString), getUserBalance(account, assetIdString) + pmt.amount)
- ])
- }
- }
- # cancel neutrino staking
- # [called by user]
- @Callable(i)
- func unlockNeutrino(unlockAmount: Int, assetIdString: String) = {
- let account = toString(i.caller)
- let assetId = fromBase58String(assetIdString)
- let balance = getUserBalance(account, assetIdString) - unlockAmount
- if (balance < 0)
- then throw("invalid amount")
- else if(assetId != neutrinoAssetId)
- then throw("can use neutrino")
- else {
- ScriptResult(
- WriteSet([
- DataEntry(getContractBalanceKey(assetIdString), getContractBalance(assetIdString) - unlockAmount),
- DataEntry(getUserBalanceKey(account, assetIdString), balance)
- ]),
- TransferSet([ScriptTransfer(addressFromStringValue(account), unlockAmount, neutrinoAssetId)])
- )
- }
- }
- # cancel neutrino staking
- # [called by user]
- @Callable(i)
- func vote(hash: String, indexArgument: Int) = {
- let arguments = getArgumentsProposal(hash).split(",")
- let argument = arguments[indexArgument]
- if(height > getExpireProposal(hash))
- then throw("proposal is expired")
- else {
- WriteSet([
- DataEntry(getVoteKey(toString(i.caller), hash), argument)
- ])
- }
- }
- @Callable(i)
- func createProposal(arguments: String, expairHeight: Int) = {
- let hash = toBase58String(keccak256(toBytes(arguments) + toBytes(expairHeight) + i.callerPublicKey))
- if(getOwnerProposal(hash) != "")
- then throw("proposal is exist")
- else {
- WriteSet([
- DataEntry(getExpireProposalKey(hash), expairHeight),
- DataEntry(getOwnerProposalKey(hash), toString(i.caller)),
- DataEntry(getArgumentsProposalKey(hash), arguments)
- ])
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement