Skip to content

Commit

Permalink
Fix initialization and editing of blog and post
Browse files Browse the repository at this point in the history
metadata
  • Loading branch information
d0rich committed Nov 22, 2023
1 parent de7cf63 commit 25aa809
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 13 deletions.
23 changes: 16 additions & 7 deletions contracts/contracts/d_social_network_blog.tact
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ contract DSocialNetworkBlog with NftCollection, NftCollectionRoyaltyExtention {
denominator: 100,
destination: master_contract
};
// TODO: set real metadata
self.collection_content = NftCollectionMetadata {
image: "",
name: "",
Expand Down Expand Up @@ -50,12 +49,22 @@ contract DSocialNetworkBlog with NftCollection, NftCollectionRoyaltyExtention {
self.owner = msg.owner;
self.collection_content = msg.collection_content;
self.is_initialized = true;
send(SendParameters{
to: self.owner,
value: self.calculateSendAmountToHoldEnoughTonForStorage(),
mode: SendIgnoreErrors,
body: Excesses { query_id: msg.query_id }.toCell()
});
self.sendExcessesTo(
self.calculateSendAmountToHoldEnoughTonForStorage(),
self.owner,
msg.query_id
);
}

receive(msg: EditBlogMetadata) {
self.requireInitialization();
self.requireOwner();
self.collection_content = msg.new_metadata;
self.sendExcessesTo(
self.calculateSendAmountToHoldEnoughTonForStorage(),
self.owner,
msg.query_id
);
}

fun requireInitialization() {
Expand Down
12 changes: 10 additions & 2 deletions contracts/contracts/d_social_network_post.tact
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ contract DSocialNetworkPost with NftItem {
self.collection_address = collection_address;
self.item_index = item_index;
self.owner = collection_address;
// TODO: set individual_content
self.individual_content = NftMetadata {
name: "",
description: "",
Expand All @@ -32,5 +31,14 @@ contract DSocialNetworkPost with NftItem {
};
}


receive(msg: EditBlogPost) {
require(self.is_initialized, "This post is not initialized yet");
self.requireOwner();
self.individual_content = msg.new_metadata;
self.sendExcessesTo(
self.calculateSendAmountToHoldEnoughTonForStorage(),
self.owner,
msg.query_id
);
}
}
13 changes: 13 additions & 0 deletions contracts/contracts/lib/trait_accounting.tact
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,17 @@ trait Accounting {
fun calculateSendAmountToHoldFeeFromRecieved(personalFee: Int): Int {
return self.calculateSendAmountToHoldEnoughTonForStorage() - personalFee;
}

fun sendExcessesTo(amount: Int, to: Address, query_id: Int?) {
let sendQueryId: Int = 0;
if (query_id != null) {
sendQueryId = query_id!!;
}
send(SendParameters{
to: to,
value: amount,
mode: SendIgnoreErrors,
body: Excesses { query_id: sendQueryId }.toCell()
});
}
}
10 changes: 10 additions & 0 deletions contracts/contracts/messages_d.tact
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ message InitializeBlog {
collection_content: NftCollectionMetadata;
}

message EditBlogMetadata {
query_id: Int as uint64;
new_metadata: NftCollectionMetadata;
}

struct BlogInfo {
id: Int;
master_contract: Address;
Expand All @@ -25,6 +30,11 @@ struct BlogInfo {

// Post contract

message EditBlogPost {
query_id: Int as uint64;
new_metadata: NftMetadata;
}

struct PostInfo {
blog: Address;
id: Int;
Expand Down
53 changes: 52 additions & 1 deletion contracts/tests/DSocialNetworkBlog.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Blockchain, SandboxContract } from '@ton-community/sandbox'
import { toNano } from 'ton-core'
import { DSocialNetworkMaster } from '../wrappers/DSocialNetworkMaster'
import { DSocialNetworkBlog, MintNft } from '../wrappers/DSocialNetworkBlog'
import {
DSocialNetworkBlog,
EditBlogMetadata,
MintNft
} from '../wrappers/DSocialNetworkBlog'
import '@ton-community/test-utils'
import {
getTestPostModel,
Expand Down Expand Up @@ -87,12 +91,59 @@ describe('DSocialNetworkMaster', () => {

expect(postAddress).not.toBeNull()

// Should top up post balance
expect(createPostResult.transactions).toHaveTransaction({
from: dBlog.address,
to: postAddress!,
success: true
})

// Should return excesses to owner
expect(createPostResult.transactions).toHaveTransaction({
from: postAddress!,
to: deployer.address,
success: true
})

expect(await dBlog.getGetNextItemIndex()).toBe(1n)
})

it('Should edit blog metadata correctly', async () => {
const editBlogMetadataMessage: EditBlogMetadata = {
$$type: 'EditBlogMetadata',
query_id: 0n,
new_metadata: {
$$type: 'NftCollectionMetadata',
name: 'New blog name',
description: 'New blog description',
image: 'New blog avatar'
}
}

const editBlogMetadataResult = await dBlog.send(
deployer.getSender(),
{ value: toNano('0.2') },
editBlogMetadataMessage
)

// Should pay for changes
expect(editBlogMetadataResult.transactions).toHaveTransaction({
from: deployer.address,
to: dBlog.address,
success: true
})

// Should return excesses
expect(editBlogMetadataResult.transactions).toHaveTransaction({
from: dBlog.address,
to: deployer.address,
success: true
})

const newMetadata = await dBlog.getGetBlogInfo()

expect(newMetadata.collection_content).toEqual(
editBlogMetadataMessage.new_metadata
)
})
})
12 changes: 10 additions & 2 deletions contracts/tests/DSocialNetworkMaster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('DSocialNetworkMaster', () => {
})

it('Create blog', async () => {
const registerResult = await dMaster.send(
const createBlogResult = await dMaster.send(
deployer.getSender(),
{ value: toNano('0.2') },
registerTestAccountMessage
Expand All @@ -48,12 +48,20 @@ describe('DSocialNetworkMaster', () => {

expect(blogAddress).not.toBeNull()

expect(registerResult.transactions).toHaveTransaction({
// Should top up blog balance
expect(createBlogResult.transactions).toHaveTransaction({
from: dMaster.address,
to: blogAddress!,
success: true
})

// Should return excesses
expect(createBlogResult.transactions).toHaveTransaction({
from: blogAddress!,
to: deployer.address,
success: true
})

expect(await dMaster.getGetBlogsCount()).toBe(1n)

blockchain.openContract(DSocialNetworkBlog.fromAddress(blogAddress!))
Expand Down
43 changes: 42 additions & 1 deletion contracts/tests/DSocialNetworkPost.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Blockchain, SandboxContract } from '@ton-community/sandbox'
import { toNano } from 'ton-core'
import { DSocialNetworkMaster } from '../wrappers/DSocialNetworkMaster'
import { DSocialNetworkBlog, MintNft } from '../wrappers/DSocialNetworkBlog'
import {
DSocialNetworkBlog,
MintNft,
NftMetadata
} from '../wrappers/DSocialNetworkBlog'
import '@ton-community/test-utils'
import { DSocialNetworkPost } from '../wrappers/DSocialNetworkPost'
import {
Expand Down Expand Up @@ -136,4 +140,41 @@ describe('DSocialNetworkMaster', () => {
description: postMetadata.nft_content.description
})
})

it('Post should be editable', async () => {
const newPostMetadata: NftMetadata = {
$$type: 'NftMetadata',
name: 'New post name',
description: 'New post description',
image: 'New post cover'
}

const editPostResult = await dPost.send(
deployer.getSender(),
{ value: toNano('0.1') },
{
$$type: 'EditBlogPost',
query_id: 0n,
new_metadata: newPostMetadata
}
)

// Should top up post balance
expect(editPostResult.transactions).toHaveTransaction({
from: deployer.address,
to: dPost.address,
success: true
})

// Should return excesses to owner
expect(editPostResult.transactions).toHaveTransaction({
from: dPost.address,
to: deployer.address,
success: true
})

const postMetadata = await dPost.getGetPostInfo()

expect(postMetadata.nft_content).toEqual(newPostMetadata)
})
})

0 comments on commit 25aa809

Please sign in to comment.