Skip to content

Commit

Permalink
#104: Make error messages consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
mvandeberg committed Aug 7, 2024
1 parent 61ac50a commit 5f21241
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
28 changes: 13 additions & 15 deletions contracts/koin/assembly/Koin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ export class Koin {
_decimals: u32 = 8;
_mana_regen_time_ms: u64 = 432000000; // 5 days

contractId: Uint8Array = System.getContractId();

supply: Storage.Obj< koin.balance_object > = new Storage.Obj(
Detail.Zone(),
SUPPLY_SPACE_ID,
Expand Down Expand Up @@ -95,8 +93,8 @@ export class Koin {
}

allowance(args: kcs4.allowance_arguments): kcs4.allowance_result {
System.require(args.owner != null, "allowance argument 'owner' cannot be null");
System.require(args.spender != null, "allowance argument 'spender' cannot be null");
System.require(args.owner != null, "account 'owner' cannot be null");
System.require(args.spender != null, "account 'spender' cannot be null");

const key = new Uint8Array(50);
key.set(args.owner, 0);
Expand All @@ -106,7 +104,7 @@ export class Koin {
}

get_allowances(args: kcs4.get_allowances_arguments): kcs4.get_allowances_result {
System.require(args.owner != null, 'owner cannot be null');
System.require(args.owner != null, "account 'owner' cannot be null");

let key = new Uint8Array(50);
key.set(args.owner, 0);
Expand Down Expand Up @@ -150,13 +148,13 @@ export class Koin {
}

transfer(args: kcs4.transfer_arguments): kcs4.transfer_result {
System.require(args.to != null, "transfer argument 'to' cannot be null");
System.require(args.from != null, "transfer argument 'from' cannot be null");
System.require(args.to != null, "account 'to' cannot be null");
System.require(args.from != null, "account 'from' cannot be null");
System.require(!Arrays.equal(args.from, args.to), 'cannot transfer to yourself');

System.require(
this._check_authority(args.from, args.value),
'from has not authorized transfer',
"account 'from' has not authorized transfer",
error.error_code.authorization_failure
);

Expand Down Expand Up @@ -187,8 +185,8 @@ export class Koin {
}

mint(args: kcs4.mint_arguments): kcs4.mint_result {
System.require(args.to != null, "mint argument 'to' cannot be null");
System.require(args.value != 0, "mint argument 'value' cannot be zero");
System.require(args.to != null, "account 'to' cannot be null");
System.require(args.value != 0, "account 'value' cannot be zero");

if (System.getCaller().caller_privilege != chain.privilege.kernel_mode) {
if (BUILD_FOR_TESTING) {
Expand Down Expand Up @@ -222,12 +220,12 @@ export class Koin {
}

burn(args: kcs4.burn_arguments): kcs4.burn_result {
System.require(args.from != null, "burn argument 'from' cannot be null");
System.require(args.from != null, "account 'from' cannot be null");

let callerData = System.getCaller();
System.require(
callerData.caller_privilege == chain.privilege.kernel_mode || this._check_authority(args.from, args.value),
'from has not authorized burn',
"account 'from' has not authorized burn",
error.error_code.authorization_failure
);

Expand Down Expand Up @@ -257,8 +255,8 @@ export class Koin {
}

approve(args: kcs4.approve_arguments): kcs4.approve_result {
System.require(args.owner != null, "approve argument 'owner' cannot be null");
System.require(args.spender != null, "approve argument 'spender' cannot be null");
System.require(args.owner != null, "account 'owner' cannot be null");
System.require(args.spender != null, "account 'spender' cannot be null");
System.requireAuthority(authority.authorization_type.contract_call, args.owner);

const key = new Uint8Array(50);
Expand All @@ -275,7 +273,7 @@ export class Koin {
return new kcs4.approve_result();
}

_check_authority(account: Uint8Array, amount: u64): boolean {
_check_authority(account: Uint8Array, amount: u64): bool {
const caller = System.getCaller().caller;
if (caller && caller.length > 0) {
let key = new Uint8Array(50);
Expand Down
5 changes: 2 additions & 3 deletions contracts/koin/assembly/__tests__/koin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Base58, MockVM, authority, Arrays, chain, Protobuf, System, kcs4, protocol, system_calls } from "@koinos/sdk-as";
import { koin } from "../proto/koin";
import { Koin } from "../Koin";

const CONTRACT_ID = Base58.decode("1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe");
Expand Down Expand Up @@ -134,7 +133,7 @@ describe("koin", () => {
}).toThrow();

// check error message
expect(MockVM.getErrorMessage()).toBe("from has not authorized burn");
expect(MockVM.getErrorMessage()).toBe("account 'from' has not authorized burn");

// check balance
balanceArgs = new kcs4.balance_of_arguments(MOCK_ACCT1);
Expand Down Expand Up @@ -538,7 +537,7 @@ describe("koin", () => {
koinContract.transfer(new kcs4.transfer_arguments(MOCK_ACCT1, MOCK_ACCT2, 10));
}).toThrow();

expect(MockVM.getErrorMessage()).toBe("from has not authorized transfer");
expect(MockVM.getErrorMessage()).toBe("account 'from' has not authorized transfer");

// create allowance for 20 tokens
MockVM.setCaller(new chain.caller_data(new Uint8Array(0), chain.privilege.kernel_mode));
Expand Down

0 comments on commit 5f21241

Please sign in to comment.