Skip to content

Commit a70fb7e

Browse files
committed
Merge branch 'master' into add-chained-dapp-tx-example
2 parents a501590 + d315341 commit a70fb7e

File tree

20 files changed

+348
-37
lines changed

20 files changed

+348
-37
lines changed

.project.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,25 +199,31 @@
199199
},
200200
"InsertIntoMap": {
201201
"sourceFile": "test/map.ral",
202-
"sourceCodeHash": "331b92fe6617c1c7ac7f742ceea1943a06717477a2bd03ca266d23c65d844de6",
202+
"sourceCodeHash": "e15d05c08920b56b57cf23d78b734666b940e8cfe4c5d815d2676c4ce13a228d",
203203
"bytecodeDebugPatch": "",
204204
"codeHashDebug": ""
205205
},
206206
"MapTest": {
207207
"sourceFile": "test/map.ral",
208-
"sourceCodeHash": "331b92fe6617c1c7ac7f742ceea1943a06717477a2bd03ca266d23c65d844de6",
208+
"sourceCodeHash": "e15d05c08920b56b57cf23d78b734666b940e8cfe4c5d815d2676c4ce13a228d",
209209
"bytecodeDebugPatch": "=6-2+a8=1-3+128=2-2+ea=1+2=1-2+7=10-2+4025=50+7a7e0214696e73657274206174206d617020706174683a2000=56+7a7e0214696e73657274206174206d617020706174683a2000=54+7a7e0214696e73657274206174206d617020706174683a2000=280-2+33=124+7a7e021472656d6f7665206174206d617020706174683a2000=46+7a7e021472656d6f7665206174206d617020706174683a2000=48+7a7e021472656d6f7665206174206d617020706174683a2000=96",
210210
"codeHashDebug": "31aed0ff7b29f2cbc2d8360a83f31af4e9db00f0084a7406bd84b7745181373d"
211211
},
212+
"MapTestSub": {
213+
"sourceFile": "test/map.ral",
214+
"sourceCodeHash": "e15d05c08920b56b57cf23d78b734666b940e8cfe4c5d815d2676c4ce13a228d",
215+
"bytecodeDebugPatch": "",
216+
"codeHashDebug": "755ebb4ca4c436991cc8363fedb6840abf16857a6c326983376db9e68fe8c985"
217+
},
212218
"MapTestWrapper": {
213219
"sourceFile": "test/map.ral",
214-
"sourceCodeHash": "331b92fe6617c1c7ac7f742ceea1943a06717477a2bd03ca266d23c65d844de6",
220+
"sourceCodeHash": "e15d05c08920b56b57cf23d78b734666b940e8cfe4c5d815d2676c4ce13a228d",
215221
"bytecodeDebugPatch": "",
216222
"codeHashDebug": "1d525d3e4cbd1c8f4c0431bf6881e888eeebae012a14532530097f62dd766e9a"
217223
},
218224
"MapValue": {
219225
"sourceFile": "test/map.ral",
220-
"sourceCodeHash": "331b92fe6617c1c7ac7f742ceea1943a06717477a2bd03ca266d23c65d844de6",
226+
"sourceCodeHash": "e15d05c08920b56b57cf23d78b734666b940e8cfe4c5d815d2676c4ce13a228d",
221227
"bytecodeDebugPatch": "",
222228
"codeHashDebug": ""
223229
},
@@ -283,7 +289,7 @@
283289
},
284290
"RemoveFromMap": {
285291
"sourceFile": "test/map.ral",
286-
"sourceCodeHash": "331b92fe6617c1c7ac7f742ceea1943a06717477a2bd03ca266d23c65d844de6",
292+
"sourceCodeHash": "e15d05c08920b56b57cf23d78b734666b940e8cfe4c5d815d2676c4ce13a228d",
287293
"bytecodeDebugPatch": "",
288294
"codeHashDebug": ""
289295
},
@@ -331,7 +337,7 @@
331337
},
332338
"UpdateMapValue": {
333339
"sourceFile": "test/map.ral",
334-
"sourceCodeHash": "331b92fe6617c1c7ac7f742ceea1943a06717477a2bd03ca266d23c65d844de6",
340+
"sourceCodeHash": "e15d05c08920b56b57cf23d78b734666b940e8cfe4c5d815d2676c4ce13a228d",
335341
"bytecodeDebugPatch": "",
336342
"codeHashDebug": ""
337343
},

