0

i'm pretty new to solidity and web3. I trying to make a faucet so obviously the person interacting with the smart contract won't have enough token to cover the fees of the transaction so it doesn't even make sens to spend time connecting the web app to the user's metamask.

Here is the problem, when I try to send() the withdrawMoney method of my SC, from the SC address I get the error 'Unknown account'. I think the problem is that I need to find a way to sing the transaction trought some king of private key but I don't think that smart contracts have them (please tell me if I am wrong).

What would you suggest? Is there a way to use the balance of the smart contract to cover the fees? Btw I'm using vue.


import Web3 from '../../node_modules/web3'
export default {
  name: 'HelloWorld',
  data(){
    return{
      NODE_URL:'https://speedy-nodes-nyc.moralis.io/6b7b3f98464588ab2f0bcda1/polygon/mainnet',
      blocknumber: '',
      SCABI: [
          {
            "anonymous": false,
            "inputs": [
              {
                "indexed": false,
                "internalType": "address",
                "name": "_to",
                "type": "address"
              }
            ],
            "name": "tokenNonTrasferiti",
            "type": "event"
          },
          {
            "anonymous": false,
            "inputs": [
              {
                "indexed": false,
                "internalType": "address",
                "name": "_to",
                "type": "address"
              }
            ],
            "name": "tokenTrasferiti",
            "type": "event"
          },
          {
            "inputs": [],
            "name": "sendMoney",
            "outputs": [],
            "stateMutability": "payable",
            "type": "function"
          },
          {
            "inputs": [],
            "stateMutability": "nonpayable",
            "type": "constructor"
          },
          {
            "inputs": [
              {
                "internalType": "address payable",
                "name": "_to",
                "type": "address"
              }
            ],
            "name": "withdrawMoney",
            "outputs": [],
            "stateMutability": "nonpayable",
            "type": "function"
          },
          {
            "stateMutability": "payable",
            "type": "receive"
          },
          {
            "inputs": [
              {
                "internalType": "address",
                "name": "_address",
                "type": "address"
              }
            ],
            "name": "getBalanceOfAddress",
            "outputs": [
              {
                "internalType": "uint256",
                "name": "",
                "type": "uint256"
              }
            ],
            "stateMutability": "view",
            "type": "function"
          },
          {
            "inputs": [],
            "name": "owner",
            "outputs": [
              {
                "internalType": "address",
                "name": "",
                "type": "address"
              }
            ],
            "stateMutability": "view",
            "type": "function"
              }],
      SCAddress: '0xd52e807a0C7127B929645748A2Bc03dc0A6298Fc',
      contractSpec: null,
      contract: null
  }
  },
  methods:{
    BCConnection(){
      let web3 = new Web3(new Web3.providers.HttpProvider(this.NODE_URL));
      console.log(web3)
      web3.eth.getBlockNumber()
      .then(function(block) {
        console.log(block)
      })
    },
    BCCInteract(){
      let web3 = new Web3(new Web3.providers.HttpProvider(this.NODE_URL));
      this.contractSpec =new web3.eth.Contract(this.SCABI, this.SCAddress);
      console.log(this.contractSpec);
      console.log(this.contractSpec.methods.getBalanceOfAddress(this.SCAddress).call())
    },
    RichiediMatic(){
      let web3 = new Web3(new Web3.providers.HttpProvider(this.NODE_URL));
      this.contractSpec =new web3.eth.Contract(this.SCABI, this.SCAddress ,{
        from: 'SCAddress', // default from address
        }
      );
      await this.contractSpec.methods.withdrawMoney(receiveraddress).send()
      .then(()=>{
        console.log(this.contractSpec.methods.getBalanceOfAddress(this.SCAddress).call())
        })
      .catch(console.log(':('))
      }
    }

  }
  
3
  • I am not sure I understand what you are trying to achieve. You want to create a smart contract that upon getting a transaction transfers funds to the caller? If so this is problematic. The miner executing the transaction won't mine it if he won't get any fees... You can create a faucet this way but the caller has to have a small amount so he could at least pay fees.
    – Epic
    Jun 24, 2021 at 15:29
  • first of all thanks for the help, I want to use the balance on the smart contract to pay for the fees, I'll edit the question to make it clearer. Jun 24, 2021 at 15:44
  • So like I said, I don't think you can do that. Think of it another way. Lets say there is not enough gas (fee) for the contract to complete its execution, then who is going to pay the miner?
    – Epic
    Jun 24, 2021 at 15:49

1 Answer 1

0

If you dont want your customer to pay for the gas fee, you can use a gas station network. Basically, the idea is that you set up/use a gas station network and the gas station pay the gas fee for the customer. Eventually you will have to pay the gas station for the gas fee

Please read: https://docs.opengsn.org/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.