This is an example that I found in the docs of: https://github.com/safe-global/safe-apps-sdk/tree/master/packages/safe-apps-sdk
I combined it with the send transactions example here: https://www.npmjs.com/package/@rmeissner/safe-apps-react-sdk
you should know that this package is deprecated and should be updated to this package: https://www.npmjs.com/package/@gnosis.pm/safe-apps-react-sdk
hope it helps!
// import the useSafe hook
import { useSafe } from "@rmeissner/safe-apps-react-sdk";
// Declare the useSafe function in your component
const safe = useSafe();
// You need to create a web3 instance if you don't already have one
const web3 = new Web3("https://rinkeby.infura.io/v3/token");
// get the contract ABI and address from the Etherscan page and create a contract instance
// you can create multiple contract instances if you want to interact with multiple contracts
const pancakeContract = new web3.eth.Contract(
pancakeAbi,
pancakeContractAddress
);
const uniswapContract = new web3.eth.Contract(
uniswapAbi,
uniswapContractAddress
);
// if you are only using one method from a contract and it's a standard method like approve you can declare the abi like this
const approveAbi = [
{
inputs: [
{ internalType: "address", name: "spender", type: "address" },
{ internalType: "uint256", name: "amount", type: "uint256" },
],
name: "approve",
outputs: [{ internalType: "bool", name: "", type: "bool" }],
stateMutability: "nonpayable",
type: "function",
},
];
const txs = [
{
to: pancakeContractAddress, // string
// the value refers to the amount of BNB not the amount of pancake tokens
value: "0",
// find the method you want to call in this case, approve
data: pancakeContract.methods.approve(spenderAddress, amount),
},
{
to: uniswapContractAddress, // string,
// the value refers to the amount of BNB not the amount of pancake tokens
value: "0",
// find the method you want to call in this case, approve
data: uniswapContract.methods.approve(spenderAddress, amount),
},
];
try {
const safeTxHashPC: string = await safe.sendTransactions(txs[0]);
const safeTxHashUni: string = await safe.sendTransactions(txs[1]);
} catch (err) {
console.error(err.message);
}