Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
lidangzzz committed May 11, 2023
1 parent bf703ba commit e57460a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 46 deletions.
2 changes: 1 addition & 1 deletion darc-protocol/contracts/MachineStateManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,5 @@ contract MachineStateManager {
//sandboxMachineState.tokenInfoList.push(currentMachineState.tokenInfoList[i]);
}
}

}
5 changes: 5 additions & 0 deletions darc-protocol/contracts/TokenOwnerListManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ contract TokenOwnerListManager is MachineStateManager {
* The reason of this function is to update the token owner list of each certain token level efficiently,
* which provides an up-to-date list of keys for token balance mapping.
*
* For example, the existing token owner list is [A,B,C,D,E],
* and the operator mint tokens to address [E,F,G],
* and burn all tokens from [A,B], then the new token owner list is [C,D,E,F,G].
*
*
* @param bIsSandbox The flag to indicate whether the operation is in the sandbox
* @param addOwnerList The list of owner addresses which receive more tokens
* @param removeOwnerList The list of owner addresses which transfer all tokens to others and balance is (probably) zero
Expand Down
133 changes: 88 additions & 45 deletions darc-protocol/test/operationUnitTest/batch_mint_token_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ import { ethers } from "hardhat";
import { BigNumber } from "ethers";

// test for batch mint token instruction on DARC
const programOperatorAddress = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266";
// batch mint token to another addresses
const addr1 = "0x90F79bf6EB2c4f870365E785982E1f101E93b906";
const addr2 = '0x976EA74026E726554dB657fA54763abd0C3a0aa9';
const addr3 = '0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199';
const addr4 = '0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65';
const addr5 = '0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc';
const addr6 = '0x14dC79964da2C08b23698B3D3cc7Ca32193d9955';

/**
* Check if an address is in an array
* @param array
* @param addr
* @returns if the address is in the array
*/
function containsAddr(array: string[], addr:string): boolean {
for (let i = 0; i < array.length; i++) {
if (array[i].toLowerCase() === addr.toLowerCase()) {
return true;
}
}
return false;
}

describe.only("batch_mint_token_test", function () {

Expand All @@ -18,12 +41,6 @@ describe.only("batch_mint_token_test", function () {
await darc.initialize();


const programOperatorAddress = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266";
// batch mint token to another addresses
const addr1 = "0x90F79bf6EB2c4f870365E785982E1f101E93b906";
const addr2 = '0x976EA74026E726554dB657fA54763abd0C3a0aa9';
const addr3 = '0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199';

// create a token class first
await darc.entrance({
programOperatorAddress: programOperatorAddress,
Expand Down Expand Up @@ -63,11 +80,11 @@ describe.only("batch_mint_token_test", function () {
PARAMETER_ARRAY: [],
PLUGIN_ARRAY: [],
UINT256_2DARRAY: [
[BigNumber.from(0), BigNumber.from(1), BigNumber.from(0), BigInt(0), BigInt(1)], // token class = 0
[BigNumber.from(0), BigNumber.from(1), BigNumber.from(0), BigInt(0), BigInt(1)],
[BigNumber.from(100), BigNumber.from(200), BigNumber.from(300), BigInt(16), BigInt(5)], // amount = 100
],
ADDRESS_2DARRAY: [
[programOperatorAddress,programOperatorAddress, programOperatorAddress, addr1, addr2], // to = programOperatorAddress
[programOperatorAddress,programOperatorAddress, programOperatorAddress, addr1, addr2],
]
}
}],
Expand All @@ -79,38 +96,17 @@ describe.only("batch_mint_token_test", function () {


console.log("Token owner list of token 0 after op: ");
console.log(await darc.getTokenOwners(0));
let owner0 = await darc.getTokenOwners(0);
console.log(owner0);
console.log("Token owner list of token 1 after op: ");
console.log(await darc.getTokenOwners(1));
// expect(balance0.toBigInt().toString()).to.equal("400");
// expect(balance1.toBigInt().toString()).to.equal("200");
// expect(await darc.getTokenOwners(0)).to.have.lengthOf(1);
// expect(await darc.getTokenOwners(1)).to.have.lengthOf(1);
console.log(JSON.stringify(result_entrance));

// const result_entrance2 = darc.entrance({
// 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: [
// [BigNumber.from(0), BigNumber.from(0), BigNumber.from(0), BigNumber.from(0)], // token class = 0
// [BigNumber.from(100), BigNumber.from(200), BigNumber.from(300), BigNumber.from(16)], // amount = 100
// ],
// ADDRESS_2DARRAY: [
// [addr3,addr3, addr3, addr3], // to = programOperatorAddress
// ]
// }
// }],
// });
let owner1 = await darc.getTokenOwners(1);
console.log(owner1);

expect( containsAddr(owner0, programOperatorAddress) ).to.equal(true);
expect( containsAddr(owner1, programOperatorAddress) ).to.equal(true);
expect( containsAddr(owner0, addr1) ).to.equal(true);
expect( containsAddr(owner1, addr2) ).to.equal(true);


const result_entrance2 = await darc.entrance({
programOperatorAddress: programOperatorAddress,
Expand All @@ -126,21 +122,68 @@ describe.only("batch_mint_token_test", function () {
PARAMETER_ARRAY: [],
PLUGIN_ARRAY: [],
UINT256_2DARRAY: [
[BigNumber.from(0), BigNumber.from(1), BigNumber.from(0), BigInt(0), BigInt(1)], // token class = 0
[BigNumber.from(100), BigNumber.from(200), BigNumber.from(300), BigInt(16), BigInt(5)], // amount = 100
[BigNumber.from(0), BigNumber.from(1), BigNumber.from(0), BigInt(0), BigInt(1),BigInt(0), BigInt(1),BigInt(0), BigInt(1)], // token class = 0
[BigNumber.from(100), BigNumber.from(200), BigNumber.from(300), BigInt(16), BigInt(5),BigInt(16), BigInt(5),BigInt(16), BigInt(5)], // amount = 100
],
ADDRESS_2DARRAY: [
[programOperatorAddress,programOperatorAddress, programOperatorAddress, addr1, addr1], // to = programOperatorAddress
[programOperatorAddress,programOperatorAddress, programOperatorAddress, addr1, addr1, addr2,addr2, addr3, addr3], // to = programOperatorAddress
]
}
}],
});

console.log("Token owner list of token 0 after op: ");
console.log(await darc.getTokenOwners(0));
owner0 = await darc.getTokenOwners(0);
console.log(owner0);
console.log("Token owner list of token 1 after op: ");
console.log(await darc.getTokenOwners(1));
owner1 = await darc.getTokenOwners(1);
console.log(owner1);

// expect addr 1, 2 has token 0 and 1
expect( containsAddr(owner0, programOperatorAddress) ).to.equal(true);
expect( containsAddr(owner1, programOperatorAddress) ).to.equal(true);
expect( containsAddr(owner0, addr1) ).to.equal(true);
expect( containsAddr(owner1, addr1) ).to.equal(true);
expect( containsAddr(owner0, addr2) ).to.equal(true);
expect( containsAddr(owner1, addr2) ).to.equal(true);

const result_entrance3 = await darc.entrance({
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: [
[BigNumber.from(0), BigNumber.from(1), BigNumber.from(0), BigInt(1), BigInt(0),BigInt(1)], // token class = 0
[BigNumber.from(100), BigNumber.from(200), BigNumber.from(300),BigNumber.from(100), BigNumber.from(200), BigNumber.from(300) ], // amount = 100
],
ADDRESS_2DARRAY: [
[addr4,addr4, addr5, addr5, addr6, addr6], // to = programOperatorAddress
]
}
}],
});

console.log(JSON.stringify(result_entrance2));
console.log("Token owner list of token 0 after op: ");
owner0 = await darc.getTokenOwners(0);
console.log(owner0);
console.log("Token owner list of token 1 after op: ");
owner1 = await darc.getTokenOwners(1);
console.log(owner1);

//expect addr 4,5,6 all have token 0 and 1
expect( containsAddr(owner0, addr4) ).to.equal(true);
expect( containsAddr(owner1, addr4) ).to.equal(true);
expect( containsAddr(owner0, addr5) ).to.equal(true);
expect( containsAddr(owner1, addr5) ).to.equal(true);
expect( containsAddr(owner0, addr6) ).to.equal(true);
expect( containsAddr(owner1, addr6) ).to.equal(true);
});
});

0 comments on commit e57460a

Please sign in to comment.