|
|
|
@ -1,16 +1,16 @@
|
|
|
|
|
import BN from "bn.js"; |
|
|
|
|
import { Cell, beginCell, Address } from "ton"; |
|
|
|
|
import { C7Config, SmartContract } from "ton-contract-executor"; |
|
|
|
|
import {Cell, beginCell, Address} from "ton"; |
|
|
|
|
import {C7Config, SmartContract} from "ton-contract-executor"; |
|
|
|
|
import {encodeOffChainContent, makeSnakeCell} from "./utils"; |
|
|
|
|
import { randomBytes } from "crypto"; |
|
|
|
|
import { keyPairFromSeed, KeyPair, sign } from "ton-crypto"; |
|
|
|
|
import { ExpansionPanelActions } from "@material-ui/core"; |
|
|
|
|
import {randomBytes} from "crypto"; |
|
|
|
|
import {keyPairFromSeed, KeyPair, sign} from "ton-crypto"; |
|
|
|
|
import {ExpansionPanelActions} from "@material-ui/core"; |
|
|
|
|
|
|
|
|
|
// encode contract storage according to save_data() contract method
|
|
|
|
|
|
|
|
|
|
export function genKeyPair(): KeyPair { |
|
|
|
|
let seed = randomBytes(32); |
|
|
|
|
return keyPairFromSeed(seed); |
|
|
|
|
let seed = randomBytes(32); |
|
|
|
|
return keyPairFromSeed(seed); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// nft_item_code:^Cell
|
|
|
|
@ -22,89 +22,100 @@ export function genKeyPair(): KeyPair {
|
|
|
|
|
// cell auction - auction info
|
|
|
|
|
// int64 last_fill_up_time
|
|
|
|
|
export function data(params: { ownerAddress: Address; collectionAddress: Address, code: Cell, domain: String, publicKey: Buffer }): Cell { |
|
|
|
|
let data_cell = beginCell() |
|
|
|
|
// For code: https://github.com/getgems-io/nft-contracts/blob/main/packages/contracts/sources/nft-auction/build.sh
|
|
|
|
|
.storeRef(params.code) |
|
|
|
|
.storeUint(0, 256) |
|
|
|
|
.storeAddress(params.collectionAddress) |
|
|
|
|
.storeAddress(params.ownerAddress) |
|
|
|
|
.storeRef(encodeOffChainContent("https://agorata.io/collection.json")) // https://github.com/ton-blockchain/TEPs/blob/master/text/0064-token-data-standard.md
|
|
|
|
|
.storeRef(makeSnakeCell(Buffer.from(params.domain))) |
|
|
|
|
.storeDict(null) |
|
|
|
|
.storeUint(0, 64).endCell(); |
|
|
|
|
let data_cell = beginCell() |
|
|
|
|
// For code: https://github.com/getgems-io/nft-contracts/blob/main/packages/contracts/sources/nft-auction/build.sh
|
|
|
|
|
.storeRef(params.code) |
|
|
|
|
.storeUint(0, 256) |
|
|
|
|
.storeAddress(params.collectionAddress) |
|
|
|
|
.storeAddress(params.ownerAddress) |
|
|
|
|
.storeRef(encodeOffChainContent("https://agorata.io/collection.json")) // https://github.com/ton-blockchain/TEPs/blob/master/text/0064-token-data-standard.md
|
|
|
|
|
.storeRef(makeSnakeCell(Buffer.from(params.domain))) |
|
|
|
|
.storeDict(null) |
|
|
|
|
.storeUint(0, 64).endCell(); |
|
|
|
|
|
|
|
|
|
return beginCell().storeRef(data_cell).storeBuffer(params.publicKey).endCell(); |
|
|
|
|
return beginCell().storeRef(data_cell).storeBuffer(params.publicKey).endCell(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function collectionData(params: { code: Cell, ownerAddress: Address, ownerKey: number }): Cell { |
|
|
|
|
export function collectionData(params: { |
|
|
|
|
code: Cell, ownerAddress: Address, ownerKey: number, |
|
|
|
|
price_multiplier?: number, price_steepness?: number |
|
|
|
|
}): Cell { |
|
|
|
|
if (params.price_multiplier == undefined) { |
|
|
|
|
params.price_multiplier = 10; |
|
|
|
|
} |
|
|
|
|
if (params.price_steepness == undefined) { |
|
|
|
|
params.price_steepness = 5; |
|
|
|
|
} |
|
|
|
|
return beginCell() |
|
|
|
|
.storeRef(encodeOffChainContent("https://agorata.io/collection.json")) |
|
|
|
|
.storeRef(params.code) |
|
|
|
|
.storeRef(beginCell().endCell()) |
|
|
|
|
.storeRef(beginCell().storeUint(params.price_multiplier, 8).storeUint(params.price_steepness, 4).endCell()) |
|
|
|
|
.storeUint(params.ownerKey, 256) |
|
|
|
|
.storeAddress(params.ownerAddress) |
|
|
|
|
.endCell(); |
|
|
|
|
.endCell(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function auctionWithWinner(winnerAddress: Address) { |
|
|
|
|
return beginCell().storeAddress(winnerAddress).storeCoins(0).storeUint(0, 64) |
|
|
|
|
return beginCell().storeAddress(winnerAddress).storeCoins(0).storeUint(0, 64) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function setContractBalance(contract: SmartContract, balance: number) { |
|
|
|
|
contract.setC7Config({balance: balance}); |
|
|
|
|
contract.setC7Config({balance: balance}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function TON(): number { return 1000000000; } |
|
|
|
|
export function TON(): number { |
|
|
|
|
return 1000000000; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// message encoders for all ops (see contracts/imports/constants.fc for consts)
|
|
|
|
|
|
|
|
|
|
export function transferOwnership(params: { newOwnerAddress: Address }): Cell { |
|
|
|
|
return beginCell().storeUint(0x5fcc3d14, 32).storeUint(0, 64).storeAddress(params.newOwnerAddress).storeAddress(null).storeInt(0, 1).storeCoins(1 * TON()).endCell(); |
|
|
|
|
return beginCell().storeUint(0x5fcc3d14, 32).storeUint(0, 64).storeAddress(params.newOwnerAddress).storeAddress(null).storeInt(0, 1).storeCoins(1 * TON()).endCell(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function createItem(params: { domain: String }): Cell { |
|
|
|
|
let signature = '000'; |
|
|
|
|
return beginCell() |
|
|
|
|
.storeUint(0, 32) |
|
|
|
|
.storeRef(makeSnakeCell(Buffer.from(params.domain + ';' + signature))) |
|
|
|
|
.endCell(); |
|
|
|
|
let signature = '000'; |
|
|
|
|
return beginCell() |
|
|
|
|
.storeUint(0, 32) |
|
|
|
|
.storeRef(makeSnakeCell(Buffer.from(params.domain + ';' + signature))) |
|
|
|
|
.endCell(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function instantBuySignature(receiverAddress: Address, issuedCollectionAddr: Address, amount: number, domain: Cell, privateKey: Buffer): Buffer { |
|
|
|
|
let messageToSign = beginCell().storeAddress(receiverAddress).storeAddress(issuedCollectionAddr).storeUint(amount, 256).storeRef(domain).endCell(); |
|
|
|
|
let hash = messageToSign.hash(); |
|
|
|
|
// console.log(hash.toString("hex"));
|
|
|
|
|
// console.log((new BN(hash)).toString(10))
|
|
|
|
|
// return sign(hash, privateKey);
|
|
|
|
|
let althash = new BN("FF11841721E4DAD5AE679A1A338B2EBFC5AAF7529C200B4EF9D71831B1DCB969", "hex"); |
|
|
|
|
return sign(althash.toBuffer(), privateKey); |
|
|
|
|
let messageToSign = beginCell().storeAddress(receiverAddress).storeAddress(issuedCollectionAddr).storeUint(amount, 256).storeRef(domain).endCell(); |
|
|
|
|
let hash = messageToSign.hash(); |
|
|
|
|
// console.log(hash.toString("hex"));
|
|
|
|
|
// console.log((new BN(hash)).toString(10))
|
|
|
|
|
// return sign(hash, privateKey);
|
|
|
|
|
let althash = new BN("FF11841721E4DAD5AE679A1A338B2EBFC5AAF7529C200B4EF9D71831B1DCB969", "hex"); |
|
|
|
|
return sign(althash.toBuffer(), privateKey); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function instantBuyMessage(params: { receiverAddress: Address, issuedCollectionAddr: Address, price: number, domain: String, privateKey: Buffer}): Cell { |
|
|
|
|
let domainSnakeCell = makeSnakeCell(Buffer.from(params.domain)); |
|
|
|
|
let signature = instantBuySignature(params.receiverAddress, params.issuedCollectionAddr, params.price, domainSnakeCell, params.privateKey); |
|
|
|
|
console.log(signature.toString("hex").toUpperCase()); |
|
|
|
|
let cell = beginCell() |
|
|
|
|
.storeUint(0x16c7d435, 32) // opcode
|
|
|
|
|
.storeUint(0, 64) // query id
|
|
|
|
|
.storeBuffer(signature) // body
|
|
|
|
|
.storeUint(params.price, 256) |
|
|
|
|
.storeRef(domainSnakeCell).endCell(); |
|
|
|
|
return cell; |
|
|
|
|
export function instantBuyMessage(params: { receiverAddress: Address, issuedCollectionAddr: Address, price: number, domain: String, privateKey: Buffer }): Cell { |
|
|
|
|
let domainSnakeCell = makeSnakeCell(Buffer.from(params.domain)); |
|
|
|
|
let signature = instantBuySignature(params.receiverAddress, params.issuedCollectionAddr, params.price, domainSnakeCell, params.privateKey); |
|
|
|
|
console.log(signature.toString("hex").toUpperCase()); |
|
|
|
|
let cell = beginCell() |
|
|
|
|
.storeUint(0x16c7d435, 32) // opcode
|
|
|
|
|
.storeUint(0, 64) // query id
|
|
|
|
|
.storeBuffer(signature) // body
|
|
|
|
|
.storeUint(params.price, 256) |
|
|
|
|
.storeRef(domainSnakeCell).endCell(); |
|
|
|
|
return cell; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function currentState(contract: SmartContract) { |
|
|
|
|
let c4 = contract.dataCell.beginParse(); |
|
|
|
|
let reader = c4.readRef(); |
|
|
|
|
let c4 = contract.dataCell.beginParse(); |
|
|
|
|
let reader = c4.readRef(); |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
nft_item_code: reader.readRef(), |
|
|
|
|
index: reader.readUint(256), |
|
|
|
|
collectionAddress: reader.readAddress(), |
|
|
|
|
ownerAddress: reader.readAddress(), |
|
|
|
|
collectionContent: reader.readRef(), |
|
|
|
|
domain: reader.readRef(), |
|
|
|
|
// auction: reader.readCell(), – TODO: still havent's figured out to load auction here
|
|
|
|
|
// lastFillUpTime: reader.readInt(64)
|
|
|
|
|
} |
|
|
|
|
return { |
|
|
|
|
nft_item_code: reader.readRef(), |
|
|
|
|
index: reader.readUint(256), |
|
|
|
|
collectionAddress: reader.readAddress(), |
|
|
|
|
ownerAddress: reader.readAddress(), |
|
|
|
|
collectionContent: reader.readRef(), |
|
|
|
|
domain: reader.readRef(), |
|
|
|
|
// auction: reader.readCell(), – TODO: still havent's figured out to load auction here
|
|
|
|
|
// lastFillUpTime: reader.readInt(64)
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|