Skip to content

Commit

Permalink
Adds private token receiver (#47)
Browse files Browse the repository at this point in the history
* updating dependencies and mint tokens script

* first draft of private forwarding

* tests for forwarding
  • Loading branch information
joshuahannan authored Feb 25, 2021
1 parent 3d12094 commit 32371bd
Show file tree
Hide file tree
Showing 17 changed files with 834 additions and 110 deletions.
82 changes: 82 additions & 0 deletions contracts/utilityContracts/PrivateReceiverForwarder.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
# Fungible Token Private Receiver Contract
This contract implements a special resource and receiver interface
whose deposit function is only callable by an admin through a public capability.
*/

import FungibleToken from 0xFUNGIBLETOKENADDRESS

pub contract PrivateReceiverForwarder {

// Event that is emitted when tokens are deposited to the target receiver
pub event PrivateDeposit(amount: UFix64, to: Address?)

pub let SenderStoragePath: StoragePath

pub let PrivateReceiverStoragePath: StoragePath
pub let PrivateReceiverPublicPath: PublicPath

pub resource Forwarder {

// This is where the deposited tokens will be sent.
// The type indicates that it is a reference to a receiver
//
access(self) var recipient: Capability<&{FungibleToken.Receiver}>

// deposit
//
// Function that takes a Vault object as an argument and forwards
// it to the recipient's Vault using the stored reference
//
access(contract) fun deposit(from: @FungibleToken.Vault) {
let receiverRef = self.recipient.borrow()!

let balance = from.balance

receiverRef.deposit(from: <-from)

emit PrivateDeposit(amount: balance, to: self.owner?.address)
}

init(recipient: Capability<&{FungibleToken.Receiver}>) {
pre {
recipient.borrow() != nil: "Could not borrow Receiver reference from the Capability"
}
self.recipient = recipient
}
}

// createNewForwarder creates a new Forwarder reference with the provided recipient
//
pub fun createNewForwarder(recipient: Capability<&{FungibleToken.Receiver}>): @Forwarder {
return <-create Forwarder(recipient: recipient)
}


pub resource Sender {
pub fun sendPrivateTokens(_ address: Address, tokens: @FungibleToken.Vault) {

let account = getAccount(address)

let privateReceiver = account.getCapability<&PrivateReceiverForwarder.Forwarder>(PrivateReceiverForwarder.PrivateReceiverPublicPath)
.borrow() ?? panic("Could not borrow reference to private forwarder")

privateReceiver.deposit(from: <-tokens)

}
}

init(senderPath: StoragePath, storagePath: StoragePath, publicPath: PublicPath) {

self.SenderStoragePath = senderPath

self.PrivateReceiverStoragePath = storagePath
self.PrivateReceiverPublicPath = publicPath

self.account.save(<-create Sender(), to: self.SenderStoragePath)

}
}
File renamed without changes.
25 changes: 19 additions & 6 deletions lib/go/contracts/contracts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package contracts

//go:generate go run github.com/kevinburke/go-bindata/go-bindata -prefix ../../../contracts -o internal/assets/assets.go -pkg assets -nometadata -nomemcopy ../../../contracts
//go:generate go run github.com/kevinburke/go-bindata/go-bindata -prefix ../../../contracts -o internal/assets/assets.go -pkg assets -nometadata -nomemcopy ../../../contracts/...

import (
"strings"
Expand All @@ -9,11 +9,12 @@ import (
)

const (
fungibleTokenFilename = "FungibleToken.cdc"
flowTokenFilename = "FlowToken.cdc"
exampleTokenFilename = "ExampleToken.cdc"
defaultFungibleTokenAddress = "FUNGIBLETOKENADDRESS"
tokenForwardingFilename = "TokenForwarding.cdc"
fungibleTokenFilename = "FungibleToken.cdc"
flowTokenFilename = "FlowToken.cdc"
exampleTokenFilename = "ExampleToken.cdc"
defaultFungibleTokenAddress = "FUNGIBLETOKENADDRESS"
tokenForwardingFilename = "utilityContracts/TokenForwarding.cdc"
privateReceiverForwarderFilename = "utilityContracts/PrivateReceiverForwarder.cdc"
)

// FungibleToken returns the FungibleToken contract interface.
Expand Down Expand Up @@ -110,3 +111,15 @@ func CustomTokenForwarding(fungibleTokenAddr, tokenName, storageName string) []b

return []byte(code)
}

func PrivateReceiverForwarder(fungibleTokenAddr string) []byte {
code := assets.MustAssetString(privateReceiverForwarderFilename)

code = strings.ReplaceAll(
code,
"0x"+defaultFungibleTokenAddress,
"0x"+fungibleTokenAddr,
)

return []byte(code)
}
49 changes: 37 additions & 12 deletions lib/go/contracts/internal/assets/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 32371bd

Please sign in to comment.