You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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");
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()
});
it('Prevents fraudulent transactions', async () => {
let hackerman = new web3.Keypair()
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.
The text was updated successfully, but these errors were encountered: