Commit 86d948d2 authored by vicotor's avatar vicotor

update scripts

parent 2ff1117b
...@@ -41,6 +41,24 @@ async function treasuryApprove(tokenA, tokenB, bridgeAddr, treasury) { ...@@ -41,6 +41,24 @@ async function treasuryApprove(tokenA, tokenB, bridgeAddr, treasury) {
console.log("Approved TTB for bridge:", bridgeAddr); console.log("Approved TTB for bridge:", bridgeAddr);
} }
async function setTokenTreasuryConfig(tokenA, tokenB, bridge) {
var tx = await bridge.setTreasuryConfig(
await tokenA.getAddress(),
hre.ethers.parseEther('0.0001'),
500, // 5%
)
await tx.wait();
console.log("Set treasury config for TTA");
tx = await bridge.setTreasuryConfig(
await tokenB.getAddress(),
hre.ethers.parseEther('0.0001'),
500, // 5%
)
await tx.wait();
console.log("Set treasury config for TTB");
}
async function setConfig(tokenA, tokenB, bridge) { async function setConfig(tokenA, tokenB, bridge) {
if (!INITIAL_VALIDATORS || !TREASURY) { if (!INITIAL_VALIDATORS || !TREASURY) {
throw new Error("Environment variables BRIDGE_VALIDATORS or TREASURY are not set."); throw new Error("Environment variables BRIDGE_VALIDATORS or TREASURY are not set.");
...@@ -50,6 +68,7 @@ async function setConfig(tokenA, tokenB, bridge) { ...@@ -50,6 +68,7 @@ async function setConfig(tokenA, tokenB, bridge) {
await addValidators(bridge, INITIAL_VALIDATORS); await addValidators(bridge, INITIAL_VALIDATORS);
await setTreasury(bridge, TREASURY); await setTreasury(bridge, TREASURY);
await setTokenTreasuryConfig(tokenA, tokenB, bridge);
// if treasury is exist, do approve . // if treasury is exist, do approve .
var bridgeAddr = await bridge.getAddress(); var bridgeAddr = await bridge.getAddress();
......
const fs = require("fs");
const path = require("path");
const hre = require("hardhat");
let INITIAL_VALIDATORS = process.env.BRIDGE_VALIDATORS
let TREASURY = process.env.TREASURY
const DEPLOY_FILE = path.join(__dirname, "deploy.json");
async function addValidators(contract, validators) {
let validatorList = validators.split(',')
for (let i = 0; i < validatorList.length; i++) {
var tx = await contract.addValidator(validatorList[i])
await tx.wait();
console.log("Added validator:", validatorList[i]);
}
}
async function setTreasury(contract, treasury) {
var tx = await contract.setTreasury(treasury);
await tx.wait();
console.log("Set treasury to:", treasury);
}
async function mintToken(tokenA, tokenB, user) {
var amount = hre.ethers.parseEther("1000000")
var tx = await tokenA.mint(user, amount);
await tx.wait();
console.log("Minted TTA to user:", user, " amount:", hre.ethers.formatEther(amount));
tx = await tokenB.mint(user, amount);
await tx.wait();
console.log("Minted TTB to user:", user, " amount:", hre.ethers.formatEther(amount));
}
async function treasuryApprove(tokenA, tokenB, bridgeAddr, treasury) {
var tx = await tokenA.connect(treasury).approve(bridgeAddr, hre.ethers.MaxUint256);
await tx.wait();
console.log("Approved TTA for bridge:", bridgeAddr);
tx = await tokenB.connect(treasury).approve(bridgeAddr, hre.ethers.MaxUint256);
await tx.wait();
console.log("Approved TTB for bridge:", bridgeAddr);
}
async function setConfig(tokenA, tokenB, bridge) {
if (!INITIAL_VALIDATORS || !TREASURY) {
throw new Error("Environment variables BRIDGE_VALIDATORS or TREASURY are not set.");
}
[owner, treasury] = await hre.ethers.getSigners();
await addValidators(bridge, INITIAL_VALIDATORS);
await setTreasury(bridge, TREASURY);
// if treasury is exist, do approve .
var bridgeAddr = await bridge.getAddress();
if (treasury.address) {
// test environment, mint token and approve.
await mintToken(tokenA, tokenB, TREASURY);
await treasuryApprove(tokenA, tokenB, bridgeAddr, treasury);
}
}
async function getDeploy(chainId) {
if (!fs.existsSync(DEPLOY_FILE)) {
throw new Error("deploy.json file not found");
}
const deployData = JSON.parse(fs.readFileSync(DEPLOY_FILE, "utf8"));
if (!deployData[chainId]) {
throw new Error(`No deployment data found for chainId ${chainId}`);
}
const addrs = deployData[chainId];
const tokenA = await hre.ethers.getContractAt("TestToken", addrs.tokenA);
const tokenB = await hre.ethers.getContractAt("TestToken", addrs.tokenB);
const bridge = await hre.ethers.getContractAt("Bridge", addrs.bridge);
return {tokenA, tokenB, bridge}
}
async function setOutConfig(curTokenMap, targetTokenMap, targetChainId, bridge) {
const outConfigA = {
receiveToken: await targetTokenMap.tokenA.getAddress(),
fee: hre.ethers.parseEther("0.05"),
limit: hre.ethers.parseEther("1000"),
isBurn: false,
enabled: true,
};
const outConfigB = {
receiveToken: await targetTokenMap.tokenB.getAddress(),
fee: hre.ethers.parseEther("0.04"),
limit: hre.ethers.parseEther("1000"),
isBurn: false,
enabled: true,
}
var tx = await bridge.changeOutConfig(
await curTokenMap.tokenA.getAddress(),
targetChainId, // Target chain ID
outConfigA
);
await tx.wait();
console.log("Set out config for tokenA to chain", targetChainId, "tx", tx.hash);
tx = await bridge.changeOutConfig(
await curTokenMap.tokenB.getAddress(),
targetChainId, // Target chain ID
outConfigB
);
await tx.wait();
console.log("Set out config for tokenB to chain", targetChainId, "tx", tx.hash);
}
async function outTransfer(bridge, token , amount, targetChainId, receiver) {
// do approve
var approve = await token.approve(await bridge.getAddress(), amount);
await approve.wait();
console.log("Approved token:", await token.getAddress(), "amount:", hre.ethers.formatEther(amount));
const tx = await bridge.outTransfer(
await token.getAddress(),
amount,
targetChainId,
receiver
);
const receipt = await tx.wait();
console.log("Bridged token:", await token.getAddress(), "to chain", targetChainId, "tx", receipt.transactionHash);
}
// Define the script
async function main() {
const chainId = await hre.ethers.provider.getNetwork().then(network => network.chainId);
const targetChainId = chainId === 269n ? 10323 : 269;
console.log("Current chain ID:", chainId, "Target chain ID:", targetChainId);
const chainDeploy = await getDeploy(chainId);
await outTransfer(chainDeploy.bridge, chainDeploy.tokenA, hre.ethers.parseEther("100"),
targetChainId, "0x3ef35d7db5f2d6a2fe7d16d4c8d4a20e9ad64a6a");
await outTransfer(chainDeploy.bridge, chainDeploy.tokenB, hre.ethers.parseEther("2"),
targetChainId, "0x3ef35d7db5f2d6a2fe7d16d4c8d4a20e9ad64a6a");
}
// Run the script
main().catch((error) => {
console.error("Error:", error);
});
\ No newline at end of file
const fs = require("fs");
const path = require("path");
const hre = require("hardhat");
const {ethers} = require("hardhat");
const DEPLOY_FILE = path.join(__dirname, "deploy.json");
async function getDeploy(chainId) {
if (!fs.existsSync(DEPLOY_FILE)) {
throw new Error("deploy.json file not found");
}
const deployData = JSON.parse(fs.readFileSync(DEPLOY_FILE, "utf8"));
if (!deployData[chainId]) {
throw new Error(`No deployment data found for chainId ${chainId}`);
}
const addrs = deployData[chainId];
const tokenA = await hre.ethers.getContractAt("TestToken", addrs.tokenA);
const tokenB = await hre.ethers.getContractAt("TestToken", addrs.tokenB);
const bridge = await hre.ethers.getContractAt("Bridge", addrs.bridge);
return {tokenA, tokenB, bridge}
}
async function submitInTransfer(bridge) {
// do approve
var approve = await token.approve(await bridge.getAddress(), amount);
await approve.wait();
console.log("Approved token:", await token.getAddress(), "amount:", hre.ethers.formatEther(amount));
const tx = await bridge.outTransfer(
await token.getAddress(),
amount,
targetChainId,
receiver
);
const receipt = await tx.wait();
console.log("Bridged token:", await token.getAddress(), "to chain", targetChainId, "tx", receipt.transactionHash);
}
// Define the script
async function main() {
let TEST_VALIDATOR_KEY = process.env.TEST_VALIDATOR_PRIVATE_KEY;
if (!TEST_VALIDATOR_KEY) {
throw new Error("TEST_VALIDATOR_PRIVATE_KEY environment variable is not set");
}
const wallet = new hre.ethers.Wallet(TEST_VALIDATOR_KEY, hre.ethers.provider);
console.log("Using wallet address:", wallet.address);
const chainId = await hre.ethers.provider.getNetwork().then(network => network.chainId);
const targetChainId = chainId === 269n ? 10323 : 269;
console.log("Current chain ID:", chainId, "Target chain ID:", targetChainId);
const chainDeploy = await getDeploy(chainId);
// 10323 0x3Ef35d7Db5F2d6A2fe7d16D4C8d4A20e9aD64A6A 0xF5c10392D841d55C41EBf696A4E437b2DC91f5D3
// 99950000000000000000 1 269 0xfeed6dB33622Fb526a89c84A0861C29f483f1d0E 0xF5c10392D841d55C41EBf696A4E437b2DC91f5D3 100000000000000000000
// [173 7 190 114 90 113 152 216 28 141 140 44 56 193 112 16 201 218 15 200 10 31 177 5 222 100 31 193 182 38 224 133]}"
// sign=ad07be725a7198d81c8d8c2c38c17010c9da0fc80a1fb105de641fc1b626e085
const params = {
toChainID: 10323,
receiver: "0x3Ef35d7Db5F2d6A2fe7d16D4C8d4A20e9aD64A6A",
token: "0xF5c10392D841d55C41EBf696A4E437b2DC91f5D3",
amount: ethers.parseEther("99.95"),
outId: 1,
fromChainID: 269,
sender: "0xfeed6dB33622Fb526a89c84A0861C29f483f1d0E",
sendToken: "0xF5c10392D841d55C41EBf696A4E437b2DC91f5D3",
sendAmount: ethers.parseEther("100"),
signature: "0xad07be725a7198d81c8d8c2c38c17010c9da0fc80a1fb105de641fc1b626e085",
};
const bridge = chainDeploy.bridge;
var tx = await bridge.connect(wallet).submitInTransfer(params);
await tx.wait();
console.log("Submitted tx :", tx.hash);
}
// Run the script
main().catch((error) => {
console.error("Error:", error);
});
\ No newline at end of file
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("TestAbi Contract", function () {
let TestAbi, testAbi, owner;
beforeEach(async function () {
// Deploy the TestAbi contract
[owner] = await ethers.getSigners();
const TestAbiFactory = await ethers.getContractFactory("TestAbi");
testAbi = await TestAbiFactory.deploy(owner.address);
await testAbi.waitForDeployment();
});
it("should return the correct keccak256 hash for getSubmitData", async function () {
// Define the input parameters for getSubmitData
const params = {
toChainID: 2,
receiver: "0x000000000000000000000000000000000000dead",
token: "0x000000000000000000000000000000000000beef",
amount: ethers.parseEther("10"),
outId: 1,
fromChainID: 1,
sender: "0x000000000000000000000000000000000000cafe",
sendToken: "0x000000000000000000000000000000000000babe",
sendAmount: ethers.parseEther("10"),
signature: "0x000000000000000000000000000000000000babe000000000000000000000000",
};
// Compute the expected hash
var data = ethers.AbiCoder.defaultAbiCoder().encode(
[
"uint256",
"uint256",
"address",
"address",
"uint256",
"uint256",
"address",
"address",
],
[
params.outId,
params.fromChainID,
params.sender,
params.sendToken,
params.sendAmount,
params.toChainID,
params.receiver,
params.token,
]
);
console.log("Encoded data:", data);
const expectedHash = ethers.keccak256(data);
// Call the getSubmitData function
const result = await testAbi.getSubmitData(params);
console.log("Computed hash:", result);
console.log("Expected hash:", expectedHash);
// Assert that the result matches the expected hash
expect(result).to.equal(expectedHash);
});
});
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment