how to transfer ETH in starknet contract cairo 1.0?

216 Views Asked by At

My Starknet contract uses cairo v2, but ETH tokens in starknet are deployed with cairo v0. But Cairo v0 and Cairo v2 are two very different versions in syntax and data types. So how to transfer ETH in cairo starknet contract v2

I tried using IERC20 to use ETH token but without success https://i.stack.imgur.com/lyBvJ.png

1

There are 1 best solutions below

0
shramee On

You we need to use IERC20Camel to interact with camel cased contract (which many of ERC20 contracts are).

#[starknet::interface]
trait IERC20Camel<TState> {
    fn totalSupply(self: @TState) -> u256;
    fn balanceOf(self: @TState, account: ContractAddress) -> u256;
    fn allowance(self: @TState, owner: ContractAddress, spender: ContractAddress) -> u256;
    fn transfer(ref self: TState, recipient: ContractAddress, amount: u256) -> bool;
    fn transferFrom(
        ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256
    ) -> bool;
    fn approve(ref self: TState, spender: ContractAddress, amount: u256) -> bool;
}

https://github.com/OpenZeppelin/cairo-contracts/blob/main/src/token/erc20/interface.cairo#L34C3-L34C3

Steps are like this,

  1. Create a dispatcher,
let erc20_dispatcher = IERC20Dispatcher { contract_address };
  1. transferFrom after you have approval
erc20_dispatcher.transferFrom(sender, recipient, amount);

Approval to move tokens with approve

Before you can call transferFrom (or transfer_from) your contract needs to have approval for the tokens you want to transfer. This is usually done as a multi-call. So when you ask the client to sign the transaction that eventually calls transferFrom you add in a call to approve right before it within the same transaction.