Web3 Swift Call Contract using Skywinder wrapper

462 Views Asked by At

I am trying to call a contract via web3 using sky winder wrapper.

What I am trying to do is call a contract where I pass x amount of token coins and pass contract address and token address. However it's failing.

the Abi contract comes from web server which gives me the json string for the Abi contract.

It's failing on the call with NodeError

Just to explain what I am doing here is we have coins on a token on the ether wallet. Our Abi contract exchanges those token coins to another coin which is not on the ether blockchain.

This is why our value is 0 for ether but we pass our 3 parameters of approveAndCall(address _spender, uint256 _amount, string _destination)

Which is our API function

Our ABI functions work fine on web but im struggling to get this running on iOS. Any help would be most helpful.

var web3 = Web3.InfuraMainnetWeb3()
    if AppDataModelManager.shared.testModeStatus() {
        
        web3 = Web3.InfuraRinkebyWeb3()
    }

    let abiContract = self.getDepositABIContract(completion: { abiJson in
    
        let value: String = "0" // Any amount of Ether you need to send
        let walletAddress = EthereumAddress(wallet.address)! // Your wallet address
        let contractMethod = "approveAndCall" // Contract method you want to write
        let contractABI = abiJson
        let contractAddress = EthereumAddress(AppDataModelManager.shared.getNetworkStatus().getDepositTokenAddress())!
        let abiVersion = 2 // Contract ABI version
        let parameters: [Any] = [walletAddress, UInt64(amountString) ?? 0,toAddress] // Parameters for contract method
        let extraData: Data = Data() // Extra data for contract method
        let contract = web3.contract(contractABI ?? "", at: contractAddress, abiVersion: abiVersion)!
        var options = TransactionOptions.defaultOptions
        options.value = Web3.Utils.parseToBigUInt(value, units: .eth)
        options.from = walletAddress
        options.gasPrice = .automatic
        options.gasLimit = .automatic

        let txMessage = contract.write(
            contractMethod,
            parameters:  parameters as [AnyObject],
            extraData: extraData,
            transactionOptions: options)!
        
        DispatchQueue.global(qos: .userInitiated).async {
            
            do {
                try txMessage.call()
                
            } catch let error as NSError {
                
                print("Failed to load: \(error.localizedDescription)")
            }
        }

    })
0

There are 0 best solutions below