public async Task <string> GetHubAddressAsync(string contractAddress) { var getHubAddr = new GetHubAddrFunction(); var getHubAddrHandler = _ethApiContractService.GetContractQueryHandler <GetHubAddrFunction>(); var address = await getHubAddrHandler .QueryAsync <string>(contractAddress, getHubAddr) .ConfigureAwait(false); if (address == null) { throw new Exception($"Contract does not support Gas Station Network"); } var code = await _ethApiContractService.GetCode .SendRequestAsync(address) .ConfigureAwait(false); if (code.Length <= 2) { throw new Exception($"Relay hub is not deployed at address {address}"); } var getHubVersion = new VersionFunction(); var getHubVersionHandler = _ethApiContractService.GetContractQueryHandler <VersionFunction>(); var hubVersion = await getHubVersionHandler .QueryAsync <string>(address, getHubVersion) .ConfigureAwait(false); if (!hubVersion.StartsWith("1")) { throw new Exception($"Unsupported relay hub version {hubVersion}"); } return(address); }
public async Task Validate( string hubAddress, string recipient, BigInteger gasLimit, BigInteger gasPrice, BigInteger relayFee) { var balanceOf = new BalanceOfFunction() { Target = recipient }; var balanceOfHandler = _ethApiContractService.GetContractQueryHandler <BalanceOfFunction>(); var balance = await balanceOfHandler .QueryAsync <BigInteger>(hubAddress, balanceOf) .ConfigureAwait(false); if (balance.IsZero) { throw new GSNLowBalanceException($"Recipient {recipient} has no funds for paying for relayed calls on the relay hub."); } var maxPossibleCharge = new MaxPossibleChargeFunction() { RelayedCallStipend = gasLimit, GasPriceParam = gasPrice, TransactionFee = relayFee }; var maxPossibleChargeHandler = _ethApiContractService.GetContractQueryHandler <MaxPossibleChargeFunction>(); var maxCharge = await maxPossibleChargeHandler .QueryAsync <BigInteger>(hubAddress, maxPossibleCharge) .ConfigureAwait(false); if (maxCharge.CompareTo(balance) == 1) { throw new GSNLowBalanceException($"Recipient {recipient} has not enough funds for paying for this relayed call (has {balance}, requires {maxCharge})."); } }