Skip to content

Commit

Permalink
Rework pools update workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix committed Nov 14, 2024
1 parent 5412d00 commit d27ff37
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 29 deletions.
55 changes: 35 additions & 20 deletions contracts/commands/contract_management/update_pools.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,41 @@ const handler = async function(argv) {

const masterGenesisAddress = getServiceGenesisAddress(keychain, "Master")
console.log("Master genesis address:", masterGenesisAddress)
const index = await archethic.transaction.getTransactionIndex(masterGenesisAddress)

let updateTx = archethic.transaction.new()
.setType("transfer")
.addRecipient(routerGenesisAddress, "update_pools_code")

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()

const pools = await archethic.network.callFunction(routerGenesisAddress, "get_pool_list", [])

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)
}

process.exit(0)
}

async function sendUpdateTx(archethic, chunk, routerAddress, masterAddress, keychain) {
return new Promise(async (resolve, _reject) => {
let updateTx = archethic.transaction.new()
.setType("transfer")
.addRecipient(routerAddress, "update_pools_code", [chunk])

const index = await archethic.transaction.getTransactionIndex(masterAddress)

updateTx = keychain.buildTransaction(updateTx, "Master", index).originSign(Utils.originPrivateKey)

updateTx.on("requiredConfirmation", async (_confirmations) => {
const txAddress = Utils.uint8ArrayToHex(updateTx.address)
console.log("Transaction validated !")
console.log("Address:", txAddress)
await new Promise(r => setTimeout(r, 2000));
resolve()
}).on("error", (context, reason) => {
console.log("Error while sending transaction")
console.log("Contest:", context)
console.log("Reason:", reason)
process.exit(1)
}).send(60)
})
}

export default {
Expand Down
2 changes: 1 addition & 1 deletion contracts/contracts/pool.exs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ actions triggered_by: transaction, on: update_code() do

new_code = Contract.call_function(@FACTORY_ADDRESS, "get_pool_code", params)

if Code.is_valid?(new_code) && !Code.is_same?(new_code, contract.code) do
if Code.is_valid?(new_code) do
Contract.set_type("contract")
Contract.set_code(new_code)
end
Expand Down
35 changes: 27 additions & 8 deletions contracts/contracts/router.exs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,27 @@ actions triggered_by: transaction, on: update_code(new_code) do
Contract.set_code(new_code)
end

condition triggered_by: transaction, on: update_pools_code(), as: [
condition triggered_by: transaction, on: update_pools_code(pool_addresses), as: [
content: (
if List.empty?(pool_addresses) do
throw code: 1, message: "pool_addresses is empty"
end

pools = State.get("pools", Map.new())
current_addresses = []

for pool_info in Map.values(pools) do
current_addresses = List.prepend(current_addresses, pool_info.address)
end

for pool_address in pool_addresses do
if !List.in?(current_addresses, String.to_hex(pool_address)) do
throw code: 2, message: "pool_address not in current pool", data: pool_address
end
end

true
),
previous_public_key: (
# Pool code update can only be requested from the master chain of the dex

Expand All @@ -185,16 +205,15 @@ condition triggered_by: transaction, on: update_pools_code(), as: [
)
]

actions triggered_by: transaction, on: update_pools_code() do
actions triggered_by: transaction, on: update_pools_code(pool_addresses) do
pools = State.get("pools", Map.new())

if Map.size(pools) > 0 do
for pool_info in Map.values(pools) do
Contract.add_recipient address: pool_info.address, action: "update_code", args: []
end

Contract.set_type("transfer")
for pool_address in pool_addresses do
address = String.to_hex(pool_address)
Contract.add_recipient address: address, action: "update_code", args: []
end

Contract.set_type("transfer")
end

condition triggered_by: transaction, on: update_farm_dates(_new_start_date, _new_end_date), as: [
Expand Down

0 comments on commit d27ff37

Please sign in to comment.