Problem with creating reference to a resource

Hi everyone! I’m new to Cadence and I’m facing a problem with resources & references.
I have a resource for certain withdrawing restrictions store in /storage/Restrictions, then I have a transaction that changes the Withdrawing date on the resource and checks if the reference to the resource was created.
I thought that the path is right and the capability should be created correctly but I always get the error that the reference was not created!
Here is the code for the transaction;

transaction {

var tempVault: &EducationFund.Rules{EducationFund.WithdrawRestrictions}

var account: AuthAccount

prepare(acct: AuthAccount){

   self.account = acct

   let fundAccount = getAccount(0x03)

   let refVault = fundAccount.getCapability(/public/Restrictions)

   .borrow<&EducationFund.Rules{EducationFund.WithdrawRestrictions}>()

   ?? panic("Could not create reference to resource!")

   self.tempVault = refVault

}

execute {

   self.tempVault.setWithdrawDate(_withdrawDate: 10000.0, account: self.account)

   log("Withraw date changed!")

}

}

Welcome to the Flow community! It is good to have you here. :smiley:

getCapability never fails, but if the capability is invalid, the borrow could fail. This could be for many reasons:

  • You are trying to get the capability from a different account than what you saved it to
  • You are trying to get the capability from a different path than what you saved it to
  • The type of the reference you are trying to borrow is different than what you saved it as
  • The type of the capability you created doesn’t match what it is actually targeting in storage

I would recommend looking into those potential issues first, then if you still can’t solve it, post the transaction that you used to create the capability and I can take a look. :slight_smile:

1 Like

Hi there flowjosh, really thankful for the response!
I’m still lost and couldn’t figure out the issue but I also didn’t understand the last two possible issues.
Would it be okay if I send you the code?

Can you please post the code here? @YBBlockDev

1 Like

Sure! here is the link for the code;

1 Like

@YBBlockDev
you have created the path self.account.save(<- create Rules(), to: /storage/Restrictions) in the education fund contract but you did not made it public that all contract can get it’s data. When you are getting it in the transaction.
let vaultRef = fundAccount.getCapability(/public/Restrictions)
.borrow<&EducationFund.Rules{EducationFund.WithdrawRestrictions}>()
actually you didn’t make it public anywhere in the contract, meaning you didn’t link it.
You also have more issue related this linking.

1 Like

Hello I really appreciate the feedback!
Can you please provide me with a little bit more instructions on how this can be fixed?

there may be multipe issues but one I solved is to make the capability public in the Education Fund contract, I updated the code in the init of this contract.

self.account.link<&EducationFund.Rules{WithdrawRestrictions}>(/public/Restrictions, target: /storage/Restrictions)
Now with this link you can call the methods and variables available in Education Fund contract.

1 Like