From 4aea02b81970bd604c2ef2696949a402d47dad98 Mon Sep 17 00:00:00 2001 From: Hjort Date: Fri, 22 Mar 2024 11:10:22 +0100 Subject: [PATCH 1/6] Remove hardcoded energy from wCCD --- examples/wCCD/src/utils.ts | 103 ++++++++++++++++++++++++++----------- examples/wCCD/src/wCCD.tsx | 3 +- 2 files changed, 75 insertions(+), 31 deletions(-) diff --git a/examples/wCCD/src/utils.ts b/examples/wCCD/src/utils.ts index 24e442c4..e1545558 100644 --- a/examples/wCCD/src/utils.ts +++ b/examples/wCCD/src/utils.ts @@ -1,13 +1,82 @@ import { createContext } from 'react'; -import { AccountTransactionType, CcdAmount, ContractAddress, Energy, ReceiveName } from '@concordium/web-sdk'; +import { + AccountAddress, + AccountTransactionType, + CcdAmount, + ConcordiumGRPCClient, + ContractAddress, + ContractContext, + ContractName, + EntrypointName, + ReceiveName, + serializeUpdateContractParameters, +} from '@concordium/web-sdk'; import { WalletConnection, moduleSchemaFromBase64 } from '@concordium/react-components'; import { CONTRACT_NAME, WRAP_FUNCTION_RAW_SCHEMA, UNWRAP_FUNCTION_RAW_SCHEMA } from './constants'; +async function getExecutionEnergy(client: ConcordiumGRPCClient, invokeInput: ContractContext) { + const invokeResult = await client.invokeContract(invokeInput); + if (invokeResult.tag === 'failure') { + throw Error('Transaction would fail!'); + } + return invokeResult.usedEnergy; +} + +/** + * Shared method for wrap and unwrap + */ +async function send( + connection: WalletConnection, + grpcClient: ConcordiumGRPCClient, + methodName: string, + base64Schema: string, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + parameter: any, + account: string, + index: bigint, + subindex = 0n, + amount = 0 +) { + const address = ContractAddress.create(index, subindex); + const receiveName = ReceiveName.fromString(`${CONTRACT_NAME}.${methodName}`); + const ccdAmount = CcdAmount.fromMicroCcd(BigInt(amount)); + const schema = moduleSchemaFromBase64(base64Schema); + const serializedParameters = serializeUpdateContractParameters( + ContractName.fromString(CONTRACT_NAME), + EntrypointName.fromString(methodName), + parameter, + schema.value + ); + const maxContractExecutionEnergy = await getExecutionEnergy(grpcClient, { + contract: address, + method: receiveName, + invoker: AccountAddress.fromBase58(account), + amount: ccdAmount, + parameter: serializedParameters, + }); + + return connection.signAndSendTransaction( + account, + AccountTransactionType.Update, + { + amount: ccdAmount, + address, + receiveName, + maxContractExecutionEnergy, + }, + { + parameters: parameter, + schema, + } + ); +} + /** * Action for wrapping some CCD to WCCD in the WCCD smart contract instance */ export async function wrap( connection: WalletConnection, + grpcClient: ConcordiumGRPCClient, account: string, index: bigint, subindex = 0n, @@ -24,21 +93,7 @@ export async function wrap( Account: [receiver], }, }; - - return connection.signAndSendTransaction( - account, - AccountTransactionType.Update, - { - amount: CcdAmount.fromMicroCcd(BigInt(amount)), - address: ContractAddress.create(index, subindex), - receiveName: ReceiveName.fromString(`${CONTRACT_NAME}.wrap`), - maxContractExecutionEnergy: Energy.create(30000), - }, - { - parameters: parameter, - schema: moduleSchemaFromBase64(WRAP_FUNCTION_RAW_SCHEMA), - } - ); + return send(connection, grpcClient, 'wrap', WRAP_FUNCTION_RAW_SCHEMA, parameter, account, index, subindex, amount); } /** @@ -46,6 +101,7 @@ export async function wrap( */ export async function unwrap( connection: WalletConnection, + grpcClient: ConcordiumGRPCClient, account: string, index: bigint, subindex = 0n, @@ -67,20 +123,7 @@ export async function unwrap( }, }; - return connection.signAndSendTransaction( - account, - AccountTransactionType.Update, - { - amount: CcdAmount.fromMicroCcd(BigInt(0)), - address: ContractAddress.create(index, subindex), - receiveName: ReceiveName.fromString(`${CONTRACT_NAME}.unwrap`), - maxContractExecutionEnergy: Energy.create(30000), - }, - { - parameters: parameter, - schema: moduleSchemaFromBase64(UNWRAP_FUNCTION_RAW_SCHEMA), - } - ); + return send(connection, grpcClient, 'unwrap', UNWRAP_FUNCTION_RAW_SCHEMA, parameter, account, index, subindex, 0); } /** diff --git a/examples/wCCD/src/wCCD.tsx b/examples/wCCD/src/wCCD.tsx index 581342ea..33878265 100644 --- a/examples/wCCD/src/wCCD.tsx +++ b/examples/wCCD/src/wCCD.tsx @@ -409,12 +409,13 @@ export default function wCCD(props: ConnectionProps) { // Amount needs to be in WEI const amount = round(multiply(input, 1000000)); - if (connection && account) { + if (connection && account && grpcClient) { setHash(''); setTransactionError(''); setWaitingForUser(true); const tx = (isWrapping ? wrap : unwrap)( connection, + grpcClient, account, wCCDContractIndex, CONTRACT_SUB_INDEX, From ca6bad1b6f8a55d930aea70991cc74f59b692730 Mon Sep 17 00:00:00 2001 From: Hjort Date: Fri, 22 Mar 2024 13:28:18 +0100 Subject: [PATCH 2/6] Remove hardcoded energy for update in nft-minting --- examples/nft-minting/src/utils.ts | 41 +++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/examples/nft-minting/src/utils.ts b/examples/nft-minting/src/utils.ts index 01e99485..3d079409 100644 --- a/examples/nft-minting/src/utils.ts +++ b/examples/nft-minting/src/utils.ts @@ -16,11 +16,13 @@ import { TransactionHash, TransactionSummaryType, TransactionKindString, + AccountAddress, + toBuffer, + EntrypointName, + serializeUpdateContractParameters, } from '@concordium/web-sdk'; import { RAW_SCHEMA } from './constant'; -export const CONTRACT_NAME = 'PiggyBank'; - /** * Action for initializing a smart contract for keeping a collections of tokens. */ @@ -51,20 +53,39 @@ export const createCollection = async (address: string): Promise => { */ export const mint = async (account: string, id: string, url: string, index: bigint, subindex = 0n): Promise => { const provider = await detectConcordiumProvider(); + const address = ContractAddress.create(index, subindex); + const receiveName = ReceiveName.fromString(`CIS2-NFT.mint`); + const parameter = { + owner: { Account: [account] }, + token_id: id, + metadata: { url, hash: { None: [] } }, + }; + const serializedParameters = serializeUpdateContractParameters( + ContractName.fromString('CIS2-NFT'), + EntrypointName.fromString('mint'), + parameter, + toBuffer(RAW_SCHEMA, 'base64') + ); + const invokeResult = await new ConcordiumGRPCClient(provider.grpcTransport).invokeContract({ + contract: address, + method: receiveName, + invoker: AccountAddress.fromBase58(account), + parameter: serializedParameters, + }); + if (invokeResult.tag === 'failure') { + throw Error('Transaction would fail!'); + } + const maxContractExecutionEnergy = invokeResult.usedEnergy; const txHash = await provider.sendTransaction( account, AccountTransactionType.Update, { amount: CcdAmount.fromMicroCcd(0n), - address: ContractAddress.create(index, subindex), - receiveName: ReceiveName.fromString(`CIS2-NFT.mint`), - maxContractExecutionEnergy: Energy.create(30000), + address, + receiveName, + maxContractExecutionEnergy, } as UpdateContractPayload, - { - owner: { Account: [account] }, - token_id: id, - metadata: { url, hash: { None: [] } }, - }, + parameter, RAW_SCHEMA ); From 272a674e2dad3c38434a971a77d7b46bdf358f38 Mon Sep 17 00:00:00 2001 From: Hjort Date: Fri, 22 Mar 2024 14:16:15 +0100 Subject: [PATCH 3/6] Remove hardcoded energy from eSealing --- examples/eSealing/src/eSealing.tsx | 3 ++- examples/eSealing/src/utils.ts | 43 ++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/examples/eSealing/src/eSealing.tsx b/examples/eSealing/src/eSealing.tsx index 55d1878d..5e8116c2 100644 --- a/examples/eSealing/src/eSealing.tsx +++ b/examples/eSealing/src/eSealing.tsx @@ -345,7 +345,7 @@ export default function SEALING(props: WalletConnectionProps) { Waiting for connection... )} - {connection && isRegisterFilePage && account && ( + {connection && isRegisterFilePage && account && grpcClient && (