How I could obtain the current earnings of a farm from Maiar Exchange via Elrond REST API? For example, for the LKMEX farm I want to determine the current earnings (My Earned MEX) in MEX and/or USDT since the latest harverst or 'reinvest'. Thanks!
1 Answers
You have two options here:
Straight from the contract via VM Query
Straightforward and low level way would be to ask the calculateRewardsForGivenPosition view function via vm query for the desired farm contract. This can be done via POST request through https://gateway.elrond.com/vm-values/query.
Check out https://docs.elrond.com/sdk-and-tools/rest-api/virtual-machine/ for vm-queries via REST API details and https://github.com/ElrondNetwork/sc-dex-rs/blob/main/dex/farm/src/lib.rs#L372 for the aforementioned view function's expected parameters.
Basically, you'll have to ask the farm contract for your latest rewards using just the Farm Token quantity and the attributes field of your Farm Token which you can find them out using the https://api.elrond.com/nfts/[token] route.
A concrete example for this case:
- MEX Farm with locked rewards:
erd1qqqqqqqqqqqqqpgq7qhsw8kffad85jtt79t9ym0a4ycvan9a2jps0zkpen
- We take the supply and attributes for my farm token
MEXFARML-28d646-360d8b
via https://api.elrond.com/nfts/MEXFARML-28d646-360d8b : supply2863539312887406729205021282
(we have to convert this to hex from decimal); attributesAAAABYENv6kdAAAAAAAAAg8AAAAAAAAC8QAAAAwGP0dX61aTZ298cosAAAAMAwFisUg/gGvN0gfWAAAADAlAqgkzlhPTPU56Yg==
(we'll have to convert this from base64 to hex); For conversion, you can use this nifty tool: http://207.244.241.38/elrond-converters/
The resulting VMQuery request looks like this:
POST https://gateway.elrond.com/vm-values/query HTTP/1.1
Content-Type: application/json
{
"scAddress": "erd1qqqqqqqqqqqqqpgq7qhsw8kffad85jtt79t9ym0a4ycvan9a2jps0zkpen",
"funcName": "calculateRewardsForGivenPosition",
"args": ["0940aa09339613d33d4e7a62", "00000005810dbfa91d000000000000020f00000000000002f10000000c063f4757eb5693676f7c728b0000000c030162b1483f806bcdd207d60000000c0940aa09339613d33d4e7a62"]
}
You'll then have to convert the response from base64 to decimal to get your rewards amount.
Via the GraphQL Maiar DEX service
GraphQL Maiar DEX service is now public and you can directly check out the schema for accessing all the exchange data: https://github.com/multiversx/mx-exchange-service

- 415
- 2
- 9
-
Url of API has been changed to the new name of Elrond (now MultiversX) => https://docs.multiversx.com/sdk-and-tools/rest-api/gateway-overview – Digital3D Feb 02 '23 at 14:45