Skip to content

Commit

Permalink
Merge pull request #306 from alephium/support-template-array-variables
Browse files Browse the repository at this point in the history
Support template array variables in script
  • Loading branch information
Lbqds authored Feb 27, 2024
2 parents e944008 + 18892ef commit 3a9dcdc
Show file tree
Hide file tree
Showing 38 changed files with 208 additions and 46 deletions.
9 changes: 8 additions & 1 deletion .project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"fullNodeVersion": "v2.8.5",
"fullNodeVersion": "v2.9.0",
"compilerOptionsUsed": {
"ignoreUnusedConstantsWarnings": false,
"ignoreUnusedVariablesWarnings": false,
Expand Down Expand Up @@ -227,6 +227,13 @@
"codeHashDebug": "513645f5c95a28d55a51070f3d5c51edbda05a98f46b23cad59952e2ee4846a1",
"warnings": []
},
"TemplateArrayVar": {
"sourceFile": "test/template_array_var.ral",
"sourceCodeHash": "9d4585b01c450c8602f797f63995d1a734a260ea5579d68b81ba84272c9de235",
"bytecodeDebugPatch": "",
"codeHashDebug": "",
"warnings": []
},
"TokenTest": {
"sourceFile": "token/token_test.ral",
"sourceCodeHash": "c1e103565399f5caa255ce48c8b441001c928754cea5722467521446d36ca3f4",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/add/Add.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "Add",
"bytecode": "02040d4036405740600100020402041600160100010200000202021605160016015f06160016015fa00016002a16012aa100a000160016010e0dce0001000201030404000b160313c40de0b6b3a7640000a2160116021401001600130164c1180102010100021600b0",
"codeHash": "2b9e382c20b4facf21eb745a46a72447dae221c274518e19c60b5ddfe478cc9c",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/add/DestroyAdd.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "DestroyAdd",
"bytecodeTemplate": "01010300000005{1}0d0c{0}0103",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/add/Main.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "Main",
"bytecodeTemplate": "0101030001000a{0}17000e0d0e0e160001001818",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/greeter/Greeter.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "Greeter",
"bytecode": "19010c010000000105030c7bce0002",
"codeHash": "3813cf61a6e0f126463190119cd861a14ca9c2f92839e193c4f9934517b02477",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/greeter/GreeterMain.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "GreeterMain",
"bytecodeTemplate": "01010300020014{0}17000c0d160001000d2f0c7b{0}17010c0d160101000d2f0c7b",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/DeprecatedNFTTest1.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "DeprecatedNFTTest1",
"bytecode": "020109010000000102ce0102",
"codeHash": "3d89da71c0a6e905dd54267f897137ec6beb9603bb787e0e4a36bfc76f7a712b",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/DeprecatedNFTTest2.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "DeprecatedNFTTest2",
"bytecode": "02020912010000000102ce0102010000000102ce0002",
"codeHash": "c3e8a33252664e2f79903788d8abd79ee2c6785c580fa6911a0868436c59f59e",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/DeprecatedNFTTest3.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "DeprecatedNFTTest3",
"bytecode": "0202090f010000000102ce0102010000000000",
"codeHash": "75181639c8575ce108d1ecb0b1b73a373a8652ffe6f5f6621d9f9eee5a0b2eb4",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/DeprecatedNFTTest4.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "DeprecatedNFTTest4",
"bytecode": "02020911010000000102ce01020100000001020402",
"codeHash": "d8f8650c15cc96211608a52ae7d43a15d7b28306ac13acec672c0f1ed02a0538",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/DeprecatedNFTTest5.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "DeprecatedNFTTest5",
"bytecode": "02020914010000000102ce0102010000000304ce000c0c02",
"codeHash": "65c9d8a07f42939e84b4ae2fdc94dbbe3545c8ddb0832df08ef69b3cab50ebe3",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/DeprecatedNFTTest6.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "DeprecatedNFTTest6",
"bytecode": "02020912010000000102ce01020100000002030c0d02",
"codeHash": "88822622be55e862a1759c4e0c02300da75fe9e3dbe73c8fbe0fa8714996629e",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/DeprecatedNFTTest7.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "DeprecatedNFTTest7",
"bytecode": "02020913010000000102ce0102010000000203ce000b02",
"codeHash": "33ddc42a153c6b9940924d989dcd107d7ff234ecbe9c494ece35ed06bd24450d",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/MintNFTTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "MintNFTTest",
"bytecodeTemplate": "01010300000015{2}4c0ab413c40de0b6b3a7640000a2{1}0d0d{0}0107184a09b413c40de0b6b3a7640000a2{1}0d0d{0}010418",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/NFTCollectionTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "NFTCollectionTest",
"bytecode": "04050912402d40404085010000000102ce0102010000000102a0000201000102010f16000001310c7b160040cb17011601c50d7b16010201000203000816010002170216001602410e7b01030105011eb41701b1a00016001406414c5048000313046413006417031702160113c40de0b6b3a7640000a2a00040ce00160216030d1601c91704a0000d2aa100160402",
"codeHash": "c84f4fd5d3fdee90b3421174c85011437a10c6f440e0c261b1f69ff77bc5ab70",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/NFTCollectionWithRoyaltyTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "NFTCollectionWithRoyaltyTest",
"bytecode": "06080912402d40404050405b406c40b1010000000102ce0102010000000102a0000201000102010f16000001310c7b160040cb17011601c50d7b16010201000203000816010002170216001602410e7b0100020201061601ce032c1367102d0201010202000316001601a9010202020008b4ce02450d7b16001601a801030105011eb41701b1a00016001406414c5048000313046413006417031702160113c40de0b6b3a7640000a2a00040ce00160216030d1601c91704a0000d2aa100160402",
"codeHash": "1c162da87d31289c9b392bd48767386336bb1d208101a8680d92b7dc74098ce0",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/NFTTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "NFTTest",
"bytecode": "04020914010000000102ce0202010000000203ce00ce0102",
"codeHash": "4897086210869e612d82995b765a447c5319a55a56e8a0c3c07b4d9ca81e15b1",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/WithdrawNFTCollectionTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "WithdrawNFTCollectionTest",
"bytecodeTemplate": "01010300000006b4{1}0e0c{0}0106",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion artifacts/nft/WrongNFTTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "WrongNFTTest",
"bytecode": "04020917010000000102ce0202010000000206040c7bce00ce0102",
"codeHash": "7dd2ed643a98b2a1a52a9b9e536fcdae60d961b583b8109f777d846bfdfcae8d",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/sub/Sub.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "Sub",
"bytecode": "01011901000202010c05160016015fa00016002a16012ba100a00002",
"codeHash": "513645f5c95a28d55a51070f3d5c51edbda05a98f46b23cad59952e2ee4846a1",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/Assert.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "Assert",
"bytecode": "00010b0100000000050d0e2f0f7b",
"codeHash": "5bd05924fb9a23ea105df065a8c2dfa463b9ee53cc14a60320140d19dd6151ca",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/Debug.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "Debug",
"bytecode": "000106010000000000",
"codeHash": "928360bf58942dd1fdd8d197e0e2ef59ecddbf71a6e8fe25d2c0665274f80ce3",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/MetaData.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "MetaData",
"bytecode": "0003144027402e010300000005b4b413c40de0b6b3a7640000a702000200000004b213c40de0b6b3a7640000a90200000000000102",
"codeHash": "cade0de390b8e15960b263ac35aa013cb84f844bce6e3e53e6bfe2cc9166623f",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/test/OwnerOnly.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "OwnerOnly",
"bytecode": "01010c010000000005b4ce00450c7b",
"codeHash": "c21e66486f3fa9f78555b71d30ba1ffd2f5a4bf8624647b97ed3748db20e295a",
Expand Down
37 changes: 37 additions & 0 deletions artifacts/test/TemplateArrayVar.ral.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"version": "v2.9.0",
"name": "TemplateArrayVar",
"bytecodeTemplate": "010103000e00408d{1}{2}{3}{4}1703170217011700{6}{7}{8}170617051704{0}18{5}1816030f2f16020e2f1a16010d2f1a16000c2f1a0c7b160016011708170716070c2f16080d2f1a0c7b16021603170a170916090e2f160a0f2f1a0c7b16060e2f16050d2f1a16040c2f1a0c7b0c170b160b0e314c40260c170c160c0e314c1b160b7a0e314d0e2c160c7a0e314d2a78160b0e2c160c2a2f0c7b160c0d2a170c4a21160b0d2a170b4a7fd60c170d160d0f314c11160d7a0f314d102a78160d2f0c7b160d0d2a170d4a2b",
"fieldsSig": {
"names": [
"address",
"numbers0",
"bytes",
"numbers1"
],
"types": [
"Address",
"[[U256;2];2]",
"ByteVec",
"[U256;3]"
],
"isMutable": [
false,
false,
false,
false
]
},
"functions": [
{
"name": "main",
"usePreapprovedAssets": true,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
"returnTypes": []
}
]
}
2 changes: 1 addition & 1 deletion artifacts/test/Warnings.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "Warnings",
"bytecode": "02010701000202000102",
"codeHash": "9a0c90d67d729a478062d6794cf7b75c27483c50f6fe2ad13c5ed8873ad1fde2",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/token/FakeTokenTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "FakeTokenTest",
"bytecode": "010509121b4024402a010000000103044d18010000000103044d18010000000103044d18010000000103044d18010000000000",
"codeHash": "88d74dcc19bfd075e97c90ab5f48d374f9ff982133d8257d4efc32305c5885b3",
Expand Down
2 changes: 1 addition & 1 deletion artifacts/token/TokenTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.8.5",
"version": "v2.9.0",
"name": "TokenTest",
"bytecode": "050409121b4024010000000102ce0002010000000102ce0102010000000102ce0202010000000102ce0302",
"codeHash": "a2800413eb2c5c23d48068db23df5f8eeaba04653e12c8ed59d589720d96dadd",
Expand Down
7 changes: 7 additions & 0 deletions artifacts/ts/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { default as DestroyAddScriptJson } from "../add/DestroyAdd.ral.json";
import { default as GreeterMainScriptJson } from "../greeter/GreeterMain.ral.json";
import { default as MainScriptJson } from "../add/Main.ral.json";
import { default as MintNFTTestScriptJson } from "../nft/MintNFTTest.ral.json";
import { default as TemplateArrayVarScriptJson } from "../test/TemplateArrayVar.ral.json";
import { default as WithdrawNFTCollectionTestScriptJson } from "../nft/WithdrawNFTCollectionTest.ral.json";