artifacts/test/MapTestSub.ral.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"version": "v3.10.0",
3+
"name": "MapTestSub",
4+
"bytecode": "0101404601030307014023d3cef11f5a14046d61703a160047441703130064130064170517041600d1a21603ce0016041605c117061600d10f2ca21600160116020f0c16060100160602",
5+
"codeHash": "755ebb4ca4c436991cc8363fedb6840abf16857a6c326983376db9e68fe8c985",
6+
"fieldsSig": {
7+
"names": [
8+
"mapTestTemplateId"
9+
],
10+
"types": [
11+
"ByteVec"
12+
],
13+
"isMutable": [
14+
false
15+
]
16+
},
17+
"eventsSig": [],
18+
"functions": [
19+
{
20+
"name": "init",
21+
"paramNames": [
22+
"caller",
23+
"value"
24+
],
25+
"paramTypes": [
26+
"Address",
27+
"MapValue"
28+
],
29+
"paramIsMutable": [
30+
false,
31+
false
32+
],
33+
"returnTypes": [
34+
"ByteVec"
35+
]
36+
}
37+
],
38+
"constants": [],
39+
"enums": []
40+
}

artifacts/ts/MapTestSub.ts

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/* Autogenerated file. Do not edit manually. */
2+
/* tslint:disable */
3+
/* eslint-disable */
4+
5+
import {
6+
Address,
7+
Contract,
8+
ContractState,
9+
TestContractResult,
10+
HexString,
11+
ContractFactory,
12+
EventSubscribeOptions,
13+
EventSubscription,
14+
CallContractParams,
15+
CallContractResult,
16+
TestContractParams,
17+
ContractEvent,
18+
subscribeContractEvent,
19+
subscribeContractEvents,
20+
testMethod,
21+
callMethod,
22+
multicallMethods,
23+
fetchContractState,
24+
Asset,
25+
ContractInstance,
26+
getContractEventsCurrentCount,
27+
TestContractParamsWithoutMaps,
28+
TestContractResultWithoutMaps,
29+
SignExecuteContractMethodParams,
30+
SignExecuteScriptTxResult,
31+
signExecuteMethod,
32+
addStdIdToFields,
33+
encodeContractFields,
34+
Narrow,
35+
} from "@alephium/web3";
36+
import { default as MapTestSubContractJson } from "../test/MapTestSub.ral.json";
37+
import { getContractByCodeHash, registerContract } from "./contracts";
38+
import {
39+
AddStruct1,
40+
AddStruct2,
41+
Balances,
42+
MapValue,
43+
TokenBalance,
44+
AllStructs,
45+
} from "./types";
46+
47+
// Custom types for the contract
48+
export namespace MapTestSubTypes {
49+
export type Fields = {
50+
mapTestTemplateId: HexString;
51+
};
52+
53+
export type State = ContractState<Fields>;
54+
55+
export interface CallMethodTable {
56+
init: {
57+
params: CallContractParams<{ caller: Address; value: MapValue }>;
58+
result: CallContractResult<HexString>;
59+
};
60+
}
61+
export type CallMethodParams<T extends keyof CallMethodTable> =
62+
CallMethodTable[T]["params"];
63+
export type CallMethodResult<T extends keyof CallMethodTable> =
64+
CallMethodTable[T]["result"];
65+
export type MultiCallParams = Partial<{
66+
[Name in keyof CallMethodTable]: CallMethodTable[Name]["params"];
67+
}>;
68+
export type MultiCallResults<T extends MultiCallParams> = {
69+
[MaybeName in keyof T]: MaybeName extends keyof CallMethodTable
70+
? CallMethodTable[MaybeName]["result"]
71+
: undefined;
72+
};
73+
export type MulticallReturnType<Callss extends MultiCallParams[]> = {
74+
[index in keyof Callss]: MultiCallResults<Callss[index]>;
75+
};
76+
77+
export interface SignExecuteMethodTable {
78+
init: {
79+
params: SignExecuteContractMethodParams<{
80+
caller: Address;
81+
value: MapValue;
82+
}>;
83+
result: SignExecuteScriptTxResult;
84+
};
85+
}
86+
export type SignExecuteMethodParams<T extends keyof SignExecuteMethodTable> =
87+
SignExecuteMethodTable[T]["params"];
88+
export type SignExecuteMethodResult<T extends keyof SignExecuteMethodTable> =
89+
SignExecuteMethodTable[T]["result"];
90+
}
91+
92+
class Factory extends ContractFactory<
93+
MapTestSubInstance,
94+
MapTestSubTypes.Fields
95+
> {
96+
encodeFields(fields: MapTestSubTypes.Fields) {
97+
return encodeContractFields(
98+
addStdIdToFields(this.contract, fields),
99+
this.contract.fieldsSig,
100+
AllStructs
101+
);
102+
}
103+
104+
at(address: string): MapTestSubInstance {
105+
return new MapTestSubInstance(address);
106+
}
107+
108+
tests = {
109+
init: async (
110+
params: TestContractParamsWithoutMaps<
111+
MapTestSubTypes.Fields,
112+
{ caller: Address; value: MapValue }
113+
>
114+
): Promise<TestContractResultWithoutMaps<HexString>> => {
115+
return testMethod(this, "init", params, getContractByCodeHash);
116+
},
117+
};
118+
119+
stateForTest(
120+
initFields: MapTestSubTypes.Fields,
121+
asset?: Asset,
122+
address?: string
123+
) {
124+
return this.stateForTest_(initFields, asset, address, undefined);
125+
}
126+
}
127+
128+
// Use this object to test and deploy the contract
129+
export const MapTestSub = new Factory(
130+
Contract.fromJson(
131+
MapTestSubContractJson,
132+
"",
133+
"755ebb4ca4c436991cc8363fedb6840abf16857a6c326983376db9e68fe8c985",
134+
AllStructs
135+
)
136+
);
137+
registerContract(MapTestSub);
138+
139+
// Use this class to interact with the blockchain
140+
export class MapTestSubInstance extends ContractInstance {
141+
constructor(address: Address) {
142+
super(address);
143+
}
144+
145+
async fetchState(): Promise<MapTestSubTypes.State> {
146+
return fetchContractState(MapTestSub, this);
147+
}
148+
149+
view = {
150+
init: async (
151+
params: MapTestSubTypes.CallMethodParams<"init">
152+
): Promise<MapTestSubTypes.CallMethodResult<"init">> => {
153+
return callMethod(
154+
MapTestSub,
155+
this,
156+
"init",
157+
params,
158+
getContractByCodeHash
159+
);
160+
},
161+
};
162+
163+
transact = {
164+
init: async (
165+
params: MapTestSubTypes.SignExecuteMethodParams<"init">
166+
): Promise<MapTestSubTypes.SignExecuteMethodResult<"init">> => {
167+
return signExecuteMethod(MapTestSub, this, "init", params);
168+
},
169+
};
170+
171+
async multicall<Calls extends MapTestSubTypes.MultiCallParams>(
172+
calls: Calls
173+
): Promise<MapTestSubTypes.MultiCallResults<Calls>>;
174+
async multicall<Callss extends MapTestSubTypes.MultiCallParams[]>(
175+
callss: Narrow<Callss>
176+
): Promise<MapTestSubTypes.MulticallReturnType<Callss>>;
177+
async multicall<
178+
Callss extends
179+
| MapTestSubTypes.MultiCallParams
180+
| MapTestSubTypes.MultiCallParams[]
181+
>(callss: Callss): Promise<unknown> {
182+
return await multicallMethods(
183+
MapTestSub,
184+
this,
185+
callss,
186+
getContractByCodeHash
187+
);
188+
}
189+
}

