Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lidangzzz committed Dec 17, 2023
1 parent e4d66f9 commit dce46c1
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 21 deletions.
3 changes: 2 additions & 1 deletion darc-js/src/DARC/DARC.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as runtime from '../deployDARC/deployDARC';
import { ethers, Contract } from 'ethers';
import * as darcBinary from '../darcBinary/darcBinary';

import { DARC_VERSION } from '../darcBinary/darcBinary';
export {DARC_VERSION}
export type InitParam = {
address: string;
version: darcBinary.DARC_VERSION;
Expand Down
8 changes: 4 additions & 4 deletions darc-js/src/SDK/includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { objectMethod } from "@babel/types";

export let operationList: OperationStruct[] = [];

export function batch_mint_tokens(addressArray: string[], amountArray: bigint[], tokenClass: bigint[], operatorAddress:string) {
let operation = op_batch_mint_tokens(addressArray, amountArray, tokenClass, operatorAddress);
export function batch_mint_tokens(addressArray: string[], amountArray: bigint[], tokenClass: bigint[]) {
let operation = op_batch_mint_tokens(addressArray, amountArray, tokenClass);
operationList.push(operation);
}

export function batch_create_token_class(nameArray: string[], tokenIndexArray: bigint[], votingWeightArray: bigint[], dividendWeightArray: bigint[], operatorAddress:string) {
let operation = op_batch_create_token_class(nameArray, tokenIndexArray, votingWeightArray, dividendWeightArray, operatorAddress);
export function batch_create_token_class(nameArray: string[], tokenIndexArray: bigint[], votingWeightArray: bigint[], dividendWeightArray: bigint[]) {
let operation = op_batch_create_token_class(nameArray, tokenIndexArray, votingWeightArray, dividendWeightArray);
operationList.push(operation);
}
22 changes: 18 additions & 4 deletions darc-js/src/SDK/opcodes/op_batch_create_token_class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export function op_batch_create_token_class(
nameArray: string[],
tokenIndexArray: bigint[] | number[],
votingWeightArray: bigint[] | number[],
dividendWeightArray: bigint[] | number[],
operatorAddress:string
dividendWeightArray: bigint[] | number[]
): OperationStruct {
// make sure all parameters are valid and the length of all arrays are the same
if (nameArray.length != tokenIndexArray.length || nameArray.length != votingWeightArray.length || nameArray.length != dividendWeightArray.length) {
Expand All @@ -41,12 +40,28 @@ export function op_batch_create_token_class(
throw new Error("The tokenIndexArray is not a valid array of bigints");
}
}
for (let i = 0; i < votingWeightArray.length; i++) {
if (typeof votingWeightArray[i] === "number") {
votingWeightArray[i] = BigInt(votingWeightArray[i]);
}
if (typeof votingWeightArray[i] !== "bigint") {
throw new Error("The votingWeightArray is not a valid array of bigints");
}
}
for (let i = 0; i < dividendWeightArray.length; i++) {
if (typeof dividendWeightArray[i] === "number") {
dividendWeightArray[i] = BigInt(dividendWeightArray[i]);
}
if (typeof dividendWeightArray[i] !== "bigint") {
throw new Error("The dividendWeightArray is not a valid array of bigints");
}
}



//create the operation
let operation = {
operatorAddress: operatorAddress,
operatorAddress: "", // address will be filled in later
opcode: 2, // mint token
param: {
UINT256_ARRAY: [],
Expand All @@ -66,5 +81,4 @@ export function op_batch_create_token_class(
}
};
return operation;

}
14 changes: 12 additions & 2 deletions darc-js/src/SDK/opcodes/op_batch_mint_tokens.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethers } from "ethers";
import {OperationStruct} from "../struct/basicTypes";

export function op_batch_mint_tokens(addressArray: string[], amountArray: bigint[], tokenClass: bigint[], operatorAddress:string): OperationStruct {
export function op_batch_mint_tokens(addressArray: string[], tokenClass: bigint[], amountArray: bigint[]): OperationStruct {
// make sure the length of addressArray and amountArray are the same
if (addressArray.length != amountArray.length) {
throw new Error("The length of addressArray and amountArray are different");
Expand Down Expand Up @@ -30,9 +30,19 @@ export function op_batch_mint_tokens(addressArray: string[], amountArray: bigint
}
}

// make sure token class is valid array of bigints or numbers, and if it is number, convert it to bigint
for (let i = 0; i < tokenClass.length; i++) {
if (typeof tokenClass[i] === "number") {
tokenClass[i] = BigInt(tokenClass[i]);
}
if (typeof tokenClass[i] !== "bigint") {
throw new Error("The tokenClass is not a valid array of bigints");
}
}

//create the operation
let operation = {
operatorAddress: operatorAddress,
operatorAddress: "",
opcode: 1, // mint token
param: {
UINT256_ARRAY: [],
Expand Down
36 changes: 32 additions & 4 deletions darc-js/src/SDK/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
import * as instructions from "./includes";
import { ethers, Contract } from 'ethers';

export function run(code:string, wallet:ethers.Wallet, provider:ethers.providers.Provider, address:string) {
import { OperationStruct, OperationStructOutput, ProgramStruct } from "./struct/basicTypes";
import * as DARC from "../DARC/DARC";
/**
* This function takes in a string of code and returns a program struct
* @param code The code to be run
* @param wallet The wallet to be used to sign the transaction
* @param provider The provider to be used to connect to the blockchain
* @param targetDARCAddress The address of the DARC contract to be used
* @returns
*/
export async function run(code:string, wallet:ethers.Wallet, provider:ethers.providers.Provider, targetDARCAddress:string) {
let include = '';
for (const key in instructions) {
include += `let ${key} = instructions.${key};\n`;
}

const fn = new Function('instructions', 'ethers', 'wallet', 'provider', 'address', include + code + '\n return operationList;');

const results = fn(instructions, ethers, wallet, provider, address);
const results = fn(instructions, ethers, wallet, provider, targetDARCAddress);
const operatorAddress = wallet.address;
const resultList:OperationStruct[] = [...results];

// add operator address to each operation
for (let i = 0; i < resultList.length; i++) {
resultList[i].operatorAddress = operatorAddress;
}

// create the program
const program:ProgramStruct = {
programOperatorAddress: operatorAddress,
operations: resultList
};

const attachedDARC = new DARC.DARC({
address: targetDARCAddress,
version: DARC.DARC_VERSION.Test,
wallet: wallet,
});

const resultList = [...results];
await attachedDARC.entrance(program);
}
4 changes: 1 addition & 3 deletions darc-js/tests/basic-DARC-test/dashboard-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'mocha';
//import { setTimeout } from "timers/promises";
const provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545/');
import { DARC_VERSION, darcBinary } from '../../src/darcBinary/darcBinary';
import * as DARC from '../../src/darc/darc';
import * as DARC from '../../src/DARC/DARC';

describe('class DARC dashboard test',
() => {
Expand Down Expand Up @@ -209,7 +209,5 @@ describe('class DARC dashboard test',
console.log("Token info: \n" + json_stringifyWithBigInt(await attached_local_darc2.getTokenInfo(BigInt(1))));

expect(balance.toString()).to.equal("20");


});
});
2 changes: 1 addition & 1 deletion darc-js/tests/deploy-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const signer = new ethers.Wallet('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed

describe('Deploy Test',
() => {
it('should compile', async () => {
it('should deploy DARC successfully', async () => {
const address = await deployDARC(DARC_VERSION.Test, signer);

console.log("deployed at: " + address);
Expand Down
35 changes: 33 additions & 2 deletions darc-js/tests/runtime-test/basic-runtime-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {run, deployDARC, DARC_VERSION} from '../../src/darcjs';
import { ethers } from 'ethers';
import * as DARC from '../../src/DARC/DARC';
import 'mocha';
import { expect } from 'chai';



Expand All @@ -20,10 +22,39 @@ batch_mint_tokens([ "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266",
[0, 1], [100,200]);
`;

describe('Runtime execution test', () => {
describe.only('Runtime execution test', () => {
it('should run the program', async () => {
const darc_contract_address = await deployDARC(DARC_VERSION.Test, signer);
run(code, signer, provider, my_wallet_address);

await run(code, signer, provider, my_wallet_address).then(async ()=>{

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

// read the token info and make sure that address 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 has 100 token_0 and 200 token_1
const token_info = await attached_local_darc2.getTokenInfo(BigInt(0));
const token_info1 = await attached_local_darc2.getTokenInfo(BigInt(1));
// check the token info
expect(token_info.votingWeight.toString()).to.equal("10");
expect(token_info.dividendWeight.toString()).to.equal("20");
expect(token_info.tokenInfo).to.equal("token_0");
expect(token_info.totalSupply.toString()).to.equal("100");
// check the token info 1
expect(token_info1.votingWeight.toString()).to.equal("20");
expect(token_info1.dividendWeight.toString()).to.equal("30");
expect(token_info1.tokenInfo).to.equal("token_1");
expect(token_info1.totalSupply.toString()).to.equal("200");

// check the token owner balance
const token_owner_balance = await attached_local_darc2.getTokenOwnerBalance(BigInt(0),my_wallet_address);
expect(token_owner_balance.toString()).to.equal("100");
const token_owner_balance1 = await attached_local_darc2.getTokenOwnerBalance(BigInt(1),my_wallet_address);
expect(token_owner_balance1.toString()).to.equal("200");
});

}
);
});

0 comments on commit dce46c1

Please sign in to comment.