Select Git revision
AssociationRegistry.jsligo
-
Joe El hajj authoredJoe El hajj authored
AssociationRegistry.jsligo 2.71 KiB
type association = {
name: string;
description: string;
admin: option<address>;
};
type storage = map<string, association>;
type return_ = [list<operation>, storage];
const myAssoAdmin = ("tz1aek2YDiM5xxqZwus1eyR3siiSNjmiJfFM" as address) ;
const registrationFeeInitial= 10 as tez;
// Register a new DAO
@entry
const registerAssociation = (association: association, storage: storage): return_ => {
let operations : list<operation> = list([]) ;
const receiver : contract<unit> =
match ((Tezos.get_contract_opt(myAssoAdmin))) {
when(Some(contract)): contract;
when(None()): (failwith ("Not a contract") as contract<unit>)
}
// Check if walletAmount is present and greater than or equal to registrationFeeInitial
if ((Tezos.get_amount())!= registrationFeeInitial) {
failwith("The amount should be 10tez exactly");
}
if(Some(Tezos.get_sender())== association.admin)
{
const payoutOperation : operation =Tezos.transaction(unit,registrationFeeInitial,receiver);
operations = list([payoutOperation]);
// Check if association already exists
}
const existingAssociation = Map.mem(association.name, storage);
if (existingAssociation) {
failwith("Association already registered");
}
// Deduct registration fee from wallet amount and create new association
const newAssociation: association = {
...association,
admin : Some(Tezos.get_sender()),
};
// Add new association to storage
const updatedStorage: storage = Map.add(association.name, newAssociation, storage);
return [operations, updatedStorage];
}
//Change Admin
@entry
const changeAdmin: (params: {associationName: string, newAdmin: address}, storage: storage) => return_ = (params, storage) => {
const {associationName, newAdmin} = params;
const existingAssociation = Map.find_opt(associationName, storage);
const association = Option.unopt_with_error(existingAssociation, "Unknown assocation");
// Check if the sender is the current admin
if (Some(Tezos.get_sender()) != association.admin) {
failwith("Only the current admin can change the admin");
}
// Update the association with the new admin address
const updatedAssociation: association = {
...association,
admin: Some(newAdmin),
};
// Update the storage with the modified association
const updatedStorage = Map.update(associationName, Some(updatedAssociation), storage);
return [list([]), updatedStorage];
};
//details d'une associations
@view
const listDetailsAssociations = (associationName:string, stor: storage): association => {
return match(Map.find_opt(associationName, stor)){
when(Some(value)): value;
when(None): failwith("No value.")
};
}
@view
const listAllAssociations = (_unused : unit,stor: storage): storage=> stor ;