Skip to content

Commit

Permalink
adapt library for cadence1 (#225)
Browse files Browse the repository at this point in the history
* adapt library for cadence1

* fix: run flow-c1 instead of flow by default

* update CI

* fix CI

* fix CI, test authorized signers

* fix shorthand imports

* Update flow-cadut

---------

Co-authored-by: Jordan Ribbink <jribbink@telus.net>
  • Loading branch information
NtTestAlert and jribbink authored Apr 2, 2024
1 parent cdb98a8 commit f4780cd
Show file tree
Hide file tree
Showing 83 changed files with 674 additions and 704 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ jobs:
run: ./check-headers.sh

- 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/master/install.sh)"
run: sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/feature/stable-cadence/install.sh)"

- name: Get Flow CLI version
id: testbed
run: |
echo "flow-version=$(echo | flow version | grep 'Version' | sed 's/[^0-9\.]*//g')" >> $GITHUB_OUTPUT
echo "flow-version=$(echo | flow-c1 version --output json | grep 'Version:' | grep -oEi '(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?')" >> $GITHUB_OUTPUT
echo "package-version=$(grep version package.json | sed 's/.*"version": "\(.*\)".*/\1/')" >> $GITHUB_OUTPUT
echo "fcl-version=$(grep 'fcl":' package.json | sed 's/.*"@onflow\/fcl": "\(.*\)".*/\1/')" >> $GITHUB_OUTPUT
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ jobs:

- name: Install Flow CLI
# We will need Flow CLI in order to run tests, so we need to install it
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/master/install.sh)"
run: sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/feature/stable-cadence/install.sh)"

