How should Sophia List.Foreach be used

Hi, everyone
I used list.foreach when I wrote Sophia
However, the contents of the loop are converted by map.tolist, which should be syntactically incorrect at the time of calling. I don’t know how to use it.
The requirement is basically to migrate the entire AEX9 token information to the new contract, and filter out individual addresses and not send it.

But even if the call succeeds, how do I make an if judgment inside the for loop?

This is the specific code, please help me look at

contract FungibleTokenInterface =

  record allowance_accounts = { from_account : address, for_account : address }

  entrypoint total_supply                  : ()                      => int
  entrypoint balance                       : (address)               => option(int)
  entrypoint allowance                     : (allowance_accounts)    => option(int)
  entrypoint balances                      : ()                      => map(address, int) 
  stateful entrypoint transfer_allowance   : (address, address, int) => unit
  stateful entrypoint create_allowance     : (address, int)          => unit
  stateful entrypoint change_allowance     : (address, int)          => unit
  stateful entrypoint reset_allowance      : (address)               => unit
  stateful entrypoint transfer             : (address, int)          => unit


contract MigrationTokens =

    record state = { init_data : map(string, hash) }

    stateful entrypoint init() = { init_data = {}}

entrypoint migration(old_token : FungibleTokenInterface,new_token : FungibleTokenInterface) = 
    let items = Map.to_list(old_token.balances())
    List.foreach(items, (addr * count) => new_token.transfer(addr, count))

@hanssv.chain
@hanssv2
Dude, I’m sorry I had to turn to you again

I think it might be easier to see how it works if we introduce a small helper function:

include "List.aes"

contract FungibleTokenInterface =

  record allowance_accounts = { from_account : address, for_account : address }

  entrypoint total_supply                  : ()                      => int
  entrypoint balance                       : (address)               => option(int)
  entrypoint allowance                     : (allowance_accounts)    => option(int)
  entrypoint balances                      : ()                      => map(address, int)
  stateful entrypoint transfer_allowance   : (address, address, int) => unit
  stateful entrypoint create_allowance     : (address, int)          => unit
  stateful entrypoint change_allowance     : (address, int)          => unit
  stateful entrypoint reset_allowance      : (address)               => unit
  stateful entrypoint transfer             : (address, int)          => unit


contract MigrationTokens =

  record state = { init_data : map(string, hash) }

  stateful entrypoint init() = { init_data = {}}

  entrypoint migration(old_token : FungibleTokenInterface, new_token : FungibleTokenInterface) =
    let items : list(address * int) = Map.to_list(old_token.balances())
    List.foreach(items, (item) => trf(item, new_token))

  function
    trf : ((address * int), FungibleTokenInterface) => unit
    trf((a, c), fti) = fti.transfer(a, c)

Possibly you could make it shorter, but I didn’t think long and hard about it :slight_smile:

2 Likes