Saulius
Saulius

Reputation: 139

Is there a way to get token price on transfer function call?

is it possible to know for which price user is buying/selling token on transfer function call?

I want to store information on the contract for with user bought/sold tokens on the transfer function.

Upvotes: 1

Views: 374

Answers (2)

hassan ahmed
hassan ahmed

Reputation: 614

Yes it is possible. this is where oracles comes into play chainlink oracle provide off-chain data like token price feeds in contract in a decentralised way. Here is how you can get token price feed in smart contract in EVM-compatible blockchains

To keep record of price on each transfer extend transfer event to emit the price as well that you fetch from oracle.
Your Flow for modified transfer function will be something like this.

  1. All Checks to ensure (valid transfer + valid oracle call)
  2. Get oracle price of token from oracle and store it in local variable
  3. Transfer
  4. Emit modified event emit Transfer(sender, recipient, amount, price); where price is current price that you got from oracle.

Upvotes: 1

jhonny
jhonny

Reputation: 858

No, this is not possible, at least in a token contract, because the token contract only "knows" that the user send or approved tokens and that's all that can know, it could be possible in a dex or cex contract

Upvotes: 1

Related Questions