Skip to content

Commit 5a9b253

Browse files
authored
Stylus updates for v0.2.0-alpha.4 + Fix Broken Templates (#478)
1 parent c869252 commit 5a9b253

14 files changed

+91
-81
lines changed

packages/core/stylus/src/contract.test.ts.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ Generated by [AVA](https://avajs.dev).
99
> Snapshot 1
1010
1111
`// SPDX-License-Identifier: MIT␊
12-
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3
12+
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.4
1313
1414
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
1515
extern crate alloc;␊
1616
17-
use stylus_sdk::prelude::{entrypoint, public, storage};␊
17+
use stylus_sdk::prelude::*;␊
1818
1919
#[entrypoint]␊
2020
#[storage]␊
@@ -30,13 +30,13 @@ Generated by [AVA](https://avajs.dev).
3030
> Snapshot 1
3131
3232
`// SPDX-License-Identifier: MIT␊
33-
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3
33+
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.4
3434
3535
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
3636
extern crate alloc;␊
3737
3838
use mod_ext::External;␊
39-
use stylus_sdk::prelude::{entrypoint, public, storage};␊
39+
use stylus_sdk::prelude::*;␊
4040
4141
#[entrypoint]␊
4242
#[storage]␊
@@ -60,13 +60,13 @@ Generated by [AVA](https://avajs.dev).
6060
> Snapshot 1
6161
6262
`// SPDX-License-Identifier: MIT␊
63-
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3
63+
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.4
6464
6565
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
6666
extern crate alloc;␊
6767
6868
use some::library::SomeLibrary;␊
69-
use stylus_sdk::prelude::{entrypoint, public, storage};␊
69+
use stylus_sdk::prelude::*;␊
7070
7171
#[entrypoint]␊
7272
#[storage]␊
@@ -82,13 +82,13 @@ Generated by [AVA](https://avajs.dev).
8282
> Snapshot 1
8383
8484
`// SPDX-License-Identifier: MIT␊
85-
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3
85+
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.4
8686
8787
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
8888
extern crate alloc;␊
8989
9090
use some::library::{Misc, SomeLibrary};␊
91-
use stylus_sdk::prelude::{entrypoint, public, storage};␊
91+
use stylus_sdk::prelude::*;␊
9292
9393
#[entrypoint]␊
9494
#[storage]␊
@@ -104,14 +104,14 @@ Generated by [AVA](https://avajs.dev).
104104
> Snapshot 1
105105
106106
`// SPDX-License-Identifier: MIT␊
107-
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3
107+
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.4
108108
109109
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
110110
extern crate alloc;␊
111111
112112
use another::library::{AnotherLibrary, Foo as Custom1, Foo as Custom2};␊
113113
use some::library::SomeLibrary;␊
114-
use stylus_sdk::prelude::{entrypoint, public, storage};␊
114+
use stylus_sdk::prelude::*;␊
115115
116116
#[entrypoint]␊
117117
#[storage]␊
@@ -127,7 +127,7 @@ Generated by [AVA](https://avajs.dev).
127127
> Snapshot 1
128128
129129
`// SPDX-License-Identifier: MIT␊
130-
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3
130+
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.4
131131
132132
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
133133
extern crate alloc;␊
@@ -136,7 +136,7 @@ Generated by [AVA](https://avajs.dev).
136136
use mod_b::B;␊
137137
use mod_special::Special;␊
138138
use mod_z::Z;␊
139-
use stylus_sdk::prelude::{entrypoint, public, storage};␊
139+
use stylus_sdk::prelude::*;␊
140140
141141
#[entrypoint]␊
142142
#[storage]␊
-8 Bytes
Binary file not shown.

packages/core/stylus/src/contract.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export class ContractBuilder implements Contract {
8686
identifier: toIdentifier(name, true),
8787
stringLiteral: escapeString(name),
8888
};
89+
this.addUseClause('stylus_sdk', 'prelude::*');
8990
}
9091

9192
get implementedTraits(): ImplementedTrait[] {

packages/core/stylus/src/erc1155.test.ts.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ Generated by [AVA](https://avajs.dev).
99
> Snapshot 1
1010
1111
`// SPDX-License-Identifier: MIT␊
12-
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3
12+
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.4
1313
1414
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
1515
extern crate alloc;␊
1616
1717
use alloy_primitives::FixedBytes;␊
1818
use openzeppelin_stylus::token::erc1155::{Erc1155, IErc1155};␊
1919
use openzeppelin_stylus::utils::introspection::erc165::IErc165;␊
20-
use stylus_sdk::prelude::{entrypoint, public, storage};␊
20+
use stylus_sdk::prelude::*;␊
2121
2222
#[entrypoint]␊
2323
#[storage]␊
@@ -40,7 +40,7 @@ Generated by [AVA](https://avajs.dev).
4040
> Snapshot 1
4141
4242
`// SPDX-License-Identifier: MIT␊
43-
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3
43+
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.4
4444
4545
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
4646
extern crate alloc;␊
@@ -50,7 +50,7 @@ Generated by [AVA](https://avajs.dev).
5050
use openzeppelin_stylus::token::erc1155::{Erc1155, IErc1155};␊
5151
use openzeppelin_stylus::token::erc1155::extensions::IErc1155Burnable;␊
5252
use openzeppelin_stylus::utils::introspection::erc165::IErc165;␊
53-
use stylus_sdk::prelude::{entrypoint, public, storage};␊
53+
use stylus_sdk::prelude::*;␊
5454
5555
#[entrypoint]␊
5656
#[storage]␊
@@ -81,16 +81,16 @@ Generated by [AVA](https://avajs.dev).
8181
> Snapshot 1
8282
8383
`// SPDX-License-Identifier: MIT␊
84-
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3
84+
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.4
8585
8686
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
8787
extern crate alloc;␊
8888
89-
use alloy_primitives::FixedBytes;␊
90-
use openzeppelin_stylus::token::erc1155::extensions::Erc1155Supply;␊
91-
use openzeppelin_stylus::token::erc1155::IErc1155;␊
89+
use alloy_primitives::{FixedBytes, U256};␊
90+
use openzeppelin_stylus::token::erc1155::{Erc1155, IErc1155};␊
91+
use openzeppelin_stylus::token::erc1155::extensions::{Erc1155Supply, IErc1155Supply};␊
9292
use openzeppelin_stylus::utils::introspection::erc165::IErc165;␊
93-
use stylus_sdk::prelude::{entrypoint, public, storage};␊
93+
use stylus_sdk::prelude::*;␊
9494
9595
#[entrypoint]␊
9696
#[storage]␊
@@ -126,17 +126,19 @@ Generated by [AVA](https://avajs.dev).
126126
> Snapshot 1
127127
128128
`// SPDX-License-Identifier: MIT␊
129-
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3
129+
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.4
130130
131131
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
132132
extern crate alloc;␊
133133
134134
use alloc::vec::Vec;␊
135135
use alloy_primitives::{Address, FixedBytes, U256};␊
136-
use openzeppelin_stylus::token::erc1155::extensions::{Erc1155Supply, IErc1155Burnable};␊
137-
use openzeppelin_stylus::token::erc1155::IErc1155;␊
136+
use openzeppelin_stylus::token::erc1155::{Erc1155, IErc1155};␊
137+
use openzeppelin_stylus::token::erc1155::extensions::{␊
138+
Erc1155Supply, IErc1155Burnable, IErc1155Supply␊
139+
};␊
138140
use openzeppelin_stylus::utils::introspection::erc165::IErc165;␊
139-
use stylus_sdk::prelude::{entrypoint, public, storage};␊
141+
use stylus_sdk::prelude::*;␊
140142
141143
#[entrypoint]␊
142144
#[storage]␊
@@ -180,17 +182,19 @@ Generated by [AVA](https://avajs.dev).
180182
> Snapshot 1
181183
182184
`// SPDX-License-Identifier: MIT␊
183-
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3
185+
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.4
184186
185187
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
186188
extern crate alloc;␊
187189
188190
use alloc::vec::Vec;␊
189191
use alloy_primitives::{Address, FixedBytes, U256};␊
190-
use openzeppelin_stylus::token::erc1155::extensions::{Erc1155Supply, IErc1155Burnable};␊
191-
use openzeppelin_stylus::token::erc1155::IErc1155;␊
192+
use openzeppelin_stylus::token::erc1155::{Erc1155, IErc1155};␊
193+
use openzeppelin_stylus::token::erc1155::extensions::{␊
194+
Erc1155Supply, IErc1155Burnable, IErc1155Supply␊
195+
};␊
192196
use openzeppelin_stylus::utils::introspection::erc165::IErc165;␊
193-
use stylus_sdk::prelude::{entrypoint, public, storage};␊
197+
use stylus_sdk::prelude::*;␊
194198
195199
#[entrypoint]␊
196200
#[storage]␊
-16 Bytes
Binary file not shown.

packages/core/stylus/src/erc1155.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ export function buildERC1155(opts: ERC1155Options): Contract {
4949
const allOpts = withDefaults(opts);
5050

5151
// Erc1155Supply reexports Erc1155 functionality
52-
const baseTrait = addBase(c, allOpts);
52+
const baseTrait = allOpts.supply ? erc1155SupplyTrait : erc1155Trait;
53+
54+
addBase(c, baseTrait);
5355

5456
if (allOpts.supply) {
55-
addSupplyFunctions(c);
57+
addSupply(c, baseTrait);
5658
}
5759

5860
// c.addImplementedTrait(erc1155MetadataTrait);
@@ -71,16 +73,16 @@ export function buildERC1155(opts: ERC1155Options): Contract {
7173
return c;
7274
}
7375

74-
function addBase(c: ContractBuilder, allOpts: ERC1155Options): BaseImplementedTrait {
75-
const baseTrait = allOpts.supply ? erc1155SupplyTrait : erc1155Trait;
76-
76+
function addBase(c: ContractBuilder, baseTrait: BaseImplementedTrait) {
7777
c.addImplementedTrait(baseTrait);
7878

7979
// the trait necessary to access Erc1155 functions within custom functions of the child contract
8080
c.addUseClause('openzeppelin_stylus::token::erc1155', 'IErc1155');
8181

8282
// Override IErc65 from Erc1155
8383
c.addUseClause('openzeppelin_stylus::utils', 'introspection::erc165::IErc165');
84+
// need this to expose the `Erc1155::supports_interface` function (Erc1155Supply does not override this function)
85+
c.addUseClause('openzeppelin_stylus::token::erc1155', 'Erc1155');
8486
c.addUseClause('alloy_primitives', 'FixedBytes');
8587
c.addFunction(baseTrait, functions(baseTrait).supports_interface); // TODO: This is currently hardcoded to call Erc1155. If other overrides are needed, consider a more generic solution. See Solidity's addOverride function in `packages/core/solidity/src/contract.ts` for example
8688

@@ -93,15 +95,22 @@ function addBase(c: ContractBuilder, allOpts: ERC1155Options): BaseImplementedTr
9395
// c.addFunctionCodeBefore(baseTrait, functions(baseTrait).safe_transfer_from, ['self.pausable.when_not_paused()?;']);
9496
// c.addFunctionCodeBefore(baseTrait, functions(baseTrait).safe_batch_transfer_from, ['self.pausable.when_not_paused()?;']);
9597
// }
96-
97-
return baseTrait;
9898
}
9999

100-
function addSupplyFunctions(c: ContractBuilder) {
101-
const fns = functions(erc1155SupplyTrait);
102-
c.addFunction(erc1155SupplyTrait, fns.total_supply);
103-
c.addFunction(erc1155SupplyTrait, fns.total_supply_all);
104-
c.addFunction(erc1155SupplyTrait, fns.exists);
100+
// This adds supply-related parts without re-adding the contract structure,
101+
// as it was already added in `addBase`.
102+
function addSupply(c: ContractBuilder, baseTrait: BaseImplementedTrait) {
103+
if (baseTrait !== erc1155SupplyTrait) {
104+
throw new Error('Base trait must be of type `Erc1155Supply`');
105+
}
106+
107+
c.addUseClause('openzeppelin_stylus::token::erc1155::extensions', 'IErc1155Supply');
108+
c.addUseClause('alloy_primitives', 'U256');
109+
110+
const fns = functions(baseTrait);
111+
c.addFunction(baseTrait, fns.total_supply);
112+
c.addFunction(baseTrait, fns.total_supply_all);
113+
c.addFunction(baseTrait, fns.exists);
105114

106115
// if (pausable) {
107116
// // Add pausable checks to appropriate functions

0 commit comments

Comments
 (0)