Skip to content

Commit

Permalink
working SRC functions
Browse files Browse the repository at this point in the history
  • Loading branch information
olegkron committed Apr 10, 2024
1 parent 90750e3 commit 9cb2190
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 144 deletions.
138 changes: 39 additions & 99 deletions CLFunctions/CLF_src/CLFsrc-CUSTOM-TRANSPORT-FUNCTIONS-SIMULATION.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,48 @@ numAllowedQueries: 2 – a minimum to initialise Viem.
// WALLET_PRIVATE_KEY: '44c04f3751b5e35344400ab7f7e561c3b80c02c2f87de69a561ecbf6d0018896',
// INFURA_API_KEY: '8acf47c71165427f8cee3a92fea12da2',
// };
// const args = ["12532609583862916517"];
// const chainSelectors = {
// "12532609583862916517": {id: 80001, url: `https://polygon-mumbai.infura.io/v3/${secrets.INFURA_API_KEY}`},
// "14767482510784806043": {id: 43113, url: `https://avalanche-fuji.infura.io/v3/${secrets.INFURA_API_KEY}`}
// };

// async function sendTx() {
// const url = `https://polygon-mumbai.infura.io/v3/${secrets.INFURA_API_KEY}`;

const url = `https://polygon-mumbai.gateway.tenderly.co`;
const { BaseError, ContractFunctionRevertedError, createWalletClient, createPublicClient, custom } = await import(
'npm:viem'
);
const { createWalletClient, custom } = await import('npm:viem');
const { privateKeyToAccount } = await import('npm:viem/accounts');
const { polygonMumbai } = await import('npm:viem/chains');

