Skip to content

Commit

Permalink
#104: Fix event names in Koin contract, update tests, and update ABI
Browse files Browse the repository at this point in the history
  • Loading branch information
mvandeberg committed Aug 6, 2024
1 parent cef2aa7 commit f5fce67
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 47 deletions.
13 changes: 3 additions & 10 deletions contracts/koin/abi/vhp.abi → contracts/koin/abi/koin.abi
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,14 @@
"description" : "Checks the balance at an address",
"read-only" : true
},
"effective_balance_of": {
"argument" : "koinos.contracts.vhp.effective_balance_of_arguments",
"return" : "koinos.contracts.vhp.effective_balance_of_result",
"entry-point" : "0x629f31e6",
"description" : "Checks the effective balance at an address",
"read-only" : true
},
"allownace": {
"allowance": {
"argument" : "koinos.standards.kcs4.allowance_arguments",
"return" : "koinos.standards.kcs4.allowance_result",
"entry-point" : "0x32f09fa1",
"description" : "Checks the allowance for a owner and spender",
"read-only" : true
},
"get_allownaces": {
"get_allowances": {
"argument" : "koinos.standards.kcs4.get_allowances_arguments",
"return" : "koinos.standards.kcs4.get_allowances_result",
"entry-point" : "0x8fa16456",
Expand Down Expand Up @@ -92,5 +85,5 @@
"read-only" : false
}
},
"types" : "CrcOChtrb2lub3Mvc3RhbmRhcmRzL2tjczQucHJvdG8SFWtvaW5vcy5zdGFuZGFyZHMua2NzNBoUa29pbm9zL29wdGlvbnMucHJvdG8iEAoObmFtZV9hcmd1bWVudHMiIwoLbmFtZV9yZXN1bHQSFAoFdmFsdWUYASABKAlSBXZhbHVlIhIKEHN5bWJvbF9hcmd1bWVudHMiJQoNc3ltYm9sX3Jlc3VsdBIUCgV2YWx1ZRgBIAEoCVIFdmFsdWUiFAoSZGVjaW1hbHNfYXJndW1lbnRzIicKD2RlY2ltYWxzX3Jlc3VsdBIUCgV2YWx1ZRgBIAEoDVIFdmFsdWUiGAoWdG90YWxfc3VwcGx5X2FyZ3VtZW50cyIvChN0b3RhbF9zdXBwbHlfcmVzdWx0EhgKBXZhbHVlGAEg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oNYXBwcm92ZV9ldmVudBIaCgVvd25lchgBIAEoDEIEgLUYBlIFb3duZXISHgoHc3BlbmRlchgCIAEoDEIEgLUYBlIHc3BlbmRlchIYCgV2YWx1ZRgDIAEoBEICMAFSBXZhbHVlQkBaPmdpdGh1Yi5jb20va29pbm9zL2tvaW5vcy1wcm90by1nb2xhbmcvdjIva29pbm9zL3N0YW5kYXJkcy9rY3M0YgZwcm90bzM=="
}
32 changes: 4 additions & 28 deletions contracts/koin/assembly/Koin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,6 @@
import { Arrays, authority, chain, error, kcs4, Protobuf, Storage, System, system_calls, u128 } from "@koinos/sdk-as";
import { koin } from "./proto/koin";

/**
* To prevent exploiting the PoB algorithm, the VHP has a "delayed" transfer system built in.
* Rather, this system does not delay property rights transferral, but delays the VHP from
* being counted towards an address for block production. Increases the balance are delayed
* while decreases are instantaneous.
*
* Calculating and storing this information uses rolling snapshots. This approach only works
* because we are only interested in a short period of history (20 blocks). Each balance has a
* list of previous snapshots by block height and balance. When we increase the balance, we
* store the new balance as a snapshot with the current head block height. When we decrease
* the balance on all snapshots.
*
* We also need to clean up the snapshots. We want at most one snapshot earlier than 20 blocks.
* So we trim all but the most recent snapshot older than 20 blocks. Then, when we want to
* retrieve the effective balance, we just have to find the most recent snapshot older than
* 20 blocks.
*
* All balance objects will "decay" back to the current balance and the snapshot equal to the
* current balance.
*
* The trick to making the algorithms on the snapshots efficient is that new snapshots are
* pushed to the back of the array and old snapshots are removed from the front.
*/

const SUPPLY_SPACE_ID = 0;
const BALANCES_SPACE_ID = 1;
const ALLOWANCES_SPACE_ID = 2;
Expand Down Expand Up @@ -202,7 +178,7 @@ export class Koin {
this.balances.put(args.to, toBalance);

System.event(
'koinos.contracts.kcs4.transfer_event',
'token.transfer_event',
Protobuf.encode(new kcs4.transfer_event(args.from, args.to, args.value, args.memo), kcs4.transfer_event.encode),
[args.to, args.from]
);
Expand Down Expand Up @@ -237,7 +213,7 @@ export class Koin {
this.balances.put(args.to, balance);

System.event(
'koinos.contracts.kcs4.mint_event',
'token.mint_event',
Protobuf.encode(new kcs4.mint_event(args.to, args.value), kcs4.mint_event.encode),
[args.to]
);
Expand Down Expand Up @@ -272,7 +248,7 @@ export class Koin {
this.balances.put(args.from, fromBalance);

System.event(
'koinos.contracts.kcs4.burn_event',
'token.burn_event',
Protobuf.encode(new kcs4.burn_event(args.from, args.value), kcs4.burn_event.encode),
[args.from]
);
Expand All @@ -291,7 +267,7 @@ export class Koin {
this.allowances.put(key, new koin.balance_object(args.value));

System.event(
"koinos.contracts.kcs4.approve",
"token.approve_event",
Protobuf.encode(new kcs4.approve_event(args.owner, args.spender, args.value), kcs4.approve_event.encode),
[args.owner, args.spender]
);
Expand Down
16 changes: 8 additions & 8 deletions contracts/koin/assembly/__tests__/koin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ describe("koin", () => {
// check events
const events = MockVM.getEvents();
expect(events.length).toBe(2);
expect(events[0].name).toBe('koinos.contracts.kcs4.mint_event');
expect(events[0].name).toBe('token.mint_event');
expect(events[0].impacted.length).toBe(1);
expect(Arrays.equal(events[0].impacted[0], MOCK_ACCT1)).toBe(true);
expect(events[1].name).toBe('koinos.contracts.kcs4.burn_event');
expect(events[1].name).toBe('token.burn_event');
expect(events[1].impacted.length).toBe(1);
expect(Arrays.equal(events[1].impacted[0], MOCK_ACCT1)).toBe(true);

Expand Down Expand Up @@ -163,7 +163,7 @@ describe("koin", () => {
// check events
const events = MockVM.getEvents();
expect(events.length).toBe(1);
expect(events[0].name).toBe('koinos.contracts.kcs4.mint_event');
expect(events[0].name).toBe('token.mint_event');
expect(events[0].impacted.length).toBe(1);
expect(Arrays.equal(events[0].impacted[0], MOCK_ACCT1)).toBe(true);

Expand Down Expand Up @@ -283,7 +283,7 @@ describe("koin", () => {
const events = MockVM.getEvents();
// 2 events, 1st one is the mint event, the second one is the transfer event
expect(events.length).toBe(2);
expect(events[1].name).toBe('koinos.contracts.kcs4.transfer_event');
expect(events[1].name).toBe('token.transfer_event');
expect(events[1].impacted.length).toBe(2);
expect(Arrays.equal(events[1].impacted[0], MOCK_ACCT2)).toBe(true);
expect(Arrays.equal(events[1].impacted[1], MOCK_ACCT1)).toBe(true);
Expand Down Expand Up @@ -436,7 +436,7 @@ describe("koin", () => {
const events = MockVM.getEvents();
// 2 events, 1st one is the mint event, the second one is the transfer event
expect(events.length).toBe(2);
expect(events[1].name).toBe('koinos.contracts.kcs4.transfer_event');
expect(events[1].name).toBe('token.transfer_event');
expect(events[1].impacted.length).toBe(2);
expect(Arrays.equal(events[1].impacted[0], MOCK_ACCT2)).toBe(true);
expect(Arrays.equal(events[1].impacted[1], MOCK_ACCT1)).toBe(true);
Expand Down Expand Up @@ -471,17 +471,17 @@ describe("koin", () => {
// check events
const events = MockVM.getEvents();
expect(events.length).toBe(3);
expect(events[0].name).toBe('koinos.contracts.kcs4.approve');
expect(events[0].name).toBe('token.approve_event');
expect(events[0].impacted.length).toBe(2);
expect(Arrays.equal(events[0].impacted[0], MOCK_ACCT1)).toBe(true);
expect(Arrays.equal(events[0].impacted[1], MOCK_ACCT2)).toBe(true);

expect(events[1].name).toBe('koinos.contracts.kcs4.approve');
expect(events[1].name).toBe('token.approve_event');
expect(events[1].impacted.length).toBe(2);
expect(Arrays.equal(events[1].impacted[0], MOCK_ACCT1)).toBe(true);
expect(Arrays.equal(events[1].impacted[1], MOCK_ACCT3)).toBe(true);

expect(events[2].name).toBe('koinos.contracts.kcs4.approve');
expect(events[2].name).toBe('token.approve_event');
expect(events[2].impacted.length).toBe(2);
expect(Arrays.equal(events[2].impacted[0], MOCK_ACCT2)).toBe(true);
expect(Arrays.equal(events[2].impacted[1], MOCK_ACCT3)).toBe(true);
Expand Down
28 changes: 27 additions & 1 deletion contracts/koin/assembly/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { System, Protobuf, authority, kcs4} from "@koinos/sdk-as";
import { System, Protobuf, authority, kcs4, system_calls} from "@koinos/sdk-as";
import { Koin as ContractClass } from "./Koin";

export function main(): i32 {
Expand All @@ -8,6 +8,32 @@ export function main(): i32 {
const c = new ContractClass();

switch (contractArgs.entry_point) {
case 0x2d464aab: {
const args = Protobuf.decode<system_calls.get_account_rc_arguments>(
contractArgs.args,
system_calls.get_account_rc_arguments.decode
);
const res = c.get_account_rc(args);
retbuf = Protobuf.encode(
res,
system_calls.get_account_rc_result.encode
);
break;
}

case 0x80e3f5c9: {
const args = Protobuf.decode<system_calls.consume_account_rc_arguments>(
contractArgs.args,
system_calls.consume_account_rc_arguments.decode
);
const res = c.consume_account_rc(args);
retbuf = Protobuf.encode(
res,
system_calls.consume_account_rc_result.encode
);
break;
}

case 0x82a3537f: {
const res = c.name();
retbuf = Protobuf.encode(
Expand Down

0 comments on commit f5fce67

Please sign in to comment.