github.com/mavryk-network/mvgo@v1.19.9/examples/fa12/README.md (about)

     1  ## Decode FA 1.2 Transfers
     2  
     3  Use MvGo to extract FA1.2 token transfer parameters from an operation receipt. 
     4  
     5  ### Usage
     6  
     7  ```sh
     8  Usage: fa12 [args] <block> <pos>
     9  
    10    Decodes FA1.2 transfer info from transaction.
    11  
    12  Arguments
    13    -node string
    14        Tezos node URL (default "https://rpc.tzpro.io")
    15    -v  be verbose
    16  ```
    17  
    18  ### Example
    19  
    20  ```sh
    21  go run . BKuRFhvhsc3Bwdxedu6t25RmrkqpERVqEk867GAQu43muvi7j4d 17
    22  ```
    23  
    24  ### Implementation details
    25  
    26  There are different ways to accomplish this for your own contracts. Here we just use the FA1.2 spec to illustrate how the proces works.
    27  
    28  ```go
    29    // you need the contract's script for type info
    30    script, err := c.GetContractScript(ctx, tx.Destination)
    31    if err != nil {
    32      return err
    33    }
    34  
    35    // unwind params for nested entrypoints
    36    ep, prim, err := tx.Parameters.MapEntrypoint(script.ParamType())
    37    if err != nil {
    38      return err
    39    }
    40  
    41    // convert Micheline params into human-readable form
    42    val := micheline.NewValue(ep.Type(), prim)
    43  
    44    // use Value interface to access data, you have multiple options
    45    // 1/ get a decoded `map[string]interface{}`
    46    m, err := val.Map()
    47    if err != nil {
    48      return err
    49    }
    50  
    51    buf, err := json.MarshalIndent(m, "", "  ")
    52    if err != nil {
    53      return err
    54    }
    55    fmt.Println("Map=", string(buf))
    56    fmt.Printf("Value=%s %[1]T\n", m.(map[string]interface{})["transfer"].(map[string]interface{})["value"])
    57  
    58    // 2/ access individual fields (ok is true when the field exists and
    59    //    has the correct type)
    60    from, ok := val.GetAddress("transfer.from")
    61    if !ok {
    62      return fmt.Errorf("No from param")
    63    }
    64    fmt.Println("Sent from", from)
    65  
    66    // 3/ unmarshal the decoded Micheline parameters into a Go struct
    67    type FA12Transfer struct {
    68      From  mavryk.Address `json:"from"`
    69      To    mavryk.Address `json:"to"`
    70      Value mavryk.Z       `json:"value"`
    71    }
    72    type FA12TransferWrapper struct {
    73      Transfer FA12Transfer `json:"transfer"`
    74    }
    75  
    76    var transfer FA12TransferWrapper
    77    err = val.Unmarshal(&transfer)
    78    if err != nil {
    79      return err
    80    }
    81    buf, _ = json.MarshalIndent(transfer, "", "  ")
    82    fmt.Printf("FA transfer %s\n", string(buf))
    83  ```