I knew that Ethereum smart contracts could be used for this functionality but could not find any technical info on doing smart contracts with the BTC blockchain but found
https://cashscript.org/ smart contracts for Bitcoin Cash blockchain.
Bitcoin also have script which can act as smart-contract (although it has limited function compared with Ethereum). Usually you need to interact with stack-based scripting, but
Miniscript and
Minsc makes it easy.