Skip to content

Commit 03d0342

Browse files
CoveMBericglau
andauthored
Use stellar scaffold cli upgrade command in setup script (#587)
Co-authored-by: Eric Lau <[email protected]>
1 parent c5ef7d8 commit 03d0342

File tree

8 files changed

+70
-216
lines changed

8 files changed

+70
-216
lines changed

.github/workflows/test.yml

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -92,37 +92,16 @@ jobs:
9292
# cargo-scaffold-
9393
# cargo-scaffold-
9494

95-
- name: Set up Rust
96-
if: matrix.package == 'stellar'
97-
uses: actions-rs/toolchain@v1
98-
with:
99-
toolchain: 1.86.0
100-
profile: minimal
101-
override: true
102-
- name: Add wasm32v1 target
103-
if: matrix.package == 'stellar'
104-
run: |
105-
if ! rustup target list --installed | grep -q wasm32v1-none; then
106-
rustup target add wasm32v1-none
107-
fi
108-
109-
- name: Set up Stellar CLI
110-
if: matrix.package == 'stellar'
111-
uses: stellar/[email protected]
112-
113-
- name: Install Scaffold & Registry CLIs
114-
if: matrix.package == 'stellar'
115-
run: |
116-
rebuildIfNotFoundInCache() {
117-
local binary_name="$1"
118-
119-
if ! [ -x "$HOME/.cargo/bin/$binary_name" ]; then
120-
cargo install --git "https://github.com/ahalabs/scaffold-stellar" "$binary_name"
121-
fi
122-
}
95+
# - name: Set up rust toolchain
96+
# uses: actions-rust-lang/setup-rust-toolchain@v1
97+
# with:
98+
# toolchain: stable, nightly
99+
# components: clippy, rustfmt, llvm-tools-preview
100+
# target: wasm32v1-none
123101

124-
rebuildIfNotFoundInCache stellar-scaffold-cli
125-
rebuildIfNotFoundInCache stellar-registry-cli
102+
# - name: Set up Stellar CLI
103+
# if: matrix.package == 'stellar'
104+
# uses: stellar/[email protected]
126105

127106
# ----------------------------
128107
- name: Compile TypeScript

packages/core/stellar/src/utils/version.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,16 @@ export const compatibleContractsSemver = '^0.3.0';
1313
* The Soroban version for which compilation and testing have passing tests
1414
*/
1515
export const compatibleSorobanVersion = '22.0.8';
16+
17+
/**
18+
* The dependencies used in contracts
19+
*/
20+
export const stellarDependencies = {
21+
base: ['stellar-default-impl-macro'],
22+
fungible: ['stellar-fungible'],
23+
nonFungible: ['stellar-non-fungible'],
24+
pausable: ['stellar-pausable', 'stellar-pausable-macros'],
25+
upgradable: ['stellar-upgradeable', 'stellar-upgradeable-macros'],
26+
accessControl: ['stellar-access-control', 'stellar-access-control-macros'],
27+
ownable: ['stellar-ownable', 'stellar-ownable-macro'],
28+
} as const;

packages/core/stellar/src/zip-rust.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Continue your development journey with [Stellar CLI](https://github.com/stellar/
4141
- See [Git installation guide](https://github.com/git-guides/install-git).
4242
`;
4343

44-
export async function zipRust(c: Contract, opts: GenericOptions) {
44+
export const createRustZipEnvironment = (c: Contract, opts: GenericOptions) => {
4545
const zip = new JSZip();
4646

4747
const contractName = contractOptionsToContractName(opts?.kind || 'contract');
@@ -51,7 +51,11 @@ export async function zipRust(c: Contract, opts: GenericOptions) {
5151
zip.file(`contracts/${contractName}/src/lib.rs`, createRustLibFile);
5252
zip.file(`contracts/${contractName}/Cargo.toml`, printContractCargo(contractName));
5353
zip.file('Cargo.toml', workspaceCargo);
54-
zip.file('README.md', readme);
5554

5655
return zip;
57-
}
56+
};
57+
58+
const addRustProjectReadme = (zip: JSZip) => zip.file('README.md', readme);
59+
60+
export const zipRustProject = async (c: Contract, opts: GenericOptions) =>
61+
addRustProjectReadme(createRustZipEnvironment(c, opts));

packages/core/stellar/src/zip-scaffold.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { TestFn, ExecutionContext } from 'ava';
22
import _test from 'ava';
33

4-
import { zipScaffold } from './zip-scaffold';
4+
import { zipScaffoldProject } from './zip-scaffold';
55

66
import { buildFungible } from './fungible';
77
import { buildNonFungible } from './non-fungible';
@@ -32,6 +32,7 @@ function assertLayout(t: ExecutionContext<Context>, zip: JSZip, opts: GenericOpt
3232
const scaffoldContractName = contractOptionsToContractName(opts?.kind || 'contract');
3333

3434
t.deepEqual(sorted, [
35+
'Cargo.toml',
3536
'README-WIZARD.md',
3637
'contracts/',
3738
`contracts/${scaffoldContractName}/`,
@@ -109,7 +110,7 @@ test.afterEach.always(async t => {
109110
async function runTest(t: ExecutionContext<Context>, c: Contract, opts: GenericOptions) {
110111
t.timeout(3_000_000);
111112

112-
const zip = await zipScaffold(c, opts);
113+
const zip = await zipScaffoldProject(c, opts);
113114

114115
assertLayout(t, zip, opts);
115116
await extractPackage(t, zip);
Lines changed: 15 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,14 @@
1-
import JSZip from 'jszip';
1+
import type JSZip from 'jszip';
22
import type { GenericOptions } from './build-generic';
33
import type { Contract } from './contract';
4-
import { printContract, removeCreateLevelAttributes } from './print';
5-
import { compatibleSorobanVersion, contractsVersionTag } from './utils/version';
6-
import {
7-
addDependenciesWith,
8-
allStellarDependencies,
9-
contractOptionsToContractName,
10-
createRustLibFile,
11-
printContractCargo,
12-
printRustNameTest,
13-
} from './zip-shared';
4+
import { createRustZipEnvironment } from './zip-rust';
145

15-
function getAddressArgs(c: Contract): string[] {
16-
return c.constructorArgs
17-
.filter(constructorArg => constructorArg.type?.toLowerCase() === 'address')
18-
.map(constructorArg => constructorArg.name);
19-
}
20-
21-
const setupSh = (c: Contract, opts: GenericOptions, scaffoldContractName: string) => {
22-
const environmentsFileUpdate = (setUpContract: Contract, setUpScaffoldContractName: string) => `
23-
# Update environments.toml: remove original contracts and insert wizard's contract
24-
setup_environment() {
25-
local file="environments.toml"
26-
local temp
27-
temp="$(mktemp)"
28-
29-
local in_dev_contracts=0
30-
local skip_entry=0
31-
local contract_entry_inserted=0
32-
insert_contract_entry() {
33-
{
34-
printf '%s\\n' "[development.contracts.${setUpScaffoldContractName}_contract]" \\
35-
"client = true" "" \\
36-
"# If your contract has a \\\`__constructor\\\`, specify your arguments to it here." \\
37-
"# These are the same arguments you could pass after the \\\`--\\\` in a call to" \\
38-
"# \\\`stellar contract deploy\\\`" \\
39-
"# Only available in \\\`development\\\` and \\\`test\\\` environments" \\
40-
${
41-
setUpContract.constructorArgs.length
42-
? // Mind the spacing
43-
`"# TODO add appropriate values for for the constructors arguments" \\
44-
"constructor_args = \\"\\"\\"" \\
45-
"${setUpContract.constructorArgs.map(constructorArg => `--${constructorArg.name} \\"ADD_${constructorArg.name.toLocaleUpperCase()}_${constructorArg.type?.toLocaleUpperCase() || 'ARGUMENT'}_HERE\\"`).join(' ')}" \\
46-
"\\"\\"\\"" \\
47-
""`
48-
: '""'
49-
}
50-
} >> "$temp"
51-
}
52-
53-
while IFS= read -r line; do
54-
if [[ $contract_entry_inserted -eq 0 && $line == '[staging.network]' ]]; then
55-
insert_contract_entry
56-
contract_entry_inserted=1
57-
fi
58-
59-
if [[ $line =~ ^\\[development\\.contracts\\]$ ]]; then
60-
printf '%s\\n' "$line" >> "$temp"
61-
in_dev_contracts=1
62-
skip_entry=0
63-
continue
64-
fi
65-
66-
if [[ $line =~ ^\\[[^]]+\\]$ ]]; then
67-
if (( in_dev_contracts )) && [[ $line =~ ^\\[development\\.contracts\\..+\\]$ ]]; then
68-
skip_entry=1
69-
in_dev_contracts=0
70-
continue
71-
fi
72-
in_dev_contracts=0
73-
skip_entry=0
74-
printf '%s\\n' "$line" >> "$temp"
75-
continue
76-
fi
77-
78-
if (( skip_entry )); then
79-
continue
80-
fi
81-
82-
if (( in_dev_contracts )); then
83-
if [[ $line =~ ^[[:space:]]*# ]]; then
84-
printf '%s\\n' "$line" >> "$temp"
85-
fi
86-
continue
87-
fi
88-
89-
printf '%s\\n' "$line" >> "$temp"
90-
done < "$file"
91-
92-
mv "$temp" "$file"
93-
}
94-
`;
95-
96-
const updateWorkspaceCargo = `update_cargo() {
97-
cp Cargo.toml Cargo.toml.bak
98-
99-
cat <<EOF > deps.tmp
100-
${addDependenciesWith(`{ git = "https://github.com/OpenZeppelin/stellar-contracts", tag = "${contractsVersionTag}" }`, allStellarDependencies)}${addDependenciesWith(`{ version = "${compatibleSorobanVersion}" }`, ['soroban'])}
101-
EOF
102-
103-
awk '
104-
BEGIN {
105-
inserted = 0
106-
deps = ""
107-
while ((getline line < "deps.tmp") > 0) {
108-
deps = deps line "\\n"
109-
}
110-
close("deps.tmp")
111-
}
112-
/^\\[workspace.dependencies\\]/ {
113-
in_deps = 1
114-
print
115-
if (!inserted) {
116-
printf "%s", deps
117-
inserted = 1
118-
}
119-
next
120-
}
121-
/^\\[/ { in_deps = 0 }
122-
in_deps { next }
123-
{ print }
124-
' Cargo.toml.bak > Cargo.toml
125-
126-
rm deps.tmp
127-
rm Cargo.toml.bak
128-
}`;
129-
130-
return `\
6+
const setupSh = `\
1317
#!/usr/bin/env bash
1328
#
1339
# setup.sh
134-
#
135-
# This script is meant to set up a Scaffold project and insert the Wizard's contracts in the project
10+
#
11+
# This script is meant to set up a Scaffold project with the Wizard's contracts
13612
13713
check_is_installed() {
13814
if ! which "$1" &> /dev/null; then
@@ -143,18 +19,7 @@ check_is_installed() {
14319
}
14420
14521
scaffold() {
146-
tmp_folder="tmp"
147-
stellar scaffold init "$tmp_folder"
148-
149-
rm -rf "$tmp_folder/contracts"
150-
151-
local current_directory
152-
current_directory="$(cd "$(dirname "\${BASH_SOURCE[0]}")" && pwd)"
153-
154-
shopt -s dotglob
155-
156-
cp -a "$current_directory/$tmp_folder"/. "$current_directory"/
157-
rm -rf "$current_directory/$tmp_folder"
22+
stellar-scaffold upgrade
15823
}
15924
16025
init_git(){
@@ -163,10 +28,6 @@ init_git(){
16328
git commit -m "openzeppelin: add wizard output" --quiet
16429
}
16530
166-
${environmentsFileUpdate(c, scaffoldContractName)}
167-
168-
${updateWorkspaceCargo}
169-
17031
build_contracts() {
17132
cargo build
17233
}
@@ -196,10 +57,6 @@ then
19657
echo "🏗️ Building Scaffold project"
19758
19859
scaffold
199-
200-
setup_environment
201-
202-
update_cargo
20360
20461
build_contracts
20562
@@ -212,15 +69,11 @@ else
21269
echo "✅ Scaffold project already initialized."
21370
fi
21471
`;
215-
};
216-
217-
const readme = (c: Contract) => {
218-
const hasTodosToResolve = (c: Contract) => getAddressArgs(c).length > 0;
21972

220-
return `\
73+
const wizardReadme = `\
22174
# Sample Scaffold Project
22275
223-
This project demonstrates a basic Scaffold use case. It comes with a contract generated by [OpenZeppelin Wizard](https://wizard.openzeppelin.com/), a test for that contract, and a script that initiate a Stellar Scaffold project with this contract. [Scaffold Stellar](https://github.com/AhaLabs/scaffold-stellar?tab=readme-ov-file#scaffold-stellar) is a convention-over-configuration toolkit for blockchain and distributed application development on the Stellar network. It provides a seamless development experience through CLI tools, smart contract management, and deployment utilities.
76+
This project demonstrates a basic Scaffold use case. It comes with a contract generated by [OpenZeppelin Wizard](https://wizard.openzeppelin.com/), a test for that contract, and a script that initiates a Stellar Scaffold project with this contract. [Scaffold Stellar](https://github.com/AhaLabs/scaffold-stellar?tab=readme-ov-file#scaffold-stellar) is a convention-over-configuration toolkit for blockchain and distributed application development on the Stellar network. It provides a seamless development experience through CLI tools, smart contract management, and deployment utilities.
22477
22578
## Installing dependencies
22679
@@ -235,7 +88,6 @@ This project demonstrates a basic Scaffold use case. It comes with a contract ge
23588
\`\`\`
23689
bash setup.sh
23790
\`\`\`
238-
${hasTodosToResolve(c) ? '\n## Resolve any TODOs \n\nSearch for any TODO comments in the project and resolve them (search for TODO with your code editor).\n' : ''}
23991
24092
## Testing the contract
24193
@@ -255,19 +107,13 @@ stellar scaffold watch --build-clients
255107
npm run dev
256108
\`\`\`
257109
`;
258-
};
259-
260-
export async function zipScaffold(c: Contract, opts: GenericOptions) {
261-
const zip = new JSZip();
262110

263-
const scaffoldContractName = contractOptionsToContractName(opts?.kind || 'contract');
264-
265-
zip.file(`contracts/${scaffoldContractName}/src/contract.rs`, removeCreateLevelAttributes(printContract(c)));
266-
zip.file(`contracts/${scaffoldContractName}/src/test.rs`, printRustNameTest(c));
267-
zip.file(`contracts/${scaffoldContractName}/src/lib.rs`, createRustLibFile);
268-
zip.file(`contracts/${scaffoldContractName}/Cargo.toml`, printContractCargo(scaffoldContractName));
269-
zip.file('setup.sh', setupSh(c, opts, scaffoldContractName));
270-
zip.file('README-WIZARD.md', readme(c));
111+
const addScaffoldProjectFiles = (zip: JSZip) => {
112+
zip.file('README-WIZARD.md', wizardReadme);
113+
zip.file('setup.sh', setupSh);
271114

272115
return zip;
273-
}
116+
};
117+
118+
export const zipScaffoldProject = async (c: Contract, opts: GenericOptions) =>
119+
addScaffoldProjectFiles(createRustZipEnvironment(c, opts));

packages/core/stellar/src/zip-shared.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ stellar-pausable = { workspace = true }
232232
stellar-pausable-macros = { workspace = true }
233233
stellar-upgradeable = { workspace = true }
234234
stellar-upgradeable-macros = { workspace = true }
235+
stellar-access-control = { workspace = true }
236+
stellar-access-control-macros = { workspace = true }
237+
stellar-ownable = { workspace = true }
238+
stellar-ownable-macro = { workspace = true }
235239
soroban-sdk = { workspace = true }
236240
237241
[dev-dependencies]

0 commit comments

Comments
 (0)