Lev
2 years ago
8 changed files with 191 additions and 133 deletions
@ -1,56 +0,0 @@
|
||||
import chai, { assert, expect } from "chai"; |
||||
import chaiBN from "chai-bn"; |
||||
import BN from "bn.js"; |
||||
chai.use(chaiBN(BN)); |
||||
|
||||
import { Builder, Cell, Slice } from "ton"; |
||||
import { SmartContract } from "ton-contract-executor"; |
||||
import * as main from "../contracts/main"; |
||||
import { internalMessage, randomAddress } from "./helpers"; |
||||
|
||||
import { hex } from "../build/main.compiled.json"; |
||||
import { makeSnakeCell } from "../contracts/utils"; |
||||
import { signVerify } from "ton-crypto"; |
||||
|
||||
describe("Selling tests (instant buy)", () => { |
||||
let contract: SmartContract; |
||||
let debug: boolean = true; |
||||
let keyPair = main.genKeyPair(); |
||||
|
||||
beforeEach(async () => { |
||||
contract = await SmartContract.fromCell( |
||||
Cell.fromBoc(hex)[0], |
||||
main.data({ |
||||
ownerAddress: randomAddress("owner"), |
||||
code: Cell.fromBoc(hex)[0], |
||||
collectionAddress: randomAddress("collection"), |
||||
domain: "alice", |
||||
publicKey: keyPair.publicKey |
||||
}), |
||||
{ debug: debug } |
||||
); |
||||
}); |
||||
|
||||
it("Sell", async () => { |
||||
main.setContractBalance(contract, 10 * main.TON()); |
||||
const sellMessage = internalMessage({ |
||||
from: randomAddress("buyer"), |
||||
body: main.instantBuyMessage({ |
||||
receiverAddress: randomAddress("buyer"), |
||||
issuedCollectionAddr: randomAddress("collection"), |
||||
price: 10 * main.TON(), |
||||
domain: "bob", |
||||
privateKey: keyPair.secretKey |
||||
}), |
||||
value: new BN(10 * main.TON()) |
||||
}); |
||||
context("Public key is correct", async () => { |
||||
const pubKeyRes = await contract.invokeGetMethod("get_public_key", []); |
||||
expect(pubKeyRes.result[0]).to.equal(keyPair.publicKey); |
||||
}); |
||||
|
||||
const res = await contract.sendInternalMessage(sellMessage);
|
||||
expect(res.type).to.equal("success"); |
||||
expect(res.exit_code).to.equal(0); |
||||
}); |
||||
}); |
@ -0,0 +1,71 @@
|
||||
import chai, {assert, expect} from "chai"; |
||||
import chaiBN from "chai-bn"; |
||||
import BN from "bn.js"; |
||||
|
||||
chai.use(chaiBN(BN)); |
||||
|
||||
import {Address, Builder, Cell, contractAddress, Slice} from "ton"; |
||||
import {runContract, SmartContract} from "ton-contract-executor"; |
||||
import * as main from "../contracts/main"; |
||||
import {internalMessage, randomAddress} from "./helpers"; |
||||
|
||||
import {hex} from "../build/nft-collection.compiled.json"; |
||||
import {makeSnakeCell} from "../contracts/utils"; |
||||
import {keyPairFromSeed, KeyPair, sign, keyPairFromSecretKey} from "ton-crypto"; |
||||
import {signBuy} from "../contracts/main"; |
||||
|
||||
let ownerKeys = keyPairFromSeed(Buffer.from("0000000000000000000000000000000000000000000000000000000000000000", "hex")); |
||||
let ownerPubNum = new BN(ownerKeys.publicKey); |
||||
let data = main.collectionData({ |
||||
ownerAddress: randomAddress("owner"), |
||||
code: Cell.fromBoc(hex)[0], |
||||
ownerKey: ownerPubNum, |
||||
}); |
||||
|
||||
describe("Creating items tests", () => { |
||||
let contract: SmartContract; |
||||
let debug: boolean = true; |
||||
|
||||
beforeEach(async () => { |
||||
contract = await SmartContract.fromCell( |
||||
Cell.fromBoc(hex)[0], |
||||
data, |
||||
{debug: debug} |
||||
); |
||||
contract.setC7Config({ |
||||
myself: randomAddress("collection") |
||||
}) |
||||
}); |
||||
|
||||
it("allows to buy an item with a valid signature", async () => { |
||||
main.setContractBalance(contract, 10 * main.TON(), randomAddress("collection")); |
||||
let ownerAddr = randomAddress("dude"); |
||||
let signature = signBuy("test", randomAddress("collection"), ownerAddr, ownerKeys.secretKey); |
||||
const sendToSelfMessage = internalMessage({ |
||||
from: ownerAddr, |
||||
body: main.createItem({domain: "test", signature: signature}), |
||||
value: new BN(100 * main.TON()), |
||||
}); |
||||
const res = await contract.sendInternalMessage(sendToSelfMessage); |
||||
// console.log(res);
|
||||
let fs = require('fs'); |
||||
fs.writeFile('logs.txt', res.logs, (_: any) => {}); |
||||
expect(res.type).to.equal("success"); |
||||
expect(res.exit_code).to.equal(0); |
||||
}); |
||||
it("does not allow to buy an item if the signature is invalid", async () => { |
||||
main.setContractBalance(contract, 10 * main.TON(), randomAddress("collection")); |
||||
let ownerAddr = randomAddress("dude"); |
||||
let signature = signBuy("test", randomAddress("collection"), ownerAddr, ownerKeys.secretKey); |
||||
const sendToSelfMessage = internalMessage({ |
||||
from: ownerAddr, |
||||
body: main.createItem({domain: "test", signature: signature}), |
||||
value: new BN(100 * main.TON()), |
||||
}); |
||||
const res = await contract.sendInternalMessage(sendToSelfMessage); |
||||
expect(res.type).to.equal("success"); |
||||
expect(res.exit_code).to.equal(0); |
||||
|
||||
}); |
||||
|
||||
}); |
Loading…
Reference in new issue