Implementations with price restriction - wavesplatform

I have one question about asset script:
It is possible to create token with script which allow to trade on WavesDex only for 1 usd? If sombody want to trade minimum or maximum of > 1< usd script will block this operation

Yes, you can.
You should get something like this:
let tokenId = base58'token id here'
match tx {
case ex : ExchangeTransaction =>
let selectedToken = ex.sellOrder.assetPair.priceAsset == tokenId || ex.sellOrder.assetPair.amountAsset == tokenId
let selectedPrice = ex.price == your_price_here
selectedToken && selectedPrice
case _ => true
}

Related

Send an email when one cell in a column contains data, but not when it's deleted

I would like an email to be sent when something is added to a cell anywhere in column K, but not when that cell is cleared. Is this possible? Here is what I have so far. The email sends okay when I edit cell K1 (it was limited to this single cell for testing) but it also sends an email when I clear this cell. Thank you for any advice, as you can see I'm mostly just guessing here.
function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet4 = ss.getSheetByName('Copy');
var emailAddress = sheet4.getRange(2,12).getValue();
var subject = sheet4.getRange(2,13).getValue();
var message = sheet4.getRangeList(['G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8'])
.getRanges()
.map(range => range.getDisplayValue())
.join('\n');
MailApp.sendEmail(emailAddress, subject, message);
}
function onEdit(e) {
if(e.range.rowStart === 1 && e.range.columnStart === 11) {
var sh = e.source.getActiveSheet();
if(sh.getName() === 'Copy') {
sendEmail()
}
}
}
Piggybacking off Karla's answer, generally when you delete single values, the event object (e) is absent of a value key. Therefore,
Try:
function onEdit(e) {
if (e.source.getActiveSheet().getName() === `Copy`) {
if (e.range.rowStart === 1 && e.range.columnStart === 11) {
if (`value` in e) sendEmail()
}
}
}
Maybe add to the "if" that if the value is different to empty it will not execute the code
Not sure which row or range belongs to K, so I name it "Range_in_K"
if(e.range.rowStart === 1 && e.range.columnStart === 11 && Range_in_K != "")

Function in textFieldDidEndEditing is running when it’s not supposed to run

I've got 5 text fields:
When I type values in any 4 of the text fields, the value should be calculated and it should appear in the 5th text field.
For example, if I enter values in the Interest, Monthly Payment, Future Value and Number of Payments text fields, a value should be calculated and it should appear on the Principal Amount text field.
The calculation takes place when the Keyboard is dismissed (textFieldDidEndEditing).
Here is the code for that:
// When the user has finished typing
func textFieldDidEndEditing(_ textField: UITextField) {
// Principal Amount
let P = Double(principalAmountTextField.text!)
// Interest rate
let r = Double(interestTextField.text!)
// Monthly Payment
let PMT = Double(monthlyPaymentTextField.text!)
// Number of Payments
let t = Double(numOfPaymentsTextField.text!)
// Future Value
let A = Double(futureValueTextField.text!)
// Check if the necessary text fields are filled to find Principal Amount
if futureValueTextField.text != "" && interestTextField.text != "" && numOfPaymentsTextField.text != "" && monthlyPaymentTextField.text != "" {
print(1)
principalAmountTextField.text = String(calculatePrincipalAmountCompound(futureValue: A!, interestRate: r! / 100, payment: PMT!, numOfPayments: t!))
}
// Check if the necessary text fields are filled to find Monthly Payment
if futureValueTextField.text != "" && principalAmountTextField.text != "" && numOfPaymentsTextField.text != "" && interestTextField.text != "" {
print(2)
monthlyPaymentTextField.text = String(calculateMonthlyPaymentCompound(principalAmount: P!, futureValue: A!, numOfPayments: PMT!, interestRate: r! / 100))
}
}
So clearly, if I enter values in the Interest, Monthly Payment, Future Value and Number of Payments text fields, only the first IF condition in the textFieldDidEndEditing method should run, but for some reason, the second IF condition runs too. Why does this happen?
Edit
Thanks to #tomerpacific I was able to make this work. But I still don't know why the above code doesn't work, I just tried running the IF conditions without calling the methods and it works properly:
// Check if the necessary text fields are filled to find Principal Amount
if futureValueTextField.text != "" && interestTextField.text != "" && numOfPaymentsTextField.text != "" {
print(11)
}
// Check if the necessary text fields are filled to find Monthly Payment
if futureValueTextField.text != "" && principalAmountTextField.text != "" && numOfPaymentsTextField.text != "" && interestTextField.text != "" {
print(22)
}
The problem lies with your logic.
Let's break it down to make things clearer.
if futureValueTextField.text != "" && interestTextField.text != "" && numOfPaymentsTextField.text != "" && monthlyPaymentTextField.text != "" {
print(1)
principalAmountTextField.text = String(calculatePrincipalAmountCompound(futureValue: A!, interestRate: r! / 100, payment: PMT!, numOfPayments: t!))
}
When this if condition is true, principalAmountTextField gets a value.
So, after this condition, the following text fields have values:
futureValueTextField
interestTextField
numOfPaymentsTextField
monthlyPaymentTextField
principalAmountTextField
// Check if the necessary text fields are filled to find Monthly Payment
if futureValueTextField.text != "" && principalAmountTextField.text != "" && numOfPaymentsTextField.text != "" && interestTextField.text != "" {
print(2)
monthlyPaymentTextField.text = String(calculateMonthlyPaymentCompound(principalAmount: P!, futureValue: A!, numOfPayments: PMT!, interestRate: r! / 100))
}
Now, since you don't return from your if clauses or don't have an if/else flow, the next if evaluates to true when the following textfields have values:
futureValueTextField
interestTextField
numOfPaymentsTextField
principalAmountTextField
And since you have given principalAmountTextField a value, the second if condition is met and the code runs.

