Skip to content

Commit

Permalink
Support testing of inline functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Nov 26, 2024
1 parent c1afd78 commit 8a0856c
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 28 deletions.
6 changes: 6 additions & 0 deletions .project.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"InlineTest": {
"sourceFile": "test/inline.ral",
"sourceCodeHash": "d03f916d84a47be0f238e9fb4c9a9c8a07966805f4f20c0081b1c2b8c651b8f8",
"bytecodeDebugPatch": "=3-1+2=2+4039=62+000000000109b413c32386f26fc10000a9a0000d2aa100a00002",
"codeHashDebug": "42eaecd6f4e1bc5a298fefad983fc698fd826d112ee83ab190e155eacb51d74e"
},
"InsertIntoMap": {
"sourceFile": "test/map.ral",
"sourceCodeHash": "331b92fe6617c1c7ac7f742ceea1943a06717477a2bd03ca266d23c65d844de6",
Expand Down
40 changes: 40 additions & 0 deletions artifacts/test/InlineTest.ral.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"version": "v3.9.0",
"name": "InlineTest",
"bytecode": "01011f01010000010ad3811965ccb413c32386f26fc10000a9a0000d2aa100a00002",
"codeHash": "5ec60ac0a6e53a40956d05de67ea90fd9ca958e6fea80e0f1c44c3fe0395871e",
"fieldsSig": {
"names": [
"count"
],
"types": [
"U256"
],
"isMutable": [
true
]
},
"eventsSig": [],
"functions": [
{
"name": "nextCount",
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
"returnTypes": [
"U256"
]
},
{
"name": "nextCount_",
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
"returnTypes": [
"U256"
]
}
],
"constants": [],
"enums": []
}
10 changes: 1 addition & 9 deletions artifacts/test/Warnings.ral.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@
"returnTypes": []
}
],
"constants": [
{
"name": "C",
"value": {
"type": "U256",
"value": "0"
}
}
],
"constants": [],
"enums": []
}
218 changes: 218 additions & 0 deletions artifacts/ts/InlineTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */

import {
Address,
Contract,
ContractState,
TestContractResult,
HexString,
ContractFactory,
EventSubscribeOptions,
EventSubscription,
CallContractParams,
CallContractResult,
TestContractParams,
ContractEvent,
subscribeContractEvent,
subscribeContractEvents,
testMethod,
callMethod,
multicallMethods,
fetchContractState,
Asset,
ContractInstance,
getContractEventsCurrentCount,
TestContractParamsWithoutMaps,
TestContractResultWithoutMaps,
SignExecuteContractMethodParams,
SignExecuteScriptTxResult,
signExecuteMethod,
addStdIdToFields,
encodeContractFields,
Narrow,
} from "@alephium/web3";
import { default as InlineTestContractJson } from "../test/InlineTest.ral.json";
import { getContractByCodeHash, registerContract } from "./contracts";
import {
AddStruct1,
AddStruct2,
Balances,
MapValue,
TokenBalance,
AllStructs,
} from "./types";

// Custom types for the contract
export namespace InlineTestTypes {
export type Fields = {
count: bigint;
};

export type State = ContractState<Fields>;

export interface CallMethodTable {
nextCount: {
params: Omit<CallContractParams<{}>, "args">;
result: CallContractResult<bigint>;
};
nextCount_: {
params: Omit<CallContractParams<{}>, "args">;
result: CallContractResult<bigint>;
};
}
export type CallMethodParams<T extends keyof CallMethodTable> =
CallMethodTable[T]["params"];
export type CallMethodResult<T extends keyof CallMethodTable> =
CallMethodTable[T]["result"];
export type MultiCallParams = Partial<{
[Name in keyof CallMethodTable]: CallMethodTable[Name]["params"];
}>;
export type MultiCallResults<T extends MultiCallParams> = {
[MaybeName in keyof T]: MaybeName extends keyof CallMethodTable
? CallMethodTable[MaybeName]["result"]
: undefined;
};
export type MulticallReturnType<Callss extends MultiCallParams[]> = {
[index in keyof Callss]: MultiCallResults<Callss[index]>;
};

export interface SignExecuteMethodTable {
nextCount: {
params: Omit<SignExecuteContractMethodParams<{}>, "args">;
result: SignExecuteScriptTxResult;
};
nextCount_: {
params: Omit<SignExecuteContractMethodParams<{}>, "args">;
result: SignExecuteScriptTxResult;
};
}
export type SignExecuteMethodParams<T extends keyof SignExecuteMethodTable> =
SignExecuteMethodTable[T]["params"];
export type SignExecuteMethodResult<T extends keyof SignExecuteMethodTable> =
SignExecuteMethodTable[T]["result"];
}

class Factory extends ContractFactory<
InlineTestInstance,
InlineTestTypes.Fields
> {
encodeFields(fields: InlineTestTypes.Fields) {
return encodeContractFields(
addStdIdToFields(this.contract, fields),
this.contract.fieldsSig,
AllStructs
);
}

at(address: string): InlineTestInstance {
return new InlineTestInstance(address);
}

tests = {
nextCount: async (
params: Omit<
TestContractParamsWithoutMaps<InlineTestTypes.Fields, never>,
"testArgs"
>
): Promise<TestContractResultWithoutMaps<bigint>> => {
return testMethod(this, "nextCount", params, getContractByCodeHash);
},
nextCount_: async (
params: Omit<
TestContractParamsWithoutMaps<InlineTestTypes.Fields, never>,
"testArgs"
>
): Promise<TestContractResultWithoutMaps<bigint>> => {
return testMethod(this, "nextCount_", params, getContractByCodeHash);
},
};

stateForTest(
initFields: InlineTestTypes.Fields,
asset?: Asset,
address?: string
) {
return this.stateForTest_(initFields, asset, address, undefined);
}
}

// Use this object to test and deploy the contract
export const InlineTest = new Factory(
Contract.fromJson(
InlineTestContractJson,
"=3-1+2=2+4039=62+000000000109b413c32386f26fc10000a9a0000d2aa100a00002",
"42eaecd6f4e1bc5a298fefad983fc698fd826d112ee83ab190e155eacb51d74e",
AllStructs
)
);
registerContract(InlineTest);

// Use this class to interact with the blockchain
export class InlineTestInstance extends ContractInstance {
constructor(address: Address) {
super(address);
}

async fetchState(): Promise<InlineTestTypes.State> {
return fetchContractState(InlineTest, this);
}

view = {
nextCount: async (
params?: InlineTestTypes.CallMethodParams<"nextCount">
): Promise<InlineTestTypes.CallMethodResult<"nextCount">> => {
return callMethod(
InlineTest,
this,
"nextCount",
params === undefined ? {} : params,
getContractByCodeHash
);
},
nextCount_: async (
params?: InlineTestTypes.CallMethodParams<"nextCount_">
): Promise<InlineTestTypes.CallMethodResult<"nextCount_">> => {
return callMethod(
InlineTest,
this,
"nextCount_",
params === undefined ? {} : params,
getContractByCodeHash
);
},
};

transact = {
nextCount: async (
params: InlineTestTypes.SignExecuteMethodParams<"nextCount">
): Promise<InlineTestTypes.SignExecuteMethodResult<"nextCount">> => {
return signExecuteMethod(InlineTest, this, "nextCount", params);
},
nextCount_: async (
params: InlineTestTypes.SignExecuteMethodParams<"nextCount_">
): Promise<InlineTestTypes.SignExecuteMethodResult<"nextCount_">> => {
return signExecuteMethod(InlineTest, this, "nextCount_", params);
},
};

async multicall<Calls extends InlineTestTypes.MultiCallParams>(
calls: Calls
): Promise<InlineTestTypes.MultiCallResults<Calls>>;
async multicall<Callss extends InlineTestTypes.MultiCallParams[]>(
callss: Narrow<Callss>
): Promise<InlineTestTypes.MulticallReturnType<Callss>>;
async multicall<
Callss extends
| InlineTestTypes.MultiCallParams
| InlineTestTypes.MultiCallParams[]
>(callss: Callss): Promise<unknown> {
return await multicallMethods(
InlineTest,
this,
callss,
getContractByCodeHash
);
}
}
2 changes: 0 additions & 2 deletions artifacts/ts/Warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ class Factory extends ContractFactory<WarningsInstance, WarningsTypes.Fields> {
);
}

consts = { C: BigInt("0") };

at(address: string): WarningsInstance {
return new WarningsInstance(address);
}
Expand Down
1 change: 1 addition & 0 deletions artifacts/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from "./DeprecatedNFTTest6";
export * from "./DeprecatedNFTTest7";
export * from "./FakeTokenTest";
export * from "./Greeter";
export * from "./InlineTest";
export * from "./MapTest";
export * from "./MapTestWrapper";
export * from "./MetaData";
Expand Down
12 changes: 12 additions & 0 deletions contracts/test/inline.ral
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Contract InlineTest(mut count: U256) {
@inline fn nextCount_() -> U256 {
transferTokenToSelf!(callerAddress!(), ALPH, 0.01 alph)
count = count + 1
return count
}

@using(updateFields = true, checkExternalCaller = false, preapprovedAssets = true, assetsInContract = true)
pub fn nextCount() -> U256 {
return nextCount_()
}
}
Loading

0 comments on commit 8a0856c

Please sign in to comment.