Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple script vm #407

Merged
merged 10 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/njsscan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ jobs:
args: '. --sarif --output results.sarif || true'

- name: Upload njsscan report
uses: github/codeql-action/upload-sarif@v1
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: results.sarif
16 changes: 14 additions & 2 deletions .project.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,18 @@
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"MultiDeposit": {
"sourceFile": "test/transact.ral",
"sourceCodeHash": "92acc48b107b7c552d1f94ae9a07b41215c0359bb10a3aae7d1a10662f654117",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"MultiWithdraw": {
"sourceFile": "test/transact.ral",
"sourceCodeHash": "92acc48b107b7c552d1f94ae9a07b41215c0359bb10a3aae7d1a10662f654117",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"NFTCollectionTest": {
"sourceFile": "nft/nft_collection_test.ral",
"sourceCodeHash": "ac1d8da86e8cb3e5c412c463d7cf0376db563d9cbc459eb034d3067dcaa2045e",
Expand Down Expand Up @@ -288,9 +300,9 @@
},
"Transact": {
"sourceFile": "test/transact.ral",
"sourceCodeHash": "be72f5d014486592f9ba43c41ded84a6c11dea338f2d0a86736ee6d1bd62f7fe",
"sourceCodeHash": "92acc48b107b7c552d1f94ae9a07b41215c0359bb10a3aae7d1a10662f654117",
"bytecodeDebugPatch": "",
"codeHashDebug": "e69364160d369d3459e19d1b2fcaae7f0cac7059357dba5616147ea18d0c1f0e"
"codeHashDebug": "85e3e9a803741af8e92bd43b1b07cde53f39b86cab0ef1a85bab12b10d691b55"
},
"UpdateMapValue": {
"sourceFile": "test/map.ral",
Expand Down
31 changes: 31 additions & 0 deletions artifacts/test/MultiDeposit.ral.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"version": "v3.5.0",
"name": "MultiDeposit",
"bytecodeTemplate": "0101030000004020b413c40de0b6b3a7640000a20c0c{0}0100b413c40de0b6b3a7640000a20c0c{1}0100b4{2}13c40de0b6b3a7640000a313c40de0b6b3a76400000d0c{0}0102b4{2}13c40de0b6b3a7640000a313c40de0b6b3a76400000d0c{1}0102",
"fieldsSig": {
"names": [
"c0",
"c1",
"tokenId"
],
"types": [
"Transact",
"Transact",
"ByteVec"
],
"isMutable": [
false,
false,
false
]
},
"functions": [
{
"name": "main",
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
"returnTypes": []
}
]
}
28 changes: 28 additions & 0 deletions artifacts/test/MultiWithdraw.ral.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"version": "v3.5.0",
"name": "MultiWithdraw",
"bytecodeTemplate": "010103000000120c0c{0}01010c0c{1}010113c40de0b6b3a76400000d0c{0}010313c40de0b6b3a76400000d0c{1}0103",
"fieldsSig": {
"names": [
"c0",
"c1"
],
"types": [
"Transact",
"Transact"
],
"isMutable": [
false,
false
]
},
"functions": [
{
"name": "main",
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
"returnTypes": []
}
]
}
49 changes: 45 additions & 4 deletions artifacts/test/Transact.ral.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
{
"version": "v3.5.0",
"name": "Transact",
"bytecode": "01034027404e405c010100000009d353dbb7aab413c40de0b6b3a7640000a9a00013c40de0b6b3a76400002aa10002010200000009d3a248861cb413c40de0b6b3a7640000a8a00013c40de0b6b3a76400002ba10002010000000103d376c7c673a00002",
"codeHash": "e69364160d369d3459e19d1b2fcaae7f0cac7059357dba5616147ea18d0c1f0e",
"bytecode": "03064027404e40674080408e409c010100010009d353dbb7aab413c40de0b6b3a7640000a9a00013c40de0b6b3a76400002aa10002010200010009d3a248861cb413c40de0b6b3a7640000a8a00013c40de0b6b3a76400002ba1000201010101000ad3bf2d01bdb4ce001600aca00116002aa1010201020101000ad35496306fb4ce001600aba00116002ba10102010000000103d3815a8d0da00002010000000103d3a51b051ca00102",
"codeHash": "85e3e9a803741af8e92bd43b1b07cde53f39b86cab0ef1a85bab12b10d691b55",
"fieldsSig": {
"names": [
"totalDeposits"
"tokenId",
"totalALPH",
"totalTokens"
],
"types": [
"ByteVec",
"U256",
"U256"
],
"isMutable": [
false,
true,
true
]
},
Expand All @@ -31,7 +37,42 @@
"returnTypes": []
},
{
"name": "getTotalDeposits",
"name": "depositToken",
"paramNames": [
"amount"
],
"paramTypes": [
"U256"
],
"paramIsMutable": [
false
],
"returnTypes": []
},
{
"name": "withdrawToken",
"paramNames": [
"amount"
],
"paramTypes": [
"U256"
],
"paramIsMutable": [
false
],
"returnTypes": []
},
{
"name": "getTotalALPH",
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
"returnTypes": [
"U256"
]
},
{
"name": "getTotalTokens",
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
Expand Down
131 changes: 112 additions & 19 deletions artifacts/ts/Transact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ import {
// Custom types for the contract
export namespace TransactTypes {
export type Fields = {
totalDeposits: bigint;
tokenId: HexString;
totalALPH: bigint;
totalTokens: bigint;
};

export type State = ContractState<Fields>;
Expand All @@ -59,7 +61,19 @@ export namespace TransactTypes {
params: Omit<CallContractParams<{}>, "args">;
result: CallContractResult<null>;
};
getTotalDeposits: {
depositToken: {
params: CallContractParams<{ amount: bigint }>;
result: CallContractResult<null>;
};
withdrawToken: {
params: CallContractParams<{ amount: bigint }>;
result: CallContractResult<null>;
};
getTotalALPH: {
params: Omit<CallContractParams<{}>, "args">;
result: CallContractResult<bigint>;
};
getTotalTokens: {
params: Omit<CallContractParams<{}>, "args">;
result: CallContractResult<bigint>;
};
Expand Down Expand Up @@ -90,7 +104,19 @@ export namespace TransactTypes {
params: Omit<SignExecuteContractMethodParams<{}>, "args">;
result: SignExecuteScriptTxResult;
};
getTotalDeposits: {
depositToken: {
params: SignExecuteContractMethodParams<{ amount: bigint }>;
result: SignExecuteScriptTxResult;
};
withdrawToken: {
params: SignExecuteContractMethodParams<{ amount: bigint }>;
result: SignExecuteScriptTxResult;
};
getTotalALPH: {
params: Omit<SignExecuteContractMethodParams<{}>, "args">;
result: SignExecuteScriptTxResult;
};
getTotalTokens: {
params: Omit<SignExecuteContractMethodParams<{}>, "args">;
result: SignExecuteScriptTxResult;
};
Expand Down Expand Up @@ -131,18 +157,37 @@ class Factory extends ContractFactory<TransactInstance, TransactTypes.Fields> {
): Promise<TestContractResultWithoutMaps<null>> => {
return testMethod(this, "withdraw", params, getContractByCodeHash);
},
getTotalDeposits: async (
depositToken: async (
params: TestContractParamsWithoutMaps<
TransactTypes.Fields,
{ amount: bigint }
>
): Promise<TestContractResultWithoutMaps<null>> => {
return testMethod(this, "depositToken", params, getContractByCodeHash);
},
withdrawToken: async (
params: TestContractParamsWithoutMaps<
TransactTypes.Fields,
{ amount: bigint }
>
): Promise<TestContractResultWithoutMaps<null>> => {
return testMethod(this, "withdrawToken", params, getContractByCodeHash);
},
getTotalALPH: async (
params: Omit<
TestContractParamsWithoutMaps<TransactTypes.Fields, never>,
"testArgs"
>
): Promise<TestContractResultWithoutMaps<bigint>> => {
return testMethod(
this,
"getTotalDeposits",
params,
getContractByCodeHash
);
return testMethod(this, "getTotalALPH", params, getContractByCodeHash);
},
getTotalTokens: async (
params: Omit<
TestContractParamsWithoutMaps<TransactTypes.Fields, never>,
"testArgs"
>
): Promise<TestContractResultWithoutMaps<bigint>> => {
return testMethod(this, "getTotalTokens", params, getContractByCodeHash);
},
};
}
Expand All @@ -152,7 +197,7 @@ export const Transact = new Factory(
Contract.fromJson(
TransactContractJson,
"",
"e69364160d369d3459e19d1b2fcaae7f0cac7059357dba5616147ea18d0c1f0e",
"85e3e9a803741af8e92bd43b1b07cde53f39b86cab0ef1a85bab12b10d691b55",
AllStructs
)
);
Expand Down Expand Up @@ -190,13 +235,46 @@ export class TransactInstance extends ContractInstance {
getContractByCodeHash
);
},
getTotalDeposits: async (
params?: TransactTypes.CallMethodParams<"getTotalDeposits">
): Promise<TransactTypes.CallMethodResult<"getTotalDeposits">> => {
depositToken: async (
params: TransactTypes.CallMethodParams<"depositToken">
): Promise<TransactTypes.CallMethodResult<"depositToken">> => {
return callMethod(
Transact,
this,
"depositToken",
params,
getContractByCodeHash
);
},
withdrawToken: async (
params: TransactTypes.CallMethodParams<"withdrawToken">
): Promise<TransactTypes.CallMethodResult<"withdrawToken">> => {
return callMethod(
Transact,
this,
"getTotalDeposits",
"withdrawToken",
params,
getContractByCodeHash
);
},
getTotalALPH: async (
params?: TransactTypes.CallMethodParams<"getTotalALPH">
): Promise<TransactTypes.CallMethodResult<"getTotalALPH">> => {
return callMethod(
Transact,
this,
"getTotalALPH",
params === undefined ? {} : params,
getContractByCodeHash
);
},
getTotalTokens: async (
params?: TransactTypes.CallMethodParams<"getTotalTokens">
): Promise<TransactTypes.CallMethodResult<"getTotalTokens">> => {
return callMethod(
Transact,
this,
"getTotalTokens",
params === undefined ? {} : params,
getContractByCodeHash
);
Expand All @@ -214,10 +292,25 @@ export class TransactInstance extends ContractInstance {
): Promise<TransactTypes.SignExecuteMethodResult<"withdraw">> => {
return signExecuteMethod(Transact, this, "withdraw", params);
},
getTotalDeposits: async (
params: TransactTypes.SignExecuteMethodParams<"getTotalDeposits">
): Promise<TransactTypes.SignExecuteMethodResult<"getTotalDeposits">> => {
return signExecuteMethod(Transact, this, "getTotalDeposits", params);
depositToken: async (
params: TransactTypes.SignExecuteMethodParams<"depositToken">
): Promise<TransactTypes.SignExecuteMethodResult<"depositToken">> => {
return signExecuteMethod(Transact, this, "depositToken", params);
},
withdrawToken: async (
params: TransactTypes.SignExecuteMethodParams<"withdrawToken">
): Promise<TransactTypes.SignExecuteMethodResult<"withdrawToken">> => {
return signExecuteMethod(Transact, this, "withdrawToken", params);
},
getTotalALPH: async (
params: TransactTypes.SignExecuteMethodParams<"getTotalALPH">
): Promise<TransactTypes.SignExecuteMethodResult<"getTotalALPH">> => {
return signExecuteMethod(Transact, this, "getTotalALPH", params);
},
getTotalTokens: async (
params: TransactTypes.SignExecuteMethodParams<"getTotalTokens">
): Promise<TransactTypes.SignExecuteMethodResult<"getTotalTokens">> => {
return signExecuteMethod(Transact, this, "getTotalTokens", params);
},
};

Expand Down
19 changes: 19 additions & 0 deletions artifacts/ts/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { default as DestroyAddScriptJson } from "../add/DestroyAdd.ral.json";
import { default as GreeterMainScriptJson } from "../greeter/GreeterMain.ral.json";
import { default as InsertIntoMapScriptJson } from "../test/InsertIntoMap.ral.json";
import { default as MintNFTTestScriptJson } from "../nft/MintNFTTest.ral.json";
import { default as MultiDepositScriptJson } from "../test/MultiDeposit.ral.json";
import { default as MultiWithdrawScriptJson } from "../test/MultiWithdraw.ral.json";
import { default as RemoveFromMapScriptJson } from "../test/RemoveFromMap.ral.json";
import { default as TemplateArrayVarScriptJson } from "../test/TemplateArrayVar.ral.json";
import { default as TestAssertScriptJson } from "../test/TestAssert.ral.json";
Expand Down Expand Up @@ -88,6 +90,23 @@ export const MintNFTTest = new ExecutableScript<{
getContractByCodeHash
);

export const MultiDeposit = new ExecutableScript<{
c0: HexString;
c1: HexString;
tokenId: HexString;
}>(
Script.fromJson(MultiDepositScriptJson, "", AllStructs),
getContractByCodeHash
);

export const MultiWithdraw = new ExecutableScript<{
c0: HexString;
c1: HexString;
}>(
Script.fromJson(MultiWithdrawScriptJson, "", AllStructs),
getContractByCodeHash
);

export const RemoveFromMap = new ExecutableScript<{
mapTest: HexString;
key: Address;
Expand Down
Loading
Loading