2021-08-18 20:20:45 +00:00
|
|
|
import { useContext, ref, onMounted, computed } from "@nuxtjs/composition-api";
|
|
|
|
import axios from "axios";
|
|
|
|
import { activeNetwork, useNetwork } from "./useNetwork";
|
2021-09-03 13:59:09 +00:00
|
|
|
import { useWeb3 } from "@instadapp/vue-web3";
|
2021-08-18 20:20:45 +00:00
|
|
|
import Web3 from "web3";
|
|
|
|
import { useDSA } from "./useDSA";
|
|
|
|
|
|
|
|
const forkId = ref(null);
|
|
|
|
export function useTenderly() {
|
|
|
|
const { $config } = useContext();
|
2021-09-02 23:00:02 +00:00
|
|
|
const { activate, deactivate, connector, library } = useWeb3();
|
2021-10-08 15:49:24 +00:00
|
|
|
const { activeNetworkId } = useNetwork();
|
2021-08-21 11:33:31 +00:00
|
|
|
const { accounts, refreshAccounts } = useDSA();
|
2021-08-18 20:20:45 +00:00
|
|
|
const canSimulate = computed(
|
2021-10-08 15:49:24 +00:00
|
|
|
() => activeNetworkId.value !== "arbitrum" && $config.TENDERLY_FORK_PATH && $config.TENDERLY_KEY
|
2021-08-18 20:20:45 +00:00
|
|
|
);
|
2021-08-19 16:28:25 +00:00
|
|
|
const loading = ref(false);
|
2021-08-18 20:20:45 +00:00
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
if (!canSimulate.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2021-09-02 23:15:14 +00:00
|
|
|
setForkId(window.localStorage.getItem("forkId"), true);
|
2021-08-18 20:20:45 +00:00
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
|
|
|
|
const startSimulation = async () => {
|
2021-08-19 16:28:25 +00:00
|
|
|
loading.value = true;
|
2021-08-18 20:20:45 +00:00
|
|
|
try {
|
|
|
|
const { data } = await axios({
|
|
|
|
method: "post",
|
|
|
|
url: `https://api.tenderly.co/api/v1/account/${$config.TENDERLY_FORK_PATH}/fork`,
|
|
|
|
headers: {
|
|
|
|
"X-Access-key": $config.TENDERLY_KEY,
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
},
|
|
|
|
data: JSON.stringify({
|
|
|
|
network_id: activeNetwork.value.chainId.toString()
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-08-21 11:33:31 +00:00
|
|
|
await setForkId(data?.simulation_fork?.id);
|
2021-08-18 20:20:45 +00:00
|
|
|
if (data?.simulation_fork?.id) {
|
2021-08-21 11:33:31 +00:00
|
|
|
await addBalance();
|
|
|
|
await refreshAccounts();
|
2021-08-18 20:20:45 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-08-21 11:33:31 +00:00
|
|
|
await stopSimulation();
|
2021-08-18 20:20:45 +00:00
|
|
|
}
|
2021-08-19 16:28:25 +00:00
|
|
|
loading.value = false;
|
2021-08-18 20:20:45 +00:00
|
|
|
};
|
|
|
|
|
2021-09-02 23:15:14 +00:00
|
|
|
const stopSimulation = async (silent = false) => {
|
2021-08-19 16:28:25 +00:00
|
|
|
loading.value = true;
|
2021-09-02 23:00:02 +00:00
|
|
|
|
2021-08-18 20:20:45 +00:00
|
|
|
try {
|
2021-08-26 22:27:26 +00:00
|
|
|
if (forkId.value) {
|
|
|
|
await axios({
|
|
|
|
method: "delete",
|
|
|
|
url: `https://api.tenderly.co/api/v1/account/${$config.TENDERLY_FORK_PATH}/fork/${forkId.value}`,
|
|
|
|
headers: {
|
|
|
|
"X-Access-key": $config.TENDERLY_KEY,
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-08-18 20:20:45 +00:00
|
|
|
} catch (error) {}
|
|
|
|
|
|
|
|
forkId.value = null;
|
|
|
|
window.localStorage.removeItem("forkId");
|
2021-09-02 23:15:14 +00:00
|
|
|
|
|
|
|
if (!silent && connector.value) {
|
|
|
|
deactivate();
|
|
|
|
activate(connector.value);
|
|
|
|
}
|
|
|
|
|
2021-08-19 16:28:25 +00:00
|
|
|
loading.value = false;
|
2021-08-18 20:20:45 +00:00
|
|
|
};
|
|
|
|
|
2021-09-02 23:15:14 +00:00
|
|
|
const setForkId = async (fork, silent = false) => {
|
2021-08-18 20:20:45 +00:00
|
|
|
if (!fork) {
|
2021-09-02 23:15:14 +00:00
|
|
|
stopSimulation(silent);
|
2021-08-18 20:20:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
forkId.value = fork;
|
2021-09-02 23:00:02 +00:00
|
|
|
|
|
|
|
library.value = new Web3(
|
|
|
|
new Web3.providers.HttpProvider(
|
|
|
|
`https://rpc.tenderly.co/fork/${forkId.value}`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2021-08-18 20:20:45 +00:00
|
|
|
window.localStorage.setItem("forkId", forkId.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const addBalance = async () => {
|
|
|
|
await axios({
|
|
|
|
method: "post",
|
|
|
|
url: `https://api.tenderly.co/api/v1/account/${$config.TENDERLY_FORK_PATH}/fork/${forkId.value}/balance`,
|
|
|
|
headers: {
|
|
|
|
"X-Access-key": $config.TENDERLY_KEY,
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
},
|
|
|
|
data: JSON.stringify({
|
|
|
|
accounts: accounts.value.map(a => a.address)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
forkId,
|
|
|
|
canSimulate,
|
|
|
|
startSimulation,
|
2021-08-19 16:28:25 +00:00
|
|
|
stopSimulation,
|
2021-09-02 23:00:02 +00:00
|
|
|
loading
|
2021-08-18 20:20:45 +00:00
|
|
|
};
|
|
|
|
}
|