diff --git a/e2e/services/CasperServiceByJsonRPC.test.ts b/e2e/services/CasperServiceByJsonRPC.test.ts index 51001174..bd7074e5 100644 --- a/e2e/services/CasperServiceByJsonRPC.test.ts +++ b/e2e/services/CasperServiceByJsonRPC.test.ts @@ -38,12 +38,7 @@ import { TransactionCategoryMint, makeV1Transaction } from '../../src/lib/TransactionUtil'; -import { - Native, - Session, - Stored, - TransactionSessionKind -} from '../../src/lib/TransactionTarget'; +import { Native, Session, Stored } from '../../src/lib/TransactionTarget'; import { Call, Custom, Transfer } from '../../src/lib/TransactionEntryPoint'; import { Standard } from '../../src/lib/TransactionScheduling'; import { InitiatorAddr } from '../../src/lib/InitiatorAddr'; @@ -460,7 +455,7 @@ describe('CasperServiceByJsonRPC', () => { assert.exists(contractHash); cep18.setContractHash(contractHash!); - cep18.setContractName(`cep18_contract_hash_${tokenName}`); + //cep18.setContractName(`cep18_contract_hash_${tokenName}`); const fetchedTokenName = await cep18.queryContractData(['name']); const fetchedTokenSymbol = await cep18.queryContractData(['symbol']); const fetchedTokenDecimals: BigNumber = await cep18.queryContractData([ @@ -512,7 +507,7 @@ describe('CasperServiceByJsonRPC', () => { result = await client.waitForDeploy(transferDeploy, 100000); assert.equal(result.deploy.hash, deploy_hash); - expect(result.deploy.session).to.have.property('StoredContractByName'); + expect(result.deploy.session).to.have.property('StoredContractByHash'); expect(result.execution_info!.execution_result!).to.have.property( 'Version2' ); @@ -526,7 +521,7 @@ describe('CasperServiceByJsonRPC', () => { }); //This needs to wait for a fix in the node which currently prevents wasm transactions - xit('CEP18 should work deployed via "account_put_transaction"', async () => { + it.only('CEP18 should work deployed via "account_put_transaction"', async () => { const casperClient = new CasperClient(NODE_URL); const cep18 = new Contract(casperClient); const wasmPath = path.resolve(__dirname, './cep18.wasm'); @@ -556,18 +551,13 @@ describe('CasperServiceByJsonRPC', () => { const transaction = makeV1Transaction( params, runtimeArgs, - Session.build( - TransactionSessionKind.Installer, - wasm, - TransactionRuntime.VmCasperV1 - ), + Session.build(wasm, TransactionRuntime.VmCasperV1), new Call(), new Standard(), TransactionCategoryInstallUpgrade ); const signedTransaction = transaction.sign([faucetKey]); await client.transaction(signedTransaction); - await sleep(2500); const result = await client.waitForTransaction(signedTransaction, 100000); @@ -585,7 +575,7 @@ describe('CasperServiceByJsonRPC', () => { assert.exists(contractHash); cep18.setContractHash(contractHash!); - cep18.setContractName(`cep18_contract_hash_${tokenName}`); + //cep18.setContractName(`cep18_contract_hash_${tokenName}`); const fetchedTokenName = await cep18.queryContractData(['name']); const fetchedTokenSymbol = await cep18.queryContractData(['symbol']); const fetchedTokenDecimals: BigNumber = await cep18.queryContractData([ diff --git a/src/lib/Contracts.ts b/src/lib/Contracts.ts index 9eb10f5c..06b19b98 100644 --- a/src/lib/Contracts.ts +++ b/src/lib/Contracts.ts @@ -17,6 +17,9 @@ export const contractHashToByteArray = (contractHash: string) => const NO_CLIENT_ERR = 'You need to either create Contract instance with casperClient or pass it as parameter to this function'; +const NO_ENTRYPOINT_QUALIFIER = + 'You need to either specify contractName or contractHash before calling an endpoint'; + /** Smart contract object for interacting with contracts on the Casper Network */ export class Contract { public contractHash?: string; @@ -86,7 +89,7 @@ export class Contract { } private checkSetup(): boolean { - if (this.contractHash) return true; + if (this.contractHash || this.contractName) return true; throw Error('You need to setContract before running this method.'); } @@ -111,14 +114,29 @@ export class Contract { ttl: number = DEFAULT_DEPLOY_TTL ): Deploy { this.checkSetup(); + let session; + if (this.contractHash) { + const hashOnly = this.contractHash!.slice(16); + const addrEntityBytes = contractHashToByteArray(hashOnly); - const deploy = DeployUtil.makeDeploy( - new DeployUtil.DeployParams(sender, chainName, 1, ttl), - DeployUtil.ExecutableDeployItem.newStoredContractByName( + session = DeployUtil.ExecutableDeployItem.newStoredContractByHash( + addrEntityBytes, + entryPoint, + args + ); + } else if (this.contractName) { + session = DeployUtil.ExecutableDeployItem.newStoredContractByName( this.contractName!, entryPoint, args - ), + ); + } else { + throw Error(NO_ENTRYPOINT_QUALIFIER); + } + + const deploy = DeployUtil.makeDeploy( + new DeployUtil.DeployParams(sender, chainName, 1, ttl), + session, DeployUtil.standardPayment(paymentAmount) ); diff --git a/src/lib/DeployUtil.ts b/src/lib/DeployUtil.ts index 27cdca26..d9622eea 100644 --- a/src/lib/DeployUtil.ts +++ b/src/lib/DeployUtil.ts @@ -323,8 +323,8 @@ export class StoredContractByHash extends ExecutableDeployItemInternal { public args: RuntimeArgs; /** - * Constructs a `StoredContractByHash` object from the `Uint8Array` typed hash, entrypoint of the contract, and associated runtime arguments - * @param hash `Uint8Array` typed smart contract hash + * Constructs a `StoredContractByHash` object from the hash, entrypoint of the contract, and associated runtime arguments + * @param hash hash of the addressable entity of the contract * @param entryPoint An entrypoint of the smart contract * @param args The runtime arguments for interaction on the `entryPoint` */ @@ -748,7 +748,7 @@ export class ExecutableDeployItem implements ToBytes { /** * Creates a new `StoredContractByHash` object from a `Uint8Array` contract hash, entrypoint, and runtime arguments - * @param hash `Uint8Array` representation of a smart contract hash + * @param hash `string` representation of a smart contract addreassable entity hash * @param entryPoint Name of an entrypoint of the stored contract * @param args The runtime arguments for the new `StoredContractByHash` object * @returns A new `ExecutableDeployItem` created from a new `StoredContractByHash` object built using `hash`, `entryPoint` and `args` diff --git a/src/lib/TransactionTarget.test.ts b/src/lib/TransactionTarget.test.ts index 5a17c8f6..1a110494 100644 --- a/src/lib/TransactionTarget.test.ts +++ b/src/lib/TransactionTarget.test.ts @@ -53,11 +53,10 @@ const expectedStoredVariantBytes = [ 0, 0 ]; -const expectedSessionVariantBytes = [2, 0, 4, 0, 0, 0, 81, 5, 6, 10, 0]; +const expectedSessionVariantBytes = [2, 4, 0, 0, 0, 81, 5, 6, 10, 0]; const mockSessionJson = { a: { Session: { - kind: 'Standard', module_bytes: '5105060a', runtime: 'VmCasperV1' } diff --git a/src/lib/TransactionTarget.ts b/src/lib/TransactionTarget.ts index d570ba0c..abad675c 100644 --- a/src/lib/TransactionTarget.ts +++ b/src/lib/TransactionTarget.ts @@ -71,36 +71,8 @@ export class Stored implements TransactionTarget { } } -/** - * enum of transaction session kind - * @enum - */ -export enum TransactionSessionKind { - Standard = 'Standard', - Installer = 'Installer', - Upgrader = 'Upgrader', - Isolated = 'Isolated' -} - -function kindToBytes(sessionKind: TransactionSessionKind): Uint8Array { - switch (sessionKind) { - case TransactionSessionKind.Standard: - return toBytesU8(0); - case TransactionSessionKind.Installer: - return toBytesU8(1); - case TransactionSessionKind.Upgrader: - return toBytesU8(2); - case TransactionSessionKind.Isolated: - return toBytesU8(3); - default: - throw new Error('Unknown session kind'); - } -} - @jsonObject export class Session implements TransactionTarget { - @jsonMember({ constructor: String }) - public kind: TransactionSessionKind; @jsonMember({ name: 'module_bytes', serializer: byteArrayJsonSerializer, @@ -112,7 +84,6 @@ export class Session implements TransactionTarget { public toJSON(): unknown { return { Session: { - kind: this.kind, module_bytes: byteArrayJsonSerializer(this.moduleBytes), runtime: this.runtime } @@ -120,7 +91,6 @@ export class Session implements TransactionTarget { } public toBytes(): ToBytesResult { - const kindBytes = kindToBytes(this.kind); const maybeRuntimeBytes = transactionRuntimeToBytes(this.runtime); if (maybeRuntimeBytes.err) { return maybeRuntimeBytes; @@ -130,19 +100,13 @@ export class Session implements TransactionTarget { return Ok( concat([ toBytesU8(SESSION_TAG), - kindBytes, toBytesArrayU8(this.moduleBytes), runtimeBytes ]) ); } - static build( - kind: TransactionSessionKind, - moduleBytes: Uint8Array, - runtime: TransactionRuntime - ): Session { + static build(moduleBytes: Uint8Array, runtime: TransactionRuntime): Session { const session = new Session(); - session.kind = kind; session.moduleBytes = moduleBytes; session.runtime = runtime; return session; diff --git a/src/services/CasperServiceByJsonRPC.ts b/src/services/CasperServiceByJsonRPC.ts index 505c1e65..f143c021 100644 --- a/src/services/CasperServiceByJsonRPC.ts +++ b/src/services/CasperServiceByJsonRPC.ts @@ -717,7 +717,6 @@ export class CasperServiceByJsonRPC { 'This method is deprecated and will be removed in the future release, please use transaction method instead.' ); this.checkDeploySize(signedDeploy); - const { checkApproval = false } = props ?? {}; if (checkApproval && signedDeploy.approvals.length == 0) { throw new Error('Required signed deploy');