Skip to main content

Executing with Web3 Provider

The easiest way to execute routes using the Socket SDK is by connecting a web3 provider. Using this method, the SDK will handle the entire process of the transaction, including approvals, prompting wallets, switching networks and more.

The SDK supports the ethers Web3Provider. Many wallet libraries like web3modal and web3onboard support retrieving the web3 provider for the connected wallet.

Connecting

Using the constructed socket instance, call connect with the web3 provider. Web3Modal is being used as example here but any web3 provider can be used.

const web3ModalProvider = await web3Modal.connect();
const web3Provider = new ethers.providers.Web3Provider(
web3ModalProvider,
"any"
);
const socket = new Socket({ apiKey: API_KEY });
const web3Socket = socket.connect(web3Provider);

Executing

To begin the execution simply call start:

const activeRouteId = await web3Socket.start(quote);

Events

In order to follow the execution process, you may specify EventCallbacks that will be called during the process.

await web3Socket.start(quote, {
onDone: (activeRouteId) => {
console.log("Executing route complete", tx);
},
});

Most callbacks can also return a callback that will be called when the action has been completed. For example, you can detect when an approval transaction is complete:

await web3Socket.start(quote, {
onApprove: (tx) => {
console.log("Requesting approval");
return (tx, hash) => {
console.log("Approval complete", hash);
};
},
});

For a complete list of callbacks see EventCallbacks.