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 all 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": "yarn@3.2.0",
"version": "1.1.4",
"version": "1.1.5",
"license": "Apache-2.0",
"dependencies": {
"@concordium/react-components": "^0.4.0",
Expand Down
7 changes: 7 additions & 0 deletions examples/eSealing/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ export const BROWSER_WALLET = ephemeralConnectorType(BrowserWalletConnector.crea
export const WALLET_CONNECT = ephemeralConnectorType(
WalletConnectConnector.create.bind(undefined, WALLET_CONNECT_OPTS)
);

// Before submitting a transaction we simulate/dry-run the transaction to get an
// estimate of the energy needed for executing the transaction. In addition, we
// allow an additional small amount of energy `EPSILON_ENERGY` to be consumed by
// the transaction to cover small variations (e.g. changes to the smart contract
// state) caused by transactions that have been executed meanwhile.
export const EPSILON_ENERGY = 200n;
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
55 changes: 49 additions & 6 deletions examples/eSealing/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,73 @@
import { createContext } from 'react';
import { AccountTransactionType, CcdAmount, ContractAddress, Energy, ReceiveName } from '@concordium/web-sdk';
import {
AccountAddress,
AccountTransactionType,
CcdAmount,
ConcordiumGRPCClient,
ContractAddress,
ContractName,
EntrypointName,
ReceiveName,
Energy,
serializeUpdateContractParameters,
InvokeContractFailedResult,
RejectedReceive,
} from '@concordium/web-sdk';
import { WalletConnection, moduleSchemaFromBase64 } from '@concordium/react-components';
import { E_SEALING_CONTRACT_NAME, E_SEALING_RAW_SCHEMA } from './constants';
import { EPSILON_ENERGY, E_SEALING_CONTRACT_NAME, E_SEALING_RAW_SCHEMA } from './constants';

/**
* Action for registering a new file has in the eSealing smart contract instance
*/
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 || invokeResult.tag === 'failure') {
const rejectReason = JSON.stringify(
((invokeResult as InvokeContractFailedResult)?.reason as RejectedReceive)?.rejectReason
);

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

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: Energy.create(invokeResult.usedEnergy.value + EPSILON_ENERGY),
},
{
parameters: fileHashHex,
schema: moduleSchemaFromBase64(E_SEALING_RAW_SCHEMA),
schema,
}
);
}
Expand Down
7 changes: 7 additions & 0 deletions examples/nft-minting/src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
export const RAW_SCHEMA =
'//8CAQAAAAgAAABDSVMyLU5GVAAJAAAACQAAAGJhbGFuY2VPZgYQARQAAgAAAAgAAAB0b2tlbl9pZB0ABwAAAGFkZHJlc3MVAgAAAAcAAABBY2NvdW50AQEAAAALCAAAAENvbnRyYWN0AQEAAAAMEAEbJQAAABUEAAAADgAAAEludmFsaWRUb2tlbklkAhEAAABJbnN1ZmZpY2llbnRGdW5kcwIMAAAAVW5hdXRob3JpemVkAgYAAABDdXN0b20BAQAAABUFAAAACwAAAFBhcnNlUGFyYW1zAgcAAABMb2dGdWxsAgwAAABMb2dNYWxmb3JtZWQCFAAAAFRva2VuSWRBbHJlYWR5RXhpc3RzAhMAAABJbnZva2VDb250cmFjdEVycm9yAgQAAABtaW50BBQAAwAAAAUAAABvd25lchUCAAAABwAAAEFjY291bnQBAQAAAAsIAAAAQ29udHJhY3QBAQAAAAwIAAAAdG9rZW5faWQdAAgAAABtZXRhZGF0YRQAAgAAAAMAAAB1cmwWAQQAAABoYXNoFQIAAAAEAAAATm9uZQIEAAAAU29tZQEBAAAAEyAAAAACFQQAAAAOAAAASW52YWxpZFRva2VuSWQCEQAAAEluc3VmZmljaW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AAwVBAAAAA4AAABJbnZhbGlkVG9rZW5JZAIRAAAASW5zdWZmaWNpZW50RnVuZHMCDAAAAFVuYXV0aG9yaXplZAIGAAAAQ3VzdG9tAQEAAAAVBQAAAAsAAABQYXJzZVBhcmFtcwIHAAAATG9nRnVsbAIMAAAATG9nTWFsZm9ybWVkAhQAAABUb2tlbklkQWxyZWFkeUV4aXN0cwITAAAASW52b2tlQ29udHJhY3RFcnJvcgIEAAAAdmlldwEUAAIAAAAFAAAAc3RhdGUQAg8VAgAAAAcAAABBY2NvdW50AQEAAAALCAAAAENvbnRyYWN0AQEAAAAMFAACAAAADAAAAG93bmVkX3Rva2VucxACHQAJAAAAb3BlcmF0b3JzEAIVAgAAAAcAAABBY2NvdW50AQEAAAALCAAAAENvbnRyYWN0AQEAAAAMCgAAAGFsbF90b2tlbnMQAh0A';