- name: Install Dependencies
run: npm ci
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
node_modules
dist
.DS_Store
coverage
80 changes: 43 additions & 37 deletions cadence/contracts/FlowManager.cdc
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
pub contract FlowManager {
access(all) contract FlowManager {

/// Account Manager
pub event AccountAdded(address: Address)
access(all) event AccountAdded(address: Address)

pub struct Mapper {
pub let accounts: {String: Address}
access(all) struct Mapper {
access(all) let accounts: {String: Address}

pub fun getAddress(_ name: String): Address? {
access(all) fun getAddress(_ name: String): Address? {
return self.accounts[name]
}

pub fun setAddress(_ name: String, address: Address){
access(all) fun setAddress(_ name: String, address: Address){
self.accounts[name] = address
emit FlowManager.AccountAdded(address: address)
}
Expand All @@ -20,35 +20,34 @@ pub contract FlowManager {
}
}

pub fun getAccountAddress(_ name: String): Address?{
access(all) fun getAccountAddress(_ name: String): Address?{
let accountManager = self.account
.getCapability(self.accountManagerPath)
.borrow<&FlowManager.Mapper>()!
.capabilities.borrow<&FlowManager.Mapper>(self.accountManagerPath)!

return accountManager.getAddress(name)
}

pub let defaultAccounts: {Address : String}
access(all) let defaultAccounts: {Address : String}

pub fun resolveDefaultAccounts(_ address: Address): Address{
access(all) fun resolveDefaultAccounts(_ address: Address): Address{
let alias = self.defaultAccounts[address]!
return self.getAccountAddress(alias)!
}

pub let accountManagerStorage: StoragePath
pub let contractManagerStorage: StoragePath
pub let accountManagerPath: PublicPath
pub let contractManagerPath: PublicPath
access(all) let accountManagerStorage: StoragePath
access(all) let contractManagerStorage: StoragePath
access(all) let accountManagerPath: PublicPath
access(all) let contractManagerPath: PublicPath

/// Environment Manager
pub event BlockOffsetChanged(offset: UInt64)
pub event TimestampOffsetChanged(offset: UFix64)
access(all) event BlockOffsetChanged(offset: UInt64)
access(all) event TimestampOffsetChanged(offset: UFix64)

pub struct MockBlock {
pub let id: [UInt8; 32]
pub let height: UInt64
pub let view: UInt64
pub let timestamp: UFix64
access(all) struct MockBlock {
access(all) let id: [UInt8; 32]
access(all) let height: UInt64
access(all) let view: UInt64
access(all) let timestamp: UFix64

init(_ id: [UInt8; 32], _ height: UInt64, _ view: UInt64, _ timestamp: UFix64){
self.id = id
Expand All @@ -58,34 +57,34 @@ pub contract FlowManager {
}
}

pub fun setBlockOffset(_ offset: UInt64){
access(all) fun setBlockOffset(_ offset: UInt64){
self.blockOffset = offset
emit FlowManager.BlockOffsetChanged(offset: offset)
}

pub fun setTimestampOffset(_ offset: UFix64){
access(all) fun setTimestampOffset(_ offset: UFix64){
self.timestampOffset = offset
emit FlowManager.TimestampOffsetChanged(offset: offset)
}

pub fun getBlockHeight(): UInt64 {
access(all) fun getBlockHeight(): UInt64 {
var block = getCurrentBlock()
return block.height + self.blockOffset
}

pub fun getBlockTimestamp(): UFix64 {
access(all) fun getBlockTimestamp(): UFix64 {
var block = getCurrentBlock()
return block.timestamp + self.timestampOffset
}

pub fun getBlock(): MockBlock {
access(all) fun getBlock(): MockBlock {
var block = getCurrentBlock()
let mockBlock = MockBlock(block.id, block.height, block.view, block.timestamp);
return mockBlock
}

pub var blockOffset: UInt64;
pub var timestampOffset: UFix64;
access(all) var blockOffset: UInt64;
access(all) var timestampOffset: UFix64;


// Initialize contract
Expand All @@ -111,16 +110,23 @@ pub contract FlowManager {

self.accountManagerPath = /public/testSuiteAccountManager
self.contractManagerPath = /public/testSuiteContractManager

// Destroy previously stored values
self.account.load<Mapper>(from: self.accountManagerStorage)
self.account.load<Mapper>(from: self.contractManagerStorage)
self.account.storage.load<Mapper>(from: self.accountManagerStorage)
self.account.storage.load<Mapper>(from: self.contractManagerStorage)

self.account.storage.save(accountManager, to: self.accountManagerStorage)
self.account.storage.save(contractManager, to: self.contractManagerStorage)


self.account.save(accountManager, to: self.accountManagerStorage)
self.account.save(contractManager, to: self.contractManagerStorage)
self.account.capabilities.publish(
self.account.capabilities.storage.issue<&Mapper>(
self.accountManagerStorage
), at: self.accountManagerPath)

self.account.link<&Mapper>(self.accountManagerPath, target: self.accountManagerStorage)
self.account.link<&Mapper>(self.contractManagerPath, target: self.contractManagerStorage)
self.account.capabilities.publish(
self.account.capabilities.storage.issue<&Mapper>(
self.contractManagerStorage
), at: self.contractManagerPath)
}
}

2 changes: 1 addition & 1 deletion cadence/scripts/check-manager.cdc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import FlowManager from 0x01

pub fun main(){
access(all) fun main(){
// the body can be empty, cause script will throw error if FlowManager is not
// added to service address
}
8 changes: 3 additions & 5 deletions cadence/scripts/get-account-address.cdc
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import FlowManager from 0x01

pub fun main(name: String, managerAccount: Address):Address? {
access(all) fun main(name: String, managerAccount: Address):Address? {
let manager = getAccount(managerAccount)
let linkPath = FlowManager.accountManagerPath
let accountManager = manager
.getCapability(linkPath)
.borrow<&FlowManager.Mapper>()!
let accountManager = manager.capabilities.borrow<&FlowManager.Mapper>(linkPath)!

return accountManager.getAddress(name)

}
}
2 changes: 1 addition & 1 deletion cadence/scripts/get-block-offset.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import FlowManager from 0x01

pub fun main():UInt64 {
access(all) fun main():UInt64 {
return FlowManager.blockOffset
}
8 changes: 3 additions & 5 deletions cadence/scripts/get-contract-address.cdc
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import FlowManager from 0x01

pub fun main(name: String, managerAccount: Address):Address? {
access(all) fun main(name: String, managerAccount: Address):Address? {
let manager = getAccount(managerAccount)
let linkPath = FlowManager.contractManagerPath
let contractManager = manager
.getCapability(linkPath)
.borrow<&FlowManager.Mapper>()!
let contractManager = manager.capabilities.borrow<&FlowManager.Mapper>(linkPath)!

return contractManager.getAddress(name)

}
}
8 changes: 3 additions & 5 deletions cadence/scripts/get-manager-address.cdc
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
pub fun main(serviceAddress: Address): Address? {
access(all) fun main(serviceAddress: Address): Address? {
let account = getAccount(serviceAddress)
let ref = account
.getCapability(/public/flowManagerAddress)
.borrow<&[Address]>()!

let ref = account.capabilities.borrow<&[Address]>(/public/flowManagerAddress)!

return ref[0]
}

2 changes: 1 addition & 1 deletion cadence/scripts/get-timestamp-offset.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import FlowManager from 0x01

pub fun main():UFix64 {
access(all) fun main():UFix64 {
return FlowManager.timestampOffset
}
22 changes: 13 additions & 9 deletions cadence/scripts/get_balance.cdc
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// This script reads the balance field of an account's FlowToken Balance
// This script reads the balance field
// of an account's ExampleToken Balance
import FungibleToken from 0xFUNGIBLETOKENADDRESS
import ExampleToken from 0xTOKENADDRESS
import FungibleToken from 0x1
import ExampleToken from 0x1
import FungibleTokenMetadataViews from 0x1

pub fun main(account: Address): UFix64 {
let acct = getAccount(account)
let vaultRef = acct.getCapability(/public/exampleTokenBalance)!.borrow<&ExampleToken.Vault{FungibleToken.Balance}>()
?? panic("Could not borrow Balance reference to the Vault")
access(all) fun main(address: Address): UFix64 {
let vaultData = ExampleToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not get vault data view for the contract")

return vaultRef.balance
}
return getAccount(address).capabilities.borrow<&{FungibleToken.Balance}>(
vaultData.metadataPath
)?.balance
?? panic("Could not borrow Balance reference to the Vault")
}
22 changes: 15 additions & 7 deletions cadence/transactions/create-account.cdc
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import FlowManager from 0x01

transaction (_ name: String?, pubKey: [String], manager: Address) {
prepare( admin: AuthAccount) {
let newAccount = AuthAccount(payer:admin)
prepare( admin: auth(BorrowValue) &Account) {
let newAccount = Account(payer:admin)
for key in pubKey {
newAccount.addPublicKey(key.decodeHex())
let keyData = RLP.decodeList(key.decodeHex())
let rawSign = RLP.decodeString(keyData[1])[0]
let rawHash = RLP.decodeString(keyData[2])[0]
newAccount.keys.add(
publicKey: PublicKey(
publicKey: RLP.decodeString(keyData[0]),
signatureAlgorithm: SignatureAlgorithm(rawValue: rawSign)!
),
hashAlgorithm: HashAlgorithm(rawValue: rawHash)!,
weight: UFix64(Int32.fromBigEndianBytes(RLP.decodeString(keyData[3]))!)!
)
}

if name != nil {
let linkPath = FlowManager.accountManagerPath
let accountManager = getAccount(manager)
.getCapability(linkPath)!
.borrow<&FlowManager.Mapper>()!

let accountManager = getAccount(manager).capabilities.borrow<&FlowManager.Mapper>(linkPath)!

// Create a record in account database
let address = newAccount.address
accountManager.setAddress(name!, address: address)
Expand Down
6 changes: 2 additions & 4 deletions cadence/transactions/deploy-contract.cdc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import FlowManager from 0x01

transaction(name:String, code: String, manager: Address ##ARGS-WITH-TYPES##) {
prepare(acct: AuthAccount){
prepare(acct: auth(AddContract) &Account) {
let decoded = code.decodeHex()
acct.contracts.add(
name: name,
Expand All @@ -10,9 +10,7 @@ transaction(name:String, code: String, manager: Address ##ARGS-WITH-TYPES##) {
)

let linkPath = FlowManager.contractManagerPath
let contractManager = getAccount(manager)
.getCapability(linkPath)!
.borrow<&FlowManager.Mapper>()!
let contractManager = getAccount(manager).capabilities.borrow<&FlowManager.Mapper>(linkPath)!

let address = acct.address
contractManager.setAddress(name, address: address)
Expand Down
2 changes: 1 addition & 1 deletion cadence/transactions/init-manager.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
transaction ( code: String ) {
prepare( admin: AuthAccount) {
prepare( admin: &Account) {
admin.contracts.add(
name: "FlowManager",
code: code.decodeHex(),
Expand Down
31 changes: 20 additions & 11 deletions cadence/transactions/mint_tokens.cdc
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import FungibleToken from 0xFUNGIBLETOKENADDRESS
import ExampleToken from 0xTOKENADDRESS
import FungibleToken from 0x1
import ExampleToken from 0x1
import FungibleTokenMetadataViews from 0x1

transaction(recipient: Address, amount: UFix64) {
let tokenAdmin: &ExampleToken.Administrator
let tokenAdmin: &FlowToken.Administrator
let tokenReceiver: &{FungibleToken.Receiver}
let supplyBefore: UFix64

prepare(signer: AuthAccount) {
self.tokenAdmin = signer
.borrow<&ExampleToken.Administrator>(from: /storage/exampleTokenAdmin)
?? panic("Signer is not the token admin")
prepare(signer: auth(BorrowValue) &Account) {
self.supplyBefore = ExampleToken.totalSupply

self.tokenReceiver = getAccount(recipient)
.getCapability(/public/exampleTokenReceiver)!
.borrow<&{FungibleToken.Receiver}>()
?? panic("Unable to borrow receiver reference")
// Borrow a reference to the admin object
self.tokenAdmin = signer.storage.borrow<&ExampleToken.Administrator>(from: ExampleToken.AdminStoragePath)
?? panic("Signer is not the token admin")

let vaultData = ExampleToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not get vault data view for the contract")

self.tokenReceiver = getAccount(recipient).capabilities.borrow<&{FungibleToken.Receiver}>(vaultData.receiverPath)
?? panic("Could not borrow receiver reference to the Vault")
}

execute {
Expand All @@ -24,4 +29,8 @@ transaction(recipient: Address, amount: UFix64) {

destroy minter
}

post {
ExampleToken.totalSupply == self.supplyBefore + amount: "The total supply must be increased by the amount"
}
}
6 changes: 2 additions & 4 deletions cadence/transactions/register-contract.cdc
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import FlowManager from 0x01

transaction(name: String, address: Address) {
prepare(signer: AuthAccount){
prepare(signer: auth(BorrowValue) &Account){
let linkPath = FlowManager.contractManagerPath
let contractManager = signer
.getCapability(linkPath)!
.borrow<&FlowManager.Mapper>()!
let contractManager = getAccount(manager).capabilities.borrow<&FlowManager.Mapper>(linkPath)!
contractManager.setAddress(name, address: address)
}
}
Loading

0 comments on commit f4780cd

Please sign in to comment.