Skip to content

Commit 6bbb326

Browse files
authored
Stylus: use ImplicitNameFunction instead of BaseFunction (#603)
1 parent a8c839b commit 6bbb326

File tree

4 files changed

+5
-43
lines changed

4 files changed

+5
-43
lines changed

packages/core/stylus/src/erc1155.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,31 +86,26 @@ const functions = {
8686
erc1155: (storageName: StorageName) =>
8787
defineFunctions({
8888
balance_of: {
89-
name: 'balance_of',
9089
args: [getSelfArg('immutable'), { name: 'account', type: 'Address' }, { name: 'id', type: 'U256' }],
9190
returns: 'U256',
9291
code: `self.${storageName}.balance_of(account, id)`,
9392
},
9493
balance_of_batch: {
95-
name: 'balance_of_batch',
9694
args: [getSelfArg('immutable'), { name: 'accounts', type: 'Vec<Address>' }, { name: 'ids', type: 'Vec<U256>' }],
9795
returns: { ok: 'Vec<U256>', err: 'Self::Error' },
9896
code: `self.${storageName}.balance_of_batch(accounts, ids)?`,
9997
},
10098
set_approval_for_all: {
101-
name: 'set_approval_for_all',
10299
args: [getSelfArg(), { name: 'operator', type: 'Address' }, { name: 'approved', type: 'bool' }],
103100
returns: { ok: '()', err: 'Self::Error' },
104101
code: `self.${storageName}.set_approval_for_all(operator, approved)?`,
105102
},
106103
is_approved_for_all: {
107-
name: 'is_approved_for_all',
108104
args: [getSelfArg('immutable'), { name: 'account', type: 'Address' }, { name: 'operator', type: 'Address' }],
109105
returns: 'bool',
110106
code: `self.${storageName}.is_approved_for_all(account, operator)`,
111107
},
112108
safe_transfer_from: {
113-
name: 'safe_transfer_from',
114109
args: [
115110
getSelfArg(),
116111
{ name: 'from', type: 'Address' },
@@ -123,7 +118,6 @@ const functions = {
123118
code: `self.${storageName}.safe_transfer_from(from, to, id, value, data)?`,
124119
},
125120
safe_batch_transfer_from: {
126-
name: 'safe_batch_transfer_from',
127121
args: [
128122
getSelfArg(),
129123
{ name: 'from', type: 'Address' },
@@ -139,7 +133,6 @@ const functions = {
139133
erc165: (storageName: StorageName) =>
140134
defineFunctions({
141135
supports_interface: {
142-
name: 'supports_interface',
143136
args: [getSelfArg('immutable'), { name: 'interface_id', type: 'FixedBytes<4>' }],
144137
returns: 'bool',
145138
code: `self.${storageName}.supports_interface(interface_id)`,
@@ -148,7 +141,6 @@ const functions = {
148141
burnable: (storageName: StorageName) =>
149142
defineFunctions({
150143
burn: {
151-
name: 'burn',
152144
args: [
153145
getSelfArg(),
154146
{ name: 'account', type: 'Address' },
@@ -162,7 +154,6 @@ const functions = {
162154
: `self.${storageName}._burn(account, token_id, value)?`,
163155
},
164156
burn_batch: {
165-
name: 'burn_batch',
166157
args: [
167158
getSelfArg(),
168159
{ name: 'account', type: 'Address' },
@@ -178,20 +169,17 @@ const functions = {
178169
}),
179170
erc1155Supply: defineFunctions({
180171
total_supply: {
181-
name: 'total_supply',
182172
args: [getSelfArg('immutable'), { name: 'id', type: 'U256' }],
183173
returns: 'U256',
184174
code: `self.${ERC1155_SUPPLY_STORAGE_NAME}.total_supply(id)`,
185175
},
186176
total_supply_all: {
187-
name: 'total_supply_all',
188177
attribute: 'selector(name = "totalSupply")',
189178
args: [getSelfArg('immutable')],
190179
returns: 'U256',
191180
code: `self.${ERC1155_SUPPLY_STORAGE_NAME}.total_supply_all()`,
192181
},
193182
exists: {
194-
name: 'exists',
195183
args: [getSelfArg('immutable'), { name: 'id', type: 'U256' }],
196184
returns: 'bool',
197185
code: `self.${ERC1155_SUPPLY_STORAGE_NAME}.exists(id)`,

packages/core/stylus/src/erc20.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,37 +94,31 @@ const FLASH_MINT_STORAGE_NAME = 'flash_mint';
9494
const functions = {
9595
erc20: defineFunctions({
9696
total_supply: {
97-
name: 'total_supply',
9897
args: [getSelfArg('immutable')],
9998
returns: 'U256',
10099
code: `self.${ERC20_STORAGE_NAME}.total_supply()`,
101100
},
102101
balance_of: {
103-
name: 'balance_of',
104102
args: [getSelfArg('immutable'), { name: 'account', type: 'Address' }],
105103
returns: 'U256',
106104
code: `self.${ERC20_STORAGE_NAME}.balance_of(account)`,
107105
},
108106
transfer: {
109-
name: 'transfer',
110107
args: [getSelfArg(), { name: 'to', type: 'Address' }, { name: 'value', type: 'U256' }],
111108
returns: { ok: 'bool', err: 'Self::Error' },
112109
code: `self.${ERC20_STORAGE_NAME}.transfer(to, value)?`,
113110
},
114111
allowance: {
115-
name: 'allowance',
116112
args: [getSelfArg('immutable'), { name: 'owner', type: 'Address' }, { name: 'spender', type: 'Address' }],
117113
returns: 'U256',
118114
code: `self.${ERC20_STORAGE_NAME}.allowance(owner, spender)`,
119115
},
120116
approve: {
121-
name: 'approve',
122117
args: [getSelfArg(), { name: 'spender', type: 'Address' }, { name: 'value', type: 'U256' }],
123118
returns: { ok: 'bool', err: 'Self::Error' },
124119
code: `self.${ERC20_STORAGE_NAME}.approve(spender, value)?`,
125120
},
126121
transfer_from: {
127-
name: 'transfer_from',
128122
args: [
129123
getSelfArg(),
130124
{ name: 'from', type: 'Address' },
@@ -136,23 +130,20 @@ const functions = {
136130
},
137131
}),
138132
nonces: defineFunctions({
139-
get_nonce: {
140-
name: 'nonces',
133+
nonces: {
141134
args: [getSelfArg('immutable'), { name: 'owner', type: 'Address' }],
142135
returns: 'U256',
143136
code: `self.${NONCES_STORAGE_NAME}.nonces(owner)`,
144137
},
145138
}),
146139
permit: defineFunctions({
147140
domain_separator: {
148-
name: 'domain_separator',
149141
attribute: 'selector(name = "DOMAIN_SEPARATOR")',
150142
args: [getSelfArg('immutable')],
151143
returns: 'B256',
152144
code: `self.${PERMIT_STORAGE_NAME}.domain_separator()`,
153145
},
154146
permit: {
155-
name: 'permit',
156147
args: [
157148
getSelfArg(),
158149
{ name: 'owner', type: 'Address' },
@@ -169,33 +160,28 @@ const functions = {
169160
}),
170161
burnable: defineFunctions({
171162
burn: {
172-
name: 'burn',
173163
args: [getSelfArg(), { name: 'value', type: 'U256' }],
174164
returns: { ok: '()', err: 'Self::Error' },
175165
code: `self.${ERC20_STORAGE_NAME}.burn(value)?`,
176166
},
177167
burn_from: {
178-
name: 'burn_from',
179168
args: [getSelfArg(), { name: 'account', type: 'Address' }, { name: 'value', type: 'U256' }],
180169
returns: { ok: '()', err: 'Self::Error' },
181170
code: `self.${ERC20_STORAGE_NAME}.burn_from(account, value)?`,
182171
},
183172
}),
184173
flash_mint: defineFunctions({
185174
max_flash_loan: {
186-
name: 'max_flash_loan',
187175
args: [getSelfArg('immutable'), { name: 'token', type: 'Address' }],
188176
returns: 'U256',
189177
code: `self.${FLASH_MINT_STORAGE_NAME}.max_flash_loan(token, &self.${ERC20_STORAGE_NAME})`,
190178
},
191179
flash_fee: {
192-
name: 'flash_fee',
193180
args: [getSelfArg('immutable'), { name: 'token', type: 'Address' }, { name: 'value', type: 'U256' }],
194181
returns: { ok: 'U256', err: 'Self::Error' },
195182
code: `self.${FLASH_MINT_STORAGE_NAME}.flash_fee(token, value)?`,
196183
},
197184
flash_loan: {
198-
name: 'flash_loan',
199185
args: [
200186
getSelfArg(),
201187
{ name: 'receiver', type: 'Address' },

packages/core/stylus/src/erc721.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,16 @@ const ENUMERABLE_STORAGE_NAME = 'enumerable';
125125
const functions = {
126126
erc721: defineFunctions({
127127
balance_of: {
128-
name: 'balance_of',
129128
args: [getSelfArg('immutable'), { name: 'owner', type: 'Address' }],
130129
returns: { ok: 'U256', err: 'Self::Error' },
131130
code: `self.${ERC721_STORAGE_NAME}.balance_of(owner)?`,
132131
},
133132
owner_of: {
134-
name: 'owner_of',
135133
args: [getSelfArg('immutable'), { name: 'token_id', type: 'U256' }],
136134
returns: { ok: 'Address', err: 'Self::Error' },
137135
code: `self.${ERC721_STORAGE_NAME}.owner_of(token_id)?`,
138136
},
139137
safe_transfer_from: {
140-
name: 'safe_transfer_from',
141138
args: [
142139
getSelfArg(),
143140
{ name: 'from', type: 'Address' },
@@ -149,7 +146,6 @@ const functions = {
149146
},
150147
safe_transfer_from_with_data: {
151148
attribute: 'selector(name = "safeTransferFrom")',
152-
name: 'safe_transfer_from_with_data',
153149
args: [
154150
getSelfArg(),
155151
{ name: 'from', type: 'Address' },
@@ -161,7 +157,6 @@ const functions = {
161157
code: `self.${ERC721_STORAGE_NAME}.safe_transfer_from_with_data(from, to, token_id, data)?`,
162158
},
163159
transfer_from: {
164-
name: 'transfer_from',
165160
args: [
166161
getSelfArg(),
167162
{ name: 'from', type: 'Address' },
@@ -172,61 +167,52 @@ const functions = {
172167
code: `self.${ERC721_STORAGE_NAME}.transfer_from(from, to, token_id)?`,
173168
},
174169
approve: {
175-
name: 'approve',
176170
args: [getSelfArg(), { name: 'to', type: 'Address' }, { name: 'token_id', type: 'U256' }],
177171
returns: { ok: '()', err: 'Self::Error' },
178172
code: `self.${ERC721_STORAGE_NAME}.approve(to, token_id)?`,
179173
},
180174
set_approval_for_all: {
181-
name: 'set_approval_for_all',
182175
args: [getSelfArg(), { name: 'operator', type: 'Address' }, { name: 'approved', type: 'bool' }],
183176
returns: { ok: '()', err: 'Self::Error' },
184177
code: `self.${ERC721_STORAGE_NAME}.set_approval_for_all(operator, approved)?`,
185178
},
186179
get_approved: {
187-
name: 'get_approved',
188180
args: [getSelfArg('immutable'), { name: 'token_id', type: 'U256' }],
189181
returns: { ok: 'Address', err: 'Self::Error' },
190182
code: `self.${ERC721_STORAGE_NAME}.get_approved(token_id)?`,
191183
},
192184
is_approved_for_all: {
193-
name: 'is_approved_for_all',
194185
args: [getSelfArg('immutable'), { name: 'owner', type: 'Address' }, { name: 'operator', type: 'Address' }],
195186
returns: 'bool',
196187
code: `self.${ERC721_STORAGE_NAME}.is_approved_for_all(owner, operator)`,
197188
},
198189
}),
199190
erc165: defineFunctions({
200191
supports_interface: {
201-
name: 'supports_interface',
202192
args: [getSelfArg('immutable'), { name: 'interface_id', type: 'FixedBytes<4>' }],
203193
returns: 'bool',
204194
code: `self.${ERC721_STORAGE_NAME}.supports_interface(interface_id)`,
205195
},
206196
}),
207197
burnable: defineFunctions({
208198
burn: {
209-
name: 'burn',
210199
args: [getSelfArg(), { name: 'token_id', type: 'U256' }],
211200
returns: { ok: '()', err: 'Self::Error' },
212201
code: `self.${ERC721_STORAGE_NAME}.burn(token_id)?`,
213202
},
214203
}),
215204
enumerable: defineFunctions({
216205
token_of_owner_by_index: {
217-
name: 'token_of_owner_by_index',
218206
args: [getSelfArg('immutable'), { name: 'owner', type: 'Address' }, { name: 'index', type: 'U256' }],
219207
returns: { ok: 'U256', err: 'Self::Error' },
220208
code: `self.${ENUMERABLE_STORAGE_NAME}.token_of_owner_by_index(owner, index)?`,
221209
},
222210
total_supply: {
223-
name: 'total_supply',
224211
args: [getSelfArg('immutable')],
225212
returns: 'U256',
226213
code: `self.${ENUMERABLE_STORAGE_NAME}.total_supply()`,
227214
},
228215
token_by_index: {
229-
name: 'token_by_index',
230216
args: [getSelfArg('immutable'), { name: 'index', type: 'U256' }],
231217
returns: { ok: 'U256', err: 'Self::Error' },
232218
code: `self.${ENUMERABLE_STORAGE_NAME}.token_by_index(index)?`,
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { BaseFunction } from '../contract';
22

3-
export function defineFunctions<F extends string>(fns: Record<F, BaseFunction>): Record<F, BaseFunction>;
3+
type ImplicitNameFunction = Omit<BaseFunction, 'name'>;
44

5-
export function defineFunctions(fns: Record<string, BaseFunction>): Record<string, BaseFunction> {
5+
export function defineFunctions<F extends string>(fns: Record<F, ImplicitNameFunction>): Record<F, BaseFunction>;
6+
7+
export function defineFunctions(fns: Record<string, ImplicitNameFunction>): Record<string, BaseFunction> {
68
return Object.fromEntries(Object.entries(fns).map(([name, fn]) => [name, Object.assign({ name }, fn)]));
79
}

0 commit comments

Comments
 (0)