Skip to content

Commit

Permalink
Add functions to give information from the tickets. (#39)
Browse files Browse the repository at this point in the history
* Add support to common resources

* Update

* Update

* First draft

* flow.json

* Add tests as overflow.go

* Add ways to get flow repayment address

* Refine tests

* Resolve conversation

* Add info to helper struct

* Add ci tests

* pr

* Amend struct

* fix test

* enhance ci tests

* enhance tests per Austins comment

* Add to mainnet

* Update test/fungible-token.test.js

Co-authored-by: Austin Kline <[email protected]>

Co-authored-by: Hui Ben <[email protected]>
Co-authored-by: Austin Kline <[email protected]>
  • Loading branch information
3 people authored Sep 1, 2022
1 parent f9bff2f commit ab82f74
Show file tree
Hide file tree
Showing 17 changed files with 2,507 additions and 19 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,27 @@ jobs:
run: npm ci
- name: Run tests
run: npm test -- --runInBand

tests-overflow:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.18
- uses: actions/cache@v2
with:
path: |
~/go/pkg/mod
~/.cache/go-build
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: run tests
run: go test -timeout 30m -json ./... > test.json
- name: Annotate tests
if: always()
uses: guyarb/[email protected]
with:
test-results: test.json
35 changes: 31 additions & 4 deletions cadence/contracts/LostAndFound.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,31 @@ pub contract LostAndFound {
return self.item != nil
}

// A function to get depositor address / flow Repayment address
pub fun getFlowRepaymentAddress() : Address? {
return self.flowTokenRepayment?.address
}

// If this is an instance of NFT, return the id , otherwise return nil
pub fun getNonFungibleTokenID() : UInt64? {
if self.type.isSubtype(of: Type<@NonFungibleToken.NFT>()) {
let ref = (&self.item as auth &AnyResource?)!
let nft = ref as! &NonFungibleToken.NFT
return nft.id
}
return nil
}

// If this is an instance of FT, return the vault balance , otherwise return nil
pub fun getFungibleTokenBalance() : UFix64? {
if self.type.isSubtype(of: Type<@FungibleToken.Vault>()) {
let ref = (&self.item as auth &AnyResource?)!
let ft = ref as! &FungibleToken.Vault
return ft.balance
}
return nil
}

pub fun withdraw(receiver: Capability) {
pre {
receiver.address == self.redeemer: "receiver address and redeemer must match"
Expand Down Expand Up @@ -392,7 +417,7 @@ pub contract LostAndFound {
display: MetadataViews.Display?,
storagePayment: &FungibleToken.Vault,
flowTokenRepayment: Capability<&FlowToken.Vault{FungibleToken.Receiver}>?
) {
) : UInt64 {
pre {
flowTokenRepayment == nil || flowTokenRepayment!.check(): "flowTokenRepayment is not valid"
storagePayment.getType() == Type<@FlowToken.Vault>(): "storage payment must be in flow tokens"
Expand Down Expand Up @@ -425,6 +450,7 @@ pub contract LostAndFound {

let storagePaymentVault <- storagePayment.withdraw(amount: storageFee)
receiver.deposit(from: <-storagePaymentVault)
return uuid
}

pub fun borrowShelf(redeemer: Address): &LostAndFound.Shelf? {
Expand Down Expand Up @@ -489,7 +515,7 @@ pub contract LostAndFound {
item: @AnyResource,
memo: String?,
display: MetadataViews.Display?
) {
) : UInt64 {
let receiver = LostAndFound.account
.getCapability<&FlowToken.Vault{FungibleToken.Receiver}>(/public/flowTokenReceiver)
.borrow()!
Expand Down Expand Up @@ -520,6 +546,7 @@ pub contract LostAndFound {
let storagePaymentVault <- self.withdrawTokens(amount: storageFee)

receiver.deposit(from: <-storagePaymentVault)
return uuid
}

pub fun trySendResource(
Expand Down Expand Up @@ -679,14 +706,14 @@ pub contract LostAndFound {
display: MetadataViews.Display?,
storagePayment: &FungibleToken.Vault,
flowTokenRepayment: Capability<&FlowToken.Vault{FungibleToken.Receiver}>?
) {
) : UInt64 {
pre {
flowTokenRepayment == nil || flowTokenRepayment!.check(): "flowTokenRepayment is not valid"
storagePayment.getType() == Type<@FlowToken.Vault>(): "storage payment must be in flow tokens"
}

let shelfManager = LostAndFound.borrowShelfManager()
shelfManager.deposit(redeemer: redeemer, item: <-item, memo: memo, display: display, storagePayment: storagePayment, flowTokenRepayment: flowTokenRepayment)
return shelfManager.deposit(redeemer: redeemer, item: <-item, memo: memo, display: display, storagePayment: storagePayment, flowTokenRepayment: flowTokenRepayment)
}

pub fun trySendResource(
Expand Down
42 changes: 42 additions & 0 deletions cadence/contracts/LostAndFoundHelper.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import LostAndFound from "./LostAndFound.cdc"

pub contract LostAndFoundHelper {

pub struct Ticket {

// An optional message to attach to this item.
pub let memo: String?
// The address that it allowed to withdraw the item fromt this ticket
pub let redeemer: Address
//The type of the resource (non-optional) so that bins can represent the true type of an item
pub let type: Type
pub let typeIdentifier: String
// State maintained by LostAndFound
pub let redeemed: Bool
pub let name : String?
pub let description : String?
pub let thumbnail : String?
pub let ticketID : UInt64?

init(_ ticket: &LostAndFound.Ticket, id: UInt64?) {
self.memo = ticket.memo
self.redeemer = ticket.redeemer
self.type = ticket.type
self.typeIdentifier = ticket.type.identifier
self.redeemed = ticket.redeemed
self.name = ticket.display?.name
self.description = ticket.display?.description
self.thumbnail = ticket.display?.thumbnail?.uri()
self.ticketID = id
}

}

pub fun constructResult(_ ticket: &LostAndFound.Ticket?, id:UInt64?) : LostAndFoundHelper.Ticket? {
if ticket != nil {
return LostAndFoundHelper.Ticket(ticket!, id: id)
}
return nil
}

}
14 changes: 14 additions & 0 deletions cadence/scripts/ExampleNFT/borrow_all_tickets_as_struct.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import LostAndFound from "../../contracts/LostAndFound.cdc"
import LostAndFoundHelper from "../../contracts/LostAndFoundHelper.cdc"

pub fun main(addr: Address): [LostAndFoundHelper.Ticket] {

let res : [LostAndFoundHelper.Ticket] = []
for ticket in LostAndFound.borrowAllTickets(addr: addr) {
if let t = LostAndFoundHelper.constructResult(ticket, id: nil) {
res.append(t)
}
}

return res
}
13 changes: 13 additions & 0 deletions cadence/scripts/ExampleNFT/get_bin_nft_id.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import ExampleNFT from "../../contracts/ExampleNFT.cdc"
import LostAndFound from "../../contracts/LostAndFound.cdc"

pub fun main(addr: Address, type: String): [UInt64] {
let tickets = LostAndFound.borrowAllTicketsByType(addr: addr, type: CompositeType(type)!)
let ids : [UInt64] = []
for ticket in tickets {
if let id = ticket.getNonFungibleTokenID() {
ids.append(id)
}
}
return ids
}
14 changes: 14 additions & 0 deletions cadence/scripts/ExampleToken/borrow_ticket_as_struct.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import FungibleToken from "../../contracts/FungibleToken.cdc"
import ExampleToken from "../../contracts/ExampleToken.cdc"

import LostAndFound from "../../contracts/LostAndFound.cdc"
import LostAndFoundHelper from "../../contracts/LostAndFoundHelper.cdc"

pub fun main(addr: Address, ticketID: UInt64): LostAndFoundHelper.Ticket? {
let shelfManager = LostAndFound.borrowShelfManager()
let shelf = shelfManager.borrowShelf(redeemer: addr)
let bin = shelf!.borrowBin(type: Type<@ExampleToken.Vault>())!

let ticket = bin.borrowTicket(id: ticketID)
return LostAndFoundHelper.constructResult(ticket, id: ticketID)
}
14 changes: 14 additions & 0 deletions cadence/scripts/ExampleToken/get_bin_vault_balance.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import FungibleToken from "../../contracts/FungibleToken.cdc"
import LostAndFound from "../../contracts/LostAndFound.cdc"

pub fun main(addr: Address, type: String): UFix64 {
let tickets = LostAndFound.borrowAllTicketsByType(addr: addr, type: CompositeType(type)!)
var balance : UFix64 = 0.0
for ticket in tickets {
if let b = ticket.getFungibleTokenBalance() {
balance = balance + b
}
}
return balance
}

14 changes: 14 additions & 0 deletions cadence/scripts/get_flowRepayment_address.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import LostAndFound from "../contracts/LostAndFound.cdc"

pub fun main(addr: Address, type: String, ticketID: UInt64): Address? {
let shelfManager = LostAndFound.borrowShelfManager()
if let shelf = shelfManager.borrowShelf(redeemer: addr) {
if let bin = shelf.borrowBin(type: CompositeType(type)!) {
if let ticket = bin.borrowTicket(id: ticketID) {
return ticket.getFlowRepaymentAddress()
}
}
}
return nil
}

Loading

0 comments on commit ab82f74

Please sign in to comment.