ameliatastic / seahorse-lang

Write Anchor-compatible Solana programs in Python
Apache License 2.0
320 stars 46 forks source link

Erroneous calculator test code #95

Open Killpit opened 1 year ago

Killpit commented 1 year ago

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 commented 1 year ago

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 ```