1
1
import * as algokit from '@algorandfoundation/algokit-utils'
2
2
import { NftMarketplaceClient } from '../artifacts/nft_marketplace/client'
3
3
import { TransactionSignerAccount } from '@algorandfoundation/algokit-utils/types/account'
4
+ import { Config as AlgokitConfig } from '@algorandfoundation/algokit-utils'
4
5
5
6
// Below is a showcase of various deployment options you can use in TypeScript Client
6
7
export async function deploy ( ) {
7
8
console . log ( '=== Deploying NftMarketplaceClient ===' )
9
+ AlgokitConfig . configure ( { populateAppCallResources : true } )
8
10
9
11
// 여러 클라이언트 생성
10
12
const algod = algokit . getAlgoClient ( )
@@ -15,8 +17,7 @@ export async function deploy() {
15
17
// 랜덤 계정 생성 후 자금 지급
16
18
const deployer = await algorand . account . random ( )
17
19
const buyer = await algorand . account . random ( )
18
- const lateBuyer = await algorand . account . random ( )
19
- const accounts = [ deployer , buyer , lateBuyer ]
20
+ const accounts = [ deployer , buyer ]
20
21
21
22
const dispenser = await algorand . account . dispenser ( )
22
23
for ( let i = 0 ; i < accounts . length ; i ++ ) {
@@ -65,25 +66,29 @@ export async function deploy() {
65
66
sender : deployer . addr ,
66
67
receiver : app . appAddress ,
67
68
amount : algokit . algos ( 0.2 ) ,
69
+ extraFee : algokit . algos ( 0.001 ) ,
68
70
} )
69
71
70
- // 앱이 판매할 준비가 되도록 Bootstrap 메서드를 호출
71
- await appClient . bootstrap (
72
- { asset : assetId , unitaryPrice : unitaryPrice , mbrPay : mbrPay } ,
73
- { sendParams : { fee : algokit . transactionFees ( 2 ) , populateAppCallResources : true , suppressLog : true } } ,
74
- )
75
- console . log ( '2. 앱 부트스트래핑 완료!' )
72
+ const sendAssetToSell = {
73
+ sender : deployer . addr ,
74
+ receiver : app . appAddress ,
75
+ assetId : assetId ,
76
+ amount : 100n ,
77
+ }
76
78
77
- // NftMarketplaceClient 앱에 판매할 NFT 에셋 송금
78
- await algorand . send . assetTransfer (
79
- {
79
+ // 앱이 판매할 준비가 되도록 Bootstrap 메서드를 호출, NftMarketplaceClient 앱에 판매할 NFT 에셋 송금
80
+ await algorand
81
+ . newGroup ( )
82
+ . addMethodCall ( {
80
83
sender : deployer . addr ,
81
- receiver : app . appAddress ,
82
- assetId : assetId ,
83
- amount : 100n ,
84
- } ,
85
- { suppressLog : true } ,
86
- )
84
+ appId : BigInt ( app . appId ) ,
85
+ method : appClient . appClient . getABIMethod ( 'bootstrap' ) ! ,
86
+ args : [ assetId , unitaryPrice , mbrPay ] ,
87
+ } )
88
+ . addAssetTransfer ( sendAssetToSell )
89
+ . execute ( )
90
+
91
+ console . log ( '2. 앱 부트스트래핑 완료!' )
87
92
console . log ( '3. IU 티켓 에셋 앱으로 송금 완료!' )
88
93
89
94
// 구매자 앱 클라이언트 생성. 이 앱 클라이언트는 구매자 계정과 연동됨.
@@ -96,15 +101,6 @@ export async function deploy() {
96
101
algod ,
97
102
)
98
103
99
- const lateBuyerAppClient = new NftMarketplaceClient (
100
- {
101
- resolveBy : 'id' ,
102
- sender : lateBuyer ,
103
- id : app . appId ,
104
- } ,
105
- algod ,
106
- )
107
-
108
104
// 구매자가 에섯에 옵트인하고 buy 메서드를 호출하는 함수
109
105
async function buyAsset (
110
106
appClient : NftMarketplaceClient ,
@@ -115,42 +111,61 @@ export async function deploy() {
115
111
appAddr : string ,
116
112
unitaryPrice : number ,
117
113
) : Promise < void > {
114
+ // NftMarketplaceClient buy 메서드 호출때 구매 비용 지불로 사용할 결제 트랜잭션 생성
115
+ const buyNftPay = await algorand . transactions . payment ( {
116
+ sender : buyer . addr ,
117
+ receiver : appAddr ,
118
+ amount : algokit . algos ( ( unitaryPrice * buyAmount ) / 1_000_000 ) ,
119
+ } )
120
+
118
121
try {
119
122
let assetInfo = await algorand . account . getAssetInformation ( buyer , assetId )
120
123
console . log ( `${ buyerName } 가 이미 에셋에 옵트인 되어있어요! 현재 보유한 티켓 수: ${ assetInfo . balance } 개` )
121
124
} catch ( e ) {
122
125
console . log ( `${ buyerName } 가 에셋에 옵트인이 안 되어있어요. 옵트인 진행할게요~` )
123
- // 구매자가 NFT에 옵트인
124
- await algorand . send . assetOptIn (
125
- {
126
+
127
+ // buy메서드 앱 호출 트랜잭션 생성
128
+ const buyAppCall = await appClient
129
+ . compose ( )
130
+ . buy (
131
+ {
132
+ buyerTxn : buyNftPay ,
133
+ quantity : buyAmount ,
134
+ } ,
135
+ { sendParams : { fee : algokit . transactionFees ( 2 ) , suppressLog : true } } ,
136
+ )
137
+ . atc ( )
138
+
139
+ // 구매자가 NFT에 옵트인, buyNftPay 결제 트랜잭션, NftMarketplaceClient buy 메서드를 어토믹 트랜잭션으로 동시에 호출
140
+ await algorand
141
+ . newGroup ( )
142
+ . addAssetOptIn ( {
126
143
sender : buyer . addr ,
127
144
assetId : assetId ,
128
- } ,
129
- { suppressLog : true } ,
130
- )
131
- }
145
+ } )
146
+ . addAtc ( buyAppCall )
147
+ . execute ( )
132
148
133
- // NftMarketplaceClient buy 메서드 호출때 구매 비용 지불로 사용할 결제 트랜잭션 생성
134
- const buyNftPay = await algorand . transactions . payment ( {
135
- sender : buyer . addr ,
136
- receiver : appAddr ,
137
- amount : algokit . algos ( ( unitaryPrice * buyAmount ) / 1_000_000 ) ,
138
- } )
149
+ const assetInfo = await algorand . account . getAssetInformation ( buyer , assetId )
150
+ console . log ( `${ buyerName } 가 티켓 ${ buyAmount } 장을 구매하여 ${ assetInfo . balance } 개의 티켓을 보유하고 있어요!` )
151
+ return
152
+ }
139
153
140
- // 위 결제 트랜잭션과 NftMarketplaceClient buy 메서드를 어토믹 트랜잭션으로 동시에 호출
154
+ // 계정이 이미 구매할 에셋(아이유 콘서트 티켓)에 옵트인 되어 있을 시 buy 메서드만 호출
141
155
await appClient . buy (
142
156
{
143
157
buyerTxn : buyNftPay ,
144
158
quantity : buyAmount ,
145
159
} ,
146
- { sendParams : { fee : algokit . transactionFees ( 2 ) , populateAppCallResources : true , suppressLog : true } } ,
160
+ { sendParams : { fee : algokit . transactionFees ( 2 ) , suppressLog : true } } ,
147
161
)
148
162
149
163
const assetInfo = await algorand . account . getAssetInformation ( buyer , assetId )
150
- console . log ( `${ buyerName } 가 티켓 1장을 구매하여 ${ assetInfo . balance } 개의 티켓을 보유하고 있어요!` )
164
+ console . log ( `${ buyerName } 가 티켓 ${ buyAmount } 장을 구매하여 ${ assetInfo . balance } 개의 티켓을 보유하고 있어요!` )
151
165
}
152
166
153
167
await buyAsset ( buyerAppClient , 'buyer' , buyer , assetId , 1 , app . appAddress , unitaryPrice )
168
+ await buyAsset ( buyerAppClient , 'buyer' , buyer , assetId , 2 , app . appAddress , unitaryPrice )
154
169
155
170
// 판매자가 NftMarketplaceClient 앱을 삭제하며 수익금과 잔여 NFT 에셋을 회수
156
171
await appClient . delete . withdrawAndDelete (
0 commit comments