Skip to content
Prev Previous commit
Next Next commit
replace burn to transfer
  • Loading branch information
sinev-valentine committed Sep 7, 2021
commit db9301aaeb47beab9aa8832c6ce7f4720f55baea
9 changes: 6 additions & 3 deletions evm_loader/program/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,11 @@ fn process_instruction<'a>(
let storage_info = next_account_info(account_info_iter)?;

let operator_sol_info = next_account_info(account_info_iter)?;
let operator_eth_info = next_account_info(account_info_iter)?;
let incinerator_info = next_account_info(account_info_iter)?;
let system_info = next_account_info(account_info_iter)?;

let trx_accounts = &accounts[4..];
let trx_accounts = &accounts[5..];

if !operator_sol_info.is_signer {
return Err!(ProgramError::InvalidAccountData);
Expand Down Expand Up @@ -577,9 +578,11 @@ fn process_instruction<'a>(
.checked_mul(gas_price_wei).ok_or_else(||E!(ProgramError::InvalidArgument))?;

let (caller_info, caller_token_info) = account_storage.get_caller_account_info().ok_or_else(||E!(ProgramError::InvalidArgument))?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why user token is being getting here, but not mentioned in accounts like in Continue or CallFromRawEthereumTX

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

token::burn_token(
trx_accounts,

token::transfer_token(
accounts,
caller_token_info,
operator_eth_info,
caller_info,
account_storage.get_caller_account().ok_or_else(||E!(ProgramError::InvalidArgument))?,
&fee)?;
Expand Down
49 changes: 0 additions & 49 deletions evm_loader/program/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,52 +162,3 @@ pub fn transfer_token(

Ok(())
}

/// Burn Tokens
///
/// # Errors
///
/// Could return:
/// `ProgramError::InvalidInstructionData`
pub fn burn_token(
accounts: &[AccountInfo],
source_token_account: &AccountInfo,
source_account: &AccountInfo,
source_solidity_account: &SolidityAccount,
value: &U256,
) -> Result<(), ProgramError> {
debug_print!("burn_token");
if get_token_account_owner(source_token_account)? != *source_account.key {
debug_print!("source ownership");
debug_print!("source owner {}", get_token_account_owner(source_token_account)?);
debug_print!("source key {}", source_account.key);
return Err!(ProgramError::InvalidInstructionData; "Invalid account owner")
}

let min_decimals = u32::from(eth_decimals() - token_mint::decimals());
let min_value = U256::from(10_u64.pow(min_decimals));
let value = value / min_value;
let value = u64::try_from(value).map_err(|_| E!(ProgramError::InvalidInstructionData))?;

let source_token_balance = get_token_account_balance(source_token_account)?;
if source_token_balance < value {
return Err!(ProgramError::InvalidInstructionData; "Insufficient funds on token account {} {}", source_token_account.key, source_token_balance)
}

debug_print!("Burn ETH tokens from {} value {}", source_token_account.key, value);

let instruction = spl_token::instruction::burn(
&spl_token::id(),
source_token_account.key,
&token_mint::id(),
source_account.key,
&[],
value
)?;

let (ether, nonce) = source_solidity_account.get_seeds();
let program_seeds = [&[ACCOUNT_SEED_VERSION], ether.as_bytes(), &[nonce]];
invoke_signed(&instruction, accounts, &[&program_seeds[..]])?;

Ok(())
}
9 changes: 8 additions & 1 deletion evm_loader/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def setUpClass(cls):
if getBalance(cls.caller) == 0:
print("Create caller account...")
_ = cls.loader.createEtherAccount(cls.caller_ether)
cls.token.transfer(ETH_TOKEN_MINT_ID, 2000, get_associated_token_address(PublicKey(cls.caller), ETH_TOKEN_MINT_ID))
print("Done\n")
cls.token.transfer(ETH_TOKEN_MINT_ID, 2000, cls.caller_token)

print('Account:', cls.acc.public_key(), bytes(cls.acc.public_key()).hex())
print("Caller:", cls.caller_ether.hex(), cls.caller_nonce, "->", cls.caller,
Expand Down Expand Up @@ -143,6 +143,10 @@ def sol_instr_12_cancel(self, storage_account):

# Operator address:
AccountMeta(pubkey=self.acc.public_key(), is_signer=True, is_writable=True),
# Operator ETH address (stub for now):
AccountMeta(pubkey=get_associated_token_address(self.acc.public_key(), ETH_TOKEN_MINT_ID),
is_signer=False, is_writable=True),

# Incenirator
AccountMeta(pubkey=PublicKey(incinerator), is_signer=False, is_writable=True),
# System program account:
Expand Down Expand Up @@ -407,13 +411,16 @@ def test_caseSuccessRunOtherTransactionAfterCancel(self):
storage = self.create_storage_account(sign[:8].hex())

caller_balance_before_cancel = self.token.balance(self.caller_token)
operator_balance_before_cancel = self.token.balance(get_associated_token_address(self.acc.public_key(), ETH_TOKEN_MINT_ID))

result = self.call_begin(storage, 10, msg, instruction)
result = self.call_continue(storage, 10)
result = self.call_cancel(storage)

caller_balance_after_cancel = self.token.balance(self.caller_token)
operator_balance_after_cancel = self.token.balance(get_associated_token_address(self.acc.public_key(), ETH_TOKEN_MINT_ID))
self.assertNotEqual(caller_balance_after_cancel, caller_balance_before_cancel)
self.assertEqual(caller_balance_before_cancel+operator_balance_before_cancel, caller_balance_after_cancel+operator_balance_after_cancel)

self.call_partial_signed(input)

Expand Down