Complex formula in server action

I have a server action that gets data from an API call and some values are then stored in my database

So I first have the api call and then a database insert action

In this db record I need to store a value that needs a (complex) formula to calculate it’s value based on different values from the api call

So in the value field I have build this formula but now I wonder is this is the right way/place to do this calculation…

This is the formula:
{{($_POST.gasfee+(api.data.last_sale.total_price/1000000000000000000))+((($_POST.gasfee+(api.data.last_sale.total_price))/100))*((api.data.collection.primary_asset_contracts[0].dev_seller_fee_basis_points/100)+(api.data.collection.primary_asset_contracts[0].opensea_seller_fee_basis_points/100))}}

This is how it looks:

Is this the way to go?

I don’t think there’s a better way to go.

You’ve got access to all the variables in the $POST request and it’s not stretching the technology - it’s some basic maths frankly and shouldn’t take more than a few milliseconds and not stretch the server resources at all.

1 Like

Great.
Thanks Scalaris. Glad I’m on the right path.

If you’re using NodeJS you can also use the RunJS extension and put the code there instead of writing a one-liner :slight_smile:

1 Like

somehow I can’t get it to work… must be an error in the formula.
Isn’t there a way to set variable values outside this field so I can do small calculations and set values to variables that I can use in this field?
Sort of breaking it up in smaller pieces.

Also I get quotation marks around my formula when I go back into the editor (click the lightning flash) and then swith from design to code view…

There must be an easier way to do this I hope?

You can use the set value step before the insert and add your formula there. Then you can use the set value step as a value in the insert.

Ah! I was just looking into this.

Thanks again Teodor.

I have set a value and change the data type to number
I save the file and come back to it and the value property data type is again set back to text (and another one to object)…?

It keeps changing back

image

then I save and…

image

The data type menu doesn’t change the actual data type. It tells the data picker and data formatter what data to expect.
Anyway this has been fixed in the latest update.

1 Like

Alright.
I asked because after setting values I get errors: “A non well formed numeric value encountered”

{"code":0,"file":"\/home\/mynftees\/domains\/mynftees.nl\/public_html\/dmxConnectLib\/lib\/core\/Parser.php","line":578,"message":"A non well formed numeric value encountered","trace":"#0 \/home\/mynftees\/domains\/mynftees.nl\/public_html\/dmxConnectLib\/lib\/core\/Parser.php(578): exception_error_handler(8, 'A non well form...', '\/home\/mynftees\/...', 578, Array)\n#1 \/home\/mynftees\/domains\/mynftees.nl\/public_html\/dmxConnectLib\/lib\/core\/Parser.php(106): lib\\core\\Parser::lib\\core\\{closure}()\n#2 \/home\/mynftees\/domains\/mynftees.nl\/public_html\/dmxConnectLib\/lib\/App.php(249): lib\\core\\Parser->parse('(api.data.asset...', NULL)\n#3 \/home\/mynftees\/domains\/mynftees.nl\/public_html\/dmxConnectLib\/lib\/App.php(228): lib\\App->parseObject('{{(api.data.ass...', NULL)\n#4 \/home\/mynftees\/domains\/mynftees.nl\/public_html\/dmxConnectLib\/modules\/core.php(128): lib\\App->parseObject(Object(stdClass))\n#5 \/home\/mynftees\/domains\/mynftees.nl\/public_html\/dmxConnectLib\/lib\/App.php(195): modules\\core->setvalue(Object(stdClass), 'sell_fee', Array)\n#6 \/home\/mynftees\/domains\/mynftees.nl\/public_html\/dmxConnectLib\/lib\/App.php(159): lib\\App->execSteps(Object(stdClass))\n#7 \/home\/mynftees\/domains\/mynftees.nl\/public_html\/dmxConnectLib\/lib\/App.php(127): lib\\App->execSteps(Array)\n#8 \/home\/mynftees\/domains\/mynftees.nl\/public_html\/dmxConnectLib\/lib\/App.php(116): lib\\App->exec(Object(stdClass), false)\n#9 \/home\/mynftees\/domains\/mynftees.nl\/public_html\/dmxConnectLib\/lib\/App.php(94): lib\\App->exec(Object(stdClass))\n#10 \/home\/mynftees\/domains\/mynftees.nl\/public_html\/dmxConnect\/api\/os_retrieve_single_asset_and_collection.php(8): lib\\App->define(Object(stdClass))\n#11 {main}"}

Any idea?

The error says some parts of the formula are not properly formatted. Probably using a string in some of the calculations.
I suggest to start debugging this by building the formula step by step and see which part is causing it.

Thanks. Getting closer.
Is it not possible to use set values in another value like this?
image

The last value does not add the ‘price’

It is possible to use it exactly like that.
Are you sure then that the api.data.last_sale.total_price is a number and not a string? How is it returned exactly by your API?

You are right. It’s returned as text:
image

But ‘price’ is calculated correctly:
api.data.last_sale.total_price/1000000000000000000

So then it must be converted to a number already right?

Oh i see now that you are using a POST var. Form inputs are always a string, so you need to use the to number formatter for the POST variable value, in order to convert it to number and to be able to make calculations.

Is this available in Wappler?
I have set it to be a number in the properties:

You need to use the to number formatter in the formula:

1 Like

Yes, thats it.

Thank you Teodor :pray:

1 Like