Skip to content

Commit

Permalink
EVM: DST20 v2 burn of sent amount (#2911)
Browse files Browse the repository at this point in the history
* Correctly burn amount on original contract

* Add tests for burn event
  • Loading branch information
Jouzo authored May 7, 2024
1 parent 3d6663d commit 6ec7477
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 59 deletions.
1 change: 1 addition & 0 deletions lib/ain-contracts/dst20_v2/DST20V2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ contract ERC20 is Context, IERC20, IERC20Metadata, IDST20Upgradeable {

// Upgrade available
if (newAmount != amount) {
_burn(msg.sender, amount);
IERC20(newTokenContractAddress).transfer(msg.sender, newAmount);
}

Expand Down
59 changes: 1 addition & 58 deletions lib/ain-evm/src/precompiles/token_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,9 @@ impl DVMStatePrecompile for TokenSplit {
reset_storage: false,
};

let Ok(storage) =
get_original_contract_storage_update(handle, sender, original_contract, input_amount)
else {
return Err(PrecompileFailure::Error {
exit_status: ExitError::Other("Error getting storage update".into()),
});
};

let original_contract_state_changes = Apply::Modify {
address: original_contract,
basic: handle.basic(original_contract),
code: None,
storage,
reset_storage: false,
};

Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
state_changes: Some(vec![
original_contract_state_changes,
new_contract_state_changes,
]),
state_changes: Some(vec![new_contract_state_changes]),
output,
})
}
Expand Down Expand Up @@ -196,41 +177,3 @@ fn get_new_contract_storage_update(
(total_supply_index, u256_to_h256(new_total_supply)),
]))
}

fn get_original_contract_storage_update(
handle: &mut impl PrecompileHandle,
sender: H160,
contract: H160,
amount: U256,
) -> Result<BTreeMap<H256, H256>> {
let contract_balance_storage_index = get_address_storage_index(H256::zero(), sender);
let sender_balance = U256::from(
handle
.storage(contract, contract_balance_storage_index)
.as_bytes(),
);

debug!("sender_balance : {}", sender_balance);

let new_sender_balance = sender_balance
.checked_sub(amount)
.ok_or_else(|| format_err!("Total supply overflow/underflow"))?;
debug!("new_sender_balance : {:x}", new_sender_balance);

let total_supply_index = H256::from_low_u64_be(2);
let total_supply = U256::from(handle.storage(contract, total_supply_index).as_bytes());

let new_total_supply = total_supply
.checked_sub(amount)
.ok_or_else(|| format_err!("Total supply overflow/underflow"))?;

debug!("new_total_supply : {:x}", new_total_supply);

Ok(BTreeMap::from([
(
contract_balance_storage_index,
u256_to_h256(new_sender_balance),
),
(total_supply_index, u256_to_h256(new_total_supply)),
]))
}
15 changes: 14 additions & 1 deletion test/functional/feature_evm_token_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ def setup_variables(self):
self.evm_address = self.nodes[0].getnewaddress("", "erc55")
self.evm_privkey = self.nodes[0].dumpprivkey(self.evm_address)

self.burn_address = self.nodes[0].w3.to_checksum_address(
"0x0000000000000000000000000000000000000000"
)

self.contract_address_metav1 = self.nodes[0].w3.to_checksum_address(
"0xff00000000000000000000000000000000000001"
)
Expand Down Expand Up @@ -769,10 +773,19 @@ def execute_split_transaction(
address=destination_contract, abi=self.dst20_v2_abi
)

# Check transfer token logs on new contract
# Check transfer from sender to burn address
events = meta_contract_new.events.Transfer().process_log(
list(tx_receipt["logs"])[1]
)

assert_equal(events["event"], "Transfer")
assert_equal(events["args"]["to"], self.burn_address)
assert_equal(events["args"]["value"], amount_to_send)

# Check transfer token logs on new contract
events = meta_contract_new.events.Transfer().process_log(
list(tx_receipt["logs"])[2]
)
assert_equal(events["event"], "Transfer")
assert_equal(events["args"]["to"], self.evm_address)
assert_equal(events["args"]["value"], amount_to_receive)
Expand Down

0 comments on commit 6ec7477

Please sign in to comment.