diff --git a/package.json b/package.json index faa9c74..b55bcb2 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "test": "echo \"Error: no test specified\" && exit 1", "test:start": "npx ts-node scripts/start.ts", "test:deploy":"npx ts-node scripts/deploy.ts", + "test:claim_ckb": "npx ts-node scripts/claim_ckb.ts", "test:e2e": "jest tests/*.data.test.ts", "test:stop": "npx ts-node scripts/stop.ts" }, diff --git a/scripts/claim_ckb.ts b/scripts/claim_ckb.ts new file mode 100644 index 0000000..f6014d5 --- /dev/null +++ b/scripts/claim_ckb.ts @@ -0,0 +1,49 @@ +import {getSecp256k1Account} from "../src/utils"; +import {CKB_RPC_URL, MNEMONIC, MNEMONIC2} from "../src/constants"; +import {RPC} from "@ckb-lumos/rpc"; +import {Indexer} from "@ckb-lumos/ckb-indexer"; +import {E2EProvider} from "../src/e2eProvider"; +import {FileFaucetQueue} from "../src/faucetQueue"; +import {AddressType} from "@ckb-lumos/hd"; + +async function main() { + console.log("---main---") + const rpc = new RPC(CKB_RPC_URL); + const indexer = new Indexer(CKB_RPC_URL); + + const e2eProvider = new E2EProvider({ + indexer, + rpc, + faucetQueue: FileFaucetQueue.getInstance(), + }); + await e2eProvider.loadLocalConfig(); + + for (let i = 0; i < 500; i++) { + console.log(i) + let account = getSecp256k1Account(MNEMONIC, AddressType.Receiving, i) + + await e2eProvider.claimCKB({ + claimer: account.address + }); + + account = getSecp256k1Account(MNEMONIC, AddressType.Change, i) + await e2eProvider.claimCKB({ + claimer: account.address + }); + + account = getSecp256k1Account(MNEMONIC2, AddressType.Receiving, i) + + await e2eProvider.claimCKB({ + claimer: account.address + }); + + account = getSecp256k1Account(MNEMONIC2, AddressType.Change, i) + await e2eProvider.claimCKB({ + claimer: account.address + }); + + } + +} + +main(); diff --git a/scripts/start.ts b/scripts/start.ts index 1c2542a..15e3376 100644 --- a/scripts/start.ts +++ b/scripts/start.ts @@ -52,7 +52,7 @@ async function main() { }); console.log("modify epoch_duration_target"); const specs_dev_path = 'specs/dev.toml'; - const modify_epoch_duration_target_command = `sed -ie 's/epoch_duration_target = 14400/epoch_duration_target = 8/g' ${specs_dev_path}`; + const modify_epoch_duration_target_command = `sed -ie 's/epoch_duration_target = 14400/epoch_duration_target = 80/g' ${specs_dev_path}`; const child = spawn(modify_epoch_duration_target_command, {shell: true, cwd: CKB_CWD}); child.on('close', (code) => { if (code === 0) { diff --git a/src/constants.ts b/src/constants.ts index 5e02e68..9feda84 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -13,6 +13,8 @@ export const CKB_TEST_NET_RPC_URL = "https://testnet.ckbapp.dev/"; export const LUMOS_CONFIG_PATH = "lumos.json" export const MNEMONIC = "brush scan basic know movie next time soccer speak loop balcony describe" +export const MNEMONIC2 = "equip slim poem depth struggle tonight define stool brave sustain spy cabbage" + // from docker/ckb/dev.toml [[genesis.system_cells]] initializeConfig(predefined.AGGRON4); diff --git a/src/e2eProvider.ts b/src/e2eProvider.ts index 76a4ccd..ec52e5d 100644 --- a/src/e2eProvider.ts +++ b/src/e2eProvider.ts @@ -1,11 +1,12 @@ import { - HexString, - Address, - utils, - Cell, - TransactionWithStatus, - Block, - Script, HashType, OutPoint, values, blockchain, CellDep + HexString, + Address, + utils, + Cell, + TransactionWithStatus, + Block, + Script, HashType, OutPoint, Output, + } from "@ckb-lumos/base"; import { helpers, @@ -20,6 +21,7 @@ import { import {common, dao, deploy, MultisigScript, parseFromInfo} from "@ckb-lumos/common-scripts"; import {bytes} from "@ckb-lumos/codec"; import {Buffer} from 'buffer'; +import {helpers} from "@ckb-lumos/lumos"; import { TransactionSkeleton, @@ -528,14 +530,18 @@ export class E2EProvider { } - public async sendAndSignTxSkeleton(txSkeleton: TransactionSkeletonType, fee: number = 1000, account: Account) { - txSkeleton = await common.payFeeByFeeRate(txSkeleton, [account.address], fee); - txSkeleton = common.prepareSigningEntries(txSkeleton); - const message = txSkeleton.get("signingEntries").get(0)?.message; - const Sig = key.signRecoverable(message!, account.privKey); - const tx = sealTransaction(txSkeleton, [Sig]); - return this.rpc.sendTransaction(tx, "passthrough"); - } + public async sendAndSignTxSkeleton(txSkeleton: TransactionSkeletonType, fee: number = 1000, account: Account) { + txSkeleton = await common.payFeeByFeeRate(txSkeleton, [account.address], fee); + txSkeleton = common.prepareSigningEntries(txSkeleton); + let sigs = [] + for (let i = 0; i < txSkeleton.get("signingEntries").size; i++) { + let message = txSkeleton.get("signingEntries").get(i)?.message; + let Sig = key.signRecoverable(message!, account.privKey); + sigs.push(Sig) + } + let tx = sealTransaction(txSkeleton, sigs); + return this.rpc.sendTransaction(tx, "passthrough"); + } public async deployContract(options: { account: Account, @@ -624,6 +630,26 @@ export class E2EProvider { writeFileSync(decPath, buffer) } + public async getTransactionDetail(txHash: string) { + let transaction = await this.rpc.getTransaction(txHash) + let ret = `${txHash}\n` + for (let i = 0; i < transaction.transaction.inputs.length; i++) { + let input = transaction.transaction.inputs[i] + let cell = await this.getCellByTransactionHashAndIdx(input.previousOutput.txHash, BI.from(input.previousOutput.index).toNumber()) + ret += `inputIdx:${i} address:${helpers.encodeToAddress(cell.lock)} cap:${BI.from(cell.capacity).toNumber()}\n` + } + ret += "\n" + transaction.transaction.outputs.forEach((output, idx) => { + ret += `outIdx:${idx} address:${helpers.encodeToAddress(output.lock)} cap:${BI.from(output.capacity).toNumber()}\n` + }) + return ret + } + + async getCellByTransactionHashAndIdx(hash: string, idx: number): Promise { + return await this.rpc.getTransaction(hash) + .then((tx) => tx.transaction.outputs[idx]) + } + } export function hexToUint8Array(hexInput: Buffer) { @@ -645,11 +671,11 @@ export function hexToUint8Array(hexInput: Buffer) { function checkFileExists(filePath: string): boolean { - try { - // 使用 fs.accessSync 来检查文件是否存在 - fs.accessSync(filePath, fs.constants.F_OK); - return true; - } catch (error) { - return false; - } + try { + // 使用 fs.accessSync 来检查文件是否存在 + fs.accessSync(filePath, fs.constants.F_OK); + return true; + } catch (error) { + return false; + } } \ No newline at end of file diff --git a/src/utils.ts b/src/utils.ts index c8081d8..3b8b857 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -69,9 +69,45 @@ export const randomSecp256k1Account = (privKey?: string): Account => { }; }; +export const randomAnyOneCanPayAccount = (privKey?: string): Account => { + const _privKey = (() => { + if (privKey) { + return privKey; + } + + return generateRandomPrivateKey(); + })(); + + const pubKey = key.privateToPublic(_privKey); + const args = key.publicKeyToBlake160(pubKey); + const template = getConfig().SCRIPTS["ANYONE_CAN_PAY"]!; + const lockScript = { + codeHash: template.CODE_HASH, + hashType: template.HASH_TYPE, + args: args, + }; + + const address = encodeToAddress(lockScript); + + return { + lockScript, + address, + pubKey, + privKey: _privKey, + }; +}; + + export const getSecp256k1Account = (mm: string, type: AddressType, index: number): Account => { - const seed = mnemonic.mnemonicToSeedSync(mm) - const extendedPrivateKey = ExtendedPrivateKey.fromSeed(seed) - let priv = extendedPrivateKey.privateKeyInfo(AddressType.Change, index) - return randomSecp256k1Account(priv.privateKey) + const seed = mnemonic.mnemonicToSeedSync(mm) + const extendedPrivateKey = ExtendedPrivateKey.fromSeed(seed) + let priv = extendedPrivateKey.privateKeyInfo(type, index) + return randomSecp256k1Account(priv.privateKey) } + +export const getAnyOneCanPayAccount = (mm: string, type: AddressType, index: number): Account => { + const seed = mnemonic.mnemonicToSeedSync(mm) + const extendedPrivateKey = ExtendedPrivateKey.fromSeed(seed) + let priv = extendedPrivateKey.privateKeyInfo(type, index) + return randomAnyOneCanPayAccount(priv.privateKey) +} \ No newline at end of file diff --git a/tests/any_one_can_pay.data.test.ts b/tests/any_one_can_pay.data.test.ts new file mode 100644 index 0000000..55639f2 --- /dev/null +++ b/tests/any_one_can_pay.data.test.ts @@ -0,0 +1,273 @@ +import {e2eProvider} from "../src/config"; +import {getAnyOneCanPayAccount, getSecp256k1Account, randomSecp256k1Account} from "../src/utils"; +import {MNEMONIC, MNEMONIC2} from "../src/constants"; +import {AddressType} from "@ckb-lumos/hd"; +import {BI} from "@ckb-lumos/bi"; +import {common, sudt} from "@ckb-lumos/common-scripts"; +import {TransactionSkeleton} from "@ckb-lumos/helpers"; + + + +describe('any_one_can_pay', function () { + + it("generate any one can pay ckb cell", async () => { + const AnyOneCanPayAccounts1 = [ + getAnyOneCanPayAccount(MNEMONIC, AddressType.Receiving, 0), + getAnyOneCanPayAccount(MNEMONIC, AddressType.Receiving, 1), + ] + + const Secp256k1Accounts = [ + getSecp256k1Account(MNEMONIC, AddressType.Receiving, 0), + getSecp256k1Account(MNEMONIC, AddressType.Change, 1), + ] + + + const AnyOneCanPayAccounts2 = [ + getAnyOneCanPayAccount(MNEMONIC2, AddressType.Receiving, 0), + getAnyOneCanPayAccount(MNEMONIC2, AddressType.Change, 1) + ] + + + const Secp256k1Accounts2 = [ + getSecp256k1Account(MNEMONIC2, AddressType.Receiving, 0), + getSecp256k1Account(MNEMONIC2, AddressType.Change, 1), + ] + + for (let i = 0; i < AnyOneCanPayAccounts1.length; i++) { + + await e2eProvider.claimCKB({ + claimer: AnyOneCanPayAccounts1[i].address, + amount: BI.from(100000 * 10 ** 8) + }); + + await e2eProvider.claimCKB({ + claimer: AnyOneCanPayAccounts2[i].address, + amount: BI.from(100000 * 10 ** 8) + }); + + await e2eProvider.claimCKB({ + claimer: Secp256k1Accounts[i].address, + amount: BI.from(100000 * 10 ** 8) + }); + + await e2eProvider.claimCKB ({ + claimer: Secp256k1Accounts2[i].address, + amount: BI.from(100000 * 10 ** 8) + }); + } + }) + + it("(any one can pay address) transfer ckb to (any one can pay address)", async () => { + const AnyOneCanPayAccounts1 = [ + getAnyOneCanPayAccount(MNEMONIC, AddressType.Receiving, 0), + getAnyOneCanPayAccount(MNEMONIC, AddressType.Receiving, 1), + ] + + const Secp256k1Accounts = [ + getSecp256k1Account(MNEMONIC, AddressType.Receiving, 0), + getSecp256k1Account(MNEMONIC, AddressType.Change, 1), + ] + + + const AnyOneCanPayAccounts2 = [ + getAnyOneCanPayAccount(MNEMONIC2, AddressType.Receiving, 0), + getAnyOneCanPayAccount(MNEMONIC2, AddressType.Change, 1) + ] + + + const Secp256k1Accounts2 = [ + getSecp256k1Account(MNEMONIC2, AddressType.Receiving, 0), + getSecp256k1Account(MNEMONIC2, AddressType.Change, 1), + ] + + let test_list = [ + {from: AnyOneCanPayAccounts1[0], to: AnyOneCanPayAccounts1[1].address, amount: 100}, + {from: AnyOneCanPayAccounts1[0], to: AnyOneCanPayAccounts2[0].address, amount: 200}, + {from: AnyOneCanPayAccounts1[0], to: AnyOneCanPayAccounts2[1].address, amount: 300}, + + {from: AnyOneCanPayAccounts2[0], to: AnyOneCanPayAccounts2[1].address, amount: 500}, + {from: AnyOneCanPayAccounts2[0], to: AnyOneCanPayAccounts1[0].address, amount: 400}, + {from: AnyOneCanPayAccounts2[0], to: AnyOneCanPayAccounts1[1].address, amount: 600}, + ] + + for (let i = 0; i < test_list.length; i++) { + let from = test_list[i].from + let to = test_list[i].to + let amount = test_list[i].amount + let txSkeleton = TransactionSkeleton({cellProvider: e2eProvider.indexer}); + console.log(from) + console.log(to) + txSkeleton = await common.transfer( + txSkeleton, + [from.address], + to, + amount + ); + let txHash = await e2eProvider.sendAndSignTxSkeleton(txSkeleton, 1000, randomSecp256k1Account(from.privKey)) + await e2eProvider.waitTransactionCommitted(txHash) + let ret = await e2eProvider.getTransactionDetail(txHash) + console.log(ret) + } + }) + + it("any one can pay transfer ckb", async () => { + + const AnyOneCanPayAccounts1 = [ + getAnyOneCanPayAccount(MNEMONIC, AddressType.Receiving, 0), + getAnyOneCanPayAccount(MNEMONIC, AddressType.Receiving, 1), + ] + + const Secp256k1Accounts = [ + getSecp256k1Account(MNEMONIC, AddressType.Receiving, 0), + getSecp256k1Account(MNEMONIC, AddressType.Change, 1), + ] + + + const AnyOneCanPayAccounts2 = [ + getAnyOneCanPayAccount(MNEMONIC2, AddressType.Receiving, 0), + getAnyOneCanPayAccount(MNEMONIC2, AddressType.Change, 1) + ] + + + const Secp256k1Accounts2 = [ + getSecp256k1Account(MNEMONIC2, AddressType.Receiving, 0), + getSecp256k1Account(MNEMONIC2, AddressType.Change, 1), + ] + + let test_list = [ + {from: AnyOneCanPayAccounts1[0], to: Secp256k1Accounts[1].address, amount: 100 * 10 ** 8}, + {from: AnyOneCanPayAccounts1[0], to: Secp256k1Accounts2[0].address, amount: 101 * 10 ** 8}, + {from: AnyOneCanPayAccounts1[0], to: Secp256k1Accounts2[1].address, amount: 102 * 10 ** 8}, + + {from: AnyOneCanPayAccounts2[0], to: Secp256k1Accounts2[1].address, amount: 103 * 10 ** 8}, + {from: AnyOneCanPayAccounts2[0], to: Secp256k1Accounts[1].address, amount: 104 * 10 ** 8}, + {from: AnyOneCanPayAccounts2[0], to: Secp256k1Accounts[0].address, amount: 105 * 10 ** 8}, + ] + for (let i = 0; i < test_list.length; i++) { + console.log(`----${i}----`) + let from = test_list[i].from + let to = test_list[i].to + let amount = test_list[i].amount + let cap = await e2eProvider.getCapacities(from.address) + let cap2 = await e2eProvider.getCapacities(randomSecp256k1Account(from.privKey).address) + + let cap1 = await e2eProvider.getCapacities(to) + console.log(`from1: ${from.address}:${cap.toNumber()}`) + console.log(`from2: ${randomSecp256k1Account(from.privKey).address}:${cap2.toNumber()}`) + console.log(`to: ${to}:${cap1.toNumber()}`) + let txSkeleton = new TransactionSkeleton({cellProvider: e2eProvider.indexer}); + txSkeleton = await common.transfer( + txSkeleton, + [from.address], + to, + amount + ); + let txHash = await e2eProvider.sendAndSignTxSkeleton(txSkeleton, 1000, randomSecp256k1Account(from.privKey)) + await e2eProvider.waitTransactionCommitted(txHash) + let ret = await e2eProvider.getTransactionDetail(txHash) + console.log(ret) + } + }) + + // it.skip("any one can pay transfer sudt", async () => { + // + // // issue sudt + // let account = getSecp256k1Account(MNEMONIC, AddressType.Change, 1) + // let issueBalance = 1000000; + // let txSkeleton = TransactionSkeleton({cellProvider: e2eProvider.indexer}); + // txSkeleton = await sudt.issueToken(txSkeleton, account.address, BI.from(issueBalance)) + // let txHash = await e2eProvider.sendAndSignTxSkeleton(txSkeleton, 1000, account) + // await e2eProvider.waitTransactionCommitted(txHash) + // console.log("tx hash committed:", txHash) + // + // // transfer sudt to any one can pay + // let fromAccount = account + // let SUDTTokenId = sudt.ownerForSudt(account.address) + // let to = getAnyOneCanPayAccount(MNEMONIC, AddressType.Change, 2).address + // let transferAmount = 1000; + // let txSkeleton2 = TransactionSkeleton({cellProvider: e2eProvider.indexer}); + // txSkeleton2 = await sudt.transfer( + // txSkeleton, + // [fromAccount.address], + // SUDTTokenId, + // to, + // transferAmount, undefined, undefined, + // ); + // txHash = await e2eProvider.sendAndSignTxSkeleton(txSkeleton2, 1000, fromAccount) + // console.log(txHash) + // await e2eProvider.waitTransactionCommitted(txHash) + // + // }) + + + // it("1111(any one can pay address) transfer ckb to (any one can pay address)", async () => { + // const AnyOneCanPayAccounts1 = [ + // getAnyOneCanPayAccount(MNEMONIC, AddressType.Receiving, 0), + // getAnyOneCanPayAccount(MNEMONIC, AddressType.Receiving, 1), + // ] + // + // const Secp256k1Accounts = [ + // getSecp256k1Account(MNEMONIC, AddressType.Receiving, 119), + // getSecp256k1Account(MNEMONIC, AddressType.Change, 119), + // ] + // + // + // const AnyOneCanPayAccounts2 = [ + // getAnyOneCanPayAccount(MNEMONIC2, AddressType.Receiving, 119), + // getAnyOneCanPayAccount(MNEMONIC2, AddressType.Change, 119) + // ] + // + // + // const Secp256k1Accounts2 = [ + // getSecp256k1Account(MNEMONIC2, AddressType.Receiving, 119), + // getSecp256k1Account(MNEMONIC2, AddressType.Change, 119), + // ] + // + // // await e2eProvider.claimCKB({ + // // claimer: AnyOneCanPayAccounts1[0].address, + // // amount: BI.from(1000 * 10 ** 8) + // // }); + // // await e2eProvider.claimCKB({ + // // claimer: getSecp256k1Account(MNEMONIC, AddressType.Change, 111).address, + // // amount: BI.from(1000 * 10 ** 8) + // // }); + // // + // // await e2eProvider.claimCKB({ + // // claimer: getAnyOneCanPayAccount(MNEMONIC, AddressType.Receiving, 111).address, + // // amount: BI.from(1000 * 10 ** 8) + // // }); + // + // // await e2eProvider.claimCKB({ + // // claimer: getSecp256k1Account(MNEMONIC, AddressType.Receiving, 120).address, + // // amount: BI.from(1000 * 10 ** 8) + // // }); + // + // // let from = getAnyOneCanPayAccount(MNEMONIC, AddressType.Change, 0) + // // let to = getAnyOneCanPayAccount(MNEMONIC, AddressType.Receiving, 0).address + // // let amount = 9999999999 + // + // // let from = getAnyOneCanPayAccount(MNEMONIC, AddressType.Change, 111) + // // let to = getSecp256k1Account(MNEMONIC, AddressType.Receiving, 111).address + // let from = getAnyOneCanPayAccount(MNEMONIC, AddressType.Receiving, 0) + // let to = AnyOneCanPayAccounts1[1].address + // let amount = 1000000000 + // console.log(from)//164000000000000 + // console.log(to) //10000000000 + // let cap = await e2eProvider.getCapacities(from.address) + // console.log("from:", cap.toNumber(), " address:", from.address) + // console.log("address:",AnyOneCanPayAccounts1[0].address) + // let txSkeleton = TransactionSkeleton({cellProvider: e2eProvider.indexer}); + // txSkeleton = await common.transfer( + // txSkeleton, + // [from.address], + // to, + // amount + // ); + // let txHash = await e2eProvider.sendAndSignTxSkeleton(txSkeleton, 1000, randomSecp256k1Account(from.privKey)) + // await e2eProvider.waitTransactionCommitted(txHash) + // let ret = await e2eProvider.getTransactionDetail(txHash) + // console.log(ret) + // + // }) + +}); \ No newline at end of file diff --git a/tests/sudt.data.test.ts b/tests/sudt.data.test.ts index c20238c..cfd0bbe 100644 --- a/tests/sudt.data.test.ts +++ b/tests/sudt.data.test.ts @@ -1,48 +1,245 @@ -import {e2eMainNetProvider, e2eProvider} from "../src/config"; -import {MNEMONIC} from "../src/constants"; +import { e2eProvider} from "../src/config"; +import {MNEMONIC, MNEMONIC2} from "../src/constants"; import {AddressType} from "@ckb-lumos/hd"; import {BI} from "@ckb-lumos/bi"; import {sudt} from "@ckb-lumos/common-scripts"; import {TransactionSkeleton} from "@ckb-lumos/helpers"; -import {utils} from "@ckb-lumos/base"; import {getConfig} from "@ckb-lumos/config-manager"; -import {getSecp256k1Account} from "../src/utils"; - - -const alice = getSecp256k1Account(MNEMONIC, AddressType.Change, 1); +import {getAnyOneCanPayAccount, getSecp256k1Account} from "../src/utils"; +import {unpackAmount} from "@ckb-lumos/common-scripts/lib/sudt"; describe('SUDT', function () { - beforeAll(async () => { - await e2eProvider.claimCKB({claimer: alice.address, amount: BI.from(1000 * 10 ** 8)}); + for (let i = 0; i < 5; i++) { + await e2eProvider.claimCKB({ + claimer: getSecp256k1Account(MNEMONIC, AddressType.Change, i).address, + amount: BI.from(1000 * 10 ** 8) + }); + await e2eProvider.claimCKB({ + claimer: getSecp256k1Account(MNEMONIC2, AddressType.Change, i).address, + amount: BI.from(1000 * 10 ** 8) + }); + + } }) it("issue simple udt", async () => { - let account = alice; - let issueBalance = 100000; - let txSkeleton = TransactionSkeleton({cellProvider: e2eProvider.indexer}); - txSkeleton = await sudt.issueToken(txSkeleton, alice.address, BI.from(issueBalance)) - let txHash = await e2eProvider.sendAndSignTxSkeleton(txSkeleton, 1000, account) - await e2eProvider.waitTransactionCommitted(txHash) - console.log("txhash committed:", txHash) + let issue_test_list = [ + { + account: getSecp256k1Account(MNEMONIC, AddressType.Change, 1), + address: getSecp256k1Account(MNEMONIC, AddressType.Change, 1).address, + issueBalance: 3340282300000n, + tx_hash: "0x0" + }, + { + account: getSecp256k1Account(MNEMONIC, AddressType.Change, 2), + address: getSecp256k1Account(MNEMONIC, AddressType.Change, 2).address, + issueBalance: 100000000000, + tx_hash: "0x0" + }, + { + account: getSecp256k1Account(MNEMONIC, AddressType.Change, 3), + address: getSecp256k1Account(MNEMONIC, AddressType.Change, 3).address, + issueBalance: 100000000000000, + tx_hash: "0x0" + }, + + { + account: getSecp256k1Account(MNEMONIC2, AddressType.Change, 1), + address: getSecp256k1Account(MNEMONIC2, AddressType.Change, 1).address, + issueBalance: 3340282300000n, + tx_hash: "0x0" + }, + { + account: getSecp256k1Account(MNEMONIC2, AddressType.Change, 2), + address: getSecp256k1Account(MNEMONIC2, AddressType.Change, 2).address, + issueBalance: 100000000000, + tx_hash: "0x0" + }, + { + account: getSecp256k1Account(MNEMONIC2, AddressType.Change, 3), + address: getSecp256k1Account(MNEMONIC2, AddressType.Change, 3).address, + issueBalance: 100000000000000, + tx_hash: "0x0" + }, + + ] + for (let i = 0; i < issue_test_list.length; i++) { + let {account, issueBalance} = issue_test_list[i]; + let txSkeleton = TransactionSkeleton({cellProvider: e2eProvider.indexer}); + txSkeleton = await sudt.issueToken(txSkeleton, account.address, BI.from(issueBalance)) + let txHash = await e2eProvider.sendAndSignTxSkeleton(txSkeleton, 1000, account) + await e2eProvider.waitTransactionCommitted(txHash) + console.log("tx hash committed:", txHash) + issue_test_list[i].tx_hash = txHash + } + console.table(issue_test_list) + + }) + + it("transfer token", async () => { + + let test_transfer_list = [ + { + fromAccount: getSecp256k1Account(MNEMONIC, AddressType.Change, 1), + fromAddress: getSecp256k1Account(MNEMONIC, AddressType.Change, 1).address, + SUDTTokenId: sudt.ownerForSudt(getSecp256k1Account(MNEMONIC, AddressType.Change, 1).address), + to: getSecp256k1Account(MNEMONIC, AddressType.Change, 2).address, + transferAmount: 1, + tx_hash: "" + }, + { + fromAccount: getSecp256k1Account(MNEMONIC, AddressType.Change, 1), + fromAddress: getSecp256k1Account(MNEMONIC, AddressType.Change, 1).address, + SUDTTokenId: sudt.ownerForSudt(getSecp256k1Account(MNEMONIC, AddressType.Change, 1).address), + to: getSecp256k1Account(MNEMONIC2, AddressType.Change, 2).address, + transferAmount: 1, + tx_hash: "" + }, + { + fromAccount: getSecp256k1Account(MNEMONIC2, AddressType.Change, 1), + fromAddress: getSecp256k1Account(MNEMONIC2, AddressType.Change, 1).address, + SUDTTokenId: sudt.ownerForSudt(getSecp256k1Account(MNEMONIC2, AddressType.Change, 1).address), + to: getSecp256k1Account(MNEMONIC, AddressType.Change, 2).address, + transferAmount: 1, + tx_hash: "" + }, + { + fromAccount: getSecp256k1Account(MNEMONIC2, AddressType.Change, 1), + fromAddress: getSecp256k1Account(MNEMONIC2, AddressType.Change, 1).address, + SUDTTokenId: sudt.ownerForSudt(getSecp256k1Account(MNEMONIC2, AddressType.Change, 1).address), + to: getSecp256k1Account(MNEMONIC2, AddressType.Change, 2).address, + transferAmount: 1, + tx_hash: "" + }, + ] + for (let i = 0; i < test_transfer_list.length; i++) { + console.log(`----${i}--------`) + let {fromAccount, SUDTTokenId, to, transferAmount} = test_transfer_list[i] + let txSkeleton = TransactionSkeleton({cellProvider: e2eProvider.indexer}); + txSkeleton = await sudt.transfer( + txSkeleton, + [fromAccount.address], + SUDTTokenId, + to, + transferAmount, undefined, undefined, + ); + let txHash = await e2eProvider.sendAndSignTxSkeleton(txSkeleton, 1000, fromAccount) + console.log(txHash) + await e2eProvider.waitTransactionCommitted(txHash) + test_transfer_list[i].tx_hash = txHash + } + console.table(test_transfer_list) + }) + + + it.skip("transfer to any one can pay",async ()=>{ + + let test_transfer_list = [ + { + fromAccount: getSecp256k1Account(MNEMONIC, AddressType.Change, 1), + fromAddress: getSecp256k1Account(MNEMONIC, AddressType.Change, 1).address, + SUDTTokenId: sudt.ownerForSudt(getSecp256k1Account(MNEMONIC, AddressType.Change, 1).address), + to: getAnyOneCanPayAccount(MNEMONIC, AddressType.Change, 2).address, + transferAmount: 1, + tx_hash: "" + }, + + // { + // fromAccount: getSecp256k1Account(MNEMONIC, AddressType.Change, 2), + // fromAddress: getSecp256k1Account(MNEMONIC, AddressType.Change, 2).address, + // SUDTTokenId: sudt.ownerForSudt(getSecp256k1Account(MNEMONIC, AddressType.Change, 2).address), + // to: getAnyOneCanPayAccount(MNEMONIC, AddressType.Change, 5).address, + // transferAmount: 100, + // tx_hash: "" + // }, + // + // { + // fromAccount: getSecp256k1Account(MNEMONIC, AddressType.Change, 3), + // fromAddress: getSecp256k1Account(MNEMONIC, AddressType.Change, 3).address, + // SUDTTokenId: sudt.ownerForSudt(getSecp256k1Account(MNEMONIC, AddressType.Change, 3).address), + // to: getAnyOneCanPayAccount(MNEMONIC, AddressType.Change, 8).address, + // transferAmount: 10000, + // tx_hash: "" + // }, + + // { + // fromAccount: getSecp256k1Account(MNEMONIC, AddressType.Change, 2), + // fromAddress: getSecp256k1Account(MNEMONIC, AddressType.Change, 2).address, + // SUDTTokenId: sudt.ownerForSudt(getSecp256k1Account(MNEMONIC, AddressType.Change, 2).address), + // to: getSecp256k1Account(MNEMONIC, AddressType.Change, 1).address, + // transferAmount: 1000, + // tx_hash: "" + // } + ] + for (let i = 0; i < test_transfer_list.length; i++) { + let {fromAccount, SUDTTokenId, to, transferAmount} = test_transfer_list[i] + let txSkeleton = TransactionSkeleton({cellProvider: e2eProvider.indexer}); + txSkeleton = await sudt.transfer( + txSkeleton, + [fromAccount.address], + SUDTTokenId, + to, + transferAmount, undefined, undefined, + ); + let txHash = await e2eProvider.sendAndSignTxSkeleton(txSkeleton, 1000, fromAccount) + console.log(txHash) + await e2eProvider.waitTransactionCommitted(txHash) + test_transfer_list[i].tx_hash = txHash + } + console.table(test_transfer_list) + }) it("query sudt balance", async () => { - let queryAccount = alice; - let issHash = sudt.ownerForSudt(alice.address) let config = await getConfig() - if (config.SCRIPTS && config.SCRIPTS.SUDT) { - let cells = await e2eProvider.findCells({ - lock: queryAccount.lockScript, - type: {args: issHash, codeHash: config.SCRIPTS.SUDT.CODE_HASH, hashType: config.SCRIPTS.SUDT.HASH_TYPE} - }) - let balance = cells.reduce((accumulator, x) => { - const bigUInt = utils.readBigUInt128LE(x.data); - return BI.from(accumulator).add(bigUInt); - }, BI.from(0)).toBigInt(); - console.log("account sudt balance:", balance) + let query_list = [ + { + queryAccount: getSecp256k1Account(MNEMONIC, AddressType.Change, 1), + address: getSecp256k1Account(MNEMONIC, AddressType.Change, 1).address, + SUDTTokenId: sudt.ownerForSudt(getSecp256k1Account(MNEMONIC, AddressType.Change, 1).address), + balance: 0n + }, + // { + // queryAccount: getSecp256k1Account(MNEMONIC, AddressType.Change, 2), + // address: getSecp256k1Account(MNEMONIC, AddressType.Change, 2).address, + // SUDTTokenId: sudt.ownerForSudt(getSecp256k1Account(MNEMONIC, AddressType.Change, 2).address), + // balance: 0n + // }, + // { + // queryAccount: getSecp256k1Account(MNEMONIC, AddressType.Change, 3), + // address: getSecp256k1Account(MNEMONIC, AddressType.Change, 3).address, + // SUDTTokenId: sudt.ownerForSudt(getSecp256k1Account(MNEMONIC, AddressType.Change, 3).address), + // balance: 0n + // }, + // { + // queryAccount: getSecp256k1Account(MNEMONIC, AddressType.Change, 4), + // address: getSecp256k1Account(MNEMONIC, AddressType.Change, 4).address, + // SUDTTokenId: sudt.ownerForSudt(getSecp256k1Account(MNEMONIC, AddressType.Change, 4).address), + // balance: 0n + // }, + + ] + + for (let i = 0; i < query_list.length; i++) { + let {queryAccount, SUDTTokenId} = query_list[i] + if (config.SCRIPTS && config.SCRIPTS.SUDT) { + let cells = await e2eProvider.findCells({ + lock: queryAccount.lockScript, + type: { + args: SUDTTokenId, + codeHash: config.SCRIPTS.SUDT.CODE_HASH, + hashType: config.SCRIPTS.SUDT.HASH_TYPE + } + }) + let balance = cells.reduce((accumulator, x) => { + const bigUInt = unpackAmount(x.data); + return BI.from(accumulator).add(bigUInt); + }, BI.from(0)).toBigInt(); + query_list[i].balance = balance + } } })