From ae4596a0abc56ad4b93f9e88c3df4e836b4e2e06 Mon Sep 17 00:00:00 2001 From: Samuel Manzanera Date: Mon, 25 Nov 2024 14:22:19 +0100 Subject: [PATCH 1/2] Add Multisig SDK & update commands --- .../contract_management/deploy_multisig.js | 91 +++++++++++++++++++ .../contract_management/deploy_router.js | 21 ++++- .../contract_management/get_router_address.js | 54 +++++++++++ .../contract_management/update_farm_dates.js | 29 +++++- .../contract_management/update_farms.js | 29 +++++- .../contract_management/update_lp_fee.js | 29 +++++- .../contract_management/update_pools.js | 33 ++++++- .../update_protocol_fee.js | 29 +++++- .../contract_management/update_router.js | 42 ++++++--- contracts/commands/test/deploy_farm.js | 70 ++++++++++---- contracts/commands/utils.js | 55 ++++++++--- contracts/dex.js | 6 ++ contracts/package-lock.json | 53 ++++++++++- contracts/package.json | 1 + 14 files changed, 475 insertions(+), 67 deletions(-) create mode 100644 contracts/commands/contract_management/deploy_multisig.js create mode 100644 contracts/commands/contract_management/get_router_address.js diff --git a/contracts/commands/contract_management/deploy_multisig.js b/contracts/commands/contract_management/deploy_multisig.js new file mode 100644 index 00000000..e18dc07c --- /dev/null +++ b/contracts/commands/contract_management/deploy_multisig.js @@ -0,0 +1,91 @@ +import Archethic, { Utils } from "@archethicjs/sdk" +import config from "../../config.js" +import { + getServiceGenesisAddress, +} from "../utils.js" + +import { getDeployTransaction } from "@archethicjs/multisig-sdk" + +const command = "deploy_multisig" +const describe = "Deploy multisig for keychain master" +const builder = { + initial_voter: { + describe: "The initial voter address", + demandOption: true, // Required + type: "string" + }, + access_seed: { + describe: "The Keychain access seed (default in env config)", + demandOption: false, + type: "string" + }, + env: { + describe: "The environment config to use (default to local)", + demandOption: false, + type: "string" + } +} + +const handler = async function(argv) { + const envName = argv["env"] ? argv["env"] : "local" + const env = config.environments[envName] + + const keychainAccessSeed = argv["access_seed"] ? argv["access_seed"] : env.keychainAccessSeed + + if (keychainAccessSeed == undefined) { + console.log("Keychain access seed not defined") + process.exit(1) + } + + const initialVoter = argv["initial_voter"] + + const archethic = new Archethic(env.endpoint) + await archethic.connect() + + let keychain + + try { + keychain = await archethic.account.getKeychain(keychainAccessSeed) + } catch (err) { + console.log(err) + process.exit(1) + } + + const masterGenesisAddress = getServiceGenesisAddress(keychain, "Master") + + const storageNoncePublicKey = await archethic.network.getStorageNoncePublicKey(); + + const res = keychain.ecEncryptServiceSeed("Master", [storageNoncePublicKey]) + const { secret, authorizedPublicKeys: authorizedKeys } = res + console.log("Master genesis address:", masterGenesisAddress) + const index = await archethic.transaction.getTransactionIndex(masterGenesisAddress) + + let updateTx = getDeployTransaction(archethic, { + voters: [ initialVoter ], + confirmationThreshold: 1, + }, secret, authorizedKeys) + + updateTx = keychain.buildTransaction(updateTx, "Master", index).originSign(Utils.originPrivateKey) + + updateTx.on("fullConfirmation", async (_confirmations) => { + const txAddress = Utils.uint8ArrayToHex(updateTx.address) + console.log("Transaction validated !") + console.log("Address:", txAddress) + console.log(env.endpoint + "/explorer/transaction/" + txAddress) + process.exit(0) + }).on("error", (context, reason) => { + console.log("Error while sending transaction") + console.log("Contest:", context) + console.log("Reason:", reason) + process.exit(1) + }).send() +} + + + +export default { + command, + describe, + builder, + handler +} \ No newline at end of file diff --git a/contracts/commands/contract_management/deploy_router.js b/contracts/commands/contract_management/deploy_router.js index b98c3297..0762c4ed 100644 --- a/contracts/commands/contract_management/deploy_router.js +++ b/contracts/commands/contract_management/deploy_router.js @@ -1,6 +1,6 @@ import Archethic, { Utils } from "@archethicjs/sdk" import config from "../../config.js" -import { getRouterCode, getServiceGenesisAddress, sendTransactionWithFunding } from "../utils.js" +import { getRouterCode, getServiceGenesisAddress, sendTransactionWithFunding, sendTransactionWithoutFunding } from "../utils.js" const command = "deploy_router" const describe = "Deploy the router" @@ -14,6 +14,12 @@ const builder = { describe: "The environment config to use (default to local)", demandOption: false, type: "string" + }, + without_funding: { + describe: "Determines if the funding must be disabled", + demandOption: false, + type: "boolean", + default: false } } @@ -61,9 +67,16 @@ const handler = async function(argv) { routerTx = keychain.buildTransaction(routerTx, "Router", index).originSign(Utils.originPrivateKey) - sendTransactionWithFunding(routerTx, keychain, archethic) - .then(() => process.exit(0)) - .catch(() => process.exit(1)) + if (argv["without_funding"]) { + sendTransactionWithoutFunding(routerTx, archethic) + .then(() => process.exit(0)) + .catch(() => process.exit(1)) + } + else { + sendTransactionWithFunding(routerTx, keychain, archethic) + .then(() => process.exit(0)) + .catch(() => process.exit(1)) + } } export default { diff --git a/contracts/commands/contract_management/get_router_address.js b/contracts/commands/contract_management/get_router_address.js new file mode 100644 index 00000000..17aebada --- /dev/null +++ b/contracts/commands/contract_management/get_router_address.js @@ -0,0 +1,54 @@ +import Archethic from "@archethicjs/sdk" +import config from "../../config.js" +import { + getServiceGenesisAddress, +} from "../utils.js" + +const command = "get_router_address" +const describe = "Get router address" +const builder = { + access_seed: { + describe: "The Keychain access seed (default in env config)", + demandOption: false, + type: "string" + }, + env: { + describe: "The environment config to use (default to local)", + demandOption: false, + type: "string" + } +} + +const handler = async function(argv) { + const envName = argv["env"] ? argv["env"] : "local" + const env = config.environments[envName] + + const keychainAccessSeed = argv["access_seed"] ? argv["access_seed"] : env.keychainAccessSeed + + if (keychainAccessSeed == undefined) { + console.log("Keychain access seed not defined") + process.exit(1) + } + + const archethic = new Archethic(env.endpoint) + await archethic.connect() + + let keychain + + try { + keychain = await archethic.account.getKeychain(keychainAccessSeed) + } catch (err) { + console.log(err) + process.exit(1) + } + + const routerGenesisAddress = getServiceGenesisAddress(keychain, "Router") + console.log("Router address:", routerGenesisAddress) +} + +export default { + command, + describe, + builder, + handler +} \ No newline at end of file diff --git a/contracts/commands/contract_management/update_farm_dates.js b/contracts/commands/contract_management/update_farm_dates.js index 71b7acc1..caf90c53 100644 --- a/contracts/commands/contract_management/update_farm_dates.js +++ b/contracts/commands/contract_management/update_farm_dates.js @@ -1,6 +1,7 @@ import Archethic, { Utils } from "@archethicjs/sdk" import config from "../../config.js" -import { getServiceGenesisAddress } from "../utils.js" +import { getServiceGenesisAddress, sendWithWallet } from "../utils.js" +import { getProposeTransaction } from "@archethicjs/multisig-sdk" const command = "update_farm_dates" const describe = "Update the start and end date of a farm" @@ -29,7 +30,13 @@ const builder = { describe: "The environment config to use (default to local)", demandOption: false, type: "string" - } + }, + with_multisig: { + describe: "Determines if the master is using a multisig", + demandOption: false, + type: "boolean", + default: false + }, } const handler = async function(argv) { @@ -43,7 +50,10 @@ const handler = async function(argv) { process.exit(1) } - const archethic = new Archethic(env.endpoint) + const archethic = new Archethic(argv["with_multisig"] ? undefined : env.endpoint) + if (archethic.rpcWallet) { + archethic.rpcWallet.setOrigin({ name: "Archethic DEX CLI" }); + } await archethic.connect() let keychain @@ -63,6 +73,19 @@ const handler = async function(argv) { console.log("Master genesis address:", masterGenesisAddress) const index = await archethic.transaction.getTransactionIndex(masterGenesisAddress) + if (argv["with_multisig"]) { + const updateTx = getProposeTransaction(archethic, masterGenesisAddress, { + recipients: [ + { address: farmAddress, action: "update_dates", args: [startDate, endDate] } + ] + }) + sendWithWallet(updateTx, archethic) + .then(() => process.exit(0)) + .catch(() => process.exit(1)) + + return + } + let updateTx = archethic.transaction.new() .setType("transfer") .addRecipient(farmAddress, "update_dates", [startDate, endDate]) diff --git a/contracts/commands/contract_management/update_farms.js b/contracts/commands/contract_management/update_farms.js index 69b5a4de..850ebc28 100644 --- a/contracts/commands/contract_management/update_farms.js +++ b/contracts/commands/contract_management/update_farms.js @@ -1,6 +1,7 @@ import Archethic, { Utils } from "@archethicjs/sdk" import config from "../../config.js" -import { getServiceGenesisAddress } from "../utils.js" +import { getServiceGenesisAddress, sendWithWallet } from "../utils.js" +import { getProposeTransaction } from "@archethicjs/multisig-sdk" const command = "update_farms" const describe = "Update all farm code" @@ -14,7 +15,13 @@ const builder = { describe: "The environment config to use (default to local)", demandOption: false, type: "string" - } + }, + with_multisig: { + describe: "Determines if the master is using a multisig", + demandOption: false, + type: "boolean", + default: false + }, } const handler = async function(argv) { @@ -28,7 +35,10 @@ const handler = async function(argv) { process.exit(1) } - const archethic = new Archethic(env.endpoint) + const archethic = new Archethic(argv["with_multisig"] ? undefined : env.endpoint) + if (archethic.rpcWallet) { + archethic.rpcWallet.setOrigin({ name: "Archethic DEX CLI" }); + } await archethic.connect() let keychain @@ -52,6 +62,19 @@ const handler = async function(argv) { console.log("Master genesis address:", masterGenesisAddress) const index = await archethic.transaction.getTransactionIndex(masterGenesisAddress) + if (argv["with_multisig"]) { + const updateTx = getProposeTransaction(archethic, masterGenesisAddress, { + recipients: [ + { address: routerGenesisAddress, action: "update_farms_code" } + ] + }) + sendWithWallet(updateTx, archethic) + .then(() => process.exit(0)) + .catch(() => process.exit(1)) + + return + } + let updateTx = archethic.transaction.new() .setType("transfer") .addRecipient(routerGenesisAddress, "update_farms_code") diff --git a/contracts/commands/contract_management/update_lp_fee.js b/contracts/commands/contract_management/update_lp_fee.js index c1537acc..01a21046 100644 --- a/contracts/commands/contract_management/update_lp_fee.js +++ b/contracts/commands/contract_management/update_lp_fee.js @@ -1,6 +1,7 @@ import Archethic, { Utils } from "@archethicjs/sdk" import config from "../../config.js" -import { getServiceGenesisAddress } from "../utils.js" +import { getServiceGenesisAddress, sendWithWallet } from "../utils.js" +import { getProposeTransaction } from "@archethicjs/multisig-sdk" const command = "update_lp_fee" const describe = "Update the liquidity provider fee for a pool" @@ -24,7 +25,13 @@ const builder = { describe: "The environment config to use (default to local)", demandOption: false, type: "string" - } + }, + with_multisig: { + describe: "Determines if the master is using a multisig", + demandOption: false, + type: "boolean", + default: false + }, } const handler = async function(argv) { @@ -38,7 +45,10 @@ const handler = async function(argv) { process.exit(1) } - const archethic = new Archethic(env.endpoint) + const archethic = new Archethic(argv["with_multisig"] ? undefined : env.endpoint) + if (archethic.rpcWallet) { + archethic.rpcWallet.setOrigin({ name: "Archethic DEX CLI" }); + } await archethic.connect() let keychain @@ -57,6 +67,19 @@ const handler = async function(argv) { console.log("Master genesis address:", masterGenesisAddress) const index = await archethic.transaction.getTransactionIndex(masterGenesisAddress) + if (argv["with_multisig"]) { + const updateTx = getProposeTransaction(archethic, masterGenesisAddress, { + recipients: [ + { address: poolAddress, action: "set_lp_fee", args: [newFee] } + ] + }) + sendWithWallet(updateTx, archethic) + .then(() => process.exit(0)) + .catch(() => process.exit(1)) + + return + } + let updateTx = archethic.transaction.new() .setType("transfer") .addRecipient(poolAddress, "set_lp_fee", [newFee]) diff --git a/contracts/commands/contract_management/update_pools.js b/contracts/commands/contract_management/update_pools.js index 6b6364fa..e1dcf3c7 100644 --- a/contracts/commands/contract_management/update_pools.js +++ b/contracts/commands/contract_management/update_pools.js @@ -1,6 +1,7 @@ import Archethic, { Utils } from "@archethicjs/sdk" import config from "../../config.js" -import { getServiceGenesisAddress } from "../utils.js" +import { getServiceGenesisAddress, sendWithWallet } from "../utils.js" +import { getProposeTransaction } from "@archethicjs/multisig-sdk" const command = "update_pools" const describe = "Update all pool code" @@ -14,7 +15,13 @@ const builder = { describe: "The environment config to use (default to local)", demandOption: false, type: "string" - } + }, + with_multisig: { + describe: "Determines if the master is using a multisig", + demandOption: false, + type: "boolean", + default: false + }, } const handler = async function(argv) { @@ -28,7 +35,10 @@ const handler = async function(argv) { process.exit(1) } - const archethic = new Archethic(env.endpoint) + const archethic = new Archethic(argv["with_multisig"] ? undefined : env.endpoint) + if (archethic.rpcWallet) { + archethic.rpcWallet.setOrigin({ name: "Archethic DEX CLI" }); + } await archethic.connect() let keychain @@ -56,14 +66,27 @@ const handler = async function(argv) { const chunkSize = 10; for (let i = 0; i < pools.length; i += chunkSize) { const chunk = pools.slice(i, i + chunkSize).map(poolInfo => poolInfo.address); - await sendUpdateTx(archethic, chunk, routerGenesisAddress, masterGenesisAddress, keychain) + await sendUpdateTx(archethic, chunk, routerGenesisAddress, masterGenesisAddress, keychain, argv["with_multisig"]) } process.exit(0) } -async function sendUpdateTx(archethic, chunk, routerAddress, masterAddress, keychain) { +async function sendUpdateTx(archethic, chunk, routerAddress, masterAddress, keychain, withMultisig) { return new Promise(async (resolve, _reject) => { + if (withMultisig) { + const updateTx = getProposeTransaction(archethic, masterAddress, { + recipients: [ + { address: routerAddress, action: "update_pools_code", args: [chunk] } + ] + }) + sendWithWallet(updateTx, archethic) + .then(() => process.exit(0)) + .catch(() => process.exit(1)) + + return + } + let updateTx = archethic.transaction.new() .setType("transfer") .addRecipient(routerAddress, "update_pools_code", [chunk]) diff --git a/contracts/commands/contract_management/update_protocol_fee.js b/contracts/commands/contract_management/update_protocol_fee.js index d69899af..8a7bdc49 100644 --- a/contracts/commands/contract_management/update_protocol_fee.js +++ b/contracts/commands/contract_management/update_protocol_fee.js @@ -1,6 +1,7 @@ import Archethic, { Utils } from "@archethicjs/sdk" import config from "../../config.js" -import { getServiceGenesisAddress } from "../utils.js" +import { getServiceGenesisAddress, sendWithWallet } from "../utils.js" +import { getProposeTransaction } from "@archethicjs/multisig-sdk" const command = "update_protocol_fee" const describe = "Update the protocol fee for a pool" @@ -24,7 +25,13 @@ const builder = { describe: "The environment config to use (default to local)", demandOption: false, type: "string" - } + }, + with_multisig: { + describe: "Determines if the master is using a multisig", + demandOption: false, + type: "boolean", + default: false + }, } const handler = async function(argv) { @@ -38,7 +45,10 @@ const handler = async function(argv) { process.exit(1) } - const archethic = new Archethic(env.endpoint) + const archethic = new Archethic(argv["with_multisig"] ? undefined : env.endpoint) + if (archethic.rpcWallet) { + archethic.rpcWallet.setOrigin({ name: "Archethic DEX CLI" }); + } await archethic.connect() let keychain @@ -57,6 +67,19 @@ const handler = async function(argv) { console.log("Master genesis address:", masterGenesisAddress) const index = await archethic.transaction.getTransactionIndex(masterGenesisAddress) + if (argv["with_multisig"]) { + const updateTx = getProposeTransaction(archethic, masterGenesisAddress, { + recipients: [ + { address: poolAddress, action: "set_protocol_fee", args: [newFee] } + ] + }) + sendWithWallet(updateTx, archethic) + .then(() => process.exit(0)) + .catch(() => process.exit(1)) + + return + } + let updateTx = archethic.transaction.new() .setType("transfer") .addRecipient(poolAddress, "set_protocol_fee", [newFee]) diff --git a/contracts/commands/contract_management/update_router.js b/contracts/commands/contract_management/update_router.js index 9f332676..639e52db 100644 --- a/contracts/commands/contract_management/update_router.js +++ b/contracts/commands/contract_management/update_router.js @@ -1,6 +1,7 @@ import Archethic, { Utils } from "@archethicjs/sdk" import config from "../../config.js" -import { getServiceGenesisAddress, getRouterCode } from "../utils.js" +import { getServiceGenesisAddress, getRouterCode, sendTransactionWithoutFunding, sendWithWallet } from "../utils.js" +import { getProposeTransaction } from "@archethicjs/multisig-sdk" const command = "update_router" const describe = "Update the router" @@ -14,6 +15,12 @@ const builder = { describe: "The environment config to use (default to local)", demandOption: false, type: "string" + }, + with_multisig: { + describe: "Determines if the master is using a multisig", + demandOption: false, + type: "boolean", + default: false } } @@ -28,7 +35,10 @@ const handler = async function(argv) { process.exit(1) } - const archethic = new Archethic(env.endpoint) + const archethic = new Archethic(argv["with_multisig"] ? undefined : env.endpoint) + if (archethic.rpcWallet) { + archethic.rpcWallet.setOrigin({ name: "Archethic DEX CLI" }); + } await archethic.connect() let keychain @@ -54,24 +64,28 @@ const handler = async function(argv) { console.log("Master genesis address:", masterGenesisAddress) const index = await archethic.transaction.getTransactionIndex(masterGenesisAddress) + if (argv["with_multisig"]) { + const updateTx = getProposeTransaction(archethic, masterGenesisAddress, { + recipients: [ + { address: routerGenesisAddress, action: "update_code", args: [routerCode] } + ] + }) + sendWithWallet(updateTx, archethic) + .then(() => process.exit(0)) + .catch(() => process.exit(1)) + + return + } + let updateTx = archethic.transaction.new() .setType("transfer") .addRecipient(routerGenesisAddress, "update_code", [routerCode]) updateTx = keychain.buildTransaction(updateTx, "Master", index).originSign(Utils.originPrivateKey) - updateTx.on("fullConfirmation", async (_confirmations) => { - const txAddress = Utils.uint8ArrayToHex(updateTx.address) - console.log("Transaction validated !") - console.log("Address:", txAddress) - console.log(env.endpoint + "/explorer/transaction/" + txAddress) - process.exit(0) - }).on("error", (context, reason) => { - console.log("Error while sending transaction") - console.log("Contest:", context) - console.log("Reason:", reason) - process.exit(1) - }).send() + sendTransactionWithoutFunding(updateTx, archethic) + .then(() => process.exit(0)) + .catch(() => process.exit(1)) } export default { diff --git a/contracts/commands/test/deploy_farm.js b/contracts/commands/test/deploy_farm.js index bfe30e34..0f7de985 100644 --- a/contracts/commands/test/deploy_farm.js +++ b/contracts/commands/test/deploy_farm.js @@ -5,8 +5,11 @@ import { getGenesisAddress, encryptSecret, getTokenAddress, - getServiceGenesisAddress + getServiceGenesisAddress, + sendTransactionWithoutFunding, + sendWithWallet } from "../utils.js" +import { getProposeTransaction } from "@archethicjs/multisig-sdk" const command = "deploy_farm" const describe = "Deploy a farm for a lp token and a reward token" @@ -56,6 +59,12 @@ const builder = { demandOption: false, type: "string" }, + with_multisig: { + describe: "Determines if the master is using a multisig", + demandOption: false, + type: "boolean", + default: false + }, } const handler = async function (argv) { @@ -69,7 +78,10 @@ const handler = async function (argv) { process.exit(1) } - const archethic = new Archethic(env.endpoint) + const archethic = new Archethic(argv["with_multisig"] ? undefined : env.endpoint) + if (archethic.rpcWallet) { + archethic.rpcWallet.setOrigin({ name: "Archethic DEX CLI" }); + } await archethic.connect() let keychain @@ -121,7 +133,12 @@ const handler = async function (argv) { // Deploy pool console.log("Create farm contract") - await sendTransactionWithFunding(farmTx, keychain, archethic) + if (argv["without_funding"]) { + await sendTransactionWithoutFunding(farmTx, archethic) + } + else { + await sendTransactionWithFunding(farmTx, keychain, archethic) + } console.log("=======================") console.log("Send reward and add farm to router") @@ -130,6 +147,33 @@ const handler = async function (argv) { const index = await archethic.transaction.getTransactionIndex(masterGenesisAddress) console.log("Master genesis address:", masterGenesisAddress) + const addFarmArgs = [lpTokenAddress, startDate, endDate, rewardTokenAddress, Utils.uint8ArrayToHex(farmTx.address), farmType] + + if (argv["with_multisig"]) { + let ucoTransfers = [], tokenTransfers = [] + if (rewardTokenAddress == "UCO") { + ucoTransfers = [{ to: farmGenesisAddress, amount: Utils.toBigInt(rewardTokenAmount)}] + } else { + tokenTransfers = [{ to: farmGenesisAddress, amount: Utils.toBigInt(rewardTokenAmount), tokenAddress: rewardTokenAddress}] + } + const tx = getProposeTransaction(archethic, masterGenesisAddress, { + ucoTransfers: ucoTransfers, + tokenTransfers: tokenTransfers, + recipients: [ + { address: routerAddress, action: "add_farm", args: [lpTokenAddress, startDate, endDate, rewardTokenAddress, Utils.uint8ArrayToHex(farmTx.address), farmType] } + ] + }) + + sendWithWallet(tx, archethic) + .then(() => { + console.log("Farm registration proposal submitted !") + process.exit(0) + }) + .catch(() => process.exit(1)) + + return + } + let tx = archethic.transaction.new() if (rewardTokenAddress == "UCO") { @@ -139,22 +183,16 @@ const handler = async function (argv) { } tx.setType("transfer") - .addRecipient(routerAddress, "add_farm", [lpTokenAddress, startDate, endDate, rewardTokenAddress, Utils.uint8ArrayToHex(farmTx.address), farmType]) + .addRecipient(routerAddress, "add_farm", addFarmArgs) tx = keychain.buildTransaction(tx, "Master", index).originSign(Utils.originPrivateKey) - tx.on("requiredConfirmation", (_confirmations) => { - console.log("Farm registration success !") - const address = Utils.uint8ArrayToHex(tx.address) - console.log("Address:", address) - console.log(env.endpoint + "/explorer/transaction/" + address) - process.exit(0) - }).on("error", (context, reason) => { - console.log("Error while sending transaction") - console.log("Contest:", context) - console.log("Reason:", reason) - process.exit(1) - }).send(50) + sendTransactionWithoutFunding(tx, archethic) + .then(() => { + console.log("Farm registration success !") + process.exit(0) + }) + .catch(() => process.exit(1)) } async function getFarmCode(archethic, lpTokenAddress, startDate, endDate, rewardToken, farmSeed, factoryAddress, farmType) { diff --git a/contracts/commands/utils.js b/contracts/commands/utils.js index 314740e6..ca2cbde1 100644 --- a/contracts/commands/utils.js +++ b/contracts/commands/utils.js @@ -34,7 +34,31 @@ export function encryptSecret(secret, publicKey) { return { encryptedSecret, authorizedKeys } } -export async function sendTransactionWithFunding(tx, keychain, archethic, fundSeedFrom = undefined) { +export function sendTransactionWithoutFunding(tx, archethic) { + return new Promise((resolve, reject) => { + tx.on("fullConfirmation", async (_confirmations) => { + const txAddress = Utils.uint8ArrayToHex(tx.address) + console.log("Transaction validated !") + console.log("Address:", txAddress) + console.log(archethic.endpoint.nodeEndpoint + "/explorer/transaction/" + txAddress) + resolve() + }).on("error", (context, reason) => { + console.log("Error while sending transaction") + console.log("Contest:", context) + console.log("Reason:", reason) + reject() + }).send(CONFIRMATION_THRESHOLD) + }) +} + +export async function sendWithWallet(tx, archethic) { + const { transactionAddress } = await archethic.rpcWallet.sendTransaction(tx) + console.log("Transaction validated !") + console.log("Address:", transactionAddress) + console.log(archethic.endpoint.nodeEndpoint + "/explorer/transaction/" + transactionAddress) +} + +export async function sendTransactionWithFunding(tx, keychain, archethic, fundSeedFrom = undefined, withWallet = false) { return new Promise(async (resolve, reject) => { let { fee } = await archethic.transaction.getTransactionFee(tx) fee = Math.trunc(fee * 1.01) @@ -44,11 +68,16 @@ export async function sendTransactionWithFunding(tx, keychain, archethic, fundSe .setType("transfer") .addUCOTransfer(txPreviousAddress, fee) + console.log("Sending funds to previous transaction address ...") + console.log("=======================") + let fundingGenesisAddress if (fundSeedFrom) { fundingGenesisAddress = getGenesisAddress(fundSeedFrom) const index = await archethic.transaction.getTransactionIndex(Crypto.deriveAddress(fundSeedFrom, 0)) refillTx.build(fundSeedFrom, index).originSign(Utils.originPrivateKey) + } else if (withWallet) { + await sendWithWallet(refillTx, archethic) } else { fundingGenesisAddress = getServiceGenesisAddress(keychain, "Master") const masterIndex = await archethic.transaction.getTransactionIndex(keychain.deriveAddress("Master")) @@ -68,18 +97,20 @@ export async function sendTransactionWithFunding(tx, keychain, archethic, fundSe reject() }) - console.log("Sending funds to previous transaction address ...") - console.log("=======================") - - refillTx.on("requiredConfirmation", async (_confirmations) => { + if (!withWallet) { + refillTx.on("requiredConfirmation", async (_confirmations) => { + tx.send(CONFIRMATION_THRESHOLD) + }).on("error", (context, reason) => { + console.log("Error while sending UCO fee transaction") + console.log("Funding genesis address:", fundingGenesisAddress) + console.log("Context:", context) + console.log("Reason:", reason) + reject() + }).send(CONFIRMATION_THRESHOLD) + } + else { tx.send(CONFIRMATION_THRESHOLD) - }).on("error", (context, reason) => { - console.log("Error while sending UCO fee transaction") - console.log("Funding genesis address:", fundingGenesisAddress) - console.log("Context:", context) - console.log("Reason:", reason) - reject() - }).send(CONFIRMATION_THRESHOLD) + } }) } diff --git a/contracts/dex.js b/contracts/dex.js index 49e8b396..ee9c2e1a 100644 --- a/contracts/dex.js +++ b/contracts/dex.js @@ -5,6 +5,7 @@ import { hideBin } from "yargs/helpers"; import init_keychain from "./commands/contract_management/init_keychain.js"; import deploy_factory from "./commands/contract_management/deploy_factory.js"; +import deploy_multisig from "./commands/contract_management/deploy_multisig.js"; import deploy_farm_scheduler from "./commands/contract_management/deploy_farm_scheduler.js"; import deploy_router from "./commands/contract_management/deploy_router.js"; import update_router from "./commands/contract_management/update_router.js"; @@ -26,12 +27,15 @@ import deposit from "./commands/test/deposit.js"; import claim from "./commands/test/claim.js"; import withdraw from "./commands/test/withdraw.js"; +import get_router_address from "./commands/contract_management/get_router_address.js"; + const y = yargs(hideBin(process.argv)); y.command(init_keychain).help(); y.command(deploy_factory).help(); y.command(deploy_farm_scheduler).help(); y.command(deploy_router).help(); +y.command(deploy_multisig).help(); y.command(update_router).help(); y.command(update_pools).help(); y.command(update_farms).help(); @@ -52,4 +56,6 @@ y.command(deposit).help(); y.command(claim).help(); y.command(withdraw).help(); +y.command(get_router_address).help() + y.parse(); diff --git a/contracts/package-lock.json b/contracts/package-lock.json index 479df183..020d5899 100644 --- a/contracts/package-lock.json +++ b/contracts/package-lock.json @@ -4,8 +4,8 @@ "requires": true, "packages": { "": { - "name": "contracts", "dependencies": { + "@archethicjs/multisig-sdk": "^1.0.10", "@archethicjs/sdk": "^1.20.1", "yargs": "^17.7.2" } @@ -26,10 +26,18 @@ "phoenix": "^1.4.0" } }, + "node_modules/@archethicjs/multisig-sdk": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/@archethicjs/multisig-sdk/-/multisig-sdk-1.0.10.tgz", + "integrity": "sha512-uvV0jWhsXZpVsu1snA5ngSS8M5/UF76P0kHA5QSWObOJZlIFj/fU8XD2h9rpsgGgsO+OT2ypDqZjpaRDwTolig==", + "dependencies": { + "@archethicjs/sdk": "^1.21.2" + } + }, "node_modules/@archethicjs/sdk": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/@archethicjs/sdk/-/sdk-1.20.1.tgz", - "integrity": "sha512-wIk9X6HOFPVGJ/+Kn5oTQHEGrHMLRIAvzBhQEjyfjYCv56Rl78lqJ+bC1mxXJZDUyAlozH/+dIJXXVWAEjVVKA==", + "version": "1.21.3", + "resolved": "https://registry.npmjs.org/@archethicjs/sdk/-/sdk-1.21.3.tgz", + "integrity": "sha512-betGzCwvPsmgpidcYYOu3+sH8zPN78NwKm8blqRtlEhFjDbgU5iwNtOMNUXa3d07x2nL+tFblUlmYigDAPaOAg==", "dependencies": { "@absinthe/socket": "^0.2.1", "blakejs": "^1.2.1", @@ -39,6 +47,7 @@ "curve25519-js": "^0.0.4", "ed2curve": "^0.3.0", "elliptic": "^6.5.4", + "fast-check": "^3.19.0", "isomorphic-ws": "^5.0.0", "js-sha3": "^0.9.0", "json-rpc-2.0": "^1.6.0", @@ -289,6 +298,27 @@ "node": ">=6" } }, + "node_modules/fast-check": { + "version": "3.23.1", + "resolved": "https://registry.npmjs.org/fast-check/-/fast-check-3.23.1.tgz", + "integrity": "sha512-u/MudsoQEgBUZgR5N1v87vEgybeVYus9VnDVaIkxkkGP2jt54naghQ3PCQHJiogS8U/GavZCUPFfx3Xkp+NaHw==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/dubzzz" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fast-check" + } + ], + "dependencies": { + "pure-rand": "^6.1.0" + }, + "engines": { + "node": ">=8.0.0" + } + }, "node_modules/fast-deep-equal": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-1.0.0.tgz", @@ -426,6 +456,21 @@ "resolved": "https://registry.npmjs.org/phoenix/-/phoenix-1.7.9.tgz", "integrity": "sha512-oHBka/WCKVtFHNCCqnVDxiSHQZ0vvV0pU1I4oBmm68rNvGH3lVjdNGLRhK0+c3zcfsrqgpRS2p+Et6N4QAd2LQ==" }, + "node_modules/pure-rand": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/pure-rand/-/pure-rand-6.1.0.tgz", + "integrity": "sha512-bVWawvoZoBYpp6yIoQtQXHZjmz35RSVHnUOTefl8Vcjr8snTPY1wnpSPMWekcFwbxI6gtmT7rSYPFvz71ldiOA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/dubzzz" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fast-check" + } + ] + }, "node_modules/regenerator-runtime": { "version": "0.12.1", "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.12.1.tgz", diff --git a/contracts/package.json b/contracts/package.json index bba91a46..8520e474 100644 --- a/contracts/package.json +++ b/contracts/package.json @@ -1,5 +1,6 @@ { "dependencies": { + "@archethicjs/multisig-sdk": "^1.0.10", "@archethicjs/sdk": "^1.20.1", "yargs": "^17.7.2" }, From 5a323e15636c6dd21a9543bf5e0170ed35c32bfe Mon Sep 17 00:00:00 2001 From: Samuel Manzanera Date: Mon, 25 Nov 2024 15:05:47 +0100 Subject: [PATCH 2/2] Adapt with latest libjs --- contracts/commands/test/deploy_pool.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contracts/commands/test/deploy_pool.js b/contracts/commands/test/deploy_pool.js index b01af86d..86435312 100644 --- a/contracts/commands/test/deploy_pool.js +++ b/contracts/commands/test/deploy_pool.js @@ -139,15 +139,15 @@ const handler = async function (argv) { const tx = archethic.transaction.new() if (token1.address == "UCO") { - tx.addUCOTransfer(poolGenesisAddress, Utils.toBigInt(token1.amount)) + tx.addUCOTransfer(poolGenesisAddress, Utils.parseBigInt(token1.amount.toString())) } else { - tx.addTokenTransfer(poolGenesisAddress, Utils.toBigInt(token1.amount), token1.address) + tx.addTokenTransfer(poolGenesisAddress, Utils.parseBigInt(token1.amount.toString()), token1.address) } if (token2.address == "UCO") { - tx.addUCOTransfer(poolGenesisAddress, Utils.toBigInt(token2.amount)) + tx.addUCOTransfer(poolGenesisAddress, Utils.parseBigInt(token2.amount.toString())) } else { - tx.addTokenTransfer(poolGenesisAddress, Utils.toBigInt(token2.amount), token2.address) + tx.addTokenTransfer(poolGenesisAddress, Utils.parseBigInt(token2.amount.toString()), token2.address) } tx.setType("transfer")