首页 > 解决方案 > 可以将 IOS 应用程序与 ERC20 令牌集成

问题描述

此时可以将 IOS 应用程序与以太坊网络上的 ERC20 令牌集成。

似乎有一个名为web3.swift的库允许与以太坊集成。也许这是要走的路,但不知道它是否已准备好用于生产应用程序。

此外,似乎还有一些关于 Swift 和区块链的在线课程,例如Lynda这一门和 Udemy 的这一门,以及AppCoda 的这一门使用Tierion 区块链即服务的这一门关于区块链集成的一些教程。事实上,AWS、Azure 等似乎都提供区块链即服务。亚马逊提供带有以太坊的托管区块链。

但是,我还没有看到任何专门解决如何将 IOS 应用程序与以太坊集成的内容。如果它是由 AWS 在后端完成的,这是否违背了使用区块链来避免集中在服务器上的目的?

我正在研究的用例是查看您拥有的代币数量,并使用户能够在服务上使用代币。然而,代币将驻留在区块链上。

感谢您就现阶段是否有可能提供任何建议,如果可以,如何进行开发。

标签: iosswiftethereumerc20

解决方案


新交易

Infura服务在这里可能会有所帮助。创建帐户并设置项目后 - 您将获得可以在特定以太坊网络上执行写入操作的节点(用于主网和一些测试网)。

以下是用 Swift 和Web3.swift编写的代码,可能会有所帮助:

func send(sender: String,
          receiver: String,
          senderSecret: String,
          tokenContractAddress: String,
          amount: Int,
          completion: @escaping Result<Transaction, Error>) {

    let web3 = Web3(rpcURL: "YOUR_INFURA_NODE_ID_GOES_HERE")

    do {
        let privateKey = try EthereumPrivateKey(hexPrivateKey: senderSecret)

        let senderWallet = try EthereumAddress(hex: sender, eip55: true)

        let receiverWallet = try EthereumAddress(hex: receiver, eip55: true)

        let contract = web3.eth.Contract(
            type: GenericERC20Contract.self,
            address: try EthereumAddress(hex: tokenContractAddress, eip55: true)
        )

        firstly {
            return web3.eth.getTransactionCount(address: privateKey.address, block: .latest)
        }.compactMap { nonce in
            guard let tx = contract
                .transfer(to: receiverWallet, value: BigUInt(amount))
                .createTransaction(
                    nonce: nonce,
                    from: senderWallet,
                    value: 0,
                    gas: 100000,
                    gasPrice: EthereumQuantity(quantity: 21.gwei)
                ) else { return nil }

            return try tx.sign(with: privateKey, chainId: 3)
        }.then { tx in
            return web3.eth.sendRawTransaction(transaction: tx!)
        }.done { hash in
            let tx = Transaction(hash: hash)
            completion.set(.success(tx))
        }.catch { error in
            completion.set(.failure(error))
        }
    } catch {
        completion.set(.failure(error))
    }
}

公开资料

如果不需要启动交易,而您只想使用代币供应/持有人余额/等公共信息。- 你可以尝试一些开放的 API,比如blockscout来获取你需要的数据。


推荐阅读