Skip to content

Commit

Permalink
Add functionality to switchboard to link receiver capabilities to dif…
Browse files Browse the repository at this point in the history
…ferent receiver types (#107)

* Link the capabilty to the FT.Receiver also

* Add addNewVaultWrapper method

* Move create forwarder tx to its own folder

* Undo folder change to avoid modify tests

* Typo

* Create tx to add vault capability specifying the type to be deposited

* Create test for adding vault wrapper capability

* Make test

* Fix test name

* Fix comment

* Update version

* Update docs

* Update README.md

Co-authored-by: Joshua Hannan <[email protected]>

* Fix ci

* Update flow-cli version in ci workflow to 0.41.2 for flow-js-testing compatibility

Co-authored-by: Joshua Hannan <[email protected]>
Co-authored-by: Giovanni Sanchez <[email protected]>
  • Loading branch information
3 people authored Dec 16, 2022
1 parent d2c9fa2 commit 1f51398
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
cache: 'npm'
cache-dependency-path: lib/js/test/package-lock.json
- name: Install Flow CLI
run: sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)"
run: sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/50058df8aa5f999a85cfd7afae6fc2661090078a/install.sh)" -- v0.41.2
- name: Flow cli Version
run: flow version
- name: Update PATH
Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,48 @@ To use the Flow Token contract as is, you need to follow these steps:
```
This function won't panic, instead it will just not add to the `@Switchboard` any capability which can not be retrieved from any of the provided `PublicPath`s. It will also ignore any type of `&{FungibleToken.Receiver}` that is already present on the `@Switchboard`
3. Adding a capability to a receiver specifying which type of token will be deposited there
using `addNewVaultWrapper(capability: Capability<&{FungibleToken.Receiver}>, type: Type)`.
This method can be used to link a token forwarder or any other wrapper to the switchboard.
Once the `Forwarder` has been properly created containing the capability to an actual `@FungibleToken.Vault`,
this method can be used to link the `@Forwarder` to the switchboard to deposit the specified type of Fungible Token.
In the template transaction `switchboard/add_vault_wrapper_capability.cdc`,
we assume that the signer has a forwarder containing a capability to an `@ExampleToken.Vault` resource:
```cadence
transaction {
let tokenForwarderCapability: Capability<&{FungibleToken.Receiver}>
let switchboardRef: &FungibleTokenSwitchboard.Switchboard
prepare(signer: AuthAccount) {
// Get the token forwarder capability from the signer's account
self.tokenForwarderCapability =
signer.getCapability<&{FungibleToken.Receiver}>
(ExampleToken.ReceiverPublicPath)
// Check if the receiver capability exists
assert(self.tokenForwarderCapability.check(),
message: "Signer does not have a working fungible token receiver capability")
// Get a reference to the signers switchboard
self.switchboardRef = signer.borrow<&FungibleTokenSwitchboard.Switchboard>
(from: FungibleTokenSwitchboard.StoragePath)
?? panic("Could not borrow reference to switchboard")
}
execute {
// Add the capability to the switchboard using addNewVault method
self.switchboardRef.addNewVaultWrapper(capability: self.tokenForwarderCapability, type: Type<@ExampleToken.Vault>())
}
}
```

## Removing a vault from the switchboard
If a user no longer wants to be able to receive deposits from a certain FT, or if they want to update the provided capability for one of them, they will need to remove the vault from the switchboard.
This can be accomplished by using `removeVault(capability: Capability<&{FungibleToken.Receiver}>)`.
Expand Down
28 changes: 28 additions & 0 deletions contracts/FungibleTokenSwitchboard.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,34 @@ pub contract FungibleTokenSwitchboard {
}
}

/// Adds a new fungible token receiver capability to the switchboard
/// resource specifying which `Type`of `@FungibleToken.Vault` can be
/// deposited to it. Use it to include in your switchboard "wrapper"
/// receivers such as a `@TokenForwarding.Forwarder`. It can also be
/// used to overwrite the type attached to a certain capability without
/// having to remove that capability first.
///
/// @param capability: The capability to expose a certain fungible
/// token vault deposit function through `{FungibleToken.Receiver}` that
/// will be added to the switchboard.
///
/// @param type: The type of fungible token that can be deposited to that
/// capability, rather than the `Type` from the reference borrowed from
/// said capability
///
pub fun addNewVaultWrapper(capability: Capability<&{FungibleToken.Receiver}>, type: Type) {
// Check if the capability is working
assert(capability.check(), message: "The passed capability is not valid")
// Use the type parameter as key for the capability
self.receiverCapabilities[type] = capability
// emit the event that indicates that a new capability has been
// added
emit VaultCapabilityAdded(type: type,
switchboardOwner: self.owner?.address,
capabilityOwner: capability.address)
}


/// Removes a fungible token receiver capability from the switchboard
/// resource.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,30 @@ Parameters:

---

### fun `addNewVaultWrapper()`

```cadence
func addNewVaultWrapper(capability Capability<&{FungibleToken.Receiver}>, type Type)
```
Adds a new fungible token receiver capability to the switchboard
resource specifying which `Type`of `@FungibleToken.Vault` can be
deposited to it. Use it to include in your switchboard "wrapper"
receivers such as a `@TokenForwarding.Forwarder`. It can also be
used to overwrite the type attached to a certain capability without
having to remove that capability first.

token vault deposit function through `{FungibleToken.Receiver}` that
will be added to the switchboard.

capability, rather than the `Type` from the reference borrowed from
said capability

Parameters:
- capability : _The capability to expose a certain fungible_
- type : _The type of fungible token that can be deposited to that_

---

### fun `removeVault()`

```cadence
Expand Down Expand Up @@ -94,7 +118,7 @@ avoiding panicking if the vault is not available.

deposited.

funds if the deposit was succesful, or still containing the funds
funds if the deposit was successful, or still containing the funds
if the reference to the needed vault was not found.

Parameters:
Expand All @@ -112,7 +136,7 @@ func getVaultTypes(): [Type]
A getter function to know which tokens a certain switchboard
resource is prepared to receive.

`{FungibleToken.Receiver}` capabilities that can be efectively
`{FungibleToken.Receiver}` capabilities that can be effectively
borrowed.

Returns: The keys from the dictionary of stored
Expand Down
6 changes: 3 additions & 3 deletions lib/go/contracts/internal/assets/assets.go

Large diffs are not rendered by default.

Loading

0 comments on commit 1f51398

Please sign in to comment.