Skip to content

Commit

Permalink
did task format so as it can pass the CI format check
Browse files Browse the repository at this point in the history
Signed-off-by: b-l-u-e <[email protected]>
  • Loading branch information
b-l-u-e committed Oct 24, 2024
1 parent b924d65 commit 829b83c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
46 changes: 23 additions & 23 deletions Examples/AddNftAllowance/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,55 +41,55 @@ internal enum Program {
.freezeWith(client)
.execute(client)
.getReceipt(client)

guard let nftTokenId = nftCreateReceipt.tokenId else {
fatalError("Failed to create NFT")
}

print("Created NFT with token ID: \(nftTokenId)")

// Step 2: Mint NFTs
let cids = [
"QmNPCiNA3Dsu3K5FxDPMG5Q3fZRwVTg14EXA92uqEeSRXn",
"QmZ4dgAgt8owvnULxnKxNe8YqpavtVCXmc1Lt2XajFpJs9",
"QmPzY5GxevjyfMUF5vEAjtyRoigzWp47MiKAtLBduLMC1T"
"QmPzY5GxevjyfMUF5vEAjtyRoigzWp47MiKAtLBduLMC1T",
]

for cid in cids {
let mintReceipt = try await TokenMintTransaction()
.tokenId(nftTokenId)
.metadata(cid.data(using: .utf8)!)
.freezeWith(client)
.execute(client)
.getReceipt(client)

guard let serials = mintReceipt.serials else {
fatalError("Failed to mint NFT")
}

print("Minted NFT (token ID: \(nftTokenId)) with serial: \(serials.first!)")
}

// Step 3: Create spender and receiver accounts
let spenderKey = PrivateKey.generateEd25519()
let receiverKey = PrivateKey.generateEd25519()

let spenderAccountId = try await AccountCreateTransaction()
.key(spenderKey.publicKey)
.initialBalance(Hbar(2))
.execute(client)
.getReceipt(client)
.accountId!

let receiverAccountId = try await AccountCreateTransaction()
.key(receiverKey.publicKey)
.initialBalance(Hbar(2))
.execute(client)
.getReceipt(client)
.accountId!

print("Created spender account ID: \(spenderAccountId), receiver account ID: \(receiverAccountId)")

// Step 4: Associate spender and receiver with the NFT
try await TokenAssociateTransaction()
.accountId(spenderAccountId)
Expand All @@ -98,55 +98,55 @@ internal enum Program {
.sign(spenderKey)
.execute(client)
.getReceipt(client)

try await TokenAssociateTransaction()
.accountId(receiverAccountId)
.tokenIds([nftTokenId])
.freezeWith(client)
.sign(receiverKey)
.execute(client)
.getReceipt(client)

print("Associated spender and receiver accounts with NFT")

// Step 5: Approve NFT allowance for spender
try await AccountAllowanceApproveTransaction()
.approveTokenNftAllowance(NftId(tokenId: nftTokenId, serial: 1), env.operatorAccountId, spenderAccountId)
.approveTokenNftAllowance(NftId(tokenId: nftTokenId, serial: 2), env.operatorAccountId, spenderAccountId)
.execute(client)

print("Approved NFT allowance for spender")

// Step 6: Transfer NFT using approved allowance
let transferReceipt = try await TransferTransaction()
.addApprovedNftTransfer(NftId(tokenId: nftTokenId, serial: 1), env.operatorAccountId, receiverAccountId)
.freezeWith(client)
.sign(spenderKey)
.execute(client)
.getReceipt(client)

print("Transfer successful with status: \(transferReceipt.status)")

// Step 7: Revoke allowance
try await AccountAllowanceDeleteTransaction()
.deleteAllTokenNftAllowances(NftId(tokenId: nftTokenId, serial: 2), env.operatorAccountId)
.execute(client)

print("Revoked NFT allowance")

// Step 8: Attempt transfer after revoking allowance
do {
_ = try await TransferTransaction()
.addApprovedNftTransfer(NftId(tokenId: nftTokenId, serial: 2), env.operatorAccountId, receiverAccountId)
.freezeWith(client)
.sign(spenderKey)
.execute(client)

print("Transfer after revoking allowance should have failed")
} catch {
print("Expected failure: \(error)")
}

// Cleanup resources by deleting tokens, accounts, etc.
// Implement cleanup steps...
}
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import PackageDescription

let exampleTargets = [
"AddNftAllowance",
"AccountAlias",
"AccountAllowance",
"AddNftAllowance",
"ConsensusPubSub",
"ConsensusPubSubChunked",
"ConsensusPubSubWithSubmitKey",
Expand Down
Binary file added bin/task
Binary file not shown.

0 comments on commit 829b83c

Please sign in to comment.