Search code examples
rustsolanaanchor-solana

Restricting solana transfers via smart contract


I want to implement restricting SOL transfers using a anchor/rust smart contract but cannot find any info on this in their documentation. Is there no option to filter or some kind of event/callback that my on chain code can connect to? Etherium has something to facilitate this called "require" as described in https://ethereum.stackexchange.com/questions/41659/how-can-i-decline-a-smart-contract-transaction

Is there anything similar for solana?


Solution

  • For a simple transfer, you cannot prevent someone from directly sending sol to your address.

    On the other hand, if you want to restrict execution of your program to require a minimum payment, that's easy. Simply transfer the minimum required sol within your instruction. The user does not need to execute any transfers of their own.

    In Solana, once someone signs a transaction, they give unrestricted access to all of their account balances to any invoked programs. If they have the balance in their account and they sign the transaction, your program can take what it wants. If they have insufficient balance, the instruction will fail, which blocks any part of the entire transaction from persisting. In other words, no transfer will be executed unless they have the minimum required amount available.