-
Notifications
You must be signed in to change notification settings - Fork 16
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
[JSS-101] Estimate smart-contract update energy costs #362
base: main
Are you sure you want to change the base?
Changes from 2 commits
441d2f9
07edcd4
87b3ada
82335c3
8453153
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -48,3 +48,7 @@ yarn run-example /path/to/example.ts [opts] | |||||
``` | ||||||
|
||||||
Where opts are any arguments that the example script takes. | ||||||
|
||||||
Default endpoint for node is 'localhost:20000', | ||||||
but instead of installing local node, | ||||||
can be used testnet node <https://node.testnet.concordium.com:20000> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This endpoint has been replaced
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,11 @@ | ||||||
# Changelog | ||||||
|
||||||
## 7.4.1 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consequently, this should be
Suggested change
|
||||||
|
||||||
### Added | ||||||
|
||||||
- Method (`getContractUpdateEnergyCost`) for estimating energy usage of contract update. | ||||||
|
||||||
## 7.4.0 | ||||||
|
||||||
### Added | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@concordium/web-sdk", | ||
"version": "7.4.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would argue that this is a minor version (as it adds new functionality). The way we usually release though, is that we create a pull request specifically for bumping the version in preparation for the release. This is to avoid confusion with regards to what has already been released and what is pending release. |
||
"version": "7.4.1", | ||
"license": "Apache-2.0", | ||
"engines": { | ||
"node": ">=16" | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -91,3 +91,16 @@ export function getNamesFromReceive(receiveName: string): { | |||||
entrypointName: receiveName.substring(splitPoint + 1), | ||||||
}; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Get contract update payload size by adding reserved offsets to parameter size and receive name size | ||||||
* Amount (8 bytes), Contract address (16 bytes), Receive name size (2 bytes), Parameter size (2 bytes) | ||||||
*/ | ||||||
export function getUpdatePayloadSize( | ||||||
parameterSize: number, | ||||||
receiveNameLength: number | ||||||
) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return ( | ||||||
8n + 16n + 2n + BigInt(parameterSize) + 2n + BigInt(receiveNameLength) | ||||||
); | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { ConcordiumGRPCClient } from './grpc/GRPCClient.js'; | ||
import { getAccountTransactionHandler } from './accountTransactions.js'; | ||
import { collapseRatio, multiplyRatio } from './ratioHelpers.js'; | ||
import { | ||
|
@@ -8,6 +9,13 @@ import { | |
} from './types.js'; | ||
import * as Energy from './types/Energy.js'; | ||
import * as CcdAmount from './types/CcdAmount.js'; | ||
import { | ||
AccountAddress, | ||
ContractAddress, | ||
Parameter, | ||
ReceiveName, | ||
} from './pub/types.js'; | ||
import { getUpdatePayloadSize } from './contractHelpers.js'; | ||
|
||
/** | ||
* These constants must be consistent with constA and constB in: | ||
|
@@ -60,6 +68,42 @@ export function getEnergyCost( | |
); | ||
} | ||
|
||
/** | ||
* Get contract update energy cost | ||
* Estimated by calculateEnergyCost, where transactionSpecificCost received from invokeContract used energy | ||
* @param {ConcordiumGRPCClient} grpcClient - The client to be used for the query | ||
* @param {ContractAddress.Type} contractAddress - The address of the contract to query | ||
* @param {AccountAddress.Type} invoker - Representation of an account address | ||
* @param {Parameter.Type} parameter - Input for contract function | ||
* @param {ReceiveName.Type} method - Represents a receive-function in a smart contract module | ||
* @param {bigint} signatureCount - Number of expected signatures | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Please document when this function throws and also that the estimate is given for the last finalized block according to the node, which means this the actual energy cost might be different depending on the implementation of the smart contract and the interaction with the instance, since this was estimated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it not make sense to add an optional |
||
export async function getContractUpdateEnergyCost( | ||
grpcClient: ConcordiumGRPCClient, | ||
contractAddress: ContractAddress.Type, | ||
invoker: AccountAddress.Type, | ||
parameter: Parameter.Type, | ||
method: ReceiveName.Type, | ||
signatureCount: bigint | ||
): Promise<Energy.Type> { | ||
const res = await grpcClient.invokeContract({ | ||
contract: contractAddress, | ||
invoker, | ||
parameter, | ||
method, | ||
}); | ||
|
||
if (!res || res.tag === 'failure') { | ||
throw new Error(res?.reason?.tag || 'no response'); | ||
} | ||
|
||
return calculateEnergyCost( | ||
signatureCount, | ||
getUpdatePayloadSize(parameter.buffer.length, method.toString().length), | ||
res.usedEnergy.value | ||
); | ||
} | ||
|
||
/** | ||
* Given the current blockchain parameters, return the microCCD per NRG exchange rate of the chain. | ||
* @returns the microCCD per NRG exchange rate as a ratio. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are migrating to another endpoint