Skip to content

Commit

Permalink
Test accounts registration
Browse files Browse the repository at this point in the history
  • Loading branch information
d0rich committed Nov 14, 2023
1 parent 4661102 commit ac319b0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 36 deletions.
14 changes: 11 additions & 3 deletions contracts/contracts/d_social_network_account.tact
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ contract DSocialNetworkAccount with NftCollection, NftCollectionRoyaltyExtention
self.master_contract = master_contract;
self.owner = master_contract;
self.royalty_params = RoyaltyParams{
numerator: 0,
denominator: 0,
numerator: 13,
denominator: 100,
destination: master_contract
};
// TODO: set real metadata
Expand All @@ -31,12 +31,20 @@ contract DSocialNetworkAccount with NftCollection, NftCollectionRoyaltyExtention
};
}

get fun get_account_info(): AccountInfo {
return AccountInfo{
id: self.account_id,
owner: self.owner,
collection_content: self.collection_content,
is_initialized: self.is_initialized
};
}

receive(msg: InitializeAccount) {
require(!self.is_initialized, "Account is already initialized");
self.requireOwner();
self.owner = msg.owner;
self.collection_content = msg.collection_content;
self.royalty_params = msg.royalty_params;
self.is_initialized = true;
}

Expand Down
31 changes: 15 additions & 16 deletions contracts/contracts/d_social_network_master.tact
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ contract DSocialNetworkMaster with Deployable, OwnableTransferable, Accounting
self.owner = ctx.sender;
}

get fun get_accounts_count(): Int {
return self.next_account_index;
}

get fun get_account_address_by_index(item_index: Int): Address?{
let initCode: StateInit = self.getAccountInit(item_index);
return contractAddress(initCode);
}

receive(msg: RegisterAccount) {
require(self.next_account_index >= 0, "non-sequential NFTs");
let ctx: Context = context();
Expand All @@ -26,8 +35,7 @@ contract DSocialNetworkMaster with Deployable, OwnableTransferable, Accounting
body: InitializeAccount {
query_id: 0,
owner: ctx.sender,
collection_content: self.generateAccountCollectionContent(ctx.sender),
royalty_params: self.generateAccountRoyaltyParams(ctx.sender)
collection_content: self.generateAccountCollectionContent(msg)
}.toCell(),
code: account_init.code,
data: account_init.data
Expand All @@ -39,22 +47,13 @@ contract DSocialNetworkMaster with Deployable, OwnableTransferable, Accounting
return initOf DSocialNetworkAccount(account_id, myAddress());
}

fun generateAccountCollectionContent(owner: Address): NftCollectionMetadata {
require(false, "not implemented");
fun generateAccountCollectionContent(msg: RegisterAccount): NftCollectionMetadata {
// TODO: add image link
return NftCollectionMetadata{
image: "",
name: "",
description: "",
image: "some-image-link",
name: msg.account_name,
description: msg.account_description,
social_links: emptyMap()
};
}

fun generateAccountRoyaltyParams(owner: Address): RoyaltyParams {
require(false, "not implemented");
return RoyaltyParams{
numerator: 0,
denominator: 0,
destination: myAddress()
};
}
}
10 changes: 9 additions & 1 deletion contracts/contracts/messages_d.tact
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import "./lib/messages_nft.tact";

message RegisterAccount {
query_id: Int as uint64;
account_name: String;
account_description: String;
}

// Account contract
Expand All @@ -12,5 +14,11 @@ message InitializeAccount {
query_id: Int as uint64;
owner: Address;
collection_content: NftCollectionMetadata;
royalty_params: RoyaltyParams;
}

struct AccountInfo {
id: Int;
owner: Address;
collection_content: NftCollectionMetadata;
is_initialized: Bool;
}
59 changes: 43 additions & 16 deletions contracts/tests/DSocialNetworkMaster.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import { Blockchain, SandboxContract } from '@ton-community/sandbox'
import { toNano } from 'ton-core'
import { DSocialNetworkMaster } from '../wrappers/DSocialNetworkMaster'
import { DSocialNetworkAccount } from '../wrappers/DSocialNetworkAccount'
import '@ton-community/test-utils'

describe('DSocialNetworkMaster', () => {
let blockchain: Blockchain
let dSocialNetworkMaster: SandboxContract<DSocialNetworkMaster>
let deployer: Awaited<ReturnType<typeof blockchain.treasury>>
let dMaster: SandboxContract<DSocialNetworkMaster>

beforeEach(async () => {
blockchain = await Blockchain.create()

dSocialNetworkMaster = blockchain.openContract(
await DSocialNetworkMaster.fromInit()
)

const deployer = await blockchain.treasury('deployer')

const deployResult = await dSocialNetworkMaster.send(
dMaster = blockchain.openContract(await DSocialNetworkMaster.fromInit())
deployer = await blockchain.treasury('deployer')
const deployResult = await dMaster.send(
deployer.getSender(),
{
value: toNano('0.05')
},
{ value: toNano('0.05') },
{
$$type: 'Deploy',
queryId: 0n
Expand All @@ -29,14 +24,46 @@ describe('DSocialNetworkMaster', () => {

expect(deployResult.transactions).toHaveTransaction({
from: deployer.address,
to: dSocialNetworkMaster.address,
to: dMaster.address,
deploy: true,
success: true
})
})

it('should deploy', async () => {
// the check is done inside beforeEach
// blockchain and dSocialNetworkMaster are ready to use
it('deployer shoud be owner', async () => {
const owner = await dMaster.getOwner()

expect(owner.toRawString()).toEqual(deployer.address.toRawString())
})

it('register account', async () => {
const registerResult = await dMaster.send(
deployer.getSender(),
{ value: toNano('0.5') },
{
$$type: 'RegisterAccount',
query_id: 0n,
account_name: 'd0rich',
account_description:
'This is my first account sadsadasdasdasdsadadasdsadsadsadsadasdasdadadasdasdsadasdsadadasdadasdasdasdasdasdasdasdasdadasdadasdadasdasdasdsadasdadadsdaddsadsadadd'
}
)

const accountAddress = await dMaster.getGetAccountAddressByIndex(0n)

expect(accountAddress).not.toBeNull()

expect(registerResult.transactions).toHaveTransaction({
from: dMaster.address,
to: accountAddress!,
success: true
})

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

blockchain.openContract(DSocialNetworkAccount.fromAddress(accountAddress!))

// console.log(await dAccount.getGetAccountInfo())
// console.log(await dAccount.getRoyaltyParams())
})
})

0 comments on commit ac319b0

Please sign in to comment.