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.
This token only has liquidity on PancakeSwap so at the end of the day, all transaction (purcahases) will originate from pancakeswap.
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 and it gets transfers, not purchases of the token on pancakeswap. Kindly help me get the transaction history involving the purchases of the token on pancakeswap since that is the only place the token has liquidity. I will appreciate your help promptly on this.