From d85fee159f58261fc26150afdb76a21967eb0a63 Mon Sep 17 00:00:00 2001 From: yusuf idi maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Sun, 11 Feb 2024 16:00:15 +0100 Subject: [PATCH 1/2] Create counterFromTx.test.ts --- tests/counterFromTx.test.ts | 57 +++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/counterFromTx.test.ts diff --git a/tests/counterFromTx.test.ts b/tests/counterFromTx.test.ts new file mode 100644 index 00000000..8c0e4b2e --- /dev/null +++ b/tests/counterFromTx.test.ts @@ -0,0 +1,57 @@ +import { expect, use } from 'chai' +import { Counter } from '../src/contracts/counter' +import { DefaultProvider, MethodCallOptions, TestWallet, bsv } from 'scrypt-ts' +import chaiAsPromised from 'chai-as-promised' +import { myPrivateKey } from './utils/privateKey' +use(chaiAsPromised) +describe('Test SmartContract `Counter`', () => { + before(() => { + Counter.loadArtifact() + }) + + it('should pass the public method unit test successfully.', async () => { + const balance = 1 + + const initialCount: bigint = 100n + const atOutputIndex = 0 + const counter = new Counter(initialCount) + const signer = new TestWallet( + myPrivateKey, + new DefaultProvider({ network: bsv.Networks.testnet }) + ) + await counter.connect(signer) + const deployTx = await counter.deploy(1) + + // set current instance to be the deployed one + let instance = counter + + // call the method of current instance to apply the updates on chain + for (let i = 0; i < 5; ++i) { + const tx = await signer.connectedProvider.getTransaction( + deployTx.id + ) + instance = Counter.fromTx(tx, atOutputIndex) + + await instance.connect(signer) + + // create the next instance from the current + const nextInstance = instance.next() + + // apply updates on the next instance off chain + nextInstance.increment() + + const { tx: callTx } = await instance.methods.incrementOnChain({ + next: { + instance: nextInstance, + balance: instance.balance, + }, + } as MethodCallOptions) + console.log( + `Counter incrementOnChain called: ${callTx.id}, the count now is: ${nextInstance.count}` + ) + + // update the current instance reference + instance = nextInstance + } + }) +}) From e26086dd0d89a5324c91a29af74b05237214eb87 Mon Sep 17 00:00:00 2001 From: yusuf idi maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Sun, 11 Feb 2024 16:42:33 +0100 Subject: [PATCH 2/2] Update counterFromTx.test.ts --- tests/counterFromTx.test.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/tests/counterFromTx.test.ts b/tests/counterFromTx.test.ts index 8c0e4b2e..a8c3a7fb 100644 --- a/tests/counterFromTx.test.ts +++ b/tests/counterFromTx.test.ts @@ -1,8 +1,8 @@ -import { expect, use } from 'chai' +import { use } from 'chai' import { Counter } from '../src/contracts/counter' -import { DefaultProvider, MethodCallOptions, TestWallet, bsv } from 'scrypt-ts' +import { getDefaultSigner } from './utils/helper' +import { MethodCallOptions,} from 'scrypt-ts' import chaiAsPromised from 'chai-as-promised' -import { myPrivateKey } from './utils/privateKey' use(chaiAsPromised) describe('Test SmartContract `Counter`', () => { before(() => { @@ -15,10 +15,7 @@ describe('Test SmartContract `Counter`', () => { const initialCount: bigint = 100n const atOutputIndex = 0 const counter = new Counter(initialCount) - const signer = new TestWallet( - myPrivateKey, - new DefaultProvider({ network: bsv.Networks.testnet }) - ) + const signer = getDefaultSigner() await counter.connect(signer) const deployTx = await counter.deploy(1) @@ -27,10 +24,8 @@ describe('Test SmartContract `Counter`', () => { // call the method of current instance to apply the updates on chain for (let i = 0; i < 5; ++i) { - const tx = await signer.connectedProvider.getTransaction( - deployTx.id - ) - instance = Counter.fromTx(tx, atOutputIndex) + + instance = Counter.fromTx(deployTx, atOutputIndex) await instance.connect(signer)