From 19a35feca3a0822e0662b3f8f008f250bb1e4155 Mon Sep 17 00:00:00 2001 From: a0p0bhv Date: Tue, 18 Jul 2023 03:25:46 +0530 Subject: [PATCH] FIX Tx --- flow.json | 44 +++++++++++++ src/utils/createBasketToken.ts | 31 +++++++++ src/utils/customTokenContract.ts | 107 +++++++++++++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 flow.json create mode 100644 src/utils/createBasketToken.ts create mode 100644 src/utils/customTokenContract.ts diff --git a/flow.json b/flow.json new file mode 100644 index 0000000..87ffe66 --- /dev/null +++ b/flow.json @@ -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" + ] + } + } +} + diff --git a/src/utils/createBasketToken.ts b/src/utils/createBasketToken.ts new file mode 100644 index 0000000..97e8eee --- /dev/null +++ b/src/utils/createBasketToken.ts @@ -0,0 +1,31 @@ +import { customTokenContract } from "./customTokenContract" + +export const tokenData: Record = { + "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) +} \ No newline at end of file diff --git a/src/utils/customTokenContract.ts b/src/utils/customTokenContract.ts new file mode 100644 index 0000000..78413ca --- /dev/null +++ b/src/utils/customTokenContract.ts @@ -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) + } + }`; + }; + \ No newline at end of file