The setInstance() method sets the current active DSA (DeFi Smart Account) instance that will be used for all subsequent operations like casting spells and executing transactions. This method fetches the account details from the blockchain and updates the internal instance state.
The unique ID of the DSA account you want to set as active. This ID can be obtained from the getAccounts() method or from when you create a new DSA with build().
// First, get all accounts for a userconst accounts = await dsa.getAccounts("0x...");if (accounts.length > 0) { // Set the first account as active const instance = await dsa.setInstance(accounts[0].id); console.log(`Active DSA: ${instance.address}`);} else { console.log("No DSA accounts found. Create one with dsa.build()");}
// Set the active DSA instanceawait dsa.setInstance(12345);// Now you can cast spells on this DSAconst spells = dsa.Spell();spells.add({ connector: "compound", method: "deposit", args: ["ETH-A", web3.utils.toWei("1", "ether"), 0, 0],});await spells.cast({ from: "0x...",});console.log("Spell executed on DSA #12345");
const accounts = await dsa.getAccounts("0x...");// Use first DSA for one operationawait dsa.setInstance(accounts[0].id);const spell1 = dsa.Spell();spell1.add(/* ... */);await spell1.cast({ from: "0x..." });// Switch to second DSA for another operationawait dsa.setInstance(accounts[1].id);const spell2 = dsa.Spell();spell2.add(/* ... */);await spell2.cast({ from: "0x..." });console.log("Operations completed on multiple DSAs");
try { const instance = await dsa.setInstance(12345); console.log(`Successfully set DSA instance: ${instance.address}`);} catch (error) { if (error.message.includes("does not exist")) { console.error("This DSA ID does not exist"); } else { console.error("Failed to set instance:", error.message); }}
// Store the DSA ID in localStorageconst dsaId = 12345;localStorage.setItem("activeDsaId", dsaId.toString());// Later, restore the instanceconst storedId = parseInt(localStorage.getItem("activeDsaId"));if (storedId) { await dsa.setInstance(storedId); console.log("DSA instance restored from storage");}