|
| 1 | +import { |
| 2 | + Contract, |
| 3 | + SorobanRpc, |
| 4 | + TransactionBuilder, |
| 5 | + BASE_FEE, |
| 6 | + nativeToScVal, |
| 7 | + Account, |
| 8 | + Transaction, |
| 9 | +} from "@stellar/stellar-sdk"; |
| 10 | +import { ILNError } from "../errors.js"; |
| 11 | +import { retry } from "../utils/retry.js"; |
| 12 | +import { validateGAddress } from "../utils/validate.js"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Set a new admin for the ILN contract. |
| 16 | + */ |
| 17 | +export async function setAdmin( |
| 18 | + server: SorobanRpc.Server, |
| 19 | + contractAddress: string, |
| 20 | + newAdmin: string, |
| 21 | + sourceAccount: Account, |
| 22 | + signTransaction: (tx: Transaction) => Promise<Transaction> | Transaction, |
| 23 | + networkPassphrase: string |
| 24 | +): Promise<{ txHash: string }> { |
| 25 | + validateGAddress(newAdmin); |
| 26 | + |
| 27 | + const contract = new Contract(contractAddress); |
| 28 | + const op = contract.call( |
| 29 | + "set_admin", |
| 30 | + nativeToScVal(newAdmin, { type: "address" }) |
| 31 | + ); |
| 32 | + |
| 33 | + const tx = new TransactionBuilder(sourceAccount, { |
| 34 | + fee: BASE_FEE, |
| 35 | + networkPassphrase, |
| 36 | + }) |
| 37 | + .addOperation(op) |
| 38 | + .setTimeout(30) |
| 39 | + .build(); |
| 40 | + |
| 41 | + const sim = await retry(() => server.simulateTransaction(tx)); |
| 42 | + if (SorobanRpc.Api.isSimulationError(sim)) { |
| 43 | + throw ILNError.fromError(sim.error); |
| 44 | + } |
| 45 | + |
| 46 | + const assembledTx = SorobanRpc.assembleTransaction(tx, sim).build(); |
| 47 | + const signedTx = await signTransaction(assembledTx); |
| 48 | + const sendResult = await retry(() => server.sendTransaction(signedTx)); |
| 49 | + if (sendResult.errorResult) { |
| 50 | + throw new Error(`Transaction failed: ${sendResult.errorResult}`); |
| 51 | + } |
| 52 | + |
| 53 | + let status = await retry(() => server.getTransaction(sendResult.hash)); |
| 54 | + let retries = 0; |
| 55 | + while (status.status === SorobanRpc.Api.GetTransactionStatus.NOT_FOUND && retries < 15) { |
| 56 | + await new Promise(r => setTimeout(r, 2000)); |
| 57 | + status = await retry(() => server.getTransaction(sendResult.hash)); |
| 58 | + retries++; |
| 59 | + } |
| 60 | + |
| 61 | + if (status.status === SorobanRpc.Api.GetTransactionStatus.FAILED) { |
| 62 | + throw new Error("Transaction failed during execution"); |
| 63 | + } |
| 64 | + |
| 65 | + return { txHash: sendResult.hash }; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Upgrade the ILN contract to a new Wasm hash. |
| 70 | + */ |
| 71 | +export async function upgrade( |
| 72 | + server: SorobanRpc.Server, |
| 73 | + contractAddress: string, |
| 74 | + newWasmHash: Buffer, |
| 75 | + sourceAccount: Account, |
| 76 | + signTransaction: (tx: Transaction) => Promise<Transaction> | Transaction, |
| 77 | + networkPassphrase: string |
| 78 | +): Promise<{ txHash: string }> { |
| 79 | + const contract = new Contract(contractAddress); |
| 80 | + const op = contract.call( |
| 81 | + "upgrade", |
| 82 | + nativeToScVal(newWasmHash, { type: "bytes" }) |
| 83 | + ); |
| 84 | + |
| 85 | + const tx = new TransactionBuilder(sourceAccount, { |
| 86 | + fee: BASE_FEE, |
| 87 | + networkPassphrase, |
| 88 | + }) |
| 89 | + .addOperation(op) |
| 90 | + .setTimeout(30) |
| 91 | + .build(); |
| 92 | + |
| 93 | + const sim = await retry(() => server.simulateTransaction(tx)); |
| 94 | + if (SorobanRpc.Api.isSimulationError(sim)) { |
| 95 | + throw ILNError.fromError(sim.error); |
| 96 | + } |
| 97 | + |
| 98 | + const assembledTx = SorobanRpc.assembleTransaction(tx, sim).build(); |
| 99 | + const signedTx = await signTransaction(assembledTx); |
| 100 | + const sendResult = await retry(() => server.sendTransaction(signedTx)); |
| 101 | + if (sendResult.errorResult) { |
| 102 | + throw new Error(`Transaction failed: ${sendResult.errorResult}`); |
| 103 | + } |
| 104 | + |
| 105 | + let status = await retry(() => server.getTransaction(sendResult.hash)); |
| 106 | + let retries = 0; |
| 107 | + while (status.status === SorobanRpc.Api.GetTransactionStatus.NOT_FOUND && retries < 15) { |
| 108 | + await new Promise(r => setTimeout(r, 2000)); |
| 109 | + status = await retry(() => server.getTransaction(sendResult.hash)); |
| 110 | + retries++; |
| 111 | + } |
| 112 | + |
| 113 | + if (status.status === SorobanRpc.Api.GetTransactionStatus.FAILED) { |
| 114 | + throw new Error("Transaction failed during execution"); |
| 115 | + } |
| 116 | + |
| 117 | + return { txHash: sendResult.hash }; |
| 118 | +} |
0 commit comments