Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add cross-account FT supporting script & transfer transaction #157

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions scripts/hybrid-custody/get_specific_vault_paths.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import "FungibleToken"

/// Struct containing the paths to the vault's storage, provider and receiver
///
pub struct FTVaultPaths {
pub let vaultIdentifier: String
pub let storagePath: StoragePath
pub let providerPath: CapabilityPath?
pub let receiverPath: CapabilityPath?

init(
vaultIdentifier: String,
storagePath: StoragePath,
providerPath: CapabilityPath?,
receiverPath: CapabilityPath?
) {
self.vaultIdentifier = vaultIdentifier
self.storagePath = storagePath
self.providerPath = providerPath
self.receiverPath = receiverPath
}
}

/* --- Helper Methods --- */
//
/// Returns a type identifier for an FungibleToken Vault
///
access(all) fun deriveVaultTypeIdentifier(_ contractAddress: Address, _ contractName: String): String {
return "A.".concat(withoutPrefix(contractAddress.toString())).concat(".").concat(contractName).concat(".Vault")
}

/// Taken from AddressUtils private method
///
access(all) fun withoutPrefix(_ input: String): String{
var address=input

//get rid of 0x
if address.length>1 && address.utf8[1] == 120 {
address = address.slice(from: 2, upTo: address.length)
}

//ensure even length
if address.length%2==1{
address="0".concat(address)
}
return address
}

/* --- Main Script --- */
//
/// Returns the paths for a given vault in the specified account address
///
pub fun main(address: Address, ftAddress: Address, ftName: String): FTVaultPaths? {

let account = getAuthAccount(address)
let vaultIdentifier = deriveVaultTypeIdentifier(ftAddress, ftName)
let receiverType = Type<Capability<&{FungibleToken.Receiver}>>()
let providerType = Type<Capability<&{FungibleToken.Provider}>>()

var storagePath: StoragePath? = nil
var providerPath: CapabilityPath? = nil
var receiverPath: CapabilityPath? = nil

// Find the StoragePath for the vault
account.forEachStored(fun (path: StoragePath, type: Type): Bool {
if type.identifier == vaultIdentifier {
storagePath = path
return false
}
return true
})

// Return nil early if the Vault was not found
if storagePath == nil {
return nil
}

// Attempt to find Provider Path
account.forEachPrivate(fun (path: PrivatePath, type: Type): Bool {
if type.isSubtype(of: providerType) &&
account.getCapability(path).borrow<&AnyResource>()?.getType()?.identifier == vaultIdentifier {
providerPath = path
return false
}
return true
})

// Attempt to find the Receiver Path
account.forEachPublic(fun (path: PublicPath, type: Type): Bool {
if type.isSubtype(of: receiverType) &&
account.getCapability(path).borrow<&AnyResource>()?.getType()?.identifier == vaultIdentifier {
receiverPath = path
return false
}
return true
})

return FTVaultPaths(
vaultIdentifier: vaultIdentifier,
storagePath: storagePath!,
providerPath: providerPath,
receiverPath: receiverPath
)
}
97 changes: 97 additions & 0 deletions transactions/hybrid-custody/transfer_ft_from_child_to_parent.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import "FungibleToken"

import "HybridCustody"

/* --- Helper Methods --- */
//
/// Returns a type identifier for an FungibleToken Vault
///
access(all) fun deriveVaultTypeIdentifier(_ contractAddress: Address, _ contractName: String): String {
return "A.".concat(withoutPrefix(contractAddress.toString())).concat(".").concat(contractName).concat(".Vault")
}

/// Taken from AddressUtils private method
///
access(all) fun withoutPrefix(_ input: String): String{
var address=input

//get rid of 0x
if address.length>1 && address.utf8[1] == 120 {
address = address.slice(from: 2, upTo: address.length)
}

//ensure even length
if address.length%2==1{
address="0".concat(address)
}
return address
}

/* --- Transaction Body --- */

/// Transaction for generalized FungibleToken transfers between child & parent account.
/// Withdraws tokens from a child account and deposits them into the signing parent's account, setting up the FT Vault
/// if needed.
///
transaction(
ftContractAddress: Address,
ftContractName: String,
amount: UFix64,
fromChild: Address,
storagePath: StoragePath,
providerPath: PrivatePath,
receiverPath: PublicPath
) {

let provider: &{FungibleToken.Provider}
let receiver: &{FungibleToken.Receiver}

prepare(signer: AuthAccount) {
/* --- Gather FT Provider from ChildAccount --- */
//
// Borrow the Manager from the signing parent's account
let m = signer.borrow<&HybridCustody.Manager>(from: HybridCustody.ManagerStoragePath)
?? panic("manager does not exist")
let childAcct = m.borrowAccount(addr: fromChild) ?? panic("child account not found")

// Get FT.Provider Capability from child account
let cap = childAcct.getCapability(path: /private/exampleTokenProvider, type: Type<&{FungibleToken.Provider}>()) ?? panic("no cap found")
let providerCap = cap as! Capability<&{FungibleToken.Provider}>

// Check that the capability is valid & borrow a reference to the Provider
assert(providerCap.check(), message: "invalid provider capability")
self.provider = providerCap.borrow()!

/* --- Parent account setup --- */
//
// Setup parent Vault if needed
let vaultIdentifier = deriveVaultTypeIdentifier(ftContractAddress, ftContractName)
if signer.type(at: storagePath)?.identifier == vaultIdentifier {
let ftContract = getAccount(ftContractAddress).contracts.borrow<&FungibleToken>(name: ftContractName)
?? panic("Could not borrow reference to the FungibleToken contract")
let vault <- ftContract.createEmptyVault()
signer.save(<-vault, to: storagePath)
}
// Link Capabilities if needed
if !signer.getCapability<&{FungibleToken.Provider}>(providerPath).check() {
signer.unlink(providerPath)
signer.link<&{FungibleToken.Provider}>(providerPath, target: storagePath)
}
if !signer.getCapability<&{FungibleToken.Balance, FungibleToken.Receiver}>(receiverPath).check() {
signer.unlink(receiverPath)
signer.link<&{FungibleToken.Balance, FungibleToken.Receiver}>(receiverPath, target: storagePath)
}

// Get a reference to the signer's Receiver
self.receiver = signer.getCapability(receiverPath)
.borrow<&{FungibleToken.Receiver}>()
?? panic("Could not borrow receiver reference to the recipient's Vault")
}

execute {
// Complete transfer
let paymentVault <- self.provider.withdraw(amount: amount)
self.receiver.deposit(from: <- paymentVault)
}
}