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

(Partially) Remove 30000 #450

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions examples/eSealing/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.1.5

- Removed hardcoded energy for update, now invokes contract to check the energy usage.

## 1.1.4

- Bumped version of @concordium/web-sdk and @concordium/react-components (fixes wallet connect serialization issue)
Expand Down
2 changes: 1 addition & 1 deletion examples/eSealing/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "e_sealing",
"packageManager": "[email protected]",
"version": "1.1.4",
"version": "1.1.5",
"license": "Apache-2.0",
"dependencies": {
"@concordium/react-components": "^0.4.0",
Expand Down
3 changes: 2 additions & 1 deletion examples/eSealing/src/eSealing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ export default function SEALING(props: WalletConnectionProps) {
Waiting for connection...
</button>
)}
{connection && isRegisterFilePage && account && (
{connection && isRegisterFilePage && account && grpcClient && (
<button
style={fileHashHex === '' ? ButtonStyleDisabled : ButtonStyle}
type="button"
Expand All @@ -362,6 +362,7 @@ export default function SEALING(props: WalletConnectionProps) {
setWaitingForUser(true);
const tx = (isRegisterFilePage ? register : register)(
connection,
grpcClient,
account,
fileHashHex,
E_SEALING_CONTRACT_INDEX,
Expand Down
43 changes: 38 additions & 5 deletions examples/eSealing/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { createContext } from 'react';
import { AccountTransactionType, CcdAmount, ContractAddress, Energy, ReceiveName } from '@concordium/web-sdk';
import {
AccountAddress,
AccountTransactionType,
CcdAmount,
ConcordiumGRPCClient,
ContractAddress,
ContractName,
EntrypointName,
ReceiveName,
serializeUpdateContractParameters,
} from '@concordium/web-sdk';
import { WalletConnection, moduleSchemaFromBase64 } from '@concordium/react-components';
import { E_SEALING_CONTRACT_NAME, E_SEALING_RAW_SCHEMA } from './constants';

Expand All @@ -8,23 +18,46 @@ import { E_SEALING_CONTRACT_NAME, E_SEALING_RAW_SCHEMA } from './constants';
*/
export async function register(
connection: WalletConnection,
grpcClient: ConcordiumGRPCClient,
account: string,
fileHashHex: string,
index: bigint,
subindex = 0n
) {
const contractAddress = ContractAddress.create(index, subindex);
const receiveName = ReceiveName.fromString(`${E_SEALING_CONTRACT_NAME}.registerFile`);

const schema = moduleSchemaFromBase64(E_SEALING_RAW_SCHEMA);
const serializedParameters = serializeUpdateContractParameters(
ContractName.fromString(E_SEALING_CONTRACT_NAME),
EntrypointName.fromString('registerFile'),
fileHashHex,
schema.value
);

const invokeResult = await grpcClient.invokeContract({
contract: contractAddress,
method: receiveName,
invoker: AccountAddress.fromBase58(account),
parameter: serializedParameters,
});

if (invokeResult.tag === 'failure') {
throw Error('Transaction would fail!');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be helpful if that error message reports the reason for the failure. The following, I suggest to cover:

    const fullEntryPointName = `${contractName.value}.${entryPoint.value}`;

    if (!res || res.tag === 'failure') {
        const rejectReason = JSON.stringify(
            ((res as InvokeContractFailedResult)?.reason as RejectedReceive)?.rejectReason
        );

        throw new Error(
            `RPC call 'invokeContract' on method '${fullEntryPointName}' of contract '${contractIndex}' failed.
            ${rejectReason !== undefined ? `Reject reason: ${rejectReason}` : ''}`
        );
    }

Reason for adding this reporting of errors now:
Before, people who reported problems while using this front end were able to give us a "failedTransactionHash" that we could use for further investigation. We don't have this anymore, so reporting potential problems here is what we have to rely on now.

This comment applies to the other examples as well.

}

return connection.signAndSendTransaction(
account,
AccountTransactionType.Update,
{
amount: CcdAmount.fromMicroCcd(0),
address: ContractAddress.create(index, subindex),
receiveName: ReceiveName.fromString(`${E_SEALING_CONTRACT_NAME}.registerFile`),
maxContractExecutionEnergy: Energy.create(30000n),
address: contractAddress,
receiveName,
maxContractExecutionEnergy: invokeResult.usedEnergy,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a small margin EPSILON_ENERGY will help in cases where someone else uses the front end at the same time and the state of the smart contract has changed between the simulation and the execution of the transaction.

    const maxContractExecutionEnergy = Energy.create(dryRunResult.usedEnergy.value + EPSILON_ENERGY);

https://github.com/Concordium/concordium-dapp-examples/blob/main/trackAndTrace/frontend/src/track_and_trace_contract.tsx#L63

Applies to the other examples below as well.

},
{
parameters: fileHashHex,
schema: moduleSchemaFromBase64(E_SEALING_RAW_SCHEMA),
schema,
}
);
}
Expand Down
41 changes: 31 additions & 10 deletions examples/nft-minting/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -51,20 +53,39 @@ export const createCollection = async (address: string): Promise<bigint> => {
*/
export const mint = async (account: string, id: string, url: string, index: bigint, subindex = 0n): Promise<string> => {
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
);

Expand Down
4 changes: 4 additions & 0 deletions examples/wCCD/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 4.1.3

- Removed hardcoded energy for updates, now invokes contract to check the energy usage.

## 4.1.2

- Bumped version of @concordium/web-sdk and @concordium/react-components (fixes wallet connect serialization issue)
Expand Down
2 changes: 1 addition & 1 deletion examples/wCCD/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wccd",
"packageManager": "[email protected]",
"version": "4.1.2",
"version": "4.1.3",
"license": "Apache-2.0",
"dependencies": {
"@concordium/react-components": "^0.4.0",
Expand Down
103 changes: 73 additions & 30 deletions examples/wCCD/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -24,28 +93,15 @@ 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);
}

/**
* Action for unwrapping some WCCD to CCD in the WCCD smart contract instance
*/
export async function unwrap(
connection: WalletConnection,
grpcClient: ConcordiumGRPCClient,
account: string,
index: bigint,
subindex = 0n,
Expand All @@ -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);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion examples/wCCD/src/wCCD.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading