From feb66b4182137632e639a90a3f067287b96d7e96 Mon Sep 17 00:00:00 2001 From: Cheng Wang Date: Sat, 27 Jul 2024 20:00:14 +0200 Subject: [PATCH 1/3] Improve subcontract-id --- packages/web3/src/address/address.test.ts | 2 ++ packages/web3/src/address/address.ts | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/web3/src/address/address.test.ts b/packages/web3/src/address/address.test.ts index 0aada1701..8292adca4 100644 --- a/packages/web3/src/address/address.test.ts +++ b/packages/web3/src/address/address.test.ts @@ -166,6 +166,8 @@ describe('address', function () { const pathInHex = '4f51cd1f0af97cf5ec9c7a3397eaeea549d55a93c216e54f2ab4a8cf29f6f865' expect(() => subContractId(parentContractId, pathInHex, -1)).toThrow('Invalid group -1') expect(() => subContractId(parentContractId, pathInHex, 4)).toThrow('Invalid group 4') + expect(() => subContractId('&&', pathInHex, 0)).toThrow(`Invalid parent contract ID: &&, expected hex string`) + expect(() => subContractId(parentContractId, '&&', 0)).toThrow(`Invalid path: &&, expected hex string`) expect(subContractId(parentContractId, pathInHex, 0)).toBe( '0e28f15ca290002c31d691aa008aa56ac12356b0380efb6c88fff929b6a26800' ) diff --git a/packages/web3/src/address/address.ts b/packages/web3/src/address/address.ts index e0d6a9eda..0258a9183 100644 --- a/packages/web3/src/address/address.ts +++ b/packages/web3/src/address/address.ts @@ -22,7 +22,7 @@ import { TOTAL_NUMBER_OF_GROUPS } from '../constants' import blake from 'blakejs' import bs58 from '../utils/bs58' import djb2 from '../utils/djb2' -import { binToHex, concatBytes, hexToBinUnsafe } from '../utils' +import { binToHex, concatBytes, hexToBinUnsafe, isHexString } from '../utils' import { KeyType } from '../signer' import { MultiSig, lockupScriptCodec } from '../codec/lockup-script-codec' import { compactSignedIntCodec } from '../codec' @@ -211,6 +211,12 @@ export function subContractId(parentContractId: string, pathInHex: string, group if (group < 0 || group >= TOTAL_NUMBER_OF_GROUPS) { throw new Error(`Invalid group ${group}`) } + if (!isHexString(parentContractId)) { + throw new Error(`Invalid parent contract ID: ${parentContractId}, expected hex string`) + } + if (!isHexString(pathInHex)) { + throw new Error(`Invalid path: ${pathInHex}, expected hex string`) + } const data = concatBytes([hexToBinUnsafe(parentContractId), hexToBinUnsafe(pathInHex)]) const bytes = new Uint8Array([ ...blake.blake2b(blake.blake2b(data, undefined, 32), undefined, 32).slice(0, -1), From 429abb0f3e0b77151b018e592fb6b6263c57a592 Mon Sep 17 00:00:00 2001 From: Cheng Wang Date: Mon, 29 Jul 2024 14:38:49 +0200 Subject: [PATCH 2/3] Fix transact methods --- packages/web3/src/contract/contract.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/web3/src/contract/contract.ts b/packages/web3/src/contract/contract.ts index e94d9260b..4255e0512 100644 --- a/packages/web3/src/contract/contract.ts +++ b/packages/web3/src/contract/contract.ts @@ -1681,10 +1681,10 @@ export async function signExecuteMethod { const methodIndex = contract.contract.getMethodIndex(methodName) const functionSig = contract.contract.functions[methodIndex] - const usePreapprovedAssets = contract.contract.decodedMethods[methodIndex].usePreapprovedAssets + const methodUsePreapprovedAssets = contract.contract.decodedMethods[methodIndex].usePreapprovedAssets const bytecodeTemplate = getBytecodeTemplate( methodIndex, - usePreapprovedAssets, + methodUsePreapprovedAssets, functionSig, contract.contract.structs, params.attoAlphAmount, @@ -1716,7 +1716,7 @@ export async function signExecuteMethod Date: Mon, 29 Jul 2024 15:05:49 +0200 Subject: [PATCH 3/3] Update template --- .../templates/base/scripts/0_deploy_faucet.ts | 5 +++-- packages/cli/templates/base/src/token.ts | 22 +++++++++++-------- .../cli/templates/base/test/token.test.ts | 11 +++++----- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/cli/templates/base/scripts/0_deploy_faucet.ts b/packages/cli/templates/base/scripts/0_deploy_faucet.ts index 5b52f7c11..389ae2d2b 100644 --- a/packages/cli/templates/base/scripts/0_deploy_faucet.ts +++ b/packages/cli/templates/base/scripts/0_deploy_faucet.ts @@ -1,6 +1,7 @@ import { Deployer, DeployFunction, Network } from '@alephium/cli' import { Settings } from '../alephium.config' import { TokenFaucet } from '../artifacts/ts' +import { stringToHex } from '@alephium/web3' // This deploy function will be called by cli deployment tool automatically // Note that deployment scripts should prefixed with numbers (starting from 0) @@ -15,8 +16,8 @@ const deployFaucet: DeployFunction = async ( issueTokenAmount: issueTokenAmount, // The initial states of the faucet contract initialFields: { - symbol: Buffer.from('TF', 'utf8').toString('hex'), - name: Buffer.from('TokenFaucet', 'utf8').toString('hex'), + symbol: stringToHex('TF'), + name: stringToHex('TokenFaucet'), decimals: 18n, supply: issueTokenAmount, balance: issueTokenAmount diff --git a/packages/cli/templates/base/src/token.ts b/packages/cli/templates/base/src/token.ts index fa925a949..89643953e 100644 --- a/packages/cli/templates/base/src/token.ts +++ b/packages/cli/templates/base/src/token.ts @@ -2,7 +2,7 @@ import { Deployments } from '@alephium/cli' import { web3, DUST_AMOUNT } from '@alephium/web3' import { testNodeWallet } from '@alephium/web3-test' import configuration from '../alephium.config' -import { TokenFaucet, Withdraw } from '../artifacts/ts' +import { TokenFaucet } from '../artifacts/ts' async function withdraw() { web3.setCurrentNodeProvider('http://127.0.0.1:22973') @@ -20,21 +20,25 @@ async function withdraw() { const accountGroup = account.group // Load the metadata of the deployed contract in the right group - const deployed = deployments.getDeployedContractResult(accountGroup, 'TokenFaucet') - if (deployed === undefined) { + const faucet = deployments.getInstance(TokenFaucet, accountGroup) + if (faucet === undefined) { console.log(`The contract is not deployed on group ${account.group}`) continue } - const tokenId = deployed.contractInstance.contractId - const tokenAddress = deployed.contractInstance.address + const tokenId = faucet.contractId + const tokenAddress = faucet.address + console.log(`Token faucet contract id: ${tokenId}`) + console.log(`Token faucet contract address: ${tokenAddress}`) // Submit a transaction to use the transaction script - await Withdraw.execute(signer, { - initialFields: { token: tokenId, amount: 1n }, - attoAlphAmount: DUST_AMOUNT * 2n + await faucet.transact.withdraw({ + signer: signer, + attoAlphAmount: DUST_AMOUNT * 2n, + args: { + amount: 1n + }, }) - const faucet = TokenFaucet.at(tokenAddress) // Fetch the latest state of the token contract const state = await faucet.fetchState() console.log(JSON.stringify(state.fields, null, ' ')) diff --git a/packages/cli/templates/base/test/token.test.ts b/packages/cli/templates/base/test/token.test.ts index 33a424af7..b4eebe516 100644 --- a/packages/cli/templates/base/test/token.test.ts +++ b/packages/cli/templates/base/test/token.test.ts @@ -1,7 +1,7 @@ -import { web3, TestContractParams, addressFromContractId, AssetOutput, DUST_AMOUNT, groupOfAddress } from '@alephium/web3' +import { web3, TestContractParams, addressFromContractId, AssetOutput, DUST_AMOUNT, enableDebugMode } from '@alephium/web3' import { expectAssertionError, randomContractId, testAddress, testNodeWallet } from '@alephium/web3-test' import { deployToDevnet } from '@alephium/cli' -import { TokenFaucet, TokenFaucetTypes, Withdraw } from '../artifacts/ts' +import { TokenFaucet, TokenFaucetTypes } from '../artifacts/ts' describe('unit tests', () => { let testContractId: string @@ -127,9 +127,10 @@ describe('integration tests', () => { // Call `withdraw` function 10 times for (let i = 0; i < 10; i++) { - await Withdraw.execute(signer, { - initialFields: { token: faucet.contractId, amount: 1n }, - attoAlphAmount: DUST_AMOUNT * 2n + await faucet.transact.withdraw({ + signer: signer, + attoAlphAmount: DUST_AMOUNT * 3n, + args: { amount: 1n } }) const newState = await faucet.fetchState()