Skip to content

Commit

Permalink
Merge pull request #34 from onflow/open-flix-types
Browse files Browse the repository at this point in the history
Fixed Template Id generation algo, add unit tests
  • Loading branch information
bthaile authored Jan 31, 2024
2 parents abbd351 + 1e37c66 commit 7db0f87
Show file tree
Hide file tree
Showing 10 changed files with 527 additions and 25 deletions.
13 changes: 7 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ go 1.20
require (
github.com/ethereum/go-ethereum v1.12.0
github.com/hexops/autogold/v2 v2.2.1
github.com/onflow/cadence v0.42.5
github.com/onflow/flow-cli/flowkit v1.6.1-0.20231110211255-b41f57a8b8c7
github.com/onflow/flow-go-sdk v0.41.16
github.com/onflow/cadence v0.42.6
github.com/onflow/flow-cli/flowkit v1.11.0
github.com/onflow/flow-go-sdk v0.41.17
github.com/spf13/afero v1.9.4
github.com/stoewer/go-strcase v1.3.0
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -128,12 +128,12 @@ require (
github.com/onflow/atree v0.6.0 // indirect
github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20231016154253-a00dbf7c061f // indirect
github.com/onflow/flow-core-contracts/lib/go/templates v1.2.4-0.20231016154253-a00dbf7c061f // indirect
github.com/onflow/flow-emulator v0.58.0 // indirect
github.com/onflow/flow-emulator v0.59.0 // indirect
github.com/onflow/flow-ft/lib/go/contracts v0.7.1-0.20230711213910-baad011d2b13 // indirect
github.com/onflow/flow-go v0.32.4-0.20231115172515-c1ec969fd6f2 // indirect
github.com/onflow/flow-go v0.32.4-0.20231130134727-3c01c7f8966c // indirect
github.com/onflow/flow-go/crypto v0.24.10 // indirect
github.com/onflow/flow-nft/lib/go/contracts v1.1.0 // indirect
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231018182244-e72527c55c63 // indirect
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231124194313-106cc495def6 // indirect
github.com/onflow/nft-storefront/lib/go/contracts v0.0.0-20221222181731-14b90207cead // indirect
github.com/onflow/sdks v0.5.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
Expand Down Expand Up @@ -161,6 +161,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.15.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/texttheater/golang-levenshtein/levenshtein v0.0.0-20200805054039-cae8b0eaed6c // indirect
Expand Down
24 changes: 12 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -666,31 +666,31 @@ github.com/onflow/atree v0.1.0-beta1.0.20211027184039-559ee654ece9/go.mod h1:+6x
github.com/onflow/atree v0.6.0 h1:j7nQ2r8npznx4NX39zPpBYHmdy45f4xwoi+dm37Jk7c=
github.com/onflow/atree v0.6.0/go.mod h1:gBHU0M05qCbv9NN0kijLWMgC47gHVNBIp4KmsVFi0tc=
github.com/onflow/cadence v0.20.1/go.mod h1:7mzUvPZUIJztIbr9eTvs+fQjWWHTF8veC+yk4ihcNIA=
github.com/onflow/cadence v0.42.5 h1:QCilotmJzfRToLd+02o3N62JIioSr8FfN7cujmR/IXQ=
github.com/onflow/cadence v0.42.5/go.mod h1:raU8va8QRyTa/eUbhej4mbyW2ETePfSaywoo36MddgE=
github.com/onflow/flow-cli/flowkit v1.6.1-0.20231110211255-b41f57a8b8c7 h1:EI/XTe2E5U23oHzjaLPIm4TKMLD+v2vI5vHjO1BkRyM=
github.com/onflow/flow-cli/flowkit v1.6.1-0.20231110211255-b41f57a8b8c7/go.mod h1:tPBuuYss8S8carovC49N1YVPIBheF6KD9Vr6Ocg3VpQ=
github.com/onflow/cadence v0.42.6 h1:VtI0EpKrdbfqITRMsvyZC4dhgcW1x1LNUQuEpdMDzus=
github.com/onflow/cadence v0.42.6/go.mod h1:raU8va8QRyTa/eUbhej4mbyW2ETePfSaywoo36MddgE=
github.com/onflow/flow-cli/flowkit v1.11.0 h1:RSfKlla/l+ZJwqAmlvA5HPFbQ6ia2wzKSG0kJ8fqVa0=
github.com/onflow/flow-cli/flowkit v1.11.0/go.mod h1:aH4shan7Ggxd0GIXZD2S4kYMemNfzP1rLWvzKnb6K3g=
github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20231016154253-a00dbf7c061f h1:S8yIZw9LFXfYD1V5H9BiixihHw3GrXVPrmfplSzYaww=
github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20231016154253-a00dbf7c061f/go.mod h1:jM6GMAL+m0hjusUgiYDNrixPQ6b9s8xjoJQoEu5bHQI=
github.com/onflow/flow-core-contracts/lib/go/templates v1.2.4-0.20231016154253-a00dbf7c061f h1:Ep+Mpo2miWMe4pjPGIaEvEzshRep30dvNgxqk+//FrQ=
github.com/onflow/flow-core-contracts/lib/go/templates v1.2.4-0.20231016154253-a00dbf7c061f/go.mod h1:ZeLxwaBkzuSInESGjL8/IPZWezF+YOYsYbMrZlhN+q4=
github.com/onflow/flow-emulator v0.58.0 h1:/Zo+qznjqN2jJNOGQu1mFK2lZT3L6c8V18AmAzXLKZ8=
github.com/onflow/flow-emulator v0.58.0/go.mod h1:pi23Sx7kuj65dJRT7OIlio53XRAhLTUUhf3zV9TN3PA=
github.com/onflow/flow-emulator v0.59.0 h1:KIfm9/+x62KqcZDjqE35fkuvVuY506OZ917xNtb3U6E=
github.com/onflow/flow-emulator v0.59.0/go.mod h1:Js1KKaXrui2yKKkXAlKTqmByRySis6/FH+vkGA6Kqu0=
github.com/onflow/flow-ft/lib/go/contracts v0.7.1-0.20230711213910-baad011d2b13 h1:B4ll7e3j+MqTJv2122Enq3RtDNzmIGRu9xjV7fo7un0=
github.com/onflow/flow-ft/lib/go/contracts v0.7.1-0.20230711213910-baad011d2b13/go.mod h1:kTMFIySzEJJeupk+7EmXs0EJ6CBWY/MV9fv9iYQk+RU=
github.com/onflow/flow-go v0.32.4-0.20231115172515-c1ec969fd6f2 h1:ujrwrJelTX0R3HtX9kSJRoU7bUtcdvf1Pmpvy31yvNQ=
github.com/onflow/flow-go v0.32.4-0.20231115172515-c1ec969fd6f2/go.mod h1:bKcX2932bIunkyXBKW9qmw8Z0zXpHol+5xCJCumV/H8=
github.com/onflow/flow-go v0.32.4-0.20231130134727-3c01c7f8966c h1:75LED6hmarR0uazKZG8nkqqDlUiqz6NdzkdQQiGjvlI=
github.com/onflow/flow-go v0.32.4-0.20231130134727-3c01c7f8966c/go.mod h1:YJDAoDjbY4OWBj44XV+Qe+dIwn+hlywUDL5xclOOLbw=
github.com/onflow/flow-go-sdk v0.24.0/go.mod h1:IoptMLPyFXWvyd9yYA6/4EmSeeozl6nJoIv4FaEMg74=
github.com/onflow/flow-go-sdk v0.41.16 h1:HsmHwEVmj+iK+GszHbFseHh7Ii5W3PWOIRNAH/En08Q=
github.com/onflow/flow-go-sdk v0.41.16/go.mod h1:bVrVNoJKiwB6vW5Qbm5tFAfJBQ5we4uSQWnn9gNAFhQ=
github.com/onflow/flow-go-sdk v0.41.17 h1:HpNn3j2fqLGA6H3HGfAuh2A+TsPBv8gWO3kvK9Hvtic=
github.com/onflow/flow-go-sdk v0.41.17/go.mod h1:ZIj2XBI9R0QiKzbI6iPwOeqyIy/M4+atczoMOEWdKYw=
github.com/onflow/flow-go/crypto v0.21.3/go.mod h1:vI6V4CY3R6c4JKBxdcRiR/AnjBfL8OSD97bJc60cLuQ=
github.com/onflow/flow-go/crypto v0.24.10 h1:nH97tnnC0RaFXO5GuJic5lt/5IEiOuuWa3gaJI2Le3w=
github.com/onflow/flow-go/crypto v0.24.10/go.mod h1:O7jjGhgJEp94t9qXfBWdwW5BArm5L5gYa6XoBJiTdHc=
github.com/onflow/flow-nft/lib/go/contracts v1.1.0 h1:rhUDeD27jhLwOqQKI/23008CYfnqXErrJvc4EFRP2a0=
github.com/onflow/flow-nft/lib/go/contracts v1.1.0/go.mod h1:YsvzYng4htDgRB9sa9jxdwoTuuhjK8WYWXTyLkIigZY=
github.com/onflow/flow/protobuf/go/flow v0.2.2/go.mod h1:gQxYqCfkI8lpnKsmIjwtN2mV/N2PIwc1I+RUK4HPIc8=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231018182244-e72527c55c63 h1:SX8OhYbyKBExhy4qEDR/Hw6MVTBTzlDb8LfCHfFyte4=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231018182244-e72527c55c63/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231124194313-106cc495def6 h1:KMN+OEVaw7KAgxL3p8ux7CMuyTvacAlYTbasOqowh4M=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231124194313-106cc495def6/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/nft-storefront/lib/go/contracts v0.0.0-20221222181731-14b90207cead h1:2j1Unqs76Z1b95Gu4C3Y28hzNUHBix7wL490e61SMSw=
github.com/onflow/nft-storefront/lib/go/contracts v0.0.0-20221222181731-14b90207cead/go.mod h1:E3ScfQb5XcWJCIAdtIeEnr5i5l2y60GT0BTXeIHseWg=
github.com/onflow/sdks v0.5.0 h1:2HCRibwqDaQ1c9oUApnkZtEAhWiNY2GTpRD5+ftdkN8=
Expand Down
237 changes: 237 additions & 0 deletions internal/v1_1/FungibleToken.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
/**
# The Flow Fungible Token standard
## `FungibleToken` contract interface
The interface that all Fungible Token contracts would have to conform to.
If a users wants to deploy a new token contract, their contract
would need to implement the FungibleToken interface.
Their contract would have to follow all the rules and naming
that the interface specifies.
## `Vault` resource
Each account that owns tokens would need to have an instance
of the Vault resource stored in their account storage.
The Vault resource has methods that the owner and other users can call.
## `Provider`, `Receiver`, and `Balance` resource interfaces
These interfaces declare pre-conditions and post-conditions that restrict
the execution of the functions in the Vault.
They are separate because it gives the user the ability to share
a reference to their Vault that only exposes the fields functions
in one or more of the interfaces.
It also gives users the ability to make custom resources that implement
these interfaces to do various things with the tokens.
For example, a faucet can be implemented by conforming
to the Provider interface.
By using resources and interfaces, users of Fungible Token contracts
can send and receive tokens peer-to-peer, without having to interact
with a central ledger smart contract. To send tokens to another user,
a user would simply withdraw the tokens from their Vault, then call
the deposit function on another user's Vault to complete the transfer.
*/

/// The interface that Fungible Token contracts implement.
///
pub contract interface FungibleToken {

/// The total number of tokens in existence.
/// It is up to the implementer to ensure that the total supply
/// stays accurate and up to date
pub var totalSupply: UFix64

/// The event that is emitted when the contract is created
pub event TokensInitialized(initialSupply: UFix64)

/// The event that is emitted when tokens are withdrawn from a Vault
pub event TokensWithdrawn(amount: UFix64, from: Address?)

/// The event that is emitted when tokens are deposited into a Vault
pub event TokensDeposited(amount: UFix64, to: Address?)

/// The interface that enforces the requirements for withdrawing
/// tokens from the implementing type.
///
/// It does not enforce requirements on `balance` here,
/// because it leaves open the possibility of creating custom providers
/// that do not necessarily need their own balance.
///
pub resource interface Provider {

/// Subtracts tokens from the owner's Vault
/// and returns a Vault with the removed tokens.
///
/// The function's access level is public, but this is not a problem
/// because only the owner storing the resource in their account
/// can initially call this function.
///
/// The owner may grant other accounts access by creating a private
/// capability that allows specific other users to access
/// the provider resource through a reference.
///
/// The owner may also grant all accounts access by creating a public
/// capability that allows all users to access the provider
/// resource through a reference.
///
/// @param amount: The amount of tokens to be withdrawn from the vault
/// @return The Vault resource containing the withdrawn funds
///
pub fun withdraw(amount: UFix64): @Vault {
post {
// `result` refers to the return value
result.balance == amount:
"Withdrawal amount must be the same as the balance of the withdrawn Vault"
}
}
}

/// The interface that enforces the requirements for depositing
/// tokens into the implementing type.
///
/// We do not include a condition that checks the balance because
/// we want to give users the ability to make custom receivers that
/// can do custom things with the tokens, like split them up and
/// send them to different places.
///
pub resource interface Receiver {

/// Takes a Vault and deposits it into the implementing resource type
///
/// @param from: The Vault resource containing the funds that will be deposited
///
pub fun deposit(from: @Vault)

/// Below is referenced from the FLIP #69 https://github.com/onflow/flips/blob/main/flips/20230206-fungible-token-vault-type-discovery.md
///
/// Returns the dictionary of Vault types that the the receiver is able to accept in its `deposit` method
/// this then it would return `{Type<@FlowToken.Vault>(): true}` and if any custom receiver
/// uses the default implementation then it would return empty dictionary as its parent
/// resource doesn't conform with the `FungibleToken.Vault` resource.
///
/// Custom receiver implementations are expected to upgrade their contracts to add an implementation
/// that supports this method because it is very valuable for various applications to have.
///
/// @return dictionary of supported deposit vault types by the implementing resource.
///
pub fun getSupportedVaultTypes(): {Type: Bool} {
// Below check is implemented to make sure that run-time type would
// only get returned when the parent resource conforms with `FungibleToken.Vault`.
if self.getType().isSubtype(of: Type<@FungibleToken.Vault>()) {
return {self.getType(): true}
} else {
// Return an empty dictionary as the default value for resource who don't
// implement `FungibleToken.Vault`, such as `FungibleTokenSwitchboard`, `TokenForwarder` etc.
return {}
}
}
}

/// The interface that contains the `balance` field of the Vault
/// and enforces that when new Vaults are created, the balance
/// is initialized correctly.
///
pub resource interface Balance {

/// The total balance of a vault
///
pub var balance: UFix64

init(balance: UFix64) {
post {
self.balance == balance:
"Balance must be initialized to the initial balance"
}
}

/// Function that returns all the Metadata Views implemented by a Fungible Token
///
/// @return An array of Types defining the implemented views. This value will be used by
/// developers to know which parameter to pass to the resolveView() method.
///
pub fun getViews(): [Type] {
return []
}

/// Function that resolves a metadata view for this fungible token by type.
///
/// @param view: The Type of the desired view.
/// @return A structure representing the requested view.
///
pub fun resolveView(_ view: Type): AnyStruct? {
return nil
}
}

/// The resource that contains the functions to send and receive tokens.
/// The declaration of a concrete type in a contract interface means that
/// every Fungible Token contract that implements the FungibleToken interface
/// must define a concrete `Vault` resource that conforms to the `Provider`, `Receiver`,
/// and `Balance` interfaces, and declares their required fields and functions
///
pub resource Vault: Provider, Receiver, Balance {

/// The total balance of the vault
pub var balance: UFix64

// The conforming type must declare an initializer
// that allows providing the initial balance of the Vault
//
init(balance: UFix64)

/// Subtracts `amount` from the Vault's balance
/// and returns a new Vault with the subtracted balance
///
/// @param amount: The amount of tokens to be withdrawn from the vault
/// @return The Vault resource containing the withdrawn funds
///
pub fun withdraw(amount: UFix64): @Vault {
pre {
self.balance >= amount:
"Amount withdrawn must be less than or equal than the balance of the Vault"
}
post {
// use the special function `before` to get the value of the `balance` field
// at the beginning of the function execution
//
self.balance == before(self.balance) - amount:
"New Vault balance must be the difference of the previous balance and the withdrawn Vault"
}
}

/// Takes a Vault and deposits it into the implementing resource type
///
/// @param from: The Vault resource containing the funds that will be deposited
///
pub fun deposit(from: @Vault) {
// Assert that the concrete type of the deposited vault is the same
// as the vault that is accepting the deposit
pre {
from.isInstance(self.getType()):
"Cannot deposit an incompatible token type"
}
post {
self.balance == before(self.balance) + before(from.balance):
"New Vault balance must be the sum of the previous balance and the deposited Vault"
}
}
}

/// Allows any user to create a new Vault that has a zero balance
///
/// @return The new Vault resource
///
pub fun createEmptyVault(): @Vault {
post {
result.balance == 0.0: "The newly created Vault must have zero balance"
}
}
}
2 changes: 2 additions & 0 deletions internal/v1_1/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ func generateDependencyNetworks(ctx context.Context, flowkit *flowkit.Flowkit, a
if err != nil {
return nil, err
}

code := account.Contracts[name]
depend := PinDetail{
PinContractName: name,
Expand All @@ -282,6 +283,7 @@ func generateDependencyNetworks(ctx context.Context, flowkit *flowkit.Flowkit, a
}
pins = append(pins, dep.PinSelf)
}

depend.Imports = detailImports
depend.Pin = ShaHex(strings.Join(pins, ""), "")
return &depend, nil
Expand Down
Loading

0 comments on commit 7db0f87

Please sign in to comment.