Skip to content

Commit

Permalink
Fixed mint processing of both eth and erc20 tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-borodulya committed Oct 30, 2024
1 parent 2f0806a commit 014353d
Show file tree
Hide file tree
Showing 3 changed files with 1,537 additions and 18 deletions.
40 changes: 31 additions & 9 deletions refiner-lib/src/near_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ impl NearStream {

blocks
}

pub async fn next_explicit_block(&mut self, near_block: &NEARBlock) -> Vec<AuroraBlock> {
let mut blocks = vec![];
let block = self.handle_block(near_block).await;
blocks.push(block);
PROCESSED_BLOCKS.inc();
blocks
}
}

#[cfg(test)]
Expand Down Expand Up @@ -370,12 +378,12 @@ pub mod tests {
NearBlock::ExistingBlock(..)
));

// Expected values from base64 decoded args:
// Expected values from base64-decoded args:
// echo eyJzZW5kZXJfaWQiOiJhdXJvcmEiLCJhbW91bnQiOiIxMDAwMDAwMDAwMDAwMDAwMDAiLCJtc2ciOiIwYzdlMWYwM2Q2NzFiMTE4NWJlYTZmYjA2ZjExNGEwYmQ4YmJhMmY4In0=|base64 -D
// {"sender_id":"aurora","amount":"100000000000000000","msg":"0c7e1f03d671b1185bea6fb06f114a0bd8bba2f8"}%
let expected_sender = Address::zero(); // but `sender_id` is "aurora"
let expected_sender = Address::decode("4444588443c3a91288c5002483449aba1054192b").unwrap(); // `sender_id` is near_account_to_evm_address("aurora")
let expected_recipient =
Address::decode("0c7e1f03d671b1185bea6fb06f114a0bd8bba2f8").unwrap();
Address::decode("0c7e1f03d671b1185bea6fb06f114a0bd8bba2f8").unwrap(); // The recipient is the address from the args-decoded msg field
let expected_amount = Wei::new(U256::from_dec_str("100000000000000000").unwrap());

let aurora_block = aurora_blocks.first().unwrap();
Expand All @@ -397,22 +405,36 @@ pub mod tests {
let ctx = TestContext::new(&db_dir);
let mut stream = ctx.create_stream();

let near_block = read_block("tests/res/block_125229395.json");
// Read the block where the wNEAR contract is created to obtain a state that contains a key-value pair representing the wrap.near and ERC20 addresses.
let near_block_wnear_contract_create = read_block("tests/res/block_42598892.json");
let aurora_blocks = stream.next_block(&near_block_wnear_contract_create).await;
assert_eq!(aurora_blocks.len(), 1);
assert_eq!(aurora_blocks[0].height, 42598892);
assert!(matches!(
aurora_blocks[0].near_metadata,
NearBlock::ExistingBlock(..)
));

let aurora_blocks = stream.next_block(&near_block).await;
// Read the block that contains the ERC20 token mint transaction.
let near_block = read_block("tests/res/block_125229395.json");
let aurora_blocks = stream.next_explicit_block(&near_block).await;
assert_eq!(aurora_blocks.len(), 1);
assert_eq!(aurora_blocks[0].height, 125229395);
assert!(matches!(
aurora_blocks[0].near_metadata,
NearBlock::ExistingBlock(..)
));

// Expected values from base64 decoded args:
// `sender_id` is near_account_to_evm_address("aurora")
let expected_sender = Address::decode("4444588443c3a91288c5002483449aba1054192b").unwrap();

// "wrap.near" -> nep141_account_id -> aurora_engine::engine::get_erc20_from_nep141
let expected_recipient =
Address::decode("c42c30ac6cc15fac9bd938618bcaa1a1fae8501d").unwrap();

// Expected amount from base64-decoded args:
// echo eyJzZW5kZXJfaWQiOiI2NmZiMWQzZDBjOGIzODkzYjFiNTNhNGE5NjRhOGIwMzU4NmNjMGRiNWM5NjIxMDE0ZjU0ZWZiMTEwNjhiNzJlIiwiYW1vdW50IjoiMTE2MzM3NDg3MDg3NTg2NzY2ODk5NTAiLCJtc2ciOiIwZmU5NTdlNmFjYmI0ZmQ5MzVjZWU1YmEwMzNlMDAwODhkZjg2YWRiIn0=|base64 -D
// {"sender_id":"66fb1d3d0c8b3893b1b53a4a964a8b03586cc0db5c9621014f54efb11068b72e","amount":"11633748708758676689950","msg":"0fe957e6acbb4fd935cee5ba033e00088df86adb"}%
let expected_sender = Address::decode("13896015e525a44360de29d4b1b55b47c28ce746").unwrap(); // near_account_to_evm_address("wrap.near");
let expected_recipient =
Address::decode("0fe957e6acbb4fd935cee5ba033e00088df86adb").unwrap();
let expected_amount = Wei::new(U256::from_dec_str("11633748708758676689950").unwrap());

let aurora_block = aurora_blocks.first().unwrap();
Expand Down
57 changes: 48 additions & 9 deletions refiner-lib/src/refiner_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,16 +861,15 @@ fn build_transaction(
hash = virtual_receipt_id.0.into();
tx = tx.hash(hash);

println!("ft_on_transfer detected. Decoding arguments...");
if let Ok(args) = serde_json::from_slice::<NEP141FtOnTransferArgs>(&raw_input) {
let from_address = match get_token_mint_kind(
&execution_outcome.execution_outcome.outcome.logs,
) {
TokenMintKind::Eth => Address::zero(),
TokenMintKind::ERC20 => {
near_account_to_evm_address(predecessor_id.as_bytes())
}
};
let to = Address::decode(&args.msg).ok(); // msg contains destination address
let (from_address, to) = determine_ft_on_transfer_addresses(
execution_outcome,
&args,
storage,
near_block,
transaction_index,
);
let value = Wei::new(U256::from(args.amount.as_u128()));
let nonce = storage
.with_engine_access(
Expand Down Expand Up @@ -997,6 +996,46 @@ fn build_transaction(
})
}

fn determine_ft_on_transfer_addresses(
execution_outcome: &ExecutionOutcomeWithReceipt,
args: &NEP141FtOnTransferArgs,
storage: &Storage,
near_block: &BlockView,
transaction_index: u32,
) -> (Address, Option<Address>) {
// For now, we use `aurora` as the `from address` for both ETH and ERC-20 tokens.
// Later, when the engine and ethconnector split is deployed on mainnet,
// this will be changed to the address of ethconnector.
let from_address = near_account_to_evm_address(b"aurora");
let to = match get_token_mint_kind(&execution_outcome.execution_outcome.outcome.logs) {
TokenMintKind::Eth => Address::decode(&args.msg).ok(), // msg contains a destination address,
TokenMintKind::ERC20 => {
let predecessor_id = &execution_outcome.receipt.predecessor_id;
Some(
storage
.with_engine_access(
near_block.header.height,
transaction_index.try_into().unwrap_or(u16::MAX),
&[],
|io| {
let from_address_nep141 =
aurora_engine_types::account_id::AccountId::new(
predecessor_id.as_str(),
)
.unwrap_or_default();
aurora_engine::engine::get_erc20_from_nep141(&io, &from_address_nep141)
},
)
.result
.ok()
.and_then(|bytes| Address::try_from_slice(&bytes).ok())
.unwrap_or(Address::zero()),
)
}
};
(from_address, to)
}

fn fill_with_submit_result(
mut tx: AuroraTransactionBuilder,
result: SubmitResult,
Expand Down
Loading

0 comments on commit 014353d

Please sign in to comment.