Sample code
All calls to the System, Holder, Ledger and Account contracts need to be signed with the user's wallet.
const systemContract = new SystemContract(investorWallet);
// Step 1: Check if the account holder exists by calling getAccountHolders() on SystemContract, and
// iterating through each account holder returned by the function. investor here is the account creator.
await systemContract.getAccountHolders(investorWallet.address)
.then(async(resp)=>{
const holders = resp.response.result;
console.log('Got account holders for '+investorWallet.address + ',' + holders);
let holderExists = false;
let holderName = "counterpartyName3";
for (const holder of holders) {
console.log('Fetching details for account holder ' + holder);
await systemContract.getHolderDetails(holder)
.then(async(resp)=>{
const name = resp.response.result[0];
if (name === holderName) {
holderExists = true;
console.log('Found account holder');
break;
}
});
}
// Step 2: If the account holder does not exist, call createHolder() on the SystemContract and get the account holder's address
// by calling getAccountHolder() on the SystemContract.
if (!holderExists){
await systemContract.createHolder(holderName, investorWallet.address)
.then(async()=>{
console.log('Created account holder ' + holderName);
await systemContract.getAccountHolder(investorWallet.address)
.then(async(resp)=>{
const holderAddress = resp.response.result[0];
console.log('Fetching account holder address created ' + holderAddress);
// Step 3: Using account holder address, check if the ledger (note : each transaction uses two ledgers, one which is credited
// and another which is debited. For test purpose, you can use sample names of ledgers) exists by calling getAccountLedgers()
// on SystemContract and iterating through each ledger address by calling getLedgerDetails() on SystemContract.
await systemContract.getAccountLedgers(holderAddress)
.then(async(resp)=>{
const ledgers = resp.response.result;
console.log('Got account ledgers for '+ holderAddress + ',' + ledgers);
let ledgerExists = false;
let ledgerName = "creditLedger1";
for (const ledger of ledgers) {
console.log('Fetching details for account ledger ' + ledger);
await systemContract.getLedgerDetails(ledger)
.then(async(resp)=>{
const details = resp.response.result;
const name = details[0];
const group = details[1];
console.log('Got account ledger name ' + name + ' , of group ' + group);
if (name === ledgerName) {
ledgerExists = true;
break;
}
})
}
// Step 4: If the ledger does not exist, call createLedger() on the HolderContract and get the ledger address by calling
// getAccountLedger() on the SystemContract.
if (!ledgerExists) {
const holderContract = new HolderContract(investorWallet, holderAddress);
await holderContract.createLedger(ledgerName, "default")
.then(async()=>{
console.log('Created account ledger with name ' + ledgerName);
await systemContract.getAccountLedger(holderAddress)
.then(async(resp)=>{
const ledgerAddress = resp.response.result[0];
console.log('Fetching account ledger address created ' + ledgerAddress);
// Step 5: Using the ledger address, check if the account (note : one account for each ledger, so two accounts for each transaction)
// exists by calling getLedgerAccounts() on the SystemContract and iterating through each account address by calling
// getAccountDetails() on the SystemContract.
await systemContract.getLedgerAccounts(ledgerAddress)
.then(async(resp)=>{
const accounts = resp.response.result;
console.log('Got accounts for ledger '+ ledgerAddress + ',' + accounts);
let accountExists = false;
let accountName = 'test';
for (const account of accounts) { // console.log('Fetching details for account ' + account);
await systemContract.getAccountDetails(account)
.then(async(resp)=>{
const details = resp.response.result;
const name = details[0];
const currency = details[1];
console.log('Got account name ' + name + ' , with currency ' + currency);
if (name === accountName) {
accountExists = true;
break;
}
})
}
// Step 6: If account does not exist, call createAccount() on the LedgerContract and get the account address by
// calling getLedgerAccount() on the SystemContract.
if (!accountExists) {
const ledgerContract = new LedgerContract(investorWallet, ledgerAddress);
await ledgerContract.createAccount(accountName, 'VXUSD')
.then(async()=>{
console.log('Created account with name ' + accountName);
await systemContract.getLedgerAccount(ledgerAddress)
.then(async(resp)=>{
const accountAddress = resp.response.result[0];
console.log('Fetching account address created ' + accountAddress);
// Step 7: Using each account address, call postEntry() on the AccountContract.
const accountContract = new AccountContract(investorWallet, accountAddress);
var dateObj = new Date();
newdate = Math.round(new Date().getTime()/1000);
console.log("Posting entry with ledger "+ newdate);
await accountContract.postEntry(investorWallet.address,
'100',
"Credit",
"1643306294",
"Test transaction",
"Journal",
"0x76f53113a334079bb59d6189b99060c39fb1b550b6e840bad15ef86758ac8a0f")
.then(async()=>{
console.log("Posted entry with ledger");
// Step 8: Once both entries are passed (one entry in each account), call getTransactions() on each HolderContract that will
// return an array of all transactions posted for that holder address.
await holderContract.fetchTransactions('0', "1643306294")
.then(async()=>{
console.log("Fetched transactions for "+ newdate);
await holderContract.getTransactions("1643306294", "VXUSD")
.then(async(res)=>{
console.log("Number of transaction "+ res.response.result[0]);
await holderContract.getEntry("1", "1643306294", "VXUSD")
.then(async(resp)=>{
console.log("Got posted entry with ledger "
+ ", party " + resp.response.result[0]
+ ", amount " + resp.response.result[1]
+ ", transaction type " + resp.response.result[2]
+ ", description " + resp.response.result[3]
+ ", date " + resp.response.result[4]
+ ", voucher type " + resp.response.result[5]);
})
})
})
})
})
})
}
})
})
})
}
})
})
})
}
})
Copy link