Skip to content

Commit

Permalink
fix: missing RawTransfer event emission (#77)
Browse files Browse the repository at this point in the history
The `RawTransfer` event that's meant to enable offchain raw balance
tracking was incorrectly not emitted in certain cases, defeating its own
purpose. This commit fixes it.

Note that since it's impossible to apply the fix retrospectively,
users before this fix is deployed would have broken history. A separate
mechanism is needed to somehow "snapshot" raw balances of old users to
allow offchain indexing applications to reconcile accordingly.
  • Loading branch information
xJonathanLEI authored Apr 12, 2024
1 parent c31ff97 commit 76f43d9
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions src/z_token/external.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,23 @@ fn mint(ref self: ContractState, to: ContractAddress, amount: felt252) -> bool {
let raw_supply_after = safe_math::add(raw_supply_before, scaled_down_amount);
self.raw_total_supply.write(raw_supply_after);

let amount: u256 = amount.into();
let amount_u256: u256 = amount.into();
self
.emit(
contract::Event::Transfer(
contract::Transfer { from: contract_address_const::<0>(), to, value: amount }
contract::Transfer { from: contract_address_const::<0>(), to, value: amount_u256 }
)
);
self
.emit(
contract::Event::RawTransfer(
contract::RawTransfer {
from: contract_address_const::<0>(),
to,
raw_value: scaled_down_amount,
accumulator: accumulator,
face_value: amount
}
)
);

Expand All @@ -227,11 +239,25 @@ fn burn(ref self: ContractState, user: ContractAddress, amount: felt252) {
let raw_supply_after = safe_math::sub(raw_supply_before, scaled_down_amount);
self.raw_total_supply.write(raw_supply_after);

let amount: u256 = amount.into();
let amount_256: u256 = amount.into();
self
.emit(
contract::Event::Transfer(
contract::Transfer { from: user, to: contract_address_const::<0>(), value: amount }
contract::Transfer {
from: user, to: contract_address_const::<0>(), value: amount_256
}
)
);
self
.emit(
contract::Event::RawTransfer(
contract::RawTransfer {
from: user,
to: contract_address_const::<0>(),
raw_value: scaled_down_amount,
accumulator: accumulator,
face_value: amount
}
)
);
}
Expand Down Expand Up @@ -259,6 +285,18 @@ fn burn_all(ref self: ContractState, user: ContractAddress) -> felt252 {
}
)
);
self
.emit(
contract::Event::RawTransfer(
contract::RawTransfer {
from: user,
to: contract_address_const::<0>(),
raw_value: raw_balance,
accumulator: accumulator,
face_value: scaled_up_amount
}
)
);

scaled_up_amount
}
Expand Down

0 comments on commit 76f43d9

Please sign in to comment.