export const DestroyAdd = new ExecutableScript<{
Expand All @@ -32,6 +33,12 @@ export const MintNFTTest = new ExecutableScript<{
uri: HexString;
royalty: boolean;
}>(Script.fromJson(MintNFTTestScriptJson));
export const TemplateArrayVar = new ExecutableScript<{
address: Address;
numbers0: [[bigint, bigint], [bigint, bigint]];
bytes: HexString;
numbers1: [bigint, bigint, bigint];
}>(Script.fromJson(TemplateArrayVarScriptJson));
export const WithdrawNFTCollectionTest = new ExecutableScript<{
collection: HexString;
amount: bigint;
Expand Down
22 changes: 22 additions & 0 deletions contracts/test/template_array_var.ral
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
TxScript TemplateArrayVar(address: Address, numbers0: [[U256; 2]; 2], bytes: ByteVec, numbers1: [U256; 3]) {
let _ = address
let _ = bytes

assert!(numbers0[1][1] == 3 && numbers0[1][0] == 2 && numbers0[0][1] == 1 && numbers0[0][0] == 0, 0)
let array0 = numbers0[0]
assert!(array0[0] == 0 && array0[1] == 1, 0)
let array1 = numbers0[1]
assert!(array1[0] == 2 && array1[1] == 3, 0)

assert!(numbers1[2] == 2 && numbers1[1] == 1 && numbers1[0] == 0, 0)

for (let mut i = 0; i < 2; i = i + 1) {
for (let mut j = 0; j < 2; j = j + 1) {
assert!(numbers0[i][j] == i * 2 + j, 0)
}
}

for (let mut k = 0; k < 3; k = k + 1) {
assert!(numbers1[k] == k, 0)
}
}
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ services:
condition: service_healthy

alephium:
image: alephium/alephium:v2.8.5
image: alephium/alephium:v2.9.0
restart: unless-stopped
ports:
- 19973:19973/tcp
Expand Down
4 changes: 3 additions & 1 deletion packages/web3-test/src/test-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ export async function transfer(from: PrivateKeyWallet, to: Address, tokenId: str

export async function expectAssertionError(p: Promise<unknown>, address: string, errorCode: number): Promise<void> {
expect(isBase58(address)).toEqual(true)
await expect(p).rejects.toThrowError(new RegExp(`AssertionFailedWithErrorCode\\\(${address},${errorCode}\\\)`, 'mg'))
await expect(p).rejects.toThrowError(
new RegExp(`VM execution error: Assertion Failed in Contract @ ${address}, Error Code: ${errorCode}`, 'mg')
)
}

export function randomContractId(): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/web3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"author": "Alephium dev <dev@alephium.org>",
"config": {
"alephium_version": "2.8.5",
"alephium_version": "2.9.0",
"explorer_backend_version": "1.16.1"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/web3/src/api/api-alephium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ export class HttpClient<SecurityDataType = unknown> {

/**
* @title Alephium API
* @version 2.8.5
* @version 2.9.0
* @baseUrl ../
*/
export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
Expand Down
2 changes: 1 addition & 1 deletion packages/web3/src/api/node-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class NodeProvider implements NodeProviderApis {
return { tokenUri, collectionId, nftIndex }
} else {
const failedCollectionIndexResult = result.results[1] as CallContractFailed
if (failedCollectionIndexResult.error.startsWith('VM execution error: InvalidMethodIndex')) {
if (failedCollectionIndexResult.error.startsWith('VM execution error: Invalid method index')) {
throw new Error('Deprecated NFT contract')
} else {
throw new Error(`Failed to call contract, error: ${failedCollectionIndexResult.error}`)
Expand Down
42 changes: 42 additions & 0 deletions packages/web3/src/contract/ralph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,48 @@ describe('contract', function () {
expect(bytecode).toEqual('144020b382fc88aa31d63f4c2f3f8a03715ba2a629552e85431fb1c1d909bab46d1aae')
})

it('should flatten script fields', () => {
const fields: Fields = {
numbers0: [
[0n, 1n],
[2n, 3n]
],
numbers1: [0n, 1n, 2n],
address: '1C2RAVWSuaXw8xtUxqVERR7ChKBE1XgscNFw73NSHE1v3',
bytes: '0011'
}
const fieldsSig: FieldsSig = {
names: ['address', 'numbers0', 'bytes', 'numbers1'],
types: ['Address', '[[U256;2];2]', 'ByteVec', '[U256;3]'],
isMutable: [false, false, false, false]
}
const result = ralph.falttenFields(fields, fieldsSig)
expect(result).toEqual([
{
name: 'address',
type: 'Address',
value: '1C2RAVWSuaXw8xtUxqVERR7ChKBE1XgscNFw73NSHE1v3'
},
{ name: 'numbers0[0][0]', type: 'U256', value: 0n },
{ name: 'numbers0[0][1]', type: 'U256', value: 1n },
{ name: 'numbers0[1][0]', type: 'U256', value: 2n },
{ name: 'numbers0[1][1]', type: 'U256', value: 3n },
{ name: 'bytes', type: 'ByteVec', value: '0011' },
{ name: 'numbers1[0]', type: 'U256', value: 0n },
{ name: 'numbers1[1]', type: 'U256', value: 1n },
{ name: 'numbers1[2]', type: 'U256', value: 2n }
])

const invalidFields0 = { ...fields, numbers0: [0n, 1n] }
expect(() => ralph.falttenFields(invalidFields0, fieldsSig)).toThrow(
'Invalid field, expected type is [U256;2], but value is 0'
)
const invalidFields1 = { ...fields, numbers1: [[0n], [1n], [2n]] }
expect(() => ralph.falttenFields(invalidFields1, fieldsSig)).toThrow(
'Invalid field, expected type is U256, but value is [0]'
)
})

it('should test buildScriptByteCode', () => {
const variables = { x: true, y: 0x05n, z: 'ff', a: '1C2RAVWSuaXw8xtUxqVERR7ChKBE1XgscNFw73NSHE1v3' }
const fieldsSig: FieldsSig = {
Expand Down
Loading

0 comments on commit 3a9dcdc

Please sign in to comment.