Retrieve coin base price (or quantity) from transaction data using BscScan APIs - api

I'm trying to write an app to track my gains/losses for tax purposes based on the swaps I've made using Bscscan APIs. I can easily retrieve all the swaps I've made using one of BscScan "accounts" module api endpoint (Get a list of 'BEP-20 Token Transfer Events' by Address to be exact). Here's an example of the body response:
{
"status": "1",
"message": "OK",
"result": [{
"blockNumber": "2304192",
"timeStamp": "1605585978",
"hash": "0x7a5abf86c82d3f97a40dd841a9f2089fbe3ac1332157c01bd1a1d89f575c45fc",
"nonce": "57",
"blockHash": "0x703438fd8ba435996895a6b9b711934cc590c3e9297a7066419aa88dd9d83acc",
"from": "0x641414e2a04c8f8ebbf49ed47cc87dccba42bf07",
"contractAddress": "0xc9849e6fdb743d08faee3e34dd2d1bc69ea11a51",
"to": "0x7bb89460599dbf32ee3aa50798bbceae2a5f7f6a",
"value": "27605634758857128698365",
"tokenName": "Bunny Token",
"tokenSymbol": "BUNNY",
"tokenDecimal": "18",
"transactionIndex": "2",
"gas": "5000000",
"gasPrice": "20000000000",
"gasUsed": "2312130",
"cumulativeGasUsed": "2475576",
"input": "deprecated",
"confirmations": "8172919"
}
]
}
So the value input ("value": "27605634758857128698365") gives the value in the underlying coin "currency" - in this case "Bunny Token" - but without knowing the quantity of that coin bought, I can't determine the cost basis of that transaction. I've looked at other bscscan APIs but I couldn't find any that would could help me retrieve the price of the coin for a given transaction.
Does anyone know if there's a way to retrieve the price of a coin (or the quantity bought) based on the transaction data using bsccan api?
TIA

Related

How to get a product / variant "committed" inventory?

Problem:
On the Shopify Admin, section Products > Inventory, there's a "committed" inventory.
Based on my comprehension it means "reserved stock for pending orders", or "inventory reserved by orders created but not yet completed".
This "committed" stock value can be very useful for stock updates by app. But pratically this value seems hard to retreive.
Question:
How to get this "committed" inventory value in an efficient way ?
Current state of researchs :
Not possible via Shopify APIs (REST & GraphQL, version 2022-07)
Can be approximated (not sure it's the right method) by looping over all unfulfilled orders, and sum up (by variant) the unfulfilled product variant quantity. However, it takes time and consumes a lot of API resources.
According to a Shopify Community Manager (ref. post)
The Committed inventory state isn't added to the API currently.
It's possible now in the 2023-01 version of the API. The InventoryLevel got a new field quantities, which returns this for a product with a total stock of 10 and two sold:
[
{
"quantity": 8,
"name": "available"
},
{
"quantity": 2,
"name": "committed"
},
{
"quantity": 0,
"name": "incoming"
},
{
"quantity": 10,
"name": "on_hand"
},
{
"quantity": 0,
"name": "reserved"
}
]

Double Discounted Lines in Shopify Order API

I came across a situation that I hope someone else has seen.
The order looks like this in shopify:
The Discounted Line Item JSON looks like this:
{
"id": XXXXX,
"name": "NAME",
"price": "19.10",
"product_exists": true,
"quantity": 1,
"sku": "SKU",
"total_discount": "9.55",
"discount_allocations": [
{
"amount": "9.55",
"amount_set": {
"shop_money": {
"amount": "9.55",
"currency_code": "USD"
},
"presentment_money": {
"amount": "9.55",
"currency_code": "USD"
}
},
"discount_application_index": 0
}
]
},
An Automatic Discount as a "BOGO at 50% off" was applied to this order, but I cant quite get the discounting right for a net unit price, without also causing issues elsewhere in this particular Shopify Tenant.
Previously I would
line_unit_price = price - (TotalDiscount/Quantity) - SumOfDiscountApplications
But in this case, that would just double discount it to zero, which is not correct. In other cases with this Shopify Tenant, I do need to use the Discount Applications as TotalDiscount would be 0. I cant quite figure out when/where/why to do the math differently for an effective Line Net Unit Price.
Very odd. Any help would be great.

create order with different extra bag for outbound and inbound

I want to test order create API by adding extra bags. And I am experiencing a strange problem.
I make a search for Paris-NYC round trip, then I send the request to offer price API using include=detailed-fare-rules,bags parameter.
In the response, I get 2 kinds of extra bag information:
1 bag, 30 EUR
2 bags, 75 EUR
"bags": {
"1": {
"quantity": 1,
"name": "CHECKED_BAG",
"price": {
"amount": "30.00",
"currencyCode": "EUR"
},
"bookableByItinerary": true,
"segmentIds": [
"1",
"3"
],
"travelerIds": [
"1"
]
},
"2": {
"quantity": 2,
"name": "CHECKED_BAG",
"price": {
"amount": "75.00",
"currencyCode": "EUR"
},
"bookableByItinerary": true,
"segmentIds": [
"1",
"3"
],
"travelerIds": [
"1"
]
}
}
Everything goes well if I create order by:
adding 1 bag for outbound(paris to NYC), and adding 1 bag for inbound(NYC to Paris)
adding only 1 bag for outbound (0 extra bag for inbound)
adding 2 bag for outbound(paris to NYC), and adding 2 bags for inbound(NYC to Paris)
The problem is for the scenario:
I create order by adding 1 bag for outbound, and adding 2 bags for inbound.
In this case, the order is created with a warning message
"warnings": [
{
"status": 200,
"code": 0,
"title": "BookingWithPriceMarginWarning",
"detail": "The prices are lower than expected"
}
]
And the created order contains 1 extra bag for outbound, and 1 extra bag for inbound.
So I have 2 questions about this strange problem:
Is it normal that my order is modified when processing order create ?
Adding different number of extra bags for different itineraries is supported ?
Thanks
Is it normal that my order is modified when processing order create ?
It depends if you are Self-Service or Enterprise user:
For Enterprise users, Flight Create Orders offer the possibility to do a "best-effort" for additional-service booking. If this option is activated, Flight Create Orders gives priority to the reservation of your flight and remove the additional service that cannot be booked. That's why you receive the warning in your request when it happens.
Self Service users have the default behavior which rejects the creation of the order if at least one additional service can not be booked. In this case you will receive the following error:
{
"errors": [
{
"status": 400,
"code": 38034,
"title": "ONE OR MORE SERVICES ARE NOT AVAILABLE",
"detail": "Error booking additional services"
}
]
}
Adding different number of extra bags for different itineraries is supported ?
Yes, that is supported. Be aware that you cannot have an infinite amount of bag on a plane, so it could happen that you get an error when adding extra bags if there are too many bags already added by other passengers.

Karate: compare csv data with api response

I have a use case where I want to assert on a API response and compare it with the csv data.
Step1:
Csv file: *test.csv*
id,date,fullname,cost,country,code
1,02-03-2002,user1,$200,Canada,CAN
2, 04-05-2016,user2,$1500,United States, USA
I read the csv file and store it in a variable
def var1 = read(test.csv)
So now, var1 is a list of jsons based on my csv
var1 = [
{
"id":1,
"date":"02-03-2002",
"fullname": "user1",
"cost": "$200",
"country": "Canada",
"code": "CAN"
},
{
"id":2,
"date":"04-05-2016",
"fullname": "user2",
"cost": "$1500",
"country": "United States",
"code": "USA"
}
]
Step2:
I hit my api and get a response
Given url "https://dummyurl.com
Given path "/userdetails"
When method get
Then status 200
* def apiResponse = response
Step 3:
My api returns a list response which is:
{
"id":1,
"date":"02-03-2002",
"fullname": "user1",
"cost": "$200",
"country": {
"name": "Canada",
"code": "CAN"
}
},
{
"id":2,
"date":"05-04-2012",
"fullname": "user2",
"cost": "$1500",
"country": {
"name": "United States",
"code": "USA"
}
},
...and more 100 records..
]
Step 4:
So there are two assertions now which I wanted to perform
Get the count of csvresponse and apiresponse and compare which I did using the .length operator
Secondly, I want to confirm if each csv records are matching with each api response.
And if possible in my case id key from csv and apiresponse is primary key, so if I can iterate on id and match the api response for any discrepancy.
Let me know if this is readable for you and if I was able to explain my use case.
Thanks for your earlier response.
Please read up on the match contains syntax, that's all you need: https://github.com/intuit/karate#match-contains
So this one line should be enough:
* match var1 contains response
Also look at this answer in case the new contains deep helps: https://stackoverflow.com/a/63103746/143475
Try to avoid iterating, it is not needed for most API tests. But you can certainly do it. Look at these answers:
https://stackoverflow.com/a/62567262/143475
Also read this - because I suspect you are trying to over-complicate your tests. Please don't. Write tests where your are 100% sure of the "shape" of the response as far as possible: https://stackoverflow.com/a/54126724/143475
And please please read the docs. It is worth it.

Charge saved credit card - Connect V2

How can I charge customers with saved credit cards?
Card object returns ID, how can I transform that ID to credit card nonce?
You don't have to.
The Charge endpoint takes either a card_nonce or a combination of a customer_id and a customer_card_id. In that case your json body would look something like this:
{
"idempotency_key": "xxxx",
"amount_money": {
"amount": 100,
"currency": "USD"
},
"customer_card_id": "{{customer_card_id}}";
"reference_id": "some optional reference id",
"note": "some optional note",
"customer_id":"{{customer_id}}",
"delay_capture": false
}
You can read more about it with a ruby example on this page: https://docs.connect.squareup.com/articles/processing-recurring-payments-ruby