Skip to content

Commit d743b83

Browse files
CoveMBericglau
andauthored
Stellar add download rust environment project (#583)
Co-authored-by: Eric Lau <[email protected]>
1 parent 7c693d4 commit d743b83

File tree

11 files changed

+474
-147
lines changed

11 files changed

+474
-147
lines changed

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import JSZip from 'jszip';
2+
import type { GenericOptions } from './build-generic';
3+
import type { Contract } from './contract';
4+
import { printContract, removeCreateLevelAttributes } from './print';
5+
import {
6+
addDependenciesWith,
7+
allStellarDependencies,
8+
contractOptionsToContractName,
9+
createRustLibFile,
10+
printContractCargo,
11+
printRustNameTest,
12+
} from './zip-shared';
13+
import { contractsVersionTag, compatibleSorobanVersion } from './utils/version';
14+
15+
const workspaceCargo = `[workspace]
16+
resolver = "2"
17+
members = ["contracts/*"]
18+
19+
[workspace.package]
20+
authors = []
21+
edition = "2021"
22+
license = "Apache-2.0"
23+
version = "0.0.1"
24+
25+
[workspace.dependencies]
26+
${addDependenciesWith(`{ git = "https://github.com/OpenZeppelin/stellar-contracts", tag = "${contractsVersionTag}" }`, allStellarDependencies)}${addDependenciesWith(`{ version = "${compatibleSorobanVersion}" }`, ['soroban'])}
27+
`;
28+
29+
const readme = `\
30+
# Sample Rust Contract Environment
31+
32+
This project demonstrates a basic Rust contract environment use case. It comes with a contract generated by [OpenZeppelin Wizard](https://wizard.openzeppelin.com/) and a test for that contract. Make sure you have the required dependencies and keep building!
33+
34+
## Go further
35+
36+
Continue your development journey with [Stellar CLI](https://github.com/stellar/stellar-cli).
37+
38+
## Installing dependencies
39+
40+
- See [Rust and Stellar installation guide](https://developers.stellar.org/docs/build/smart-contracts/getting-started/setup).
41+
- See [Git installation guide](https://github.com/git-guides/install-git).
42+
`;
43+
44+
export async function zipRust(c: Contract, opts: GenericOptions) {
45+
const zip = new JSZip();
46+
47+
const contractName = contractOptionsToContractName(opts?.kind || 'contract');
48+
49+
zip.file(`contracts/${contractName}/src/contract.rs`, removeCreateLevelAttributes(printContract(c)));
50+
zip.file(`contracts/${contractName}/src/test.rs`, printRustNameTest(c));
51+
zip.file(`contracts/${contractName}/src/lib.rs`, createRustLibFile);
52+
zip.file(`contracts/${contractName}/Cargo.toml`, printContractCargo(contractName));
53+
zip.file('Cargo.toml', workspaceCargo);
54+
zip.file('README.md', readme);
55+
56+
return zip;
57+
}

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

Lines changed: 10 additions & 9 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, contractOptionsToScaffoldContractName } from './zip-scaffold';
4+
import { zipScaffold } from './zip-scaffold';
55

66
import { buildFungible } from './fungible';
77
import { buildNonFungible } from './non-fungible';
@@ -15,6 +15,7 @@ import { rimraf } from 'rimraf';
1515
import type { JSZipObject } from 'jszip';
1616
import type JSZip from 'jszip';
1717
import type { GenericOptions } from './build-generic';
18+
import { contractOptionsToContractName } from './zip-shared';
1819
const asyncExec = util.promisify(child.exec);
1920

2021
interface Context {
@@ -28,7 +29,7 @@ function assertLayout(t: ExecutionContext<Context>, zip: JSZip, opts: GenericOpt
2829
.map(f => f.name)
2930
.sort();
3031

31-
const scaffoldContractName = contractOptionsToScaffoldContractName(opts?.kind || 'contract');
32+
const scaffoldContractName = contractOptionsToContractName(opts?.kind || 'contract');
3233

3334
t.deepEqual(sorted, [
3435
'README-WIZARD.md',
@@ -73,7 +74,7 @@ async function runContractTest(t: ExecutionContext<Context>) {
7374
}
7475

7576
async function assertContents(t: ExecutionContext<Context>, zip: JSZip, opts: GenericOptions) {
76-
const scaffoldContractName = contractOptionsToScaffoldContractName(opts?.kind || 'contract');
77+
const scaffoldContractName = contractOptionsToContractName(opts?.kind || 'contract');
7778

7879
const contentComparison = [
7980
await getItemString(zip, `contracts/${scaffoldContractName}/src/contract.rs`),
@@ -117,12 +118,12 @@ async function runTest(t: ExecutionContext<Context>, c: Contract, opts: GenericO
117118
await assertContents(t, zip, opts);
118119
}
119120

120-
test('contractOptionsToScaffoldContractName converts PascalCase to snake_case', t => {
121-
t.is(contractOptionsToScaffoldContractName('Fungible'), 'fungible');
122-
t.is(contractOptionsToScaffoldContractName('NonFungible'), 'non_fungible');
123-
t.is(contractOptionsToScaffoldContractName('Pausable'), 'pausable');
124-
t.is(contractOptionsToScaffoldContractName('Upgradeable'), 'upgradeable');
125-
t.is(contractOptionsToScaffoldContractName('MyCustomKind'), 'my_custom_kind');
121+
test('contractOptionsToContractName converts PascalCase to snake_case', t => {
122+
t.is(contractOptionsToContractName('Fungible'), 'fungible');
123+
t.is(contractOptionsToContractName('NonFungible'), 'non_fungible');
124+
t.is(contractOptionsToContractName('Pausable'), 'pausable');
125+
t.is(contractOptionsToContractName('Upgradeable'), 'upgradeable');
126+
t.is(contractOptionsToContractName('MyCustomKind'), 'my_custom_kind');
126127
});
127128

128129
// test('fungible simple', async t => {

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ Generated by [AVA](https://avajs.dev).
114114
115115
[dev-dependencies]␊
116116
soroban-sdk = { workspace = true, features = ["testutils"] }␊
117-
118117
`,
119118
`#!/usr/bin/env bash␊
120119
#␊
@@ -526,7 +525,6 @@ Generated by [AVA](https://avajs.dev).
526525
527526
[dev-dependencies]␊
528527
soroban-sdk = { workspace = true, features = ["testutils"] }␊
529-
530528
`,
531529
`#!/usr/bin/env bash␊
532530
#␊
@@ -890,7 +888,6 @@ Generated by [AVA](https://avajs.dev).
890888
891889
[dev-dependencies]␊
892890
soroban-sdk = { workspace = true, features = ["testutils"] }␊
893-
894891
`,
895892
`#!/usr/bin/env bash␊
896893
#␊
@@ -1253,7 +1250,6 @@ Generated by [AVA](https://avajs.dev).
12531250
12541251
[dev-dependencies]␊
12551252
soroban-sdk = { workspace = true, features = ["testutils"] }␊
1256-
12571253
`,
12581254
`#!/usr/bin/env bash␊
12591255
#␊
@@ -1644,7 +1640,6 @@ Generated by [AVA](https://avajs.dev).
16441640
16451641
[dev-dependencies]␊
16461642
soroban-sdk = { workspace = true, features = ["testutils"] }␊
1647-
16481643
`,
16491644
`#!/usr/bin/env bash␊
16501645
#␊
@@ -2020,7 +2015,6 @@ Generated by [AVA](https://avajs.dev).
20202015
20212016
[dev-dependencies]␊
20222017
soroban-sdk = { workspace = true, features = ["testutils"] }␊
2023-
20242018
`,
20252019
`#!/usr/bin/env bash␊
20262020
#␊
@@ -2430,7 +2424,6 @@ Generated by [AVA](https://avajs.dev).
24302424
24312425
[dev-dependencies]␊
24322426
soroban-sdk = { workspace = true, features = ["testutils"] }␊
2433-
24342427
`,
24352428
`#!/usr/bin/env bash␊
24362429
#␊
@@ -2835,7 +2828,6 @@ Generated by [AVA](https://avajs.dev).
28352828
28362829
[dev-dependencies]␊
28372830
soroban-sdk = { workspace = true, features = ["testutils"] }␊
2838-
28392831
`,
28402832
`#!/usr/bin/env bash␊
28412833
#␊
@@ -3211,7 +3203,6 @@ Generated by [AVA](https://avajs.dev).
32113203
32123204
[dev-dependencies]␊
32133205
soroban-sdk = { workspace = true, features = ["testutils"] }␊
3214-
32153206
`,
32163207
`#!/usr/bin/env bash␊
32173208
#␊
@@ -3644,7 +3635,6 @@ Generated by [AVA](https://avajs.dev).
36443635
36453636
[dev-dependencies]␊
36463637
soroban-sdk = { workspace = true, features = ["testutils"] }␊
3647-
36483638
`,
36493639
`#!/usr/bin/env bash␊
36503640
#␊
@@ -4038,7 +4028,6 @@ Generated by [AVA](https://avajs.dev).
40384028
40394029
[dev-dependencies]␊
40404030
soroban-sdk = { workspace = true, features = ["testutils"] }␊
4041-
40424031
`,
40434032
`#!/usr/bin/env bash␊
40444033
#␊
@@ -4428,7 +4417,6 @@ Generated by [AVA](https://avajs.dev).
44284417
44294418
[dev-dependencies]␊
44304419
soroban-sdk = { workspace = true, features = ["testutils"] }␊
4431-
44324420
`,
44334421
`#!/usr/bin/env bash␊
44344422
#␊
@@ -4832,7 +4820,6 @@ Generated by [AVA](https://avajs.dev).
48324820
48334821
[dev-dependencies]␊
48344822
soroban-sdk = { workspace = true, features = ["testutils"] }␊
4835-
48364823
`,
48374824
`#!/usr/bin/env bash␊
48384825
#␊
@@ -5235,7 +5222,6 @@ Generated by [AVA](https://avajs.dev).
52355222
52365223
[dev-dependencies]␊
52375224
soroban-sdk = { workspace = true, features = ["testutils"] }␊
5238-
52395225
`,
52405226
`#!/usr/bin/env bash␊
52415227
#␊
@@ -5658,7 +5644,6 @@ Generated by [AVA](https://avajs.dev).
56585644
56595645
[dev-dependencies]␊
56605646
soroban-sdk = { workspace = true, features = ["testutils"] }␊
5661-
56625647
`,
56635648
`#!/usr/bin/env bash␊
56645649
#␊
@@ -6076,7 +6061,6 @@ Generated by [AVA](https://avajs.dev).
60766061
60776062
[dev-dependencies]␊
60786063
soroban-sdk = { workspace = true, features = ["testutils"] }␊
6079-
60806064
`,
60816065
`#!/usr/bin/env bash␊
60826066
#␊
@@ -6438,7 +6422,6 @@ Generated by [AVA](https://avajs.dev).
64386422
64396423
[dev-dependencies]␊
64406424
soroban-sdk = { workspace = true, features = ["testutils"] }␊
6441-
64426425
`,
64436426
`#!/usr/bin/env bash␊
64446427
#␊
@@ -6879,7 +6862,6 @@ Generated by [AVA](https://avajs.dev).
68796862
68806863
[dev-dependencies]␊
68816864
soroban-sdk = { workspace = true, features = ["testutils"] }␊
6882-
68836865
`,
68846866
`#!/usr/bin/env bash␊
68856867
#␊
@@ -7256,7 +7238,6 @@ Generated by [AVA](https://avajs.dev).
72567238
72577239
[dev-dependencies]␊
72587240
soroban-sdk = { workspace = true, features = ["testutils"] }␊
7259-
72607241
`,
72617242
`#!/usr/bin/env bash␊
72627243
#␊
@@ -7629,7 +7610,6 @@ Generated by [AVA](https://avajs.dev).
76297610
76307611
[dev-dependencies]␊
76317612
soroban-sdk = { workspace = true, features = ["testutils"] }␊
7632-
76337613
`,
76347614
`#!/usr/bin/env bash␊
76357615
#␊
@@ -8035,7 +8015,6 @@ Generated by [AVA](https://avajs.dev).
80358015
80368016
[dev-dependencies]␊
80378017
soroban-sdk = { workspace = true, features = ["testutils"] }␊
8038-
80398018
`,
80408019
`#!/usr/bin/env bash␊
80418020
#␊
@@ -8427,7 +8406,6 @@ Generated by [AVA](https://avajs.dev).
84278406
84288407
[dev-dependencies]␊
84298408
soroban-sdk = { workspace = true, features = ["testutils"] }␊
8430-
84318409
`,
84328410
`#!/usr/bin/env bash␊
84338411
#␊
@@ -8789,7 +8767,6 @@ Generated by [AVA](https://avajs.dev).
87898767
87908768
[dev-dependencies]␊
87918769
soroban-sdk = { workspace = true, features = ["testutils"] }␊
8792-
87938770
`,
87948771
`#!/usr/bin/env bash␊
87958772
#␊
@@ -9204,7 +9181,6 @@ Generated by [AVA](https://avajs.dev).
92049181
92059182
[dev-dependencies]␊
92069183
soroban-sdk = { workspace = true, features = ["testutils"] }␊
9207-
92089184
`,
92099185
`#!/usr/bin/env bash␊
92109186
#␊
@@ -9577,7 +9553,6 @@ Generated by [AVA](https://avajs.dev).
95779553
95789554
[dev-dependencies]␊
95799555
soroban-sdk = { workspace = true, features = ["testutils"] }␊
9580-
95819556
`,
95829557
`#!/usr/bin/env bash␊
95839558
#␊
@@ -9954,7 +9929,6 @@ Generated by [AVA](https://avajs.dev).
99549929
99559930
[dev-dependencies]␊
99569931
soroban-sdk = { workspace = true, features = ["testutils"] }␊
9957-
99589932
`,
99599933
`#!/usr/bin/env bash␊
99609934
#␊
@@ -10397,7 +10371,6 @@ Generated by [AVA](https://avajs.dev).
1039710371
1039810372
[dev-dependencies]␊
1039910373
soroban-sdk = { workspace = true, features = ["testutils"] }␊
10400-
1040110374
`,
1040210375
`#!/usr/bin/env bash␊
1040310376
#␊
Binary file not shown.

0 commit comments

Comments
 (0)