From 4ad8801a00c3d34aaaa0fe2390ebee9b3d797414 Mon Sep 17 00:00:00 2001 From: yusuf idi maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Tue, 14 Nov 2023 14:56:44 +0100 Subject: [PATCH 1/6] Create counterFromTx.test.ts --- tests/counterFromTx.test.ts | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 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..f934f672 --- /dev/null +++ b/tests/counterFromTx.test.ts @@ -0,0 +1,41 @@ +import { + DefaultProvider, + TestWallet, + MethodCallOptions, + bsv, +} from 'scrypt-ts' +import { Counter } from '../src/contracts/counter' +import { myPrivateKey } from './utils/privateKey' + +async function main() { + const contract_id = { + txId: '50a0736bfb78d6695d93a9af7e24a4c3a18884ac99d653131af075f59ed5a628', + OutputIndex: 0, + } + + await Counter.loadArtifact() + const provider = new DefaultProvider({ network: bsv.Networks.testnet }) + const signer = new TestWallet(myPrivateKey, provider) + try { + let tx = new bsv.Transaction() + tx = await provider.getTransaction(contract_id.txId) + let instance = new Counter(100n) + await instance.connect(signer) + + const nextInstance = instance.next() + 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}` + ) + } catch (e) { + console.error('Error interacting with the contract => ', e) + } +} +main() From ca1bf61545fe11d309c53e3d63750e32b653a3dd Mon Sep 17 00:00:00 2001 From: yusuf idi maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Fri, 26 Jan 2024 13:15:01 +0100 Subject: [PATCH 2/6] Update counterFromTx.test.ts --- tests/counterFromTx.test.ts | 80 ++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/tests/counterFromTx.test.ts b/tests/counterFromTx.test.ts index f934f672..149b9c4e 100644 --- a/tests/counterFromTx.test.ts +++ b/tests/counterFromTx.test.ts @@ -1,41 +1,47 @@ -import { - DefaultProvider, - TestWallet, - MethodCallOptions, - bsv, -} from 'scrypt-ts' +import { expect, use } from 'chai' import { Counter } from '../src/contracts/counter' -import { myPrivateKey } from './utils/privateKey' +import { getDefaultSigner } from './utils/helper' +import { MethodCallOptions} from 'scrypt-ts' +import chaiAsPromised from 'chai-as-promised' -async function main() { - const contract_id = { - txId: '50a0736bfb78d6695d93a9af7e24a4c3a18884ac99d653131af075f59ed5a628', - OutputIndex: 0, - } +use(chaiAsPromised) +describe('Test SmartContract `Counter`', () => { + before(() => { + Counter.loadArtifact() + }) - await Counter.loadArtifact() - const provider = new DefaultProvider({ network: bsv.Networks.testnet }) - const signer = new TestWallet(myPrivateKey, provider) - try { - let tx = new bsv.Transaction() - tx = await provider.getTransaction(contract_id.txId) - let instance = new Counter(100n) - await instance.connect(signer) - - const nextInstance = instance.next() - nextInstance.increment() + it('should pass the public method unit test successfully.', async () => { + const balance = 1 - 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}` - ) - } catch (e) { - console.error('Error interacting with the contract => ', e) - } -} -main() + const atOutputIndex = 0 + const signer = getDefaultSigner() + const counter = new Counter(0n) + await counter.connect(signer) + + const deploytx = await counter.deploy(1) + const tx = await signer.connectedProvider.getTransaction(deploytx.id) + let instance = Counter.fromTx(tx, atOutputIndex) + + // call the method of current instance to apply the updates on chain + for (let i = 0; i < 5; ++i) { + // create the next instance from the current + const nextInstance = instance.next() + + // apply updates on the next instance off chain + nextInstance.increment() + + // call the method of current instance to apply the updates on chain + 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 08a07d263d6c0e06b36ae702ae488ed393e8ea78 Mon Sep 17 00:00:00 2001 From: yusuf idi maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Fri, 26 Jan 2024 16:11:15 +0100 Subject: [PATCH 3/6] Update counterFromTx.test.ts --- tests/counterFromTx.test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/counterFromTx.test.ts b/tests/counterFromTx.test.ts index 149b9c4e..a613e1ac 100644 --- a/tests/counterFromTx.test.ts +++ b/tests/counterFromTx.test.ts @@ -18,8 +18,8 @@ describe('Test SmartContract `Counter`', () => { const counter = new Counter(0n) await counter.connect(signer) - const deploytx = await counter.deploy(1) - const tx = await signer.connectedProvider.getTransaction(deploytx.id) + const deplotx = await counter.deploy(1) + const tx = await signer.connectedProvider.getTransaction(deplotx.id) let instance = Counter.fromTx(tx, atOutputIndex) // call the method of current instance to apply the updates on chain @@ -37,6 +37,7 @@ describe('Test SmartContract `Counter`', () => { balance: instance.balance, }, } as MethodCallOptions) + return expect(callTx).not.rejected; console.log(`Counter incrementOnChain called: ${callTx.id}, the count now is: ${nextInstance.count}`) // update the current instance reference @@ -45,3 +46,4 @@ describe('Test SmartContract `Counter`', () => { }) }) + From ff48c987f3e5d1935defa80088b48222dc973186 Mon Sep 17 00:00:00 2001 From: yusuf idi maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Fri, 26 Jan 2024 17:56:32 +0100 Subject: [PATCH 4/6] Update counterFromTx.test.ts --- tests/counterFromTx.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/counterFromTx.test.ts b/tests/counterFromTx.test.ts index a613e1ac..32facb30 100644 --- a/tests/counterFromTx.test.ts +++ b/tests/counterFromTx.test.ts @@ -1,8 +1,8 @@ import { expect, use } from 'chai' import { Counter } from '../src/contracts/counter' -import { getDefaultSigner } from './utils/helper' -import { MethodCallOptions} from 'scrypt-ts' +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`', () => { @@ -14,7 +14,8 @@ describe('Test SmartContract `Counter`', () => { const balance = 1 const atOutputIndex = 0 - const signer = getDefaultSigner() + const provider = new DefaultProvider({network : bsv.Networks.testnet}) + const signer = new TestWallet(myPrivateKey, provider) const counter = new Counter(0n) await counter.connect(signer) @@ -46,4 +47,3 @@ describe('Test SmartContract `Counter`', () => { }) }) - From dc87cb6eeb6ebf45b87d4e137bbf71334da890ba Mon Sep 17 00:00:00 2001 From: yusuf idi maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Fri, 26 Jan 2024 22:46:33 +0100 Subject: [PATCH 5/6] change add() to static --- src/contracts/demo.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/contracts/demo.ts b/src/contracts/demo.ts index b5bd943d..4c7ac672 100644 --- a/src/contracts/demo.ts +++ b/src/contracts/demo.ts @@ -19,13 +19,13 @@ export class Demo extends SmartContract { // for the two equations `x + y = summ` and `x - y = diff`. @method() public unlock(x: bigint, y: bigint) { - assert(this.add(x, y) == this.sum, 'incorrect sum') + assert(Demo.add(x, y) == this.sum, 'incorrect sum') assert(x - y == this.diff, 'incorrect diff') } // Non-public methods cannot be directly called and are intended for internal use within the contract. @method() - add(x: bigint, y: bigint): bigint { + static add(x: bigint, y: bigint): bigint { return x + y } } From c78b1e7512d7257ff12fde013bd46bd6a5ff483c Mon Sep 17 00:00:00 2001 From: yusuf idi maina <120538505+yusufidimaina9989@users.noreply.github.com> Date: Sat, 27 Jan 2024 09:45:59 +0100 Subject: [PATCH 6/6] Delete tests/counterFromTx.test.ts --- tests/counterFromTx.test.ts | 49 ------------------------------------- 1 file changed, 49 deletions(-) delete mode 100644 tests/counterFromTx.test.ts diff --git a/tests/counterFromTx.test.ts b/tests/counterFromTx.test.ts deleted file mode 100644 index 32facb30..00000000 --- a/tests/counterFromTx.test.ts +++ /dev/null @@ -1,49 +0,0 @@ -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 atOutputIndex = 0 - const provider = new DefaultProvider({network : bsv.Networks.testnet}) - const signer = new TestWallet(myPrivateKey, provider) - const counter = new Counter(0n) - await counter.connect(signer) - - const deplotx = await counter.deploy(1) - const tx = await signer.connectedProvider.getTransaction(deplotx.id) - let instance = Counter.fromTx(tx, atOutputIndex) - - // call the method of current instance to apply the updates on chain - for (let i = 0; i < 5; ++i) { - // create the next instance from the current - const nextInstance = instance.next() - - // apply updates on the next instance off chain - nextInstance.increment() - - // call the method of current instance to apply the updates on chain - const { tx: callTx } = await instance.methods.incrementOnChain({ - next: { - instance: nextInstance, - balance: instance.balance, - }, - } as MethodCallOptions) - return expect(callTx).not.rejected; - console.log(`Counter incrementOnChain called: ${callTx.id}, the count now is: ${nextInstance.count}`) - - // update the current instance reference - instance = nextInstance - } - - }) -})