forked from getgems-io/nft-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NftFixpriceSaleV3.data.ts
141 lines (122 loc) · 4.07 KB
/
NftFixpriceSaleV3.data.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { Address, Builder, Cell, contractAddress, StateInit } from 'ton'
import BN from 'bn.js'
import { NftFixPriceSaleV3CodeCell } from './NftFixpriceSaleV3.source'
export type NftFixPriceSaleV3Data = {
isComplete: boolean
createdAt: number
marketplaceAddress: Address
nftAddress: Address
nftOwnerAddress: Address | null
fullPrice: BN
marketplaceFeeAddress: Address
marketplaceFee: BN
royaltyAddress: Address
royaltyAmount: BN
canDeployByExternal?: boolean
}
export function buildNftFixPriceSaleV3DataCell(data: NftFixPriceSaleV3Data) {
const feesCell = new Cell()
feesCell.bits.writeAddress(data.marketplaceFeeAddress)
feesCell.bits.writeCoins(data.marketplaceFee)
feesCell.bits.writeAddress(data.royaltyAddress)
feesCell.bits.writeCoins(data.royaltyAmount)
const dataCell = new Cell()
dataCell.bits.writeUint(data.isComplete ? 1 : 0, 1)
dataCell.bits.writeUint(data.createdAt, 32)
dataCell.bits.writeAddress(data.marketplaceAddress)
dataCell.bits.writeAddress(data.nftAddress)
dataCell.bits.writeAddress(data.nftOwnerAddress)
dataCell.bits.writeCoins(data.fullPrice)
dataCell.refs.push(feesCell)
dataCell.bits.writeUint(data.canDeployByExternal ? 1 : 0, 1) // can_deploy_by_external
return dataCell
}
export function buildNftFixPriceSaleV3StateInit(
data: Omit<NftFixPriceSaleV3Data, 'nftOwnerAddress' | 'isComplete'>
) {
const dataCell = buildNftFixPriceSaleV3DataCell({
...data,
// Nft owner address would be set by NFT itself by ownership_assigned callback
nftOwnerAddress: null,
isComplete: false,
})
const stateInit = new StateInit({
code: NftFixPriceSaleV3CodeCell,
data: dataCell,
})
const address = contractAddress({
workchain: 0,
initialCode: NftFixPriceSaleV3CodeCell,
initialData: dataCell,
})
return {
address,
stateInit,
}
}
export const OperationCodes = {
AcceptCoins: 1,
Buy: 2,
CancelSale: 3,
ChangePrice: 0x6c6c2080,
}
export const Queries = {
cancelSale: (params: { queryId?: number }) => {
const msgBody = new Cell()
msgBody.bits.writeUint(OperationCodes.CancelSale, 32)
msgBody.bits.writeUint(params.queryId ?? 0, 64)
return msgBody
},
deployMsg: (params: { queryId?: number }) => {
const msgBody = new Cell()
msgBody.bits.writeUint(OperationCodes.AcceptCoins, 32)
msgBody.bits.writeUint(params.queryId ?? 0, 64)
return msgBody
},
changePrice: (params: { price: BN, marketplaceFee: BN, royaltyAmount: BN }) => {
const msgBody = new Cell()
msgBody.bits.writeUint(OperationCodes.ChangePrice, 32)
msgBody.bits.writeUint(0, 64) // query id
msgBody.bits.writeCoins(params.price)
msgBody.bits.writeCoins(params.marketplaceFee)
msgBody.bits.writeCoins(params.royaltyAmount)
return msgBody
},
buyMessage(param: { queryId: bigint }) {
return new Builder().storeUint(OperationCodes.Buy, 32)
.storeUint(new BN(param.queryId.toString()), 64)
.endCell()
},
}
export type NftFixPriceSaleV3R3Data = {
isComplete: boolean
createdAt: number
marketplaceAddress: Address
nftAddress: Address
nftOwnerAddress: Address | null
fullPrice: BN
marketplaceFeeAddress: Address
marketplaceFee: BN
royaltyAddress: Address
royaltyAmount: BN
soldAt: number
queryId: BN
}
export function buildNftFixPriceSaleV3R3DataCell(data: NftFixPriceSaleV3Data | NftFixPriceSaleV3R3Data) {
const feesCell = new Cell()
feesCell.bits.writeAddress(data.marketplaceFeeAddress)
feesCell.bits.writeCoins(data.marketplaceFee)
feesCell.bits.writeAddress(data.royaltyAddress)
feesCell.bits.writeCoins(data.royaltyAmount)
const dataCell = new Cell()
dataCell.bits.writeUint(data.isComplete ? 1 : 0, 1)
dataCell.bits.writeUint(data.createdAt, 32)
dataCell.bits.writeAddress(data.marketplaceAddress)
dataCell.bits.writeAddress(data.nftAddress)
dataCell.bits.writeAddress(data.nftOwnerAddress)
dataCell.bits.writeCoins(data.fullPrice)
dataCell.refs.push(feesCell)
dataCell.bits.writeUint('soldAt' in data ? data.soldAt : 0, 32)
dataCell.bits.writeUint('queryId' in data ? data.queryId : 0, 64)
return dataCell
}