artifacts/ts/contracts.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { Contract, ContractFactory } from "@alephium/web3";
66

77
let contracts: ContractFactory<any>[] | undefined = undefined;
88

9+
export function getAllContracts(): ContractFactory<any>[] {
10+
return contracts ?? [];
11+
}
12+
913
export function registerContract(factory: ContractFactory<any>) {
1014
if (contracts === undefined) {
1115
contracts = [factory];

artifacts/ts/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export * from "./FakeTokenTest";
1616
export * from "./Greeter";
1717
export * from "./InlineTest";
1818
export * from "./MapTest";
19+
export * from "./MapTestSub";
1920
export * from "./MapTestWrapper";
2021
export * from "./MetaData";
2122
export * from "./NFTCollectionTest";
@@ -30,4 +31,5 @@ export * from "./Transact";
3031
export * from "./UserAccount";
3132
export * from "./Warnings";
3233
export * from "./WrongNFTTest";
34+
export * from "./contracts";
3335
export * from "./scripts";

contracts/test/map.ral

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@ Contract MapTest() {
77
mapping[U256, U256] map1
88
mapping[ByteVec, U256] map2
99

10-
@using(preapprovedAssets = true)
10+
@using(preapprovedAssets = true, checkExternalCaller = false)
1111
pub fn insert(key: Address, value: MapValue) -> () {
1212
map0.insert!(key, key, value)
1313
map1.insert!(key, value.id, value.balance)
1414
map2.insert!(key, #0011, value.balance)
1515
}
1616

17+
@using(checkExternalCaller = false)
1718
pub fn update(key: Address) -> () {
1819
let value = map0[key]
1920
map0[key].balance = value.balance + 1
2021
map1[value.id] = value.balance + 1
2122
map2[#0011] = value.balance + 1
2223
}
2324

25+
@using(checkExternalCaller = false)
2426
pub fn remove(key: Address) -> () {
2527
let value = map0[key]
2628
map0.remove!(key, key)
@@ -61,3 +63,19 @@ TxScript RemoveFromMap(mapTest: MapTest, key: Address) {
6163
TxScript UpdateMapValue(mapTest: MapTest, key: Address) {
6264
mapTest.update(key)
6365
}
66+
67+
Contract MapTestSub(mapTestTemplateId: ByteVec) {
68+
@using(preapprovedAssets = true, checkExternalCaller = false)
69+
pub fn init(caller: Address, value: MapValue) -> ByteVec {
70+
let path = b`map:` ++ toByteVec!(caller)
71+
let (encodedImmFields, encodedMutFields) = MapTest.encodeFields!()
72+
let mapTestId = copyCreateSubContract!{caller -> ALPH: minimalContractDeposit!()}(
73+
path,
74+
mapTestTemplateId,
75+
encodedImmFields,
76+
encodedMutFields
77+
)
78+
MapTest(mapTestId).insert{caller -> ALPH: mapEntryDeposit!() * 3}(caller, value)
79+
return mapTestId
80+
}
81+
}

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alephium/cli",
3-
"version": "1.11.1",
3+
"version": "1.11.5",
44
"description": "Alephium command line tool",
55
"license": "GPL",
66
"repository": {

packages/cli/src/codegen.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,14 +635,18 @@ function genIndexTs(outDir: string, exports: string[]) {
635635
formatAndSaveToFile(indexPath, header + exportStatements)
636636
}
637637

638-
function genContractByCodeHash(outDir: string) {
638+
function genContractByCodeHash(outDir: string, exports: string[]) {
639639
const source = `
640640
${header}
641641
642642
import { Contract, ContractFactory } from '@alephium/web3'
643643
644644
let contracts: ContractFactory<any>[] | undefined = undefined
645645
646+
export function getAllContracts(): ContractFactory<any>[] {
647+
return contracts ?? []
648+
}
649+
646650
export function registerContract(factory: ContractFactory<any>) {
647651
if (contracts === undefined) {
648652
contracts = [factory]
@@ -659,6 +663,7 @@ function genContractByCodeHash(outDir: string) {
659663
}
660664
`
661665
const filename = 'contracts.ts'
666+
exports.push('./contracts')
662667
const sourcePath = path.join(outDir, filename)
663668
formatAndSaveToFile(sourcePath, source)
664669
}
@@ -914,7 +919,7 @@ export function codegen(project: Project) {
914919
genStructTypes(outDir)
915920
genGlobalConstants(project, outDir)
916921
genContracts(outDir, artifactDir, exports)
917-
genContractByCodeHash(outDir)
922+
genContractByCodeHash(outDir, exports)
918923
genScripts(outDir, artifactDir, exports)
919924
genIndexTs(outDir, exports)
920925
} catch (error) {

0 commit comments

Comments
 (0)