nodejs - how to handle payment - mysql

Background:
I am working on making nodejs function for payment using stripe.
My clients would have their own reward point, which can turn into cash dollar for payment
Current Design
Customer Table(mySQL)
-id: string(primary key)
-rewardPoints: int
Therefore I am working on 2 api seperately.
/transaction/debit-points
-this api is used to debit the reward point of clients when they want to turn it into cash dollar(debit the rewardPoints in Customer table)
/payment
-this api will use stripe api to handle some credit card payment etc
Example
Let's say 100 reward points can turn into $1 cash dollar
Customer A is going to settle payment which is $200.
Customer A have 1000 reward points and want to turn all of it into cash dollar in this payment.
At first, I will use /transaction/debit-points to debit all 1000 reward points for the customer, and then use /payment to settle the payment.
1. /transaction/debit-points
2. /payment (run if /transaction/debit-points is successful)
If /payment fails, I want to reverse the effect of /transaction/debit-points, is it possible to do it in nodejs?
Or is there any better way?

Related

How to calculate dollar value of transaction fee from Gas price details in Ethereum transaction

I am looking to convert gas fee info from this api end point: into dollars (calculating taxes).
The endpoint gives 'gas', 'gasPrice' and 'gasUsed' values that I'm sure would be enough, but I can't quite work it out.
You can calculate the price in dollars using the following formula:
usdPrice = gasUsed * gasPrice * ethToUsd
You can take the first two parameters (gasUsed and gasPrice) from etherscan. To get the current conversion rate between ETH and USD (ethToUsd) you need to query some exchange API, e.g., https://min-api.cryptocompare.com/.

How to set price for all NFTs in a collection on opensea?

If I mint a collection of n items on my own address (via smart contract) , it'll be publicly visible on my account under the collected / created section like this .
The problem is that , in order to complete the listing I must manually set the price of each NFT individually. Obviously this is not practical , since I cannot set the price of every NFT one by one manually , not to mention that every time a listing is completed (by setting the price) , gas fee has to be paid.
Therefore I want to be able to achieve setting the price programatically such that after minting , the collection looks like this.
I tried to approach this problem via OpenSea-Creatures SDK (which uses Truffle) , but Im not sure if their sales-contract can achieve this. (I couldn't get it to run either).
I know this question is very generic but I've been looking for a solution for a while and I haven't found anything helpful.
Any guidance in the right direction would be of tremendous help!
I also faced the same issue when i was creating a large NFT collections of over 3,000. The best solution i will recommend is Creating a Custom Sale Contract.
The downside of the simple item sale is that you'll need pre-mint the items that you want to sell (as opposed to minting them on the fly when they're purchased). This can make it difficult to sell many items to a broad user base. Additionally, with the simple item sale method, it's difficult to create custom logic for the sale of your items (like packs of random items, randomized stats for the items that are minted, etc.). If you want any of these features, the Factory contract is for you!
In order to mint items only when they're purchased, OpenSea provides a Factory interface that you can use to define how your items will be minted. You then use OpenSea to create custom "orders" that call your minting function. Here's a look at the main mint method of the Factory contract.
Selling many of one option
While the OpenSea UI lets you sell one Factory item at a time, in many circumstances, you may want to sell off a large number of items to your initial users: for example, 100 swords or 100 packs of cards. For this, we recommend using the SDK to instantly create sales with a given supply. As an example, suppose you wanted to put 100 items up for sale in your crowdsale for a fixed price, expiring tomorrow. You could do the following:
// Expire these auctions one day from now. Omit `expirationTime` or set to 0 to never expire:
const expirationTime = (Date.now() / 1000 + 60 * 60 * 24)
const sellOrders = await seaport.createFactorySellOrders({
assetId: <ASSET_OPTION_ID>,
factoryAddress: <FACTORY_CONTRACT_ADDRESS>,
accountAddress,
startAmount,
expirationTime,
numberOfOrders: 100 // Will create 100 sell orders in parallel batches to speed things up
})
Check this link to see how to create a factory contract
https://docs.opensea.io/docs/2-custom-item-sale-contract
#Ngyfsoau

How to calculate LP token worth

I am trying to implement my own USDC liquidity pool that allows various users to deposit USDC into it to earn a fixed interest rate based on how long they park the USDC in my pool (meanwhile I will use the USDC for other things) But I am trying to work out how I can give them a ‘live’ balance on their deposit and interest earned so far. Anyone have any ideas on how I could do this ?
I am guessing that once they deposit 100 USDC then I should give them 100 LP tokens in return right ? but how to auto increment the value of those LP tokens etc has me confused

