Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Erroneous calculator test code #95

Open
Killpit opened this issue Mar 6, 2023 · 1 comment
Open

Erroneous calculator test code #95

Killpit opened this issue Mar 6, 2023 · 1 comment

Comments

@Killpit
Copy link

Killpit commented Mar 6, 2023

Python code for calculator:

`# calculator

Built with Seahorse v0.2.7

from seahorse.prelude import *

declare_id('Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS')

class Calculator(Account):
owner: Pubkey
display: i64

class Operation(Enum):
ADD = 0
SUB = 1
MUL = 2
DIV = 3

@Instruction
def init_calculator(owner: Signer, calculator: Empty[Calculator]):
calculator = calculator.init(payer = owner, seeds = ['Calculator', owner])
calculator.owner = owner.key()

@Instruction
def reset_calculator(owner: Signer, calculator: Calculator):
print(owner.key(), 'is resetting a calculator', calculator.key())

assert owner.key() == calculator.owner, 'This is not your calculator!'

calculator.display = 0

@Instruction
def do_operation(owner: Signer, calculator: Calculator, op: Operation, num: i64):
assert owner.key() == calculator.owner, 'This is not your calculator!'

if op == Operation.ADD:
calculator.display += num
elif op == Operation.SUB:
calculator.display -= num
elif op == Operation.MUL:
calculator.display *= num
elif op == Operation.DIV:
calculator.display //= num'

TypeScript test

`import * as anchor from "@project-serum/anchor";
import { BN, Program, web3 } from "@project-serum/anchor";
import { Calculator } from "../target/types/calculator";
const assert = require("assert");

describe("calculator", () => {
const provider = anchor.AnchorProvider.env()
anchor.setProvider(provider);

const program = anchor.workspace.Calculator as Program;

const owner = provider.wallet.publicKey
const calculator = web3.PublicKey.findProgramAddressSync(
[Buffer.from('Calculator'), owner.toBuffer()],
program.programId
)[0]

it("Initializes a calculator", async () => {
const tx = await program.methods.initCalculator().accounts({ owner, calculator }).rpc();
});

it('Does arithmetic operations', async () => {
const add2 = await program.methods
.doOperation({ add: true }, new BN(2))
.accounts({ owner, calculator })
.instruction()

const mul3 = await program.methods
.doOperation({ mul: true}, new BN(3))
.accounts({ owner, calculator })
.instruction()

const sub1 = await program.methods
.doOperation({ sub: true }, new BN(1))
.accounts({ owner, calculator })
.instruction()

const tx = new web3.Transaction()
tx.add(add2, mul3, sub1)
await provider.sendAndConfirm(tx)

const calculatorAccount = await program.account.calculator.fetch(calculator)

assert.ok(calculatorAccount.display.toNumber() === 5)

});

it('Prevents fraudulent transactions', async () => {
let hackerman = new web3.Keypair()

let shouldFail = await program.methods
.resetCalculator()
.accounts({
  owner: hackerman.publicKey,
  calculator,
})
.instruction()

let tx = new web3.Transaction(
tx.add(shouldFail)
await provider
.sendAndConfirm(tx, [hackerman])
.then(() => assert.ok(false))
.catch(console.log)
)
})
});
`

TypeScript errors:

1-Argument of type '[{ add: boolean; }, BN]' is not assignable to parameter of type 'ArgsTuple<[{ name: "op"; type: { defined: "Operation"; }; }, { name: "num"; type: "i64"; }], DecodedHelper<[{ name: "Operation"; type: { kind: "enum"; variants: [{ name: "ADD"; }, { name: "SUB"; }, { name: "MUL"; }, { name: "DIV"; }]; }; }], EmptyDefined>>'.
Types of property '0' are incompatible.
Type '{ add: boolean; }' is not assignable to type 'DecodeEnum<{ kind: "enum"; variants: [{ name: "ADD"; }, { name: "SUB"; }, { name: "MUL"; }, { name: "DIV"; }]; }, EmptyDefined>'.
Object literal may only specify known properties, but 'add' does not exist in type 'DecodeEnum<{ kind: "enum"; variants: [{ name: "ADD"; }, { name: "SUB"; }, { name: "MUL"; }, { name: "DIV"; }]; }, EmptyDefined>'. Did you mean to write 'aDD'?

2-Argument of type '[{ mul: boolean; }, BN]' is not assignable to parameter of type 'ArgsTuple<[{ name: "op"; type: { defined: "Operation"; }; }, { name: "num"; type: "i64"; }], DecodedHelper<[{ name: "Operation"; type: { kind: "enum"; variants: [{ name: "ADD"; }, { name: "SUB"; }, { name: "MUL"; }, { name: "DIV"; }]; }; }], EmptyDefined>>'.
Types of property '0' are incompatible.
Type '{ mul: boolean; }' is not assignable to type 'DecodeEnum<{ kind: "enum"; variants: [{ name: "ADD"; }, { name: "SUB"; }, { name: "MUL"; }, { name: "DIV"; }]; }, EmptyDefined>'.
Object literal may only specify known properties, but 'mul' does not exist in type 'DecodeEnum<{ kind: "enum"; variants: [{ name: "ADD"; }, { name: "SUB"; }, { name: "MUL"; }, { name: "DIV"; }]; }, EmptyDefined>'. Did you mean to write 'mUL'?

3-Argument of type '[{ sub: boolean; }, BN]' is not assignable to parameter of type 'ArgsTuple<[{ name: "op"; type: { defined: "Operation"; }; }, { name: "num"; type: "i64"; }], DecodedHelper<[{ name: "Operation"; type: { kind: "enum"; variants: [{ name: "ADD"; }, { name: "SUB"; }, { name: "MUL"; }, { name: "DIV"; }]; }; }], EmptyDefined>>'.
Types of property '0' are incompatible.
Type '{ sub: boolean; }' is not assignable to type 'DecodeEnum<{ kind: "enum"; variants: [{ name: "ADD"; }, { name: "SUB"; }, { name: "MUL"; }, { name: "DIV"; }]; }, EmptyDefined>'.
Object literal may only specify known properties, but 'sub' does not exist in type 'DecodeEnum<{ kind: "enum"; variants: [{ name: "ADD"; }, { name: "SUB"; }, { name: "MUL"; }, { name: "DIV"; }]; }, EmptyDefined>'. Did you mean to write 'sUB'?

4-Block-scoped variable 'tx' used before its declaration.

5-',' expected.

6-Expected 0-1 arguments, but got 2.

7-Block-scoped variable 'tx' used before its declaration.

@mcintyre94
Copy link
Contributor

Hey thanks for opening this! Do you mind formatting the code with code blocks? It's hard to read ATM because of the markdown

If you use 3 backticks like this, it'll put in code blocks:

```
multi
line
code here
```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants