Skip to content

Commit

Permalink
FIX Tx
Browse files Browse the repository at this point in the history
  • Loading branch information
a0p0bhv committed Jul 17, 2023
1 parent 7d6e789 commit 19a35fe
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 0 deletions.
44 changes: 44 additions & 0 deletions flow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"contracts": {
"MetadataViews": {
"source": "./src/cadence/contracts/MetadataViews.cdc",
"aliases": {
"emulator": "f8d6e0586b0a20c7",
"testnet": "631e88ae7f1d7c20"
}
},
"NonFungibleToken": {
"source": "./src/cadence/contracts/NonFungibleToken.cdc",
"aliases": {
"emulator": "f8d6e0586b0a20c7",
"testnet": "631e88ae7f1d7c20"
}
},
"FungibleToken": {
"source": "./src/cadence/contracts/FungibleToken.cdc",
"aliases": {
"emulator": "ee82856bf20e2aa6",
"testnet": "9a0766d93b6608b7"
}
}
},
"networks": {
"emulator": "127.0.0.1:3569",
"mainnet": "access.mainnet.nodes.onflow.org:9000",
"testnet": "access.devnet.nodes.onflow.org:9000"
},
"accounts": {
"admin": {
"address": "fe38a2978b72a792",
"key": "6cd89cf81377378c20b823743788e88e8d504200d87adf6a2619c79be9afd40e"
}
},
"deployments": {
"testnet": {
"admin" : [
"FlowForm"
]
}
}
}

31 changes: 31 additions & 0 deletions src/utils/createBasketToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { customTokenContract } from "./customTokenContract"

export const tokenData: Record<string, any> = {
"A.e45c64ecfe31e465.stFlowToken.Vault": {
receiver: "/public/stFlowTokenReceiver",
storage: "/storage/stFlowTokenVault",
balance: "/public/stFlowTokenBalance"
},
"A.a983fecbed621163.FiatToken.Vault": {
receiver: "/public/USDCVaultReceiver",
storage: "/storage/USDCVault",
balance: "/public/USDCVaultBalance",
}
}

const createBasketToken = async () => {
const name = "DFI"
const underlyingTokens = '["A.e45c64ecfe31e465.stFlowToken.Vault", "A.a983fecbed621163.FiatToken.Vault"]'
const contractCode = customTokenContract(name, "0x9a0766d93b6608b7", underlyingTokens)

const transactionId = await fcl.mutate({
cadence: `transaction() { prepare(acct: AuthAccount) {} execute { log("Hello, Flow!") } }`,
limit: 9999,
payer: signer,
proposer: signer,
authorizations: [signer],
})

console.log(transactionId)
subscribeTxStatus(transactionId)
}
107 changes: 107 additions & 0 deletions src/utils/customTokenContract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
export const customTokenContract = (
tokenName: string,
fungibleTokenStandardAddress: string,
underlyingTokens: string
) => {
return `
import FungibleToken from ${fungibleTokenStandardAddress}
access(all) contract ${tokenName}: FungibleToken {
pub var totalSupply: UFix64
/// TokensInitialized
///
/// The event that is emitted when the contract is created
pub event TokensInitialized(initialSupply: UFix64)
/// TokensWithdrawn
///
/// The event that is emitted when tokens are withdrawn from a Vault
pub event TokensWithdrawn(amount: UFix64, from: Address?)
/// TokensDeposited
///
/// The event that is emitted when tokens are deposited to a Vault
pub event TokensDeposited(amount: UFix64, to: Address?)
/// TokensMinted
///
/// The event that is emitted when new tokens are minted
pub event TokensMinted(amount: UFix64)
pub let TokenVaultStoragePath: StoragePath
pub let TokenVaultPublicPath: PublicPath
pub let TokenMinterStoragePath: StoragePath
/// Underlying Token
access(self) let underlyingTokens: [String]
pub resource Vault: FungibleToken.Provider, FungibleToken.Receiver, FungibleToken.Balance {
pub var balance: UFix64
init(balance: UFix64) {
self.balance = balance
}
pub fun withdraw(amount: UFix64): @FungibleToken.Vault {
self.balance = self.balance - amount
emit TokensWithdrawn(amount: amount, from: self.owner?.address)
return <- create Vault(balance: amount)
}
pub fun deposit(from: @FungibleToken.Vault) {
let vault <- from as! @${tokenName}.Vault
self.balance = self.balance + vault.balance
emit TokensDeposited(amount: vault.balance, to: self.owner?.address)
vault.balance = 0.0
destroy vault // Make sure we get rid of the vault
}
destroy() {
${tokenName}.totalSupply = ${tokenName}.totalSupply - self.balance
}
}
pub fun createEmptyVault(): @FungibleToken.Vault {
return <- create Vault(balance: 0.0)
}
access(contract) fun initialMint(initialMintValue: UFix64): @FungibleToken.Vault {
return <- create Vault(balance: initialMintValue)
}
pub fun getUnderlyingTokens(): [String] {
return self.underlyingTokens
}
pub resource Minter {
pub fun mintTokens(amount: UFix64): @FungibleToken.Vault {
pre {
amount > 0.0: "Amount minted must be greater than zero"
}
${tokenName}.totalSupply = ${tokenName}.totalSupply + amount
return <- create Vault(balance:amount)
}
pub fun destroyToken(token: @FungibleToken.Vault) {
destroy token
}
}
init() {
self.totalSupply = 0.0
self.TokenVaultStoragePath = /storage/${tokenName}Vault
self.TokenVaultPublicPath = /public/${tokenName}Vault
self.TokenMinterStoragePath = /storage/${tokenName}Minter
self.account.save(<- create Minter(), to: ${tokenName}.TokenMinterStoragePath)
self.underlyingTokens = ${underlyingTokens}
//
// Create an Empty Vault for the Minter
//
self.account.save(<- ${tokenName}.initialMint(initialMintValue: self.totalSupply), to: ${tokenName}.TokenVaultStoragePath)
self.account.link<&${tokenName}.Vault{FungibleToken.Balance, FungibleToken.Receiver}>(${tokenName}.TokenVaultPublicPath, target: ${tokenName}.TokenVaultStoragePath)
}
}`;
};

0 comments on commit 19a35fe

Please sign in to comment.