// Before submitting a transaction we simulate/dry-run the transaction to get an
// estimate of the energy needed for executing the transaction. In addition, we
// allow an additional small amount of energy `EPSILON_ENERGY` to be consumed by
// the transaction to cover small variations (e.g. changes to the smart contract
// state) caused by transactions that have been executed meanwhile.
export const EPSILON_ENERGY = 200n;
54 changes: 43 additions & 11 deletions examples/nft-minting/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ import {
TransactionHash,
TransactionSummaryType,
TransactionKindString,
AccountAddress,
toBuffer,
EntrypointName,
serializeUpdateContractParameters,
InvokeContractFailedResult,
RejectedReceive,
} from '@concordium/web-sdk';
import { RAW_SCHEMA } from './constant';

export const CONTRACT_NAME = 'PiggyBank';
import { EPSILON_ENERGY, RAW_SCHEMA } from './constant';

/**
* Action for initializing a smart contract for keeping a collections of tokens.
Expand Down Expand Up @@ -51,20 +55,48 @@ 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 || invokeResult.tag === 'failure') {
const rejectReason = JSON.stringify(
((invokeResult as InvokeContractFailedResult)?.reason as RejectedReceive)?.rejectReason
);

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

const maxContractExecutionEnergy = Energy.create(invokeResult.usedEnergy.value + EPSILON_ENERGY);
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": "yarn@3.2.0",
"version": "4.1.2",
"version": "4.1.3",
"license": "Apache-2.0",
"dependencies": {
"@concordium/react-components": "^0.4.0",
Expand Down
7 changes: 7 additions & 0 deletions examples/wCCD/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ export const WALLET_CONNECT = ephemeralConnectorType(
*/
export const WCCD_CONTRACT_INDEX_MAINNET = 9354n;
export const WCCD_CONTRACT_INDEX_TESTNET = 2059n;

// Before submitting a transaction we simulate/dry-run the transaction to get an
// estimate of the energy needed for executing the transaction. In addition, we
// allow an additional small amount of energy `EPSILON_ENERGY` to be consumed by
// the transaction to cover small variations (e.g. changes to the smart contract
// state) caused by transactions that have been executed meanwhile.
export const EPSILON_ENERGY = 200n;
117 changes: 86 additions & 31 deletions examples/wCCD/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,94 @@
import { createContext } from 'react';
import { AccountTransactionType, CcdAmount, ContractAddress, Energy, ReceiveName } from '@concordium/web-sdk';
import {
AccountAddress,
AccountTransactionType,
CcdAmount,
ConcordiumGRPCClient,
ContractAddress,
ContractContext,
ContractName,
Energy,
EntrypointName,
InvokeContractFailedResult,
ReceiveName,
RejectedReceive,
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';
import { CONTRACT_NAME, WRAP_FUNCTION_RAW_SCHEMA, UNWRAP_FUNCTION_RAW_SCHEMA, EPSILON_ENERGY } from './constants';

async function getExecutionEnergy(client: ConcordiumGRPCClient, invokeInput: ContractContext) {
const invokeResult = await client.invokeContract(invokeInput);
if (!invokeResult || invokeResult.tag === 'failure') {
const rejectReason = JSON.stringify(
((invokeResult as InvokeContractFailedResult)?.reason as RejectedReceive)?.rejectReason
);

throw new Error(
`RPC call 'invokeContract' on method '${invokeInput.method.value}' of contract '${
invokeInput.contract.index
}' failed.
${rejectReason !== undefined ? `Reject reason: ${rejectReason}` : ''}`
);
}
return Energy.create(invokeResult.usedEnergy.value + EPSILON_ENERGY);
}

/**
* 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 +105,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 +135,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
Loading
Loading