# DApp(APLink)-Unity对接示例

## 相关 <a href="#h1-u76f8u5173" id="h1-u76f8u5173"></a>

[unity端工具](https://github.com/GetScatter/eos-sharp)

## 网络 <a href="#wang-luo" id="wang-luo"></a>

```
export const network = {
  blockchain: 'amax',
  expireInSeconds: 600,
  host: isProd ? 'expnode.amaxscan.io' : 'test-chain.ambt.art',
  // port: 80, // ( or null if defaulting to 80 )
  chainId: isProd
    ? '2403d6f602a87977f898aa3c62c79a760f458745904a15b3cd63a106f62adc16'
    : '208dacab3cd2e181c86841613cf05d9c60786c677e4ce86b266d0a58884968f7', // Or null to fetch automatically ( takes longer )
  protocol: 'https',
};
// 线上： expnode.amaxscan.io
// 测试： test-chain.ambt.art
```

## 查询合约 <a href="#cha-xun-he-yue" id="cha-xun-he-yue"></a>

接口：[get\_table\_rows](https://developers.eos.io/manuals/eos/latest/nodeos/plugins/chain_api_plugin/api-reference/index#operation/get_table_rows)

参数如下：

```
{
    "code": "nftone.mart", // 合约名称
    "scope": "nftone.mart", // 范围，一般都是和合约名称一样，如果特殊，合约开发者也会告知
    "table": "global", // 查询表
    "json": true, 
    "limit": 1 // 数量
    // 其它参数用到可以看上面链接
}
```

返回值

```
{
    "rows": [ // 这里就是返回的数据，一行一行。
        {
            "admin": "ad",
            "dev_fee_collector": "user1",
            "dev_fee_rate": "0.10000000000000001",
            "creator_fee_rate": "0.00000000000000000",
            "ipowner_fee_rate": "0.10000000000000001",
            "notary_fee_rate": "0.00000000000000000",
            "order_expiry_hours": 72,
            "pay_symbol": "6,MUSDT",
            "pay_contract": "amax.mtoken",
            "apl_farm": {
                "contract": "aplink.farm",
                "lease_id": 5,
                "unit_reward": "0.0500 APL"
            },
            "last_buy_order_idx": 42,
            "last_deal_idx": 696
        }
    ],
    "more": false,
    "next_key": ""
}
```

## 交易生成 <a href="#jiao-yi-sheng-cheng" id="jiao-yi-sheng-cheng"></a>

Transaction 如下：\
[https://www.cnblogs.com/Evsward/p/trx.html](https://www.cnblogs.com/Evsward/p/trx.html)

expiration & ref\_block\_num & ref\_block\_prefix，通过get\_info，get\_block等接口拿到的信息再经过计算而得到。具体计算方法得看相应文档。

如果unity没有相应库来生成，那么只需要传`actions`部分，然后由aplink生成`Transaction`，但这样明显不太好（需要aplink作一些非标准化处理，这样代码多了aplink也不好维护）。最好还是找个c++的库。

```
{
    "expiration": "2022-11-28T10:08:26", // 过期时间，一般1-10分钟左右
    "ref_block_num": 18539,  // get_info的last_irreversible_block_num - X 所得，也就是区块小于最后区块
    "ref_block_prefix": 1564216872, // 用上面的ref_block_num通过get_block的id has计算所得
    "max_net_usage_words": 0,
    "max_cpu_usage_ms": 0,
    "delay_sec": 0,
    "context_free_actions": [],
    "actions": [
        {
            "account": "amax.mtoken", // 合约名
            "name": "transfer", // 合约方法
            "authorization": [ // 授权用户
                {
                    "actor": "testuser1", // 帐号
                    "permission": "active" // 权限
                }
            ],
            "data": "00000857619db1ca00f23512a849f39a40420f0000000000064d555344540000143632353037393938333a33383a31303030303030" // 参数，经过get_abi序列化所得，可以明文传递
        }
    ],
    "transaction_extensions": []
}
```

把这写数据发给aplink签名，然后aplink会给出反馈

成功如下返回：

```
{
    "transaction_id": "e7b58481788638f21f9c8614726e4e0375b48de4669379c1a5c83ec279c9e8d6",
    "processed": {
        "id": "e7b58481788638f21f9c8614726e4e0375b48de4669379c1a5c83ec279c9e8d6",
        "block_num": 1919250,
        "block_time": "2022-11-28T10:05:10.000",
        "producer_block_id": null,
        "receipt": {
            "status": "executed",
            "cpu_usage_us": 522,
            "net_usage_words": 19
        },
        "elapsed": 522,
        "net_usage": 152,
        "scheduled": false,
        "action_traces": [
            {
                "action_ordinal": 1,
                "creator_action_ordinal": 0,
                "closest_unnotified_ancestor_action_ordinal": 0,
                "receipt": {
                    "receiver": "amax.mtoken",
                    "act_digest": "6db3ec0f3b12eda41f6ada77a6c01bc607f878b43edef025e8c62b75ce0c2e4a",
                    "global_sequence": 1966781,
                    "recv_sequence": 3057,
                    "auth_sequence": [
                        [
                            "testuser1",
                            192
                        ]
                    ],
                    "code_sequence": 1,
                    "abi_sequence": 1
                },
                "receiver": "amax.mtoken",
                "act": {
                    "account": "amax.mtoken",
                    "name": "transfer",
                    "authorization": [
                        {
                            "actor": "testuser1",
                            "permission": "active"
                        }
                    ],
                    "data": {
                        "from": "testuser1",
                        "to": "nftone.mart",
                        "quantity": "1.000000 MUSDT",
                        "memo": "625079983:38:1000000"
                    },
                    "hex_data": "00000857619db1ca00f23512a849f39a40420f0000000000064d555344540000143632353037393938333a33383a31303030303030"
                },
                "context_free": false,
                "elapsed": 90,
                "console": "",
                "trx_id": "e7b58481788638f21f9c8614726e4e0375b48de4669379c1a5c83ec279c9e8d6",
                "block_num": 1919250,
                "block_time": "2022-11-28T10:05:10.000",
                "producer_block_id": null,
                "account_ram_deltas": [],
                "except": null,
                "error_code": null,
                "inline_traces": [
                    {
                        "action_ordinal": 2,
                        "creator_action_ordinal": 1,
                        "closest_unnotified_ancestor_action_ordinal": 1,
                        "receipt": {
                            "receiver": "testuser1",
                            "act_digest": "6db3ec0f3b12eda41f6ada77a6c01bc607f878b43edef025e8c62b75ce0c2e4a",
                            "global_sequence": 1966782,
                            "recv_sequence": 256,
                            "auth_sequence": [
                                [
                                    "testuser1",
                                    193
                                ]
                            ],
                            "code_sequence": 1,
                            "abi_sequence": 1
                        },
                        "receiver": "testuser1",
                        "act": {
                            "account": "amax.mtoken",
                            "name": "transfer",
                            "authorization": [
                                {
                                    "actor": "testuser1",
                                    "permission": "active"
                                }
                            ],
                            "data": {
                                "from": "testuser1",
                                "to": "nftone.mart",
                                "quantity": "1.000000 MUSDT",
                                "memo": "625079983:38:1000000"
                            },
                            "hex_data": "00000857619db1ca00f23512a849f39a40420f0000000000064d555344540000143632353037393938333a33383a31303030303030"
                        },
                        "context_free": false,
                        "elapsed": 3,
                        "console": "",
                        "trx_id": "e7b58481788638f21f9c8614726e4e0375b48de4669379c1a5c83ec279c9e8d6",
                        "block_num": 1919250,
                        "block_time": "2022-11-28T10:05:10.000",
                        "producer_block_id": null,
                        "account_ram_deltas": [],
                        "except": null,
                        "error_code": null,
                        "inline_traces": []
                    },
                    {
                        "action_ordinal": 3,
                        "creator_action_ordinal": 1,
                        "closest_unnotified_ancestor_action_ordinal": 1,
                        "receipt": {
                            "receiver": "nftone.mart",
                            "act_digest": "6db3ec0f3b12eda41f6ada77a6c01bc607f878b43edef025e8c62b75ce0c2e4a",
                            "global_sequence": 1966783,
                            "recv_sequence": 1551,
                            "auth_sequence": [
                                [
                                    "testuser1",
                                    194
                                ]
                            ],
                            "code_sequence": 1,
                            "abi_sequence": 1
                        },
                        "receiver": "nftone.mart",
                        "act": {
                            "account": "amax.mtoken",
                            "name": "transfer",
                            "authorization": [
                                {
                                    "actor": "testuser1",
                                    "permission": "active"
                                }
                            ],
                            "data": {
                                "from": "testuser1",
                                "to": "nftone.mart",
                                "quantity": "1.000000 MUSDT",
                                "memo": "625079983:38:1000000"
                            },
                            "hex_data": "00000857619db1ca00f23512a849f39a40420f0000000000064d555344540000143632353037393938333a33383a31303030303030"
                        },
                        "context_free": false,
                        "elapsed": 203,
                        "console": "",
                        "trx_id": "e7b58481788638f21f9c8614726e4e0375b48de4669379c1a5c83ec279c9e8d6",
                        "block_num": 1919250,
                        "block_time": "2022-11-28T10:05:10.000",
                        "producer_block_id": null,
                        "account_ram_deltas": [
                            {
                                "account": "nftone.mart",
                                "delta": 596
                            }
                        ],
                        "except": null,
                        "error_code": null,
                        "inline_traces": []
                    }
                ]
            }
        ],
        "account_ram_delta": null,
        "except": null,
        "error_code": null
    }
}
```
