-
Notifications
You must be signed in to change notification settings - Fork 45
Smart Contract intents factories #327
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
Changes from all commits
b2e6b92
2099f3d
a8091a9
7db0a4a
5ba4506
08dd376
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 |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| { | ||
| "buildInfo": { | ||
| "rustc": { | ||
| "version": "1.71.0-nightly", | ||
| "commitHash": "7f94b314cead7059a71a265a8b64905ef2511796", | ||
| "commitDate": "2023-04-23", | ||
| "channel": "Nightly", | ||
| "short": "rustc 1.71.0-nightly (7f94b314c 2023-04-23)" | ||
| }, | ||
| "contractCrate": { | ||
| "name": "adder", | ||
| "version": "0.0.0" | ||
| }, | ||
| "framework": { | ||
| "name": "multiversx-sc", | ||
| "version": "0.41.3" | ||
| } | ||
| }, | ||
| "docs": [ | ||
| "One of the simplest smart contracts possible,", | ||
| "it holds a single variable in storage, which anyone can increment." | ||
| ], | ||
| "name": "Adder", | ||
| "constructor": { | ||
| "inputs": [ | ||
| { | ||
| "name": "initial_value", | ||
| "type": "BigUint" | ||
| } | ||
| ], | ||
| "outputs": [] | ||
| }, | ||
| "endpoints": [ | ||
| { | ||
| "name": "getSum", | ||
| "mutability": "readonly", | ||
| "inputs": [], | ||
| "outputs": [ | ||
| { | ||
| "type": "BigUint" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "docs": [ | ||
| "Add desired amount to the storage variable." | ||
| ], | ||
| "name": "add", | ||
| "mutability": "mutable", | ||
| "inputs": [ | ||
| { | ||
| "name": "value", | ||
| "type": "BigUint" | ||
| } | ||
| ], | ||
| "outputs": [] | ||
| } | ||
| ], | ||
| "events": [], | ||
| "hasCallback": false, | ||
| "types": {} | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { BigNumber } from "bignumber.js"; | ||
|
|
||
| export class TransactionIntent { | ||
| public sender: string; | ||
| public receiver: string; | ||
| public gasLimit: BigNumber.Value; | ||
| public value?: BigNumber.Value; | ||
| public data?: Uint8Array; | ||
|
|
||
| public constructor(options: { | ||
| sender: string, | ||
| receiver: string, | ||
| gasLimit: BigNumber.Value, | ||
| value?: BigNumber.Value, | ||
| data?: Uint8Array | ||
| }) { | ||
| this.sender = options.sender; | ||
| this.receiver = options.receiver; | ||
| this.gasLimit = options.gasLimit; | ||
| this.value = options.value; | ||
| this.data = options.data; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { assert, expect } from "chai"; | ||
| import { SmartContractTransactionIntentsFactory } from "./smartContractTransactionIntentsFactory"; | ||
| import { Address } from "../address"; | ||
| import { Code } from "../smartcontracts/code"; | ||
| import { AbiRegistry } from "../smartcontracts/typesystem/abiRegistry"; | ||
| import { U32Value } from "../smartcontracts"; | ||
| import { CONTRACT_DEPLOY_ADDRESS } from "../constants"; | ||
| import { loadContractCode, loadAbiRegistry } from "../testutils/utils"; | ||
| import { Err } from "../errors"; | ||
|
|
||
| describe("test smart contract intents factory", function () { | ||
| let factory: SmartContractTransactionIntentsFactory; | ||
| let abiAwareFactory: SmartContractTransactionIntentsFactory; | ||
| let adderByteCode: Code; | ||
| let abiRegistry: AbiRegistry; | ||
|
|
||
| before(async function () { | ||
| factory = new SmartContractTransactionIntentsFactory({ | ||
| config: | ||
| { | ||
| chainID: "D", | ||
| minGasLimit: 50000, | ||
| gasLimitPerByte: 1500 | ||
| } | ||
| }); | ||
|
|
||
| adderByteCode = await loadContractCode("src/testdata/adder.wasm"); | ||
| abiRegistry = await loadAbiRegistry("src/testdata/adder.abi.json"); | ||
|
|
||
| abiAwareFactory = new SmartContractTransactionIntentsFactory({ | ||
| config: | ||
| { | ||
| chainID: "D", | ||
| minGasLimit: 50000, | ||
| gasLimitPerByte: 1500 | ||
| }, | ||
| abi: abiRegistry | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| it("should throw error when args are not of type 'TypedValue'", async function () { | ||
| const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); | ||
| const gasLimit = 6000000; | ||
| const args = [0]; | ||
|
|
||
| assert.throws(() => factory.createTransactionIntentForDeploy({ | ||
| sender: sender, | ||
| bytecode: adderByteCode.valueOf(), | ||
| gasLimit: gasLimit, | ||
| args: args | ||
| }), Err, "Can't convert args to TypedValues"); | ||
| }); | ||
|
Contributor
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 test succeeds even if no exception is thrown. Use
Contributor
Author
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. Now using |
||
|
|
||
| it("should build intent for deploy", async function () { | ||
| const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); | ||
| const gasLimit = 6000000; | ||
| const args = [new U32Value(0)]; | ||
|
|
||
| const deployIntent = factory.createTransactionIntentForDeploy({ | ||
| sender: sender, | ||
| bytecode: adderByteCode.valueOf(), | ||
| gasLimit: gasLimit, | ||
| args: args | ||
| }); | ||
| const abiDeployIntent = abiAwareFactory.createTransactionIntentForDeploy({ | ||
| sender: sender, | ||
| bytecode: adderByteCode.valueOf(), | ||
| gasLimit: gasLimit, | ||
| args: args | ||
| }); | ||
|
|
||
| assert.equal(deployIntent.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); | ||
| assert.equal(deployIntent.receiver, CONTRACT_DEPLOY_ADDRESS); | ||
| assert.isDefined(deployIntent.data); | ||
|
Contributor
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. A length-based assertion would work, as well.
Contributor
Author
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. Added a length-based assertion just for this test. |
||
| expect(deployIntent.data!.length).to.be.greaterThan(0); | ||
|
|
||
| const expectedGasLimit = 6000000 + 50000 + 1500 * deployIntent.data!.length; | ||
| assert.equal(deployIntent.gasLimit.valueOf(), expectedGasLimit); | ||
| assert.equal(deployIntent.value, 0); | ||
|
|
||
| assert.deepEqual(deployIntent, abiDeployIntent); | ||
| }); | ||
|
|
||
| it("should build intent for execute", async function () { | ||
| const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); | ||
| const contract = Address.fromBech32("erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); | ||
| const func = "add"; | ||
| const gasLimit = 6000000; | ||
| const args = [new U32Value(7)]; | ||
|
|
||
| const deployIntent = factory.createTransactionIntentForExecute({ | ||
| sender: sender, | ||
| contractAddress: contract, | ||
| func: func, | ||
| gasLimit: gasLimit, | ||
| args: args | ||
| }); | ||
| const abiDeployIntent = abiAwareFactory.createTransactionIntentForExecute({ | ||
| sender: sender, | ||
| contractAddress: contract, | ||
| func: func, | ||
| gasLimit: gasLimit, | ||
| args: args | ||
| }); | ||
|
|
||
| assert.equal(deployIntent.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); | ||
| assert.equal(deployIntent.receiver, "erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); | ||
|
|
||
| assert.isDefined(deployIntent.data); | ||
| assert.deepEqual(deployIntent.data, Buffer.from("add@07")); | ||
|
|
||
| assert.equal(deployIntent.gasLimit.valueOf(), 6059000); | ||
| assert.equal(deployIntent.value, 0); | ||
|
|
||
| assert.deepEqual(deployIntent, abiDeployIntent); | ||
| }); | ||
|
|
||
| it("should build intent for upgrade", async function () { | ||
| const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); | ||
| const contract = Address.fromBech32("erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); | ||
| const gasLimit = 6000000; | ||
| const args = [new U32Value(0)]; | ||
|
|
||
| const deployIntent = factory.createTransactionIntentForUpgrade({ | ||
| sender: sender, | ||
| contract: contract, | ||
| bytecode: adderByteCode.valueOf(), | ||
| gasLimit: gasLimit, | ||
| args: args | ||
| }); | ||
|
Contributor
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. Formatting / newline.
Contributor
Author
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. Will fix. |
||
| const abiDeployIntent = abiAwareFactory.createTransactionIntentForUpgrade({ | ||
| sender: sender, | ||
| contract: contract, | ||
| bytecode: adderByteCode.valueOf(), | ||
| gasLimit: gasLimit, | ||
| args: args | ||
| }); | ||
|
|
||
| assert.equal(deployIntent.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); | ||
| assert.equal(deployIntent.receiver, "erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4"); | ||
| assert.isDefined(deployIntent.data); | ||
| assert(checkIfByteArrayStartsWith(deployIntent.data!, "upgradeContract@")); | ||
|
Contributor
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. Alternatively, shorter, without
Contributor
Author
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. Will do this in the next PR. |
||
|
|
||
| const expectedGasLimit = 6000000 + 50000 + 1500 * deployIntent.data!.length; | ||
| assert.equal(deployIntent.gasLimit.valueOf(), expectedGasLimit); | ||
| assert.equal(deployIntent.value, 0); | ||
|
|
||
| assert.deepEqual(deployIntent, abiDeployIntent); | ||
| }); | ||
|
|
||
| function checkIfByteArrayStartsWith(array: Uint8Array, sequence: string) { | ||
| const sequenceBytes = Buffer.from(sequence); | ||
|
|
||
| for (let i = 0; i < sequenceBytes.length; i++) { | ||
| if (sequenceBytes[i] !== array[i]) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| }); | ||
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.
This is a non-breaking change.
In the future, this file will be removed (new intent factories are to be used).