Skip to content

Commit

Permalink
Use enum (#340)
Browse files Browse the repository at this point in the history
* use enum
  • Loading branch information
yusufidimaina9989 authored Jul 19, 2024
1 parent 1004682 commit ce6e947
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
39 changes: 20 additions & 19 deletions src/contracts/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,37 @@ 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
}

@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)
Expand All @@ -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) {
Expand All @@ -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)
}
Expand All @@ -90,15 +91,15 @@ export class Arrays extends SmartContractLib {

@method()
length(): bigint {
return BigInt(len(this.data)) / Arrays.DATALEN
return BigInt(len(this.data)) / BigInt(ArrayConstants.DATALEN)
}

@method()
clear(): boolean {
let done: boolean = false
const length: bigint = this.length()
if (length > 0n) {
this.data = toByteString('')
this.data = Arrays.EMPTY
done = true
}
return done
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
27 changes: 13 additions & 14 deletions src/contracts/rockPaperScissors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import {
bsv,
} from 'scrypt-ts'

export enum Choice {
ROCK,
PAPER,
SCISSORS
}

export class RockPaperScissors2 extends SmartContract {
@prop()
playerA: PubKey
Expand All @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions tests/rockPaperScissors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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(
Expand All @@ -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,
{
Expand All @@ -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,
{
Expand Down

0 comments on commit ce6e947

Please sign in to comment.