try {
const client = createWalletClient({
chain: polygonMumbai,
transport: custom({
async request({ method, params }) {
if (method === 'eth_chainId') return '0x13881';
if (method === 'eth_estimateGas') return '0x3d090';
if (method === 'eth_maxPriorityFeePerGas') return '0x3b9aca00';
const response = await Functions.makeHttpRequest({
url,
method: 'post',
headers: { 'Content-Type': 'application/json' },
data: { jsonrpc: '2.0', id: 1, method, params },
});
return response.data.result;
},
}),
});

const publicClient = createPublicClient({
const { polygonMumbai, avalancheFuji } = await import('npm:viem/chains');
const [contractAddress, ccipMessageId, sender, recipient, amount, srcChainSelector, dstChainSelector, token] = args;
const chainSelectors = {
'12532609583862916517': {
url: `https://polygon-mumbai.infura.io/v3/${secrets.INFURA_API_KEY}`,
chain: polygonMumbai,
},
'14767482510784806043': {
url: `https://avalanche-fuji.infura.io/v3/${secrets.INFURA_API_KEY}`,
chain: avalancheFuji,
},
};
const abi = [
{
name: 'addUnconfirmedTX',
type: 'function',
inputs: [
{ type: 'bytes32', name: 'ccipMessageId' },
{ type: 'address', name: 'sender' },
{ type: 'address', name: 'recipient' },
{ type: 'uint256', name: 'amount' },
{ type: 'uint64', name: 'srcChainSelector' },
{ type: 'address', name: 'token' },
],
outputs: [],
},
];
try {
const account = privateKeyToAccount('0x' + secrets.WALLET_PRIVATE_KEY);
const walletClient = createWalletClient({
account,
chain: chainSelectors[dstChainSelector].chain,
transport: custom({
async request({ method, params }) {
if (method === 'eth_chainId') return '0x13881';
if (method === 'eth_chainId') return chainSelectors[dstChainSelector].chain.id;
if (method === 'eth_estimateGas') return '0x3d090';
if (method === 'eth_maxPriorityFeePerGas') return '0x3b9aca00';
const response = await Functions.makeHttpRequest({
url,
url: chainSelectors[dstChainSelector].url,
method: 'post',
headers: { 'Content-Type': 'application/json' },
data: { jsonrpc: '2.0', id: 1, method, params },
Expand All @@ -62,72 +60,14 @@ try {
},
}),
});

const account = privateKeyToAccount('0x' + secrets.WALLET_PRIVATE_KEY);
const abi = [
{
name: 'addUnconfirmedTX',
type: 'function',
inputs: [
{ type: 'bytes32', name: 'ccipMessageId' },
{ type: 'address', name: 'sender' },
{ type: 'address', name: 'recipient' },
{ type: 'uint256', name: 'amount' },
{ type: 'uint64', name: 'srcChainSelector' },
{ type: 'address', name: 'token' },
],
outputs: [],
},
{
inputs: [
{
internalType: 'bytes32',
name: 'txHash',
type: 'bytes32',
},
{
internalType: 'bool',
name: 'isConfirmed',
type: 'bool',
},
],
name: 'TXAlreadyExists',
type: 'error',
},
];
const sim = await publicClient.simulateContract({
account,
const hash = await walletClient.writeContract({
abi,
functionName: 'addUnconfirmedTX',
address: '0x59d607709841174d20aAFc5e8A1357C4940e8e9F',
args: [
'0x2a326e9ea0f849048bfb59996e02f0082df9298550249d7c6cefec78e7e24cd8',
'0x70E73f067a1fC9FE6D53151bd271715811746d3a',
'0x70E73f067a1fC9FE6D53151bd271715811746d3a',
'1000000000000000000',
parseInt('12532609583862916517'),
'0x9999f7Fea5938fD3b1E26A12c3f2fb024e194f97',
],
address: contractAddress,
args: [ccipMessageId, sender, recipient, amount, BigInt(srcChainSelector), token],
gas: 1000000n,
});
// const hash = await client.writeContract({
// account,
// abi,
// functionName: 'addUnconfirmedTX',
// address: '0x59d607709841174d20aAFc5e8A1357C4940e8e9F',
// args: [
// '0x8b666e9ea0f849048bfb59996e02f0082df9298550249d7c6cefec78e7e24cd8',
// '0x70E73f067a1fC9FE6D53151bd271715811746d3a',
// '0x70E73f067a1fC9FE6D53151bd271715811746d3a',
// '1000000000000000000',
// parseInt('12532609583862916517'),
// '0x9999f7Fea5938fD3b1E26A12c3f2fb024e194f97',
// ],
// chain: polygonMumbai,
// });
console.log(sim);
// return Functions.encodeString(hash);
return Functions.encodeString(hash);
} catch (err) {
console.log('error');
console.log(err);
return Functions.encodeString('error');
}
97 changes: 53 additions & 44 deletions CLFunctions/simulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,67 @@ COMPATIBLE WITH NODE v18.20.1 or earlier
run with: node simulate.js
*/

const { simulateScript } = require('@chainlink/functions-toolkit');
const fs = require('node:fs');
const dotenv = require('dotenv');
const decodeHexString = require('./utils/decodeHexString');
dotenv.config({ path: '../.env' });
const { simulateScript } = require("@chainlink/functions-toolkit");
const fs = require("node:fs");
const dotenv = require("dotenv");
const decodeHexString = require("./utils/decodeHexString");
dotenv.config({ path: "../.env" });

const secrets = {
WALLET_PRIVATE_KEY: process.env.SECOND_TEST_WALLET_PRIVATE_KEY,
INFURA_API_KEY: process.env.INFURA_API_KEY,
WALLET_PRIVATE_KEY: process.env.SECOND_TEST_WALLET_PRIVATE_KEY,
INFURA_API_KEY: process.env.INFURA_API_KEY,
};

async function simulate(pathToFile, args) {
const { responseBytesHexstring, errorString, capturedTerminalOutput } = await simulateScript({
source: fs.readFileSync(pathToFile, 'utf8'),
args,
secrets,
maxOnChainResponseBytes: 256,
maxExecutionTimeMs: 100000,
maxMemoryUsageMb: 128,
numAllowedQueries: 5,
maxQueryDurationMs: 10000,
maxQueryUrlLength: 2048,
maxQueryRequestBytes: 2048,
maxQueryResponseBytes: 2097152,
});
const { responseBytesHexstring, errorString, capturedTerminalOutput } = await simulateScript({
source: fs.readFileSync(pathToFile, "utf8"),
args,
secrets,
maxOnChainResponseBytes: 256,
maxExecutionTimeMs: 100000,
maxMemoryUsageMb: 128,
numAllowedQueries: 5,
maxQueryDurationMs: 10000,
maxQueryUrlLength: 2048,
maxQueryRequestBytes: 2048,
maxQueryResponseBytes: 2097152,
});

if (errorString) {
console.log('CAPTURED ERROR:');
console.log(errorString);
}
if (errorString) {
console.log("CAPTURED ERROR:");
console.log(errorString);
}

if (capturedTerminalOutput) {
console.log('CAPTURED TERMINAL OUTPUT:');
console.log(capturedTerminalOutput);
}
if (capturedTerminalOutput) {
console.log("CAPTURED TERMINAL OUTPUT:");
console.log(capturedTerminalOutput);
}

if (responseBytesHexstring) {
console.log('RESPONSE BYTES HEXSTRING:');
console.log(responseBytesHexstring);
console.log('RESPONSE BYTES DECODED:');
console.log(responseBytesHexstring);
}
if (responseBytesHexstring) {
console.log("RESPONSE BYTES HEXSTRING:");
console.log(responseBytesHexstring);
console.log("RESPONSE BYTES DECODED:");
console.log(responseBytesHexstring);
}
}

// simulate('./CLF_src/CLFsrc-CUSTOM-TRANSPORT-FUNCTIONS-SIMULATION.js', []);
simulate('./CLF_dst/CLFunctionsDST.js', [
'0x4200A2257C399C1223f8F3122971eb6fafaaA976',
'0xb47d30d9660222539498f85cefc5337257f8e0ebeabbce312108f218555ced50',
'0x70E73f067a1fC9FE6D53151bd271715811746d3a',
'0x70E73f067a1fC9FE6D53151bd271715811746d3a',
'0xf1E3A5842EeEF51F2967b3F05D45DD4f4205FF40',
'1000000000000000',
'12532609583862916517',
'14767482510784806043',
simulate("./CLF_src/CLFsrc-CUSTOM-TRANSPORT-FUNCTIONS-SIMULATION.js", [
"0xa866BAcF9b8cf8beFC424Ec1EA253c0Ee7240118",
"0x1ab32e9ea01849048bfb59996e02f0082df9298550249d7c6cefec78e7e24cd8",
"0x70E73f067a1fC9FE6D53151bd271715811746d3a",
"0x70E73f067a1fC9FE6D53151bd271715811746d3a",
"1000000000000000000",
"14767482510784806043",
"12532609583862916517",
"0x9999f7Fea5938fD3b1E26A12c3f2fb024e194f97",
]);
// simulate('./CLF_dst/CLFunctionsDST.js', [
// '0x4200A2257C399C1223f8F3122971eb6fafaaA976',
// '0xb47d30d9660222539498f85cefc5337257f8e0ebeabbce312108f218555ced50',
// '0x70E73f067a1fC9FE6D53151bd271715811746d3a',
// '0x70E73f067a1fC9FE6D53151bd271715811746d3a',
// '0xf1E3A5842EeEF51F2967b3F05D45DD4f4205FF40',
// '1000000000000000',
// '12532609583862916517',
// '14767482510784806043',
// ]);
1 change: 0 additions & 1 deletion packages/hardhat/deploy/00_CFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ const deployCFunctions: DeployFunction = async function (hre: HardhatRuntimeEnvi

export default deployCFunctions;
deployCFunctions.tags = ["CFunctions"];
// yarn deploy --tags CFunctions

0 comments on commit 9cb2190

Please sign in to comment.