-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add stellar upgrader contract to the deploy-contract.js script #487
base: main
Are you sure you want to change the base?
Changes from all commits
e2b3466
373689f
05dd06e
eaa929c
2217259
a5c1433
b55af11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,3 +119,6 @@ sui/move | |
# VSCode | ||
.vscode | ||
.DS_Store | ||
|
||
# Jetbrains | ||
.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
'use strict'; | ||
|
||
const { Address, nativeToScVal, scValToNative, Operation, StrKey } = require('@stellar/stellar-sdk'); | ||
const { Address, nativeToScVal, scValToNative, Operation, StrKey, xdr, authorizeInvocation, rpc } = require('@stellar/stellar-sdk'); | ||
const { Command, Option } = require('commander'); | ||
const { loadConfig, printInfo, saveConfig } = require('../evm/utils'); | ||
const { getWallet, broadcast, serializeValue, addBaseOptions } = require('./utils'); | ||
const { getWallet, broadcast, serializeValue, addBaseOptions, getNetworkPassphrase, createAuthorizedFunc } = require('./utils'); | ||
const { getDomainSeparator, getChainConfig } = require('../common'); | ||
const { prompt, validateParameters } = require('../common/utils'); | ||
const { weightedSignersToScVal } = require('./type-utils'); | ||
|
@@ -74,6 +74,10 @@ async function getInitializeArgs(config, chain, contractName, wallet, options) { | |
return { owner, gasCollector }; | ||
} | ||
|
||
case 'upgrader': { | ||
return {}; | ||
} | ||
|
||
case 'example': { | ||
const gatewayAddress = nativeToScVal(Address.fromString(chain?.contracts?.axelar_gateway?.address), { type: 'address' }); | ||
const gasServiceAddress = nativeToScVal(Address.fromString(chain?.contracts?.axelar_gas_service?.address), { type: 'address' }); | ||
|
@@ -139,30 +143,58 @@ async function uploadWasm(filePath, wallet, chain) { | |
|
||
async function upgrade(options, _, chain, contractName) { | ||
const { wasmPath, yes } = options; | ||
const contractAddress = chain.contracts[contractName].address; | ||
let contractAddress = chain.contracts[contractName]?.address; | ||
const upgraderAddress = chain.contracts.upgrader?.address; | ||
const wallet = await getWallet(chain, options); | ||
|
||
if (prompt(`Proceed with upgrade on ${chain.name}?`, yes)) { | ||
return; | ||
} | ||
|
||
validateParameters({ | ||
isNonEmptyString: { contractAddress }, | ||
isNonEmptyString: { contractAddress, upgraderAddress }, | ||
}); | ||
|
||
contractAddress = Address.fromString(contractAddress); | ||
|
||
const newWasmHash = await uploadWasm(wasmPath, wallet, chain); | ||
printInfo('New Wasm hash', serializeValue(newWasmHash)); | ||
|
||
const operation = Operation.invokeContractFunction({ | ||
contract: contractAddress, | ||
contract: chain.contracts.upgrader.address, | ||
cgorenflo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
function: 'upgrade', | ||
args: [nativeToScVal(newWasmHash)], | ||
args: [contractAddress, options.newVersion, newWasmHash, [options.migrationData]].map(nativeToScVal), | ||
auth: await createUpgradeAuths(contractAddress, newWasmHash, options.migrationData, chain, wallet), | ||
}); | ||
|
||
await broadcast(operation, wallet, chain, 'Upgraded contract', options); | ||
chain.contracts[contractName].wasmHash = serializeValue(newWasmHash); | ||
printInfo('Contract upgraded successfully!', contractAddress); | ||
} | ||
|
||
async function createUpgradeAuths(contractAddress, newWasmHash, migrationData, chain, wallet) { | ||
// 20 seems a reasonable number of ledgers to allow for the upgrade to take effect | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create a global constant for it. Could also create an option with a default of 20 though I don't see us configuring it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you mean for the 20? What would be the benefit of that? It's literally just used here once, and, as you point out, that doesn't seem like something we would ever parameterize There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, parameterization seems overkill. Would still be nice to define a constant variable for it though for style |
||
const validUntil = await new rpc.Server(chain.rpc).getLatestLedger().then((info) => info.sequence + 20); | ||
|
||
return Promise.all( | ||
[ | ||
createAuthorizedFunc(contractAddress, 'upgrade', [nativeToScVal(newWasmHash)]), | ||
createAuthorizedFunc(contractAddress, 'migrate', [nativeToScVal(migrationData)]), | ||
].map((auth) => | ||
authorizeInvocation( | ||
wallet, | ||
validUntil, | ||
new xdr.SorobanAuthorizedInvocation({ | ||
function: auth, | ||
subInvocations: [], | ||
}), | ||
wallet.publicKey(), | ||
getNetworkPassphrase(chain.networkType), | ||
), | ||
), | ||
); | ||
} | ||
|
||
async function mainProcessor(options, processor, contractName) { | ||
const config = loadConfig(options.env); | ||
const chain = getChainConfig(config, options.chainName); | ||
|
@@ -200,6 +232,8 @@ function main() { | |
.description('Upgrade a Stellar contract') | ||
.argument('<contract-name>', 'contract name to deploy') | ||
.addOption(new Option('--wasm-path <wasmPath>', 'path to the WASM file')) | ||
.addOption(new Option('--new-version <newVersion>', 'new version of the contract')) | ||
.addOption(new Option('--migration-data <migrationData>', 'migration data')) | ||
cgorenflo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.action((contractName, options) => { | ||
mainProcessor(options, upgrade, contractName); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you mention how MIGRATION_DATA is encoded for the no migration case and a more complex migration case?