Swift 3 enum with associated value AND function comparison

I have this struct that has an enum property as well as a function:
struct UserInput {
enum State {
case unrestricted
case restricted(because: WarningType)
enum WarningType {
case offline
case forbidden
}
}
var config: UserInputConfig?
var state: State = .unrestricted
func isConfigured() -> Bool {
// Arbitrary checks about the config...
}
}
Is there a way to rewrite the following conditionals so that the check for isConfigured() and state are in the same statement?
if case .restricted = userInput.state {
return 1
} else if userInput.isConfigured() {
return 1
} else {
return 0
}
It seems because the State enum uses associated values, you cannot simply write if userInput.state == .restricted || userInput.isConfigured(), you need to use the if case syntax. There must be a way around this?
You would like to do this:
if case .restricted = userInput.state || userInput.isConfigured() {
return 1
} else {
return 0
}
but there is currently no way to do an OR with pattern matching. There are a couple of ways of doing AND.
By using DeMorgan's Laws, you can turn if a || b into if !(!a && !b) and by reversing the then and else clauses of your if statement, you can just check for if !a && !b.
Unfortunately, you can't say if !(case .restricted = userInput.state), but since your enum has only 2 cases, you can replace that with if case .unrestricted = userInput.state.
Now, how do you use that with another statement? You can't use && for the same reason you can't use ||.
You can check for the failing case by using a pattern that matches both failing conditions (which is using AND) and then return 1 if both failing conditions aren't met:
if case (.unrestricted, false) = (userInput.state, userInput.isConfigured()) {
return 0
} else {
return 1
}
Equivalently you can use a multi-clause condition:
if case .unrestricted = userInput.state, !userInput.isConfigured() {
return 0
} else {
return 1
}
In addition to being shorter and IMO easier to read, this second method can short circuit and skip calling userInput.isConfigured in the case where case .unrestricted = userInput.state fails.
You can do it really cleanly with a switch statement, and pattern matching:
switch userInput.state
{
case .unrestricted:
return userInput.isConfigured() ? 1 : 0;
case .restricted(_):
return 1
}

Using && In an If Statement Swift 2.0

I am trying to make a very basic console game of rock paper scissors. I'm not sure how to go about using && in Swift 2.0. I want to basically say:
if (computerChoice == rock && userChoice == paper) {
print("User wins")
}
Well you appear to have it setup correctly. You could set this up using an enumeration variable.
enum RPSOption {
case Rock, Paper, Scissors
}
Then you just use it like so:
let computerChoice: RPSOption = RPSOption.Rock // or something else
let userChoice: RPSOption = RPSOption.Paper // or something else
if( computerChoice == .Rock && userChoice == .Paper ){
// do something
}
// do similar if statements for other conditions

Binary operator '~=' cannot be applied to two UmRet operands

I'm working on integrating an IDTech swiper into my app and I've gotten pretty far along, added the library, registered notifications, unregistered them, and now I'm working on a function that connects the reader. I can't seem to figure out what I'm doing wrong here when I'm attempting to switch cases based on a return value.. could someone please help me?
func displayUmRet(operation: String, returnValue: UmRet) {
var string = ""
do {
switch returnValue {
case UMRET_SUCCESS: string = ""
case UMRET_NO_READER: string="No reader attached"
case UMRET_SDK_BUSY: string="Communication with reader in progress"
case UMRET_MONO_AUDIO: string="Mono audio enabled"
case UMRET_ALREADY_CONNECTED: string="Already connected"
case UMRET_LOW_VOLUME: string="Low volume"
case UMRET_NOT_CONNECTED: string="Not connected"
case UMRET_NOT_APPLICABLE: string="Not applicable to reader type"
case UMRET_INVALID_ARG: string="Invalid argument"
case UMRET_UF_INVALID_STR: string="Invalid firmware update string"
case UMRET_UF_NO_FILE: string="Firmware file not found"
case UMRET_UF_INVALID_FILE: string="Invalid firmware file"
default: string="<unknown code>"
}
} while (0)
// var retStatus = UMRET_SUCCESS==ret
//self.textResponse.text = "\(operation), \(retStatus), \(string)"
self.hexResponse.text = "";
}
You need to put a . before your cases:
enum UmRet {
case UMRET_SUCCESS, UMRET_FAILURE
}
var string = " "
let returnValue = UmRet.UMRET_SUCCESS
switch returnValue {
case .UMRET_SUCCESS: string = "y"
case .UMRET_FAILURE: string = "n"
}
Also, 0 isn't the same as false in Swift, so:
do {
...
} while (0)
Shouldn't work either.
And you don't need semicolons at the end of a line, so this:
self.hexResponse.text = "";
can be this:
self.hexResponse.text = ""
And finally, if your switch statement has every case for every case in your enum, you don't need a default case. (that's why mine didn't have one in the example)
By the way, ~= is just the operator for the pattern-matching function, which is what Swift does in a switch statement. It works kind of like the == function, for instance, Int ~= Int is the same as Int == Int. But it's a bit more versatile: for instance Range ~= Int, eg 0...3 ~= 2 returns whether or not the Int is in the range. (So true in this case) For enums, it matches cases to cases. In my example, it'll match UMRET_SUCCESS, and string will be set to y.