diff --git a/src/contracts/array.ts b/src/contracts/array.ts index 59c8fb61..27872f4f 100644 --- a/src/contracts/array.ts +++ b/src/contracts/array.ts @@ -12,16 +12,17 @@ import { byteString2Int, } from 'scrypt-ts' +export enum ArrayConstants { + DATALEN = 1, + MAX_SIZE = 9, + INVALID = -9999999999, +} + export class Arrays extends SmartContractLib { @prop() data: ByteString - - static readonly DATALEN: bigint = 1n @prop() static readonly EMPTY: ByteString = toByteString('') - static readonly MAX_SIZE = 9 - static readonly INVALID: bigint = -9999999999n - constructor(data: ByteString) { super(...arguments) this.data = Arrays.EMPTY @@ -29,19 +30,19 @@ export class Arrays extends SmartContractLib { @method() push(x: bigint): boolean { - this.data += int2ByteString(x, Arrays.DATALEN) + this.data += int2ByteString(x, BigInt(ArrayConstants.DATALEN)) return true } @method() pop(): bigint { - let result: bigint = Arrays.INVALID + let result: bigint = BigInt(ArrayConstants.INVALID) const maxIndex = this.length() - 1n if (maxIndex > -1) { const valueRaw: ByteString = slice( this.data, - maxIndex * Arrays.DATALEN, - (maxIndex + 1n) * Arrays.DATALEN + maxIndex * BigInt(ArrayConstants.DATALEN), + (maxIndex + 1n) * BigInt(ArrayConstants.DATALEN) ) result = byteString2Int(valueRaw) this.data = slice(this.data, 0n, maxIndex) @@ -54,13 +55,13 @@ export class Arrays extends SmartContractLib { let result: bigint = -1n let done: boolean = false const length = this.length() - for (let i = 0n; i < Arrays.MAX_SIZE; i++) { + for (let i = 0n; i < 9n; i++) { if (i < length) { if (!done) { const valueRaw: ByteString = slice( this.data, - i * Arrays.DATALEN, - (i + 1n) * Arrays.DATALEN + i * BigInt(ArrayConstants.DATALEN), + (i + 1n) * BigInt(ArrayConstants.DATALEN) ) const value: bigint = byteString2Int(valueRaw) if (value == x) { @@ -75,13 +76,13 @@ export class Arrays extends SmartContractLib { @method() at(index: bigint): bigint { - let result: bigint = Arrays.INVALID + let result: bigint = BigInt(ArrayConstants.INVALID) const length: bigint = this.length() if (index >= 0n && index < length) { const valueRaw: ByteString = slice( this.data, - index * Arrays.DATALEN, - (index + 1n) * Arrays.DATALEN + index * BigInt(ArrayConstants.DATALEN), + (index + 1n) * BigInt(ArrayConstants.DATALEN) ) result = byteString2Int(valueRaw) } @@ -90,7 +91,7 @@ export class Arrays extends SmartContractLib { @method() length(): bigint { - return BigInt(len(this.data)) / Arrays.DATALEN + return BigInt(len(this.data)) / BigInt(ArrayConstants.DATALEN) } @method() @@ -98,7 +99,7 @@ export class Arrays extends SmartContractLib { let done: boolean = false const length: bigint = this.length() if (length > 0n) { - this.data = toByteString('') + this.data = Arrays.EMPTY done = true } return done @@ -135,7 +136,7 @@ export class ArraysTest extends SmartContract { value = a.at(4n) assert(value == -9n) value = a.at(5n) - assert(value == Arrays.INVALID) + assert(value == BigInt(ArrayConstants.INVALID)) let top: bigint = a.pop() assert(top == -9n) @@ -148,7 +149,7 @@ export class ArraysTest extends SmartContract { top = a.pop() assert(top == 33n) top = a.pop() - assert(top == Arrays.INVALID) + assert(top == BigInt(ArrayConstants.INVALID)) a.push(-9n) a.clear() assert(a.length() == 0n) diff --git a/src/contracts/rockPaperScissors.ts b/src/contracts/rockPaperScissors.ts index 42db2946..f6da553e 100644 --- a/src/contracts/rockPaperScissors.ts +++ b/src/contracts/rockPaperScissors.ts @@ -16,6 +16,12 @@ import { bsv, } from 'scrypt-ts' +export enum Choice { + ROCK, + PAPER, + SCISSORS +} + export class RockPaperScissors2 extends SmartContract { @prop() playerA: PubKey @@ -26,13 +32,6 @@ export class RockPaperScissors2 extends SmartContract { @prop() playerBHash: Sha256 - @prop() - static readonly ROCK: bigint = 0n - @prop() - static readonly PAPER: bigint = 1n - @prop() - static readonly SCISSORS: bigint = 2n - constructor( playerA: PubKey, playerB: PubKey, @@ -52,7 +51,7 @@ export class RockPaperScissors2 extends SmartContract { playerBMove: bigint, playerASalt: ByteString, playerBSalt: ByteString - ) { + ) { // Check players move commitments. // Salt is used to prevent hash collisions. @@ -86,12 +85,12 @@ export class RockPaperScissors2 extends SmartContract { // Winner takes all. let winner = this.playerB if ( - (playerAMove == RockPaperScissors2.ROCK && - playerBMove == RockPaperScissors2.SCISSORS) || - (playerAMove == RockPaperScissors2.SCISSORS && - playerBMove == RockPaperScissors2.PAPER) || - (playerAMove == RockPaperScissors2.PAPER && - playerBMove == RockPaperScissors2.ROCK) + (playerAMove == BigInt(Choice.ROCK) && + playerBMove == BigInt(Choice.SCISSORS)) || + (playerAMove == BigInt(Choice.SCISSORS) && + playerBMove == BigInt(Choice.PAPER)) || + (playerAMove == BigInt(Choice.PAPER) && + playerBMove == BigInt(Choice.ROCK)) ) { winner = this.playerA } diff --git a/tests/rockPaperScissors.test.ts b/tests/rockPaperScissors.test.ts index 590bf31c..ce31929d 100644 --- a/tests/rockPaperScissors.test.ts +++ b/tests/rockPaperScissors.test.ts @@ -8,7 +8,7 @@ import { toByteString, toHex, } from 'scrypt-ts' -import { RockPaperScissors2 } from '../src/contracts/rockPaperScissors' +import { RockPaperScissors2, Choice } from '../src/contracts/rockPaperScissors' import { getDefaultSigner, randomPrivateKey } from './utils/helper' import { expect, use } from 'chai' import chaiAsPromised from 'chai-as-promised' @@ -29,8 +29,8 @@ describe('Test SmartContract `RockPaperScirssors`', async () => { instance = new RockPaperScissors2( PubKey(toHex(playerApublickey)), PubKey(toHex(playerBpublickey)), - Sha256(int2ByteString(RockPaperScissors2.ROCK, 1n) + playerASalt), - Sha256(int2ByteString(RockPaperScissors2.PAPER, 1n) + playerBSalt) + Sha256(int2ByteString(BigInt(Choice.ROCK), 1n) + playerASalt), + Sha256(int2ByteString(BigInt(Choice.PAPER), 1n) + playerBSalt) ) await instance.connect( @@ -45,8 +45,8 @@ describe('Test SmartContract `RockPaperScirssors`', async () => { const callContract = async () => { await instance.methods.play( - RockPaperScissors2.ROCK, - RockPaperScissors2.PAPER, + BigInt(Choice.ROCK), + BigInt(Choice.PAPER), playerASalt, playerBSalt, { @@ -63,8 +63,8 @@ describe('Test SmartContract `RockPaperScirssors`', async () => { const callContract = async () => { await instance.methods.play( - RockPaperScissors2.ROCK, - RockPaperScissors2.PAPER, + BigInt(Choice.ROCK), + BigInt(Choice.PAPER), playerASalt + toByteString('00'), playerBSalt, {