Convert amount to gas value

What I want to achieve is to create a UI(e-Commerce user interface) to allow the user to enter the amount.In this case, the amount would be in USD; from the back-end, it will calculate and convert the USD amount to the corresponding gas value then send it to the Smart Contract. Has anyone experienced that?

Exponential increase

I'm smashing my head from hours trying to achieve an exponential increase. I tried with second degree equation but the result is not what i expected. Let me explain.
I have a pay per use service based on credit balance. Users can upload funds anytime to their accounts. I want to incentivize clients to upload more funds to avoid micro-payments and higher transaction fees. To make it short the more you pay, the more bonus you get. For example an user could deposit 100$ in his account in two ways:
Jack: 10 transactions 10$ each
Paul: 2 transactions 50$ each
Mark: 1 transaction of 100$
Paul should get an higher bonus than Jack but less compared to Mark. Now I'm trying with this function.
for($amount=10;$amount<=5000;$amount +=10)
{
$rate=0.005;
$bonus=exp($rate*$amount);
echo "Deposit ".$amount."$ and gain ".$bonus." bonus<br>";
}
Minimum deposit is 10$ and Maximum deposit is 5000$ that's why i loop from 10 to 5000. The problem is simple.
Deposit 10$ and gain 1.05127109638 bonus
...
Deposit 100$ and gain 1.6487212707 bonus
...
Deposit 1000$ and gain 148.413159103 bonus
...
Deposit 2500$ and gain 268337.286521 bonus
...
Deposit 5000$ and gain 72004899337.4 bonus
You get a too little for small amounts and way too much for big amounts. I've also tried with different ranges for example 10 to 100$ with a certain rate, 200 to 1000 with another (...) but of course when you deposit an amount that is near to these limits you get less bonus. It's not logical.
Deposit 1000$ and gain 54.5981500331 bonus
... here starts the next range ...
Deposit 1250$ and gain 42.5210820001 bonus
I've also tried this approach:
function foo($wanted=1000, $rangeLow=10, $rangeHigh=5000){
$increment=($rangeHigh-$rangeLow)/($wanted+1);
$r=array();
for ($i=$rangeLow+$increment;$i<$rangeHigh;$i+=$increment)
$r[]=$i;
return $r;
}
I "spread" 1000 bonus points between 10$ and 5000$ but with this I get a linear increase while I need an exponential one. Probably the solution is mixing both approaches but I don't know how to.
I think you want the pow function instead of exp
pow Returns base raised to the power of exp.
exp Returns e raised to the power of arg.
e.g.
for($amount=10;$amount<=5000;$amount +=10)
{
$rate=0.05; //adjust as needed, 0.08 makes 5000 -> about x2
$bonus=pow($amount, $rate);
echo "Deposit ".$amount."$ and gain ".$bonus." bonus<br>\n";
}
Outputs:
Deposit 10$ and gain 1.122018454302 bonus
Deposit 20$ and gain 1.1615863496415 bonus
Deposit 30$ and gain 1.1853758165593 bonus
..
Deposit 1000$ and gain 1.4125375446228 bonus
..
Deposit 5000$ and gain 1.5309059120639 bonus
Don't you just need to keep your exp function but multiply it by a value less than 1? If I've got my maths right, that should flatten the exponential curve.
I finally managed to complete this task. It's not something that you can achieve only using PHP. You also need an equation otherwise you have no controls over the increases.
In my case i have an equation of parabola with 3 points given where have to define the lowest and the highest value. I wanted to give a maximum bonus of 1000 and a minimum of 0.1 for imports from 10 to 5000$. I did it on Excel with matrix product of a matrix inversion of values. Anyway I'm not going to share the whole formula since it's too specific. I prefer to share with you the PHP-side of the story.
I wanted to "convert" my Excel formula in PHP but it took me too much time to code matrix calculations then i decided look for an existing library. Pear has a Math_Matrix package. If you don't want to recompile your webserver to get Pear, you can probably use this PHP class. I tested matrix inversions and it works.
Said that i suggest you not to use any of the above solutions unless you use matrix math frequently. It's more convenient and efficient to you calculate all necessary values on Excel or with the "old" pen and paper so that you can pass to PHP simple calculations like +, -, *, ^2 etc.