Skip to content

Commit

Permalink
Merge pull request #382 from alephium/fix-generating-constants
Browse files Browse the repository at this point in the history
Fix generating constants
  • Loading branch information
polarker authored Jun 24, 2024
2 parents cfe659e + 2a74985 commit f92f489
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 26 deletions.
6 changes: 3 additions & 3 deletions .project.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
},
"Assert": {
"sourceFile": "test/assert.ral",
"sourceCodeHash": "65f8387941a38e88ef555f5d0b4992dab390d97ff97b2473fdea45b8d58a74a5",
"sourceCodeHash": "9f400f63bcdefcbccb09253140532bea0054f543fa7264b555abcadc0da2d87d",
"bytecodeDebugPatch": "",
"codeHashDebug": "46dc5e3835be6551dacbf81565912ec67575aa77522312ceed88472817735d6b",
"warnings": [
"Found unused constants in Assert: A, Addresses.A, Addresses.B, B, ByteVecs.A, ByteVecs.B, C, Numbers.A, Numbers.B"
"Found unused constants in Assert: A, Addresses.A, Addresses.B, B, ByteVecs.A, ByteVecs.B, C, Numbers.A, Numbers.B, Numbers.C"
]
},
"Balances": {
Expand Down Expand Up @@ -299,7 +299,7 @@
},
"TestAssert": {
"sourceFile": "test/assert.ral",
"sourceCodeHash": "65f8387941a38e88ef555f5d0b4992dab390d97ff97b2473fdea45b8d58a74a5",
"sourceCodeHash": "9f400f63bcdefcbccb09253140532bea0054f543fa7264b555abcadc0da2d87d",
"bytecodeDebugPatch": "",
"codeHashDebug": "",
"warnings": []
Expand Down
12 changes: 8 additions & 4 deletions artifacts/test/Assert.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.7",
"version": "v3.1.4",
"name": "Assert",
"bytecode": "000110010000000006d362d460120d0e2f0f7b",
"codeHash": "46dc5e3835be6551dacbf81565912ec67575aa77522312ceed88472817735d6b",
Expand All @@ -12,9 +12,6 @@
"functions": [
{
"name": "test",
"usePreapprovedAssets": false,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
Expand Down Expand Up @@ -87,6 +84,13 @@
"type": "U256",
"value": "1"
}
},
{
"name": "C",
"value": {
"type": "U256",
"value": "115792089237316195423570985008687907853269984665640564039457584007913129639935"
}
}
]
},
Expand Down
5 changes: 1 addition & 4 deletions artifacts/test/TestAssert.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.7",
"version": "v3.1.4",
"name": "TestAssert",
"bytecodeTemplate": "010103000000040c0c{0}0100",
"fieldsSig": {
Expand All @@ -16,9 +16,6 @@
"functions": [
{
"name": "main",
"usePreapprovedAssets": true,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
Expand Down
12 changes: 9 additions & 3 deletions artifacts/ts/Assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,21 @@ class Factory extends ContractFactory<AssertInstance, {}> {
}

consts = {
Error: BigInt(3),
A: BigInt(-3),
Error: BigInt("3"),
A: BigInt("-3"),
B: "1DrDyTr9RpRsQnDnXo2YRiPzPW4ooHX5LLoqXrqfMrpQH",
C: "0011",
Addresses: {
A: "1DrDyTr9RpRsQnDnXo2YRiPzPW4ooHX5LLoqXrqfMrpQH",
B: "14UAjZ3qcmEVKdTo84Kwf4RprTQi86w2TefnnGFjov9xF",
},
Numbers: { A: BigInt(0), B: BigInt(1) },
Numbers: {
A: BigInt("0"),
B: BigInt("1"),
C: BigInt(
"115792089237316195423570985008687907853269984665640564039457584007913129639935"
),
},
ByteVecs: { A: "00", B: "11" },
};

Expand Down
6 changes: 3 additions & 3 deletions artifacts/ts/NFTCollectionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ class Factory extends ContractFactory<

consts = {
ErrorCodes: {
IncorrectTokenIndex: BigInt(0),
NFTNotFound: BigInt(1),
NFTNotPartOfCollection: BigInt(2),
IncorrectTokenIndex: BigInt("0"),
NFTNotFound: BigInt("1"),
NFTNotPartOfCollection: BigInt("2"),
},
};

Expand Down
8 changes: 4 additions & 4 deletions artifacts/ts/NFTCollectionWithRoyaltyTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ class Factory extends ContractFactory<

consts = {
ErrorCodes: {
IncorrectTokenIndex: BigInt(0),
NFTNotFound: BigInt(1),
NFTNotPartOfCollection: BigInt(2),
CollectionOwnerAllowedOnly: BigInt(1),
IncorrectTokenIndex: BigInt("0"),
NFTNotFound: BigInt("1"),
NFTNotPartOfCollection: BigInt("2"),
CollectionOwnerAllowedOnly: BigInt("1"),
},
};

Expand Down
2 changes: 1 addition & 1 deletion artifacts/ts/Warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Factory extends ContractFactory<WarningsInstance, WarningsTypes.Fields> {
return this.contract.getInitialFieldsWithDefaultValues() as WarningsTypes.Fields;
}

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

at(address: string): WarningsInstance {
return new WarningsInstance(address);
Expand Down
1 change: 1 addition & 0 deletions contracts/test/assert.ral
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Contract Assert() {
enum Numbers {
A = 0
B = 1
C = 115792089237316195423570985008687907853269984665640564039457584007913129639935
}

enum Addresses {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function nodeValToString(value: node.Val): string {
function valToString(v: Val): string {
if (typeof v === 'bigint') {
// use BigInt(...) format to avoid that some projects do not support es2020
return `BigInt(${v.toString()})`
return `BigInt('${v.toString()}')`
} else if (typeof v === 'string') {
return `"${v}"`
} else if (Array.isArray(v)) {
Expand Down
6 changes: 5 additions & 1 deletion packages/web3-test/src/test-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ export async function transfer(from: PrivateKeyWallet, to: Address, tokenId: str
return await from.signAndSubmitTransferTx({ signerAddress: from.address, destinations: [destination] })
}

export async function expectAssertionError(p: Promise<unknown>, address: string, errorCode: number): Promise<void> {
export async function expectAssertionError(
p: Promise<unknown>,
address: string,
errorCode: number | bigint
): Promise<void> {
expect(isBase58(address)).toEqual(true)
await expect(p).rejects.toThrowError(
new RegExp(`Assertion Failed in Contract @ ${address}, Error Code: ${errorCode}`, 'mg')
Expand Down
10 changes: 8 additions & 2 deletions test/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,19 @@ describe('contract', function () {
})

it('should test assert!', async () => {
expect(Assert.consts.Numbers.C).toEqual((1n << 256n) - 1n)

const contractAddress = randomContractAddress()
expectAssertionError(Assert.tests.test({ address: contractAddress }), contractAddress, 3)
expectAssertionError(Assert.tests.test({ address: contractAddress }), contractAddress, Assert.consts.Error)

const assertDeployResult = await Assert.deploy(signer, { initialFields: {} })
const assertAddress = assertDeployResult.contractInstance.address

expectAssertionError(TestAssert.execute(signer, { initialFields: { assert: assertAddress } }), assertAddress, 3)
expectAssertionError(
TestAssert.execute(signer, { initialFields: { assert: assertAddress } }),
assertAddress,
Assert.consts.Error
)

expectAssertionError(
TestAssert.execute(signer, {
Expand Down

0 comments on commit f92f489

Please sign in to comment.