Hey guys,
I am just trying to deploy and execute our smart contract. Currently I am struggling with calling encodeCalldata
successfully.
This is our contract:
contract PaymentSplitter =
record state =
{ owner: address,
recipientConditions: map(address, int) } // map of recipients with percentage to receive (value between 1 and 100)
// CONTRACT EVENTS
datatype event = AddingInitialRecipients()
| RecipientAdded(indexed address, indexed int)
| AddressUpdated(indexed address, indexed address)
| UpdatingAllRecipients()
| PaymentReceivedAndSplitted(indexed address, indexed int, indexed int)
// CONSTRUCTOR
public stateful function init(recipientConditions': map(address, int)) : state =
require(sumWeights(recipientConditions') == 100, "sum of weights needs to be 100")
Chain.event(AddingInitialRecipients)
{ owner = Call.caller,
recipientConditions = recipientConditions'}
// READ ONLY FUNCTIONS
public function getOwner() : address =
state.owner
public function getRecipientsCount() : int =
Map.size(state.recipientConditions)
public function isRecipient(who': address) : bool =
Map.member(who', state.recipientConditions)
public function getWeight(who': address) : int =
Map.lookup_default(who', state.recipientConditions, 0)
// PAY-AND-SPLIT FUNCTION
public function payAndSplit() =
require(Contract.balance > 0, "contract didn't receive any payment")
let recipientConditions: list((address, int)) = Map.to_list(state.recipientConditions)
split(recipientConditions, Contract.balance)
Chain.event(PaymentReceivedAndSplitted(Call.caller, Call.value, Contract.balance))
// STATEFUL FUNCTIONS
public stateful function transferOwnership(newOwner': address) =
onlyOwner()
put(state{owner = newOwner'})
public stateful function updateAddress(oldAddress': address, newAddress': address) =
onlyOwnerOrRecipient(oldAddress')
let weight: int = state.recipientConditions[oldAddress']
put(state{recipientConditions @ rc = Map.delete(oldAddress', rc)}) // remove old address
put(state{recipientConditions[newAddress'] = weight}) // add new address
Chain.event(AddressUpdated(oldAddress', newAddress'))
public stateful function updateRecipientConditions(recipients': map(address, int)) =
onlyOwner()
Chain.event(UpdatingAllRecipients)
require(sumWeights(recipients') == 100, "sum of weights needs to be 100")
put(state{recipientConditions = recipients'})
fireRecipientAddedEvents(Map.to_list(state.recipientConditions))
// PRIVATE FUNCTIONS
private function onlyOwner() =
require(Call.caller == state.owner, "caller must be the owner")
private function onlyOwnerOrRecipient(recipient': address) =
require(Call.caller == state.owner || Call.caller == recipient', "caller must be the owner or the recipient")
private function sumWeights(recipients': map(address, int)) : int =
let recipientList: list((address, int)) = Map.to_list(recipients')
let intList: list(int) = map(pair_second, recipientList)
sum(intList, (x) => x)
private function fireRecipientAddedEvents(recipientConditions': list((address, int))) =
switch(recipientConditions')
[] => ()
(recipient, weight) :: l' =>
Chain.event(RecipientAdded(recipient, weight))
private function split(recipientConditions': list((address, int)), totalValue: int) =
switch(recipientConditions')
[] => ()
(recipient, weight) :: l' =>
Chain.spend(recipient, totalValue / 100 * weight)
split(l', totalValue)
// GENERIC HELPER FUNCTIONS
private function map(f : 'a => 'b, l : list('a)) : list('b) =
switch(l)
[] => []
e :: l' => f(e) :: map(f, l')
private function foldr(f : (('a, 'b) => 'b), z: 'b, l : list('a)) : 'b =
switch(l)
[] => z
e :: l' => f(e, foldr(f, z, l'))
private function sum(l : list('a), f : 'a => int) : int =
foldr((x, y) => x + y, 0, map(f, l))
private function pair_second(tuple) =
switch(tuple)
(_, e) => e
private function require(b: bool, err: string) =
if(!b) abort(err)
Currently I am trying to encode the calldata for the init function in order to deploy the contract. What I am doing is calling the API with the following parameters:
- source: String that contains the code above
- function:
init
- arguments: a list that contains the following string:
[{'ak_YZj5WoWTBvga5MBXHa49SLaScbqbop2NVn5WLDsbXwS71aFrM': 40}, {'ak_ERsQrUnSN7RX3NNGRbuu9skZuBJ6cJLHwcE4FZ2XAotKoR6bo': 40}, {'ak_TtQYek584v6M2XYEKzBMPRqdat8FSUr2dqNshvN14RjvX6q8W': 20}]
Can somebody of the team please tell how the argument must look like?