Skip to content

Commit

Permalink
update actions
Browse files Browse the repository at this point in the history
  • Loading branch information
nvitorovic committed Apr 23, 2024
1 parent 602597a commit 084ae77
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 53 deletions.
15 changes: 6 additions & 9 deletions virtual-testnets/src/viem-send-tx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createWalletClient, http, parseEther } from "viem";
import { vMainnet } from "./tenderly.config";
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";

import { tenderlySetBalance } from "./viem-tenderly-actions";

const EXPLORER_BASE_URL = vMainnet.blockExplorers.default;

Expand All @@ -13,14 +13,11 @@ const account = privateKeyToAccount(generatePrivateKey());
transport: http(vMainnet.rpcUrls.default.http[0]),
});

await client.request({
//@ts-ignore
method: "tenderly_setBalance",
params: [
account.address,
"0xDE0B6B3A7640000",
],
});
await tenderlySetBalance(client, [
[account.address],
"0xDE0B6B3A7640000",
]);


const tx = await client.sendTransaction({
to: "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC",
Expand Down
9 changes: 4 additions & 5 deletions virtual-testnets/src/viem-simulate.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { createPublicClient, http } from "viem";
import { vMainnet } from "./tenderly.config";
import { tenderlySimulateTransaction } from "./viem-tenderly-actions";

const client = createPublicClient({
chain: vMainnet,
transport: http(process.env.VIRTUAL_MAINNET_RPC),
});

(async () => {
const simulation: any = await client.request({
//@ts-ignore
method: "tenderly_simulateTransaction",
params: [
const simulation = await tenderlySimulateTransaction(
client, [
// transaction object
{
from: "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
Expand All @@ -23,7 +22,7 @@ const client = createPublicClient({
// the block
"latest",
],
});
);

console.log("Trace");
console.log(JSON.stringify(simulation.trace, null, 2));
Expand Down
126 changes: 126 additions & 0 deletions virtual-testnets/src/viem-tenderly-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { Client, Hex } from "viem";

type TSetBalanceParams = [addresses: Hex[], value: Hex];

async function tenderlySetBalance(client: Client, params: TSetBalanceParams) {
return client.request<{
method: "tenderly_setBalance",
Parameters: TSetBalanceParams,
ReturnType: Hex
}>({
method: "tenderly_setBalance",
params: params,
});
}

type TSetErc20BalanceParams = [erc20: Hex, to: Hex, value: Hex]

async function tenderlySetErc20Balance(client: Client, params: TSetErc20BalanceParams) {
return client.request<{
method: "tenderly_setErc20Balance",
Parameters: TSetErc20BalanceParams,
ReturnType: Hex
}>({
method: "tenderly_setErc20Balance",
params: params,
});
}

type TSimulationRequestParams = [simulationRequest: {
from: Hex,
to: Hex,
gas: Hex,
gasPrice: Hex,
value: Hex,
data: Hex,
}, blockNumber: "latest" | Hex];


type AssetChange = {
"assetInfo": {
"standard": "ERC20" | "",
"type": "Fungible" | "NonFungible",
"contractAddress": Hex,
"symbol": string,
"name": string,
"logo": string,
"decimals": number,
"dollarValue": "${number}"
},
"type": string,
"from": Hex,
"to": Hex,
"rawAmount": Hex,
"amount": `${number}`,
"dollarValue": `${number}`,
}

type Log = {
"name": String,
"anonymous": boolean,
"inputs": {
"value": Hex,
"type": string,
"name": string
}[],
"raw": {
"address": Hex,
"topics": [
Hex,
Hex,
Hex
],
"data": Hex
}
}

type Trace = {
"type": string,
"from": Hex,
"to": Hex,
"gas": Hex,
"gasUsed": Hex,
"value": Hex,
"input": Hex,
"decodedInput": {
"value": Hex,
"type": string,
"name": string
}[],
"method": "string",
"output": Hex,
"decodedOutput": {
"value": boolean,
"type": string,
"name": string
}[],
"subtraces": number,
"traceAddress": any[]
}


async function tenderlySimulateTransaction(client: Client, params: TSimulationRequestParams) {
return client.request<
{
method: "tenderly_simulateTransaction",
Parameters: TSimulationRequestParams,
ReturnType: {
trace: Trace[],
logs: Log[],
assetChanges: AssetChange[],
balanceChanges: {
"address": Hex
"dollarValue": `${number}`,
"transfers": any
}[]
}
}
>({
method: "tenderly_simulateTransaction",
params: params,
});
}

export {
tenderlySetBalance, tenderlySetErc20Balance, tenderlySimulateTransaction,
};
47 changes: 8 additions & 39 deletions virtual-testnets/src/viem.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,26 @@
import { createPublicClient, http } from "viem";
import { vMainnet } from "./tenderly.config";
import { tenderlySetBalance, tenderlySetErc20Balance } from "./viem-tenderly-actions";

async function example() {

const EXPLORER_BASE_URL = vMainnet.blockExplorers.default;

const client = createPublicClient({
chain: vMainnet,
transport: http(vMainnet.rpcUrls.default.http[0]),
});


console.log("Block Number", await client.getBlockNumber());
console.log("Chain", await client.getChainId());

const strings = ["0x0d2026b3EE6eC71FC6746ADb6311F6d3Ba1C000B", "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"];

function tenderly_setBalance(addresses: string[], value: string = "0xDE0B6B3A7640000") {
return client.request<{
method: "tenderly_setBalance",
Parameters: [string[], string],
ReturnType: `0x${string}`
}>({
method: "tenderly_setBalance",
params: [addresses, value],
});
}

function tenderly_setErc20Balance(erc20: `0x${string}`, to: `0x${string}`, value: `0x${string}` = "0xDE0B6B3A7640000") {
return client.request<{
method: "tenderly_setErc20Balance",
Parameters: [`0x${string}`, `0x${string}`, `0x${string}`],
ReturnType: `0x${string}`
}>({
method: "tenderly_setErc20Balance",
params: [erc20, to, value],
});
}

``;
const strings: `0x${string}`[] = ["0x0d2026b3EE6eC71FC6746ADb6311F6d3Ba1C000B", "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"];

const balanceTxs = await Promise.all([
tenderly_setBalance(strings),
tenderly_setErc20Balance("0xdAC17F958D2ee523a2206206994597C13D831ec7",
"0x40BdB4497614bAe1A67061EE20AAdE3c2067AC9e"),
// USDT
tenderly_setErc20Balance("0x6B175474E89094C44Da98b954EedeAC495271d0F",
"0x40BdB4497614bAe1A67061EE20AAdE3c2067AC9e"),
tenderly_setErc20Balance("0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE",
"0x40BdB4497614bAe1A67061EE20AAdE3c2067AC9e",
"0xDE0B6B3A7640000"),
])
;
tenderlySetBalance(client, [strings, "0xDE0B6B3A7640000"]),
tenderlySetErc20Balance(client, ["0xdAC17F958D2ee523a2206206994597C13D831ec7", "0x40BdB4497614bAe1A67061EE20AAdE3c2067AC9e", "0xDE0B6B3A7640000"]),
// USDT
tenderlySetErc20Balance(client, ["0x6B175474E89094C44Da98b954EedeAC495271d0F", "0x40BdB4497614bAe1A67061EE20AAdE3c2067AC9e", "0xDE0B6B3A7640000"]),
tenderlySetErc20Balance(client, ["0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE", "0x40BdB4497614bAe1A67061EE20AAdE3c2067AC9e", "0xDE0B6B3A7640000"]),
]);

console.log(
"ETH balance, DAI and ShibaInu topups",
Expand Down

0 comments on commit 084ae77

Please sign in to comment.