I am developing an affiliate system for a crypto token built on the BNB Smart chain in Laravel. When user A refer user B to signup on the affiliate program and purchase the toke, user A should get a commission on user B purchase. It has been a challenge getting the dollar value of the transaction at the time of purchase so I can calculate the commission on it.
I have not been able to get one api that returns both the transaction history and dollar value at the time of purchase.
I used bscscan api to get the transaction history in the code below:
$apiUrl = "https://api.bscscan.com/api";
$httpClient = new Client(['base_uri' => $apiUrl]);
$response = $httpClient->request('GET', '', [
'query' => [
'module' => 'account',
'action' => 'tokentx',
'contractaddress' => $ashTokenContractAddress,
'address' => $walletId,
'apikey' => $apiKey
]
]);
I used an api from coingecko to get the historic price given a timestamp using this api:
$url = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=ash-token&date=$timestamp";
When I get the historic price, I convert the value of the transaction in Wei to the quantity of tokens bought and multiply it by the historic price (priceAtTime) in the code below:
$quantity = $transfer['value'] / (10 ** $transfer['tokenDecimal']);
$transactionPrice = $priceAtTime * $quantity;
However, I do not get the dollar value I was expecting. Is there something I am doing wrong and could I have achieved this using one api or apis from one explorer? I will appreciate your help promptly on this.