Skip to content

Commit

Permalink
minimize contract calls needed
Browse files Browse the repository at this point in the history
  • Loading branch information
thepiwo committed Feb 5, 2021
1 parent 35dd219 commit f2dad54
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
12 changes: 10 additions & 2 deletions contracts/FungibleTokenCustom.aes
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

@compiler >= 4

contract TokenSale =
stateful entrypoint set_token : () => unit

contract WordRegistry =
stateful entrypoint add_token : (TokenSale, string) => unit

include "Option.aes"

/// @title - Fungible token with all the extensions - burn, mint, allowances
Expand Down Expand Up @@ -64,15 +70,17 @@ contract FungibleTokenFull =
// Create a fungible token with
// the following `name` `symbol` and `decimals`
// and set the inital smart contract state
entrypoint init(name: string, decimals : int, symbol : string, owner : address) =
entrypoint init(name: string, decimals : int, symbol : string, token_sale : TokenSale, word_registry : WordRegistry) =
// If the `name` lenght is less than 1 symbol abort the execution
require(String.length(name) >= 1, "STRING_TOO_SHORT_NAME")
// If the `symbol` length is less than 1 symbol abort the execution
require(String.length(symbol) >= 1, "STRING_TOO_SHORT_SYMBOL")
// If the provided value for `decimals` is negative abort the execution
require_non_negative_value(decimals)

{ owner = owner,
token_sale.set_token()
word_registry.add_token(token_sale, symbol)
{ owner = token_sale.address,
total_supply = 0,
balances = {},
meta_info = { name = name, symbol = symbol, decimals = decimals },
Expand Down
3 changes: 2 additions & 1 deletion contracts/TokenSale.aes
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ contract TokenSale =
// require(vote_timeout == (60 / 3) * 24 * 7, "INCORRECT_VOTE_TIMEOUT")
{ token = None, votes = {}, spread = 0, vote_timeout = vote_timeout, bonding_curve = bonding_curve, description = description }

stateful entrypoint set_token(token : Token) : unit =
stateful entrypoint set_token() : unit =
let token : Token = Address.to_contract(Call.caller)
require(!has_token(), "TOKEN_ALREADY_SET")
put(state{ token = Some(token) })

Expand Down
3 changes: 1 addition & 2 deletions contracts/WordRegistry.aes
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ contract WordRegistry =
{ tokens = {},
owner = Call.caller }

stateful entrypoint add_token(token_sale : TokenSale) : unit =
let symbol = token_sale.get_token().meta_info().symbol
stateful entrypoint add_token(token_sale : TokenSale, symbol : string) : unit =
require(!Map.member(symbol, state.tokens), "SYMBOL_ALREADY_EXISTENT")
put(state{ tokens = state.tokens{ [symbol] = token_sale } })

Expand Down
13 changes: 9 additions & 4 deletions test/tokenSaleVotingTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const TOKEN_SALE_INTERFACE = readFileRelative('./contracts/interfaces/TokenSaleI
const TOKEN = readFileRelative('./contracts/FungibleTokenCustom.aes', 'utf-8');
const TOKEN_VOTING = readFileRelative('./contracts/TokenVoting.aes', 'utf-8');
const TOKEN_VOTING_INTERFACE = readFileRelative('./contracts/interfaces/TokenVotingInterface.aes', 'utf-8');
const WORD_REGISTRY = readFileRelative('./contracts/WordRegistry.aes', 'utf-8');
const BONDING_CURVE = require('sophia-bonding-curve/BondCurveLinear.aes')

const config = {
Expand All @@ -21,7 +22,7 @@ const config = {
};

describe('Token- Sale and Voting Contracts', () => {
let client, sale, voting, token, bondingCurve;
let client, sale, voting, token, registry, bondingCurve;

before(async () => {
client = await Universal({
Expand Down Expand Up @@ -54,12 +55,16 @@ describe('Token- Sale and Voting Contracts', () => {
assert.equal(init.result.returnType, 'ok');
});

it('Deploy Registry', async () => {
registry = await client.getContractInstance(WORD_REGISTRY);
const init = await registry.methods.init();
assert.equal(init.result.returnType, 'ok');
});

it('Deploy and set Token', async () => {
token = await client.getContractInstance(TOKEN);
const init = await token.methods.init("Test Token", 0, "TT", sale.deployInfo.address.replace('ct_', 'ak_'));
const init = await token.methods.init("Test Token", 0, "TT", sale.deployInfo.address, registry.deployInfo.address);
assert.equal(init.result.returnType, 'ok');
const set = await sale.methods.set_token(token.deployInfo.address);
assert.equal(set.result.returnType, 'ok');
});

it('Deploy Voting Contract', async () => {
Expand Down
5 changes: 1 addition & 4 deletions test/wordRegistryTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ describe('WordRegistry Contract', () => {

it('Deploy and add Token', async () => {
token = await client.getContractInstance(TOKEN);
const init = await token.methods.init("Test Token", 0, "TT", contract.deployInfo.address.replace('ct_', 'ak_'));
const init = await token.methods.init("Test Token", 0, "TT", sale.deployInfo.address, contract.deployInfo.address);
assert.equal(init.result.returnType, 'ok');
await sale.methods.set_token(token.deployInfo.address);
const set = await contract.methods.add_token(sale.deployInfo.address);
assert.equal(set.result.returnType, 'ok');
});

it('Get State', async () => {
Expand Down

0 comments on commit f2dad54

Please sign in to comment.