Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lidangzzz committed May 2, 2023
1 parent c78a035 commit 314b018
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 8 deletions.
68 changes: 63 additions & 5 deletions darc-js/src/DARC/DARC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import * as runtime from '../runtime/runtime';
import { ethers, Contract } from 'ethers';
import * as darcBinary from '../darcBinary/darcBinary';

type initParam = {
type InitParam = {
address: string;
version: darcBinary.DARC_VERSION;
wallet?: ethers.Wallet;
provider?: ethers.providers.Provider;
}

type TokenInfo = {
votingWeight: BigInt,
dividendWeight: BigInt,
tokenInfo: string,
totalSupply: BigInt,
}

/**
* The DARC class is used to interact with the DARC contract.
Expand All @@ -24,7 +30,7 @@ export class DARC {
* The constructor of the DARC class.
* @param param: the init param including the address, version, wallet and/or provider.
*/
constructor(param: initParam) {
constructor(param: InitParam) {
const { address, version, wallet, provider } = param;
this.darcAddress = address;
this.darcVersion = version;
Expand All @@ -43,12 +49,12 @@ export class DARC {
* Run the program on the DARC contract.
* @param program: the program to be run.
*/
runProgram(program:any){
async entrance(program:any){
if (this.wallet === undefined){
throw new Error("Wallet is not provided for this DARC instance.");
}
try{
this.darcContract.entrance(program);
await this.darcContract.entrance(program);
}
catch(e){
console.log("Error when running the program: " + e);
Expand All @@ -58,5 +64,57 @@ export class DARC {
/**
* Below are all DARC's dashboard functions, read parameters from the DARC contract.
*/

async getTokenOwners(tokenClass: BigInt): Promise<string[]> {
return await this.darcContract.getTokenOwners(tokenClass);
}

async getTokenInfo(tokenClass: BigInt): Promise<TokenInfo> {
const result = await this.darcContract.getTokenInfo(tokenClass);
console.log("result: " + JSON.stringify(result));
const {0: returnVotingWeight, 1: returnDividendWeight, 2: returnTokenInfo, 3: returnTotalSupply} = result;

let returnStruct: TokenInfo = {
votingWeight: returnVotingWeight,
dividendWeight: returnDividendWeight,
tokenInfo: returnTokenInfo,
totalSupply: returnTotalSupply,
};

return returnStruct;
}

/**
* Return the address of the DARC contract.
* @returns the address of the DARC contract.
*/
address(): string {
return this.darcAddress;
}

/**
* Return the number of token classes.
* @returns the number of token classes.
*/
async getNumberOfTokenClasses(): Promise<BigInt> {
return await this.darcContract.getNumberOfTokenClasses();
}

/**
* Return the balance of the owner for the token class.
* @param tokenClass the index of the token class.
* @param owner the address of the owner.
* @returns the balance of the owner for the token class.
*/
async getTokenOwnerBalance(tokenClass: BigInt, owner: string): Promise<BigInt> {
return await this.darcContract.getTokenOwnerBalance(tokenClass, owner);
}

/**
* Get the DARC plugins.
* @returns the number of token classes.
*/
async getPluginInfo(): Promise<any> {
return await this.darcContract.getPluginInfo();
}

}
4 changes: 2 additions & 2 deletions darc-js/src/darcBinary/DARC-test.json

Large diffs are not rendered by default.

215 changes: 215 additions & 0 deletions darc-js/tests/class-DARC-test/class-DARC-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
import {ethers} from 'ethers';
import { expect } from 'chai';
import { deployDARC, attachDARCwithWallet } from '../../src/runtime/runtime';

import 'mocha';
//import { setTimeout } from "timers/promises";
const provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545/');

import { runtime_RunProgram, runtime_getTokenOwners } from '../../src/runtime/runtime';
import { DARC_VERSION, darcBinary } from '../../src/darcBinary/darcBinary';
import * as DARC from '../../src/darc/darc';

describe.only('class DARC test',
() => {
it('should return true', async () => {


/**
* create a signer with a private key
* Account #0: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 (10000 ETH)
* Private Key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
*
* (This is a test account, do not use it for anything other than testing)
*/

const provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545/');
const signer = new ethers.Wallet('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80', provider);


const wallet_address = ethers.utils.getAddress('0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266');


const darc_contract_address =
await deployDARC(DARC_VERSION.Test, {
wallet: signer,
provider: provider
});

console.log("Class DARC test - deployed at: " + darc_contract_address);

const programOperatorAddress = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266";
const target1 = '0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC';

const target2 = '0x90F79bf6EB2c4f870365E785982E1f101E93b906';

// create a token class first
//const my_addr = await darc.getMyInfo();
//console.log("my_addr: " + my_addr);
//console.log(JSON.stringify(wallet_address));

const mint_and_transfer_program = {
programOperatorAddress: programOperatorAddress,
operations: [
{
operatorAddress: programOperatorAddress,
opcode: 1, // mint token
param: {
UINT256_ARRAY: [],
ADDRESS_ARRAY: [],
STRING_ARRAY: [],
BOOL_ARRAY: [],
VOTING_RULE_ARRAY: [],
PARAMETER_ARRAY: [],
PLUGIN_ARRAY: [],
UINT256_2DARRAY: [
[BigInt(0), BigInt(1)], // token class = 0
[BigInt(100), BigInt(200)], // amount = 100
],
ADDRESS_2DARRAY: [
[programOperatorAddress,programOperatorAddress], // to = programOperatorAddress
]
}
},
{
operatorAddress: programOperatorAddress,
opcode: 3, // transfer tokens
param:{
UINT256_ARRAY: [],
ADDRESS_ARRAY: [],
STRING_ARRAY: [],
BOOL_ARRAY: [],
VOTING_RULE_ARRAY: [],
PARAMETER_ARRAY: [],
PLUGIN_ARRAY: [],
UINT256_2DARRAY: [
[BigInt(0),BigInt(0), BigInt(1), BigInt(1)], // token class = 0
[BigInt(10), BigInt(20), BigInt(30), BigInt(40)], // amount = 100
],
ADDRESS_2DARRAY: [
[target1, target2, target1, target2],
]
}
}
],
};

const create_mint_and_transter_program = {
programOperatorAddress: programOperatorAddress,
operations: [{
operatorAddress: programOperatorAddress,
opcode: 2, // create token class
param: {
UINT256_ARRAY: [],
ADDRESS_ARRAY: [],
STRING_ARRAY: ["Class1", "Class2"],
BOOL_ARRAY: [],
VOTING_RULE_ARRAY: [],
PARAMETER_ARRAY: [],
PLUGIN_ARRAY: [],
UINT256_2DARRAY: [
// [BigInt(0), BigInt(1)],
// [BigInt(10), BigInt(1)],
// [BigInt(10), BigInt(1)],
[BigInt(0), BigInt(1)],
[BigInt(1000), BigInt(200)],
[BigInt(300), BigInt(400)],
],
ADDRESS_2DARRAY: []
}
},
{
operatorAddress: programOperatorAddress,
opcode: 1, // mint token
param: {
UINT256_ARRAY: [],
ADDRESS_ARRAY: [],
STRING_ARRAY: [],
BOOL_ARRAY: [],
VOTING_RULE_ARRAY: [],
PARAMETER_ARRAY: [],
PLUGIN_ARRAY: [],
UINT256_2DARRAY: [
[BigInt(0), BigInt(1)], // token class = 0
[BigInt(100), BigInt(200)], // amount = 100
],
ADDRESS_2DARRAY: [
[programOperatorAddress,programOperatorAddress], // to = programOperatorAddress
]
}
},
{
operatorAddress: programOperatorAddress,
opcode: 3, // transfer tokens
param:{
UINT256_ARRAY: [],
ADDRESS_ARRAY: [],
STRING_ARRAY: [],
BOOL_ARRAY: [],
VOTING_RULE_ARRAY: [],
PARAMETER_ARRAY: [],
PLUGIN_ARRAY: [],
UINT256_2DARRAY: [
[BigInt(0),BigInt(0), BigInt(1), BigInt(1)], // token class = 0
[BigInt(10), BigInt(20), BigInt(30), BigInt(40)], // amount = 100
],
ADDRESS_2DARRAY: [
[target1, target2, target1, target2],
]
}
}
],
};
const param = {
address: darc_contract_address,
wallet: signer,
provider: provider
};


// const attached_local_darc2 = await attachDARCwithWallet(
// darc_contract_address,
// DARC_VERSION.Test,
// signer,
// );

const attached_local_darc2 = new DARC.DARC({
address: darc_contract_address,
wallet: signer,
version: DARC_VERSION.Test,
});

console.log("The attached contract of local_darc 2 is " + attached_local_darc2.address());

await new Promise(resolve1 => setTimeout(resolve1, 100));
// check the number of token classes. If it is 0, then create a token class first
const token_class_count = await attached_local_darc2.getNumberOfTokenClasses();

console.log("token_class_count: " + token_class_count.toString());

console.log(token_class_count.toString() === "0");
if (token_class_count.toString() === "0") {
await attached_local_darc2.entrance(create_mint_and_transter_program);
await attached_local_darc2.entrance(mint_and_transfer_program);

console.log(" Executed create_mint_and_transter_program");
}
else {
await attached_local_darc2.entrance(mint_and_transfer_program);
console.log(" Executed mint_and_transfer_program");
}

//// Delay of 1000ms (1 second)
await new Promise(resolve1 => setTimeout(resolve1, 100));
console.log(attached_local_darc2.address);
console.log("Here is the token owner balance: ");
console.log("target1: " + target1);

const balance = await attached_local_darc2.getTokenOwnerBalance(BigInt(0), target1);
console.log("balance: " + balance.toString());

console.log("Token info: \n" + JSON.stringify(await attached_local_darc2.getTokenInfo(BigInt(1))));

expect(balance.toString()).to.equal("20");
});
});
2 changes: 1 addition & 1 deletion darc-js/tests/rpcCall-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { runtime_RunProgram, runtime_getTokenOwners } from '../src/runtime/runti
import { DARC_VERSION, darcBinary } from '../src/darcBinary/darcBinary';


describe.only('RPC call test',
describe('RPC call test',
() => {
it('should return true', async () => {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ contract TokenInstructions is MachineStateManager{
bool bIsValid = false;
for (uint256 i = 0; i < target.length; i++) {
if (bIsSandbox) {
require(sandboxMachineState.tokenList[tokenClass[i]].bIsInitialized == true, "The token class is not initialized");

(bIsValid, sandboxMachineState.tokenList[tokenClass[i]].totalSupply) =
SafeMathUpgradeable.tryAdd( amount[i],
sandboxMachineState.tokenList[tokenClass[i]].totalSupply);
Expand All @@ -44,6 +46,8 @@ contract TokenInstructions is MachineStateManager{
sandboxMachineState.tokenList[tokenClass[i]].tokenBalance[target[i]]);
require(bIsValid, "The balance of the token is overflow");
} else {
require(currentMachineState.tokenList[tokenClass[i]].bIsInitialized == true, "The token class is not initialized");

(bIsValid, currentMachineState.tokenList[tokenClass[i]].totalSupply) =
SafeMathUpgradeable.tryAdd( amount[i],
currentMachineState.tokenList[tokenClass[i]].totalSupply);
Expand Down

0 comments on commit 314b018

Please sign in to comment.