Skip to content

Commit

Permalink
Add Erc1155Supply template (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNeshi authored Feb 27, 2025
1 parent 157115b commit cc444b4
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 38 deletions.
6 changes: 3 additions & 3 deletions packages/core/stylus/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ export interface BaseFunction {
code: string[];
returns?: string;
comments?: string[];
attribute?: string;
}

export interface ContractFunction extends BaseFunction {
codeBefore?: string[];
tag?: string;
}

export interface Variable {
Expand Down Expand Up @@ -176,9 +176,9 @@ export class ContractBuilder implements Contract {
existingFn.codeBefore = [...(existingFn.codeBefore ?? []), ...codeBefore];
}

addFunctionTag(baseTrait: BaseImplementedTrait, fn: BaseFunction, tag: string): void {
addFunctionAttribute(baseTrait: BaseImplementedTrait, fn: BaseFunction, attribute: string): void {
this.addImplementedTrait(baseTrait);
const existingFn = this.addFunction(baseTrait, fn);
existingFn.tag = tag;
existingFn.attribute = attribute;
}
}
22 changes: 22 additions & 0 deletions packages/core/stylus/src/erc1155.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,30 @@ testERC1155('erc1155 burnable', {
// pausable: true,
// });

testERC1155('erc1155 supply', {
supply: true,
});

testERC1155('erc1155 supply burnable', {
supply: true,
burnable: true,
});

// testERC1155('erc1155 supply pausable', {
// supply: true,
// pausable: true,
// });

// testERC1155('erc1155 supply burnable pausable', {
// supply: true,
// burnable: true,
// pausable: true,
// });

testERC1155('erc1155 full - complex name', {
name: 'Custom $ Token',
burnable: true,
supply: true,
// pausable: true,
});

Expand All @@ -60,6 +81,7 @@ testAPIEquivalence('erc1155 API basic', { name: 'CustomToken' });
testAPIEquivalence('erc1155 API full', {
name: 'CustomToken',
burnable: true,
supply: true,
// pausable: true,
});

Expand Down
124 changes: 118 additions & 6 deletions packages/core/stylus/src/erc1155.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,105 @@ Generated by [AVA](https://avajs.dev).
}␊
`

## erc1155 supply

> Snapshot 1
`// SPDX-License-Identifier: MIT␊
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3␊
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
extern crate alloc;␊
use alloy_primitives::FixedBytes;␊
use openzeppelin_stylus::token::erc1155::extensions::Erc1155Supply;␊
use openzeppelin_stylus::token::erc1155::IErc1155;␊
use openzeppelin_stylus::utils::introspection::erc165::IErc165;␊
use stylus_sdk::prelude::{entrypoint, public, storage};␊
#[entrypoint]␊
#[storage]␊
struct MyToken {␊
#[borrow]␊
erc1155_supply: Erc1155Supply,␊
}␊
#[public]␊
#[inherit(Erc1155Supply)]␊
impl MyToken {␊
fn total_supply(&self, id: U256) -> U256 {␊
self.erc1155_supply.total_supply(id)␊
}␊
#[selector(name = "totalSupply")]␊
fn total_supply_all(&self) -> U256 {␊
self.erc1155_supply.total_supply_all()␊
}␊
fn exists(&self, id: U256) -> bool {␊
self.erc1155_supply.exists(id)␊
}␊
fn supports_interface(interface_id: FixedBytes<4>) -> bool {␊
Erc1155::supports_interface(interface_id)␊
}␊
}␊
`

## erc1155 supply burnable

> Snapshot 1
`// SPDX-License-Identifier: MIT␊
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3␊
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
extern crate alloc;␊
use alloc::vec::Vec;␊
use alloy_primitives::{Address, FixedBytes, U256};␊
use openzeppelin_stylus::token::erc1155::extensions::{Erc1155Supply, IErc1155Burnable};␊
use openzeppelin_stylus::token::erc1155::IErc1155;␊
use openzeppelin_stylus::utils::introspection::erc165::IErc165;␊
use stylus_sdk::prelude::{entrypoint, public, storage};␊
#[entrypoint]␊
#[storage]␊
struct MyToken {␊
#[borrow]␊
erc1155_supply: Erc1155Supply,␊
}␊
#[public]␊
#[inherit(Erc1155Supply)]␊
impl MyToken {␊
fn total_supply(&self, id: U256) -> U256 {␊
self.erc1155_supply.total_supply(id)␊
}␊
#[selector(name = "totalSupply")]␊
fn total_supply_all(&self) -> U256 {␊
self.erc1155_supply.total_supply_all()␊
}␊
fn exists(&self, id: U256) -> bool {␊
self.erc1155_supply.exists(id)␊
}␊
fn supports_interface(interface_id: FixedBytes<4>) -> bool {␊
Erc1155::supports_interface(interface_id)␊
}␊
fn burn(&mut self, account: Address, token_id: U256, value: U256) -> Result<(), Vec<u8>> {␊
self.erc1155_supply.burn(account, token_id, value).map_err(|e| e.into())␊
}␊
fn burn_batch(&mut self, account: Address, token_ids: Vec<U256>, values: Vec<U256>) -> Result<(), Vec<u8>> {␊
self.erc1155_supply.burn_batch(account, token_ids, values).map_err(|e| e.into())␊
}␊
}␊
`

## erc1155 full - complex name

> Snapshot 1
Expand All @@ -88,31 +187,44 @@ Generated by [AVA](https://avajs.dev).
use alloc::vec::Vec;␊
use alloy_primitives::{Address, FixedBytes, U256};␊
use openzeppelin_stylus::token::erc1155::{Erc1155, IErc1155};␊
use openzeppelin_stylus::token::erc1155::extensions::IErc1155Burnable;␊
use openzeppelin_stylus::token::erc1155::extensions::{Erc1155Supply, IErc1155Burnable};␊
use openzeppelin_stylus::token::erc1155::IErc1155;␊
use openzeppelin_stylus::utils::introspection::erc165::IErc165;␊
use stylus_sdk::prelude::{entrypoint, public, storage};␊
#[entrypoint]␊
#[storage]␊
struct CustomToken {␊
#[borrow]␊
erc1155: Erc1155,␊
erc1155_supply: Erc1155Supply,␊
}␊
#[public]␊
#[inherit(Erc1155)]␊
#[inherit(Erc1155Supply)]␊
impl CustomToken {␊
fn total_supply(&self, id: U256) -> U256 {␊
self.erc1155_supply.total_supply(id)␊
}␊
#[selector(name = "totalSupply")]␊
fn total_supply_all(&self) -> U256 {␊
self.erc1155_supply.total_supply_all()␊
}␊
fn exists(&self, id: U256) -> bool {␊
self.erc1155_supply.exists(id)␊
}␊
fn supports_interface(interface_id: FixedBytes<4>) -> bool {␊
Erc1155::supports_interface(interface_id)␊
}␊
fn burn(&mut self, account: Address, token_id: U256, value: U256) -> Result<(), Vec<u8>> {␊
self.erc1155.burn(account, token_id, value).map_err(|e| e.into())␊
self.erc1155_supply.burn(account, token_id, value).map_err(|e| e.into())␊
}␊
fn burn_batch(&mut self, account: Address, token_ids: Vec<U256>, values: Vec<U256>) -> Result<(), Vec<u8>> {␊
self.erc1155.burn_batch(account, token_ids, values).map_err(|e| e.into())␊
self.erc1155_supply.burn_batch(account, token_ids, values).map_err(|e| e.into())␊
}␊
}␊
`
Binary file modified packages/core/stylus/src/erc1155.test.ts.snap
Binary file not shown.
Loading

0 comments on commit cc444b4

Please sign in to comment.