I want to swap the tokens inside a smart contract through uniswap contract for another token(Let's say i want to swap SHIB for USDC). How do i do this? Are there any standards/built contracts for doing so?
Yes it is possible, I dont know if there is an standard on that but you can check out the uniswap router: https://docs.uniswap.org/protocol/reference/periphery/SwapRouter.
I was able to do a swap like this:
function swap(address _token, bytes memory path, uint amount) public returns(uint256 result){
IERC20 erc20 = IERC20(_token);
erc20.approve(address(router), amount);
ISwapRouter.ExactInputParams memory params = ISwapRouter.ExactInputParams(
path, address(this), block.timestamp, amount, 0
);
if(address(this).balance == 0){
amount = 0;
}
result = router.exactInput{ value:amount }(params);
}