Skip to content

Commit 7149d9c

Browse files
committed
Support customizing decimals in cairo
1 parent 73727c7 commit 7149d9c

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

packages/core/cairo/src/erc20.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Contract } from './contract';
1+
import type { BaseImplementedTrait, Contract } from './contract';
22
import { ContractBuilder } from './contract';
33
import type { Access } from './set-access-control';
44
import { requireAccessControl, setAccessControl } from './set-access-control';
@@ -19,6 +19,7 @@ import { addVotesComponent } from './common-components';
1919
export const defaults: Required<ERC20Options> = {
2020
name: 'MyToken',
2121
symbol: 'MTK',
22+
decimals: '18',
2223
burnable: false,
2324
pausable: false,
2425
premint: '0',
@@ -38,6 +39,7 @@ export function printERC20(opts: ERC20Options = defaults): string {
3839
export interface ERC20Options extends CommonContractOptions {
3940
name: string;
4041
symbol: string;
42+
decimals: string;
4143
burnable?: boolean;
4244
pausable?: boolean;
4345
premint?: string;
@@ -70,7 +72,9 @@ export function buildERC20(opts: ERC20Options): Contract {
7072

7173
const allOpts = withDefaults(opts);
7274

73-
addBase(c, toByteArray(allOpts.name), toByteArray(allOpts.symbol));
75+
const decimals = toUint(opts.decimals, 'decimals', 'u8');
76+
addBase(c, toByteArray(allOpts.name), toByteArray(allOpts.symbol), decimals);
77+
7478
addERC20Mixin(c);
7579

7680
if (allOpts.premint) {
@@ -183,9 +187,22 @@ function addERC20Mixin(c: ContractBuilder) {
183187
});
184188
}
185189

186-
function addBase(c: ContractBuilder, name: string, symbol: string) {
187-
c.addUseClause('openzeppelin::token::erc20', 'DefaultConfig');
190+
function addBase(c: ContractBuilder, name: string, symbol: string, decimals: bigint) {
191+
// Add ERC20 component
188192
c.addComponent(components.ERC20Component, [name, symbol], true);
193+
194+
// Add immutable config with decimals
195+
const trait: BaseImplementedTrait = {
196+
name: 'ERC20ImmutableConfig',
197+
of: 'ERC20Component::ImmutableConfig',
198+
tags: [],
199+
};
200+
c.addImplementedTrait(trait);
201+
c.addSuperVariableToTrait(trait, {
202+
name: 'DECIMALS',
203+
type: 'u8',
204+
value: decimals.toString(),
205+
});
189206
}
190207

191208
function addBurnable(c: ContractBuilder) {

packages/core/cairo/src/generate/erc20.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const booleans = [true, false];
99
const blueprint = {
1010
name: ['MyToken'],
1111
symbol: ['MTK'],
12+
decimals: ['18'],
1213
burnable: booleans,
1314
pausable: booleans,
1415
mintable: booleans,

packages/ui/src/cairo/ERC20Controls.svelte

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737
</label>
3838
</div>
3939

40+
<label class="labeled-input">
41+
<span class="flex justify-between pr-2">
42+
Decimals
43+
<HelpTooltip>The number of decimals the token uses.</HelpTooltip>
44+
</span>
45+
<input bind:value={opts.decimals} use:error={errors?.decimals} />
46+
</label>
47+
4048
<label class="labeled-input">
4149
<span class="flex justify-between pr-2">
4250
Premint

0 commit comments

Comments
 (0)