diff --git a/test/02.contract_query.test.ts b/test/02.contract_query.test.ts index 9896699..9083028 100644 --- a/test/02.contract_query.test.ts +++ b/test/02.contract_query.test.ts @@ -2,90 +2,109 @@ import { expect } from 'chai'; import { FirmaSDK } from "../sdk/FirmaSDK" import { FirmaUtil } from "../sdk/FirmaUtil" import { ContractFileType, ContractLogType } from '../sdk/firmachain/contract'; -import { TestChainConfig } from './config_test'; +import { aliceMnemonic, bobMnemonic, TestChainConfig } from './config_test'; describe('[02. Contract Query Test]', () => { let firma: FirmaSDK; + let timeStamp = Math.round(+new Date() / 1000); + let fileHash = "0xklsdjflaksjflaksjf" + timeStamp; + let aliceAddress: string; + let bobAddress: string; beforeEach(function() { firma = new FirmaSDK(TestChainConfig); }) + it('Pre-execution for testing for contract file hash', async () => { + + const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); + + aliceAddress = await aliceWallet.getAddress(); + bobAddress = await bobWallet.getAddress(); + + const eventName = "CreateContract"; + const logJsonString = "{}"; + await firma.Contract.addContractLog(aliceWallet, fileHash, timeStamp, eventName, aliceAddress, logJsonString); + await firma.Contract.addContractLog(bobWallet, fileHash, timeStamp, eventName, bobAddress, logJsonString); + + const ownerList = [aliceAddress, bobAddress]; + const fileJsonString = "{\"fileHash\": \"" + fileHash + "\"" + "}"; + await firma.Contract.createContractFile(aliceWallet, fileHash, timeStamp, ownerList, fileJsonString); + }); + it('Contract getContractLogAll-pagination', async () => { - var result = await firma.Contract.getContractLogAll(); + let result = await firma.Contract.getContractLogAll(); const total = result.pagination.total; - var totalItemList: ContractLogType[] = []; - var index = 0; + let totalItemList: ContractLogType[] = []; + let index = 0; while (result.pagination.next_key != null) { - for (var i = 0; i < result.dataList.length; i++) { + for (let i = 0; i < result.dataList.length; i++) { totalItemList[index++] = result.dataList[i]; } result = await firma.Contract.getContractLogAll(result.pagination.next_key); } - for (var i = 0; i < result.dataList.length; i++) { + for (let i = 0; i < result.dataList.length; i++) { totalItemList[index++] = result.dataList[i]; } expect(totalItemList.length).to.be.equal(total); }); - it('Contract getContractLogAll', async () => { - var contractLogList = await firma.Contract.getContractLogAll(); - expect(contractLogList.dataList.length).to.be.equal(contractLogList.dataList.length); + const contractLogList = await firma.Contract.getContractLogAll(); + expect(contractLogList.dataList.length).to.be.equal(contractLogList.dataList.length); expect(contractLogList.dataList.length).to.be.greaterThan(0); }); it('Contract getContractLog', async () => { const logId = "0"; + const contractLog = await firma.Contract.getContractLog(logId); - var contractLog = await firma.Contract.getContractLog(logId); expect(contractLog.id).to.be.equal(logId); }); - it('Contract getContractFileAll-pagination', async () => { - var result = await firma.Contract.getContractFileAll(); + let result = await firma.Contract.getContractFileAll(); const total = result.pagination.total; - var totalItemList: ContractFileType[] = []; - var index = 0; + let totalItemList: ContractFileType[] = []; + let index = 0; while (result.pagination.next_key != null) { - for (var i = 0; i < result.dataList.length; i++) { + for (let i = 0; i < result.dataList.length; i++) { totalItemList[index++] = result.dataList[i]; } result = await firma.Contract.getContractFileAll(result.pagination.next_key); } - for (var i = 0; i < result.dataList.length; i++) { + for (let i = 0; i < result.dataList.length; i++) { totalItemList[index++] = result.dataList[i]; } expect(totalItemList.length).to.be.equal(total); }); - it('Contract getContractFileAll', async () => { - var contractFileList = await firma.Contract.getContractFileAll(); + const contractFileList = await firma.Contract.getContractFileAll(); - // for(var i = 0; i < contractFileList.length; i++){ + // for(let i = 0; i < contractFileList.length; i++){ // console.log(contractFileList[i]); // } //console.log("contractFileList:" + contractFileList.length); @@ -95,37 +114,30 @@ describe('[02. Contract Query Test]', () => { }); - it.skip('Contract getContractFile', async () => { - - const contractFileHash = "e3b0c44afbf4c8996fb92427ae41e4649b934ca495991b7852b85531231asddaqwqe"; + it('Contract getContractFile', async () => { - var contractFile = await firma.Contract.getContractFile(contractFileHash); - expect(contractFile.fileHash).to.be.equal(contractFileHash); + const contractFile = await firma.Contract.getContractFile(fileHash); + expect(contractFile.fileHash).to.be.equal(fileHash); }); it('Contract getContractListFromHash', async () => { - const contractHash = "0xsalkdjfasldkjf2"; - var idList = await firma.Contract.getContractListFromHash(contractHash); + const idList = await firma.Contract.getContractListFromHash(fileHash); for (let i = 0; i < idList.length; i++) { - var data = await firma.Contract.getContractLog(idList[i]); + const data = await firma.Contract.getContractLog(idList[i]); //console.log(data); } expect(idList.length).to.be.greaterThan(0); }); - it.skip('Contract isContractOwner', async () => { - - const contractFileHash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85531231asddaqwqe"; - const owner1 = "firma1nssuz67am2uwc2hjgvphg0fmj3k9l6cx65ux9u0kdjaaldlq"; - const owner2 = "firma106a9nzxcxu526z4cx6nq4zqpx7ctrx65a020yk23kdjaaldlq"; + it('Contract isContractOwner', async () => { - var isOwner = await firma.Contract.isContractOwner(contractFileHash, owner1); + let isOwner = await firma.Contract.isContractOwner(fileHash, aliceAddress); expect(isOwner).to.be.equal(true); - isOwner = await firma.Contract.isContractOwner(contractFileHash, owner2); + isOwner = await firma.Contract.isContractOwner(fileHash, bobAddress); expect(isOwner).to.be.equal(true); }); diff --git a/test/04.bank_tx.test.ts b/test/04.bank_tx.test.ts index f64ba76..ebab703 100644 --- a/test/04.bank_tx.test.ts +++ b/test/04.bank_tx.test.ts @@ -34,7 +34,7 @@ describe('[04. Bank Tx Test]', () => { expect(result.code).to.equal(5); }); - it.skip('bank send Fail - Big fee remittance', async function () { + it('bank send Fail - Big fee remittance', async function () { const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const targetWallet = await firma.Wallet.fromMnemonic(bobMnemonic); diff --git a/test/07.feegrant_query.test.ts b/test/07.feegrant_query.test.ts index e16fe3b..2ac0f58 100644 --- a/test/07.feegrant_query.test.ts +++ b/test/07.feegrant_query.test.ts @@ -11,17 +11,15 @@ describe('[07. Feegrant Query Test]', () => { firma = new FirmaSDK(TestChainConfig); }) - it.skip('feegrant getGranteeAllowance', async () => { - + it('feegrant getGranteeAllowance', async () => { const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); + const bobAddress = await bobWallet.getAddress(); - var result = await firma.FeeGrant.getGranteeAllowance(await aliceWallet.getAddress(), await bobWallet.getAddress()); - /*console.log(result['@type']); - console.log(result.spendLimit); - console.log(result.expiration);*/ - + await firma.FeeGrant.grantBasicAllowance(aliceWallet, bobAddress); + const result = await firma.FeeGrant.getGranteeAllowance(await aliceWallet.getAddress(), await bobWallet.getAddress()); + await firma.FeeGrant.revokeAllowance(aliceWallet, bobAddress); }); it('feegrant getGranteeAllowanceAll', async () => { diff --git a/test/08.gas_estimate.test.ts b/test/08.gas_estimate.test.ts index 5b368f0..7180e3c 100644 --- a/test/08.gas_estimate.test.ts +++ b/test/08.gas_estimate.test.ts @@ -2,8 +2,9 @@ import { FirmaSDK } from "../sdk/FirmaSDK" import { FirmaUtil } from '../sdk/FirmaUtil'; import { aliceMnemonic, bobMnemonic, validatorMnemonic, TestChainConfig } from './config_test'; import { VotingOption } from '../sdk/firmachain/common'; +import { Timestamp } from "../sdk/firmachain/google/protobuf/timestamp"; -describe.skip('[08. Gas Estimation Test]', () => { +describe('[08. Gas Estimation Test]', () => { let firma: FirmaSDK; @@ -24,26 +25,35 @@ describe.skip('[08. Gas Estimation Test]', () => { } catch (error) { console.log(error); } - }); it("1-2. bank sendToken gas estimation", async () => { - const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); - const targetWallet = await firma.Wallet.fromMnemonic(bobMnemonic); - const amount = 1; + const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); + const aliceAddress = await aliceWallet.getAddress(); + const bobAddress = await bobWallet.getAddress(); + + const timeStamp = Math.round(+new Date() / 1000); - const tokenID = "ukomx"; + const tokenName = `KOMX TOKEN${timeStamp}`; + const tokenSymbol = `KOMX${timeStamp}`; + const tokenID = `ukomx${timeStamp}`; + const tokenURI = "https://firmachain.org"; + const totalSupply = 100000; const decimal = 6; + const mintable = true; + const burnable = true; + const sendAmount = 1; try { - const gas = await firma.Bank.getGasEstimationSendToken(wallet, await targetWallet.getAddress(), tokenID, amount, decimal); + await firma.Token.createToken(aliceWallet, tokenName, tokenSymbol, tokenURI, totalSupply, decimal, mintable, burnable); + const gas = await firma.Bank.getGasEstimationSendToken(aliceWallet, bobAddress, tokenID, sendAmount, decimal); console.log("estimateGas : " + gas); - + await firma.Token.burn(aliceWallet, tokenID, totalSupply, decimal); } catch (error) { console.log(error); } - }); it("2-1. Contract addContractLog getGasEstimationFromUnSignedTxList gas estimation", async () => { @@ -125,7 +135,7 @@ describe.skip('[08. Gas Estimation Test]', () => { console.log("estimateGas : " + gas); }); - it.skip("4-1. Feegrant GrantPeriodicAllowance gas estimation", async () => { + it("4-1. Feegrant GrantPeriodicAllowance gas estimation", async () => { const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); @@ -147,7 +157,7 @@ describe.skip('[08. Gas Estimation Test]', () => { console.log("estimateGas : " + gas); }); - it.skip("4-2. Feegrant GrantBasicAllowance gas estimation", async () => { + it("4-2. Feegrant GrantBasicAllowance gas estimation", async () => { const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); @@ -161,7 +171,9 @@ describe.skip('[08. Gas Estimation Test]', () => { const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); + await firma.FeeGrant.grantBasicAllowance(aliceWallet, await bobWallet.getAddress()); const gas = await firma.FeeGrant.getGasEstimationRevokeAllowance(aliceWallet, await bobWallet.getAddress()); + await firma.FeeGrant.revokeAllowance(aliceWallet, await bobWallet.getAddress()); console.log("estimateGas : " + gas); }); @@ -217,9 +229,6 @@ describe.skip('[08. Gas Estimation Test]', () => { } catch (error) { console.log(error); } - - - }); it("6-1. Distribution withdrawAllRewards gas estimation", async () => { @@ -342,34 +351,44 @@ describe.skip('[08. Gas Estimation Test]', () => { console.log("estimateGas : " + gas); }); - it("7-6. Gov deposit gas estimation", async () => { + it("7-6. Gov deposit & vote gas estimation", async () => { - const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); - const proposalId = 1; - const amount = 1000; - var result = await firma.Gov.deposit(wallet, proposalId, amount); + const proposalList = await firma.Gov.getProposalList(); + const proposalId = Number(proposalList[proposalList.length - 1].proposal_id) + 1; - const gas = await firma.Gov.getGasEstimationDeposit(wallet, proposalId, amount); - console.log("estimateGas : " + gas); + const title = `Text Proposal ${proposalId}`; + const description = "This is a text proposal"; + const initDeposit = 5000; + const depositAmount = 5000; + + const result = await firma.Gov.submitTextProposal(aliceWallet, title, description, initDeposit); + console.log(result); + + const depositGas = await firma.Gov.getGasEstimationDeposit(aliceWallet, proposalId, depositAmount); + console.log("deposit estimateGas : " + depositGas); + + const voteGas = await firma.Gov.getGasEstimationVote(aliceWallet, proposalId, VotingOption.VOTE_OPTION_YES); + console.log("vote estimateGas : " + voteGas); }); - it("7-7. Gov vote gas estimation", async () => { + // it("7-7. Gov vote gas estimation", async () => { - const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); - const proposalId = 1; + // const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + // const proposalId = 1; - const gas = await firma.Gov.getGasEstimationVote(wallet, proposalId, VotingOption.VOTE_OPTION_YES); - console.log("estimateGas : " + gas); - }); + // const gas = await firma.Gov.getGasEstimationVote(wallet, proposalId, VotingOption.VOTE_OPTION_YES); + // console.log("estimateGas : " + gas); + // }); it("8-1. Token createToken gas estimation", async () => { const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const tokenName = "KOMX TOKEN"; - const symbol = "KOMX63232"; - const tokenURI = "https://naver.com"; + const symbol = "KOMX"; + const tokenURI = "https://firmachain.org"; const totalSupply = 10000; const decimal = 6; const mintable = true; @@ -379,39 +398,63 @@ describe.skip('[08. Gas Estimation Test]', () => { console.log("estimateGas : " + gas); }); - it("8-2. Token mint gas estimation", async () => { + it("8-2. Token mint & burn gas estimation", async () => { const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const bobAddress = await (await firma.Wallet.fromMnemonic(bobMnemonic)).getAddress(); - const tokenID = "ukomx6"; - const amount = 10000; + const timeStamp = Math.round(+new Date() / 1000); + + const tokenName = `KOMX TOKEN${timeStamp}`; + const symbol = `KOMX${timeStamp}`; + const tokenID = `ukomx${timeStamp}`; + const tokenURI = "https://firmachain.org"; + const totalSupply = 10000000; const decimal = 6; + const isMintable = true; + const isBurnable = true; + const amount = 10000; - const gas = await firma.Token.getGasEstimationMint(wallet, tokenID, amount, decimal, bobAddress); - console.log("estimateGas : " + gas); + await firma.Token.createToken(wallet, tokenName, symbol, tokenURI, totalSupply, decimal, isMintable, isBurnable); + const mintGas = await firma.Token.getGasEstimationMint(wallet, tokenID, amount, decimal, bobAddress); + console.log("estimate mint gas : " + mintGas); + const burnGas = await firma.Token.getGasEstimationBurn(wallet, tokenID, amount, decimal); + console.log("estimate burn gas : " + burnGas); + await firma.Token.burn(wallet, tokenID, totalSupply, decimal); }); - it("8-3. Token burn gas estimation", async () => { + // it("8-3. Token burn gas estimation", async () => { - const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + // const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); - const tokenID = "ukomx6"; - const amount = 10; - const decimal = 6; + // const tokenID = "ukomx6"; + // const amount = 10; + // const decimal = 6; - const gas = await firma.Token.getGasEstimationBurn(wallet, tokenID, amount, decimal); - console.log("estimateGas : " + gas); - }); + // const gas = await firma.Token.getGasEstimationBurn(wallet, tokenID, amount, decimal); + // console.log("estimateGas : " + gas); + // }); - it("8-4. Token updateTokenURI gas estimation", async () => { + it("8-3. Token updateTokenURI gas estimation", async () => { const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); - const tokenID = "ukomx6"; + const timeStamp = Math.round(+new Date() / 1000); + + const tokenName = `KOMX TOKEN${timeStamp}`; + const symbol = `KOMX${timeStamp}`; + const tokenID = `ukomx${timeStamp}`; const tokenURI = "https://firmachain.org"; - - const gas = await firma.Token.getGasEstimationUpdateTokenURI(wallet, tokenID, tokenURI); + const totalSupply = 10000000; + const decimal = 6; + const isMintable = true; + const isBurnable = true; + + await firma.Token.createToken(wallet, tokenName, symbol, tokenURI, totalSupply, decimal, isMintable, isBurnable); + + const updateTokenURI = "https://firmachain.org"; + const gas = await firma.Token.getGasEstimationUpdateTokenURI(wallet, tokenID, updateTokenURI); console.log("estimateGas : " + gas); + await firma.Token.burn(wallet, tokenID, totalSupply, decimal); }); }); \ No newline at end of file diff --git a/test/12.staking_tx.test.ts b/test/12.staking_tx.test.ts index 03f1f7b..57badf7 100644 --- a/test/12.staking_tx.test.ts +++ b/test/12.staking_tx.test.ts @@ -102,7 +102,7 @@ describe('[12. Staking Tx Test]', () => { }); // NOTICE: not decide to include firma.js spec - it.skip('editValidator OK', async () => { + it('editValidator OK', async () => { const wallet = await firma.Wallet.fromMnemonic(validatorMnemonic); diff --git a/test/13.staking_query.test.ts b/test/13.staking_query.test.ts index 3c350d9..5d06bc5 100644 --- a/test/13.staking_query.test.ts +++ b/test/13.staking_query.test.ts @@ -115,6 +115,7 @@ describe('[13. Staking Query Test]', () => { var validatorList = (await firma.Staking.getValidatorList()).dataList; const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + await firma.Staking.undelegate(wallet, validatorList[0].operator_address, 1); var result = await firma.Staking.getUndelegationInfoFromValidator(await wallet.getAddress(), validatorList[0].operator_address); // If there is no data in the list, throw 404 exception. diff --git a/test/16.gov_tx.test.ts b/test/16.gov_tx.test.ts index 9b5c649..b912a10 100644 --- a/test/16.gov_tx.test.ts +++ b/test/16.gov_tx.test.ts @@ -6,7 +6,7 @@ import { aliceMnemonic, bobMnemonic, TestChainConfig, validatorMnemonic } from ' // If test it, the properties of the chain change, so skip it. -describe.skip('[16. Gov Tx Test]', () => { +describe('[16. Gov Tx Test]', () => { let firma: FirmaSDK; @@ -78,7 +78,7 @@ describe.skip('[16. Gov Tx Test]', () => { const title = "Software Upgrade proposal1"; const description = "This is a software upgrade proposal"; - const upgradeName = "v0.2.7"; + const upgradeName = "v0.3.5"; const upgradeHeight = 20000000; var result = await firma.Gov.submitSoftwareUpgradeProposalByHeight(aliceWallet, title, description, initialDepositFCT, upgradeName, upgradeHeight); @@ -124,40 +124,61 @@ describe.skip('[16. Gov Tx Test]', () => { expect(result.code).to.equal(0); });*/ - // TODO: get recent gov proposal list and then set proposalId for below case - const tempProposalId = 1; - // more deposit after initial deposit case it('Deposit OK', async () => { const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); - const proposalId = tempProposalId; - const amount = 1000; - var result = await firma.Gov.deposit(wallet, proposalId, amount); - //console.log(result); - expect(result.code).to.equal(0); - }); + const proposalList = await firma.Gov.getProposalList(); + const lastProposalId = Number(proposalList[proposalList.length - 1].proposal_id) + 1; - it('Vote - alice YES', async () => { + const title = `Text proposal ${lastProposalId}`; + const description = `This is a text proposal(${lastProposalId})`; + const initialDeposit = 1000; - const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); - const proposalId = tempProposalId; + await firma.Gov.submitTextProposal(wallet, title, description, initialDeposit); - var result = await firma.Gov.vote(wallet, proposalId, VotingOption.VOTE_OPTION_YES); + const depositAmount = 1000; + + const result = await firma.Gov.deposit(wallet, lastProposalId, depositAmount); //console.log(result); expect(result.code).to.equal(0); }); - it('Vote - bob NO', async () => { + it('Vote - alice YES & bob NO', async () => { + - const wallet = await firma.Wallet.fromMnemonic(bobMnemonic); - const proposalId = tempProposalId; + const proposalList = await firma.Gov.getProposalList(); + const lastProposalId = Number(proposalList[proposalList.length - 1].proposal_id) + 1; + + const title = `Text proposal ${lastProposalId}`; + const description = `This is a text proposal(${lastProposalId})`; + const initialDeposit = 5000; + + const validatorWallet = await firma.Wallet.fromMnemonic(validatorMnemonic); + await firma.Gov.submitTextProposal(validatorWallet, title, description, initialDeposit); + + // Alice + const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + const aliceVotingResult = await firma.Gov.vote(aliceWallet, lastProposalId, VotingOption.VOTE_OPTION_YES); + // Bob + const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); + const bobVotingResult = await firma.Gov.vote(bobWallet, lastProposalId, VotingOption.VOTE_OPTION_NO); - var result = await firma.Gov.vote(wallet, proposalId, VotingOption.VOTE_OPTION_NO); //console.log(result); - expect(result.code).to.equal(0); + expect(aliceVotingResult.code).to.equal(0); + expect(bobVotingResult.code).to.equal(0); }); + // it('Vote - bob NO', async () => { + + // const wallet = await firma.Wallet.fromMnemonic(bobMnemonic); + // const proposalId = tempProposalId; + + // var result = await firma.Gov.vote(wallet, proposalId, VotingOption.VOTE_OPTION_NO); + // //console.log(result); + // expect(result.code).to.equal(0); + // }); + // TODO: more voting case need it! }); \ No newline at end of file diff --git a/test/17.gov_query.test.ts b/test/17.gov_query.test.ts index b994ef3..be0fae9 100644 --- a/test/17.gov_query.test.ts +++ b/test/17.gov_query.test.ts @@ -1,6 +1,6 @@ -import { ProposalStatus } from '../sdk/firmachain/gov'; +import { ProposalStatus, VotingOption } from '../sdk/firmachain/gov'; import { FirmaSDK } from "../sdk/FirmaSDK" -import { TestChainConfig } from './config_test'; +import { aliceMnemonic, bobMnemonic, TestChainConfig, validatorMnemonic } from './config_test'; describe('[17. Gov Query Test]', () => { @@ -28,10 +28,14 @@ describe('[17. Gov Query Test]', () => { let proposalList = await firma.Gov.getProposalList(); - if (proposalList.length > 0) { - const id = "1"; - let proposal = await firma.Gov.getProposal(id); - //console.log(proposal); + try { + if (proposalList.length > 0) { + const id = proposalList[proposalList.length - 1].proposal_id; + let proposal = await firma.Gov.getProposal(id); + //console.log(proposal); + } + } catch (error) { + console.log(error); } }) @@ -45,12 +49,27 @@ describe('[17. Gov Query Test]', () => { // current tally info it('get getCurrentVoteInfo', async () => { - let proposalList = await firma.Gov.getProposalList(); + try { + const proposalList = await firma.Gov.getProposalList(); + const lastProposalId = Number(proposalList[proposalList.length - 1].proposal_id) + 1; + + const title = `Text proposal ${lastProposalId}`; + const description = `This is a text proposal(${lastProposalId})`; + const initialDeposit = 5000; + + const validatorWallet = await firma.Wallet.fromMnemonic(validatorMnemonic); + await firma.Gov.submitTextProposal(validatorWallet, title, description, initialDeposit); + + // Alice + const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + const aliceVotingResult = await firma.Gov.vote(aliceWallet, lastProposalId, VotingOption.VOTE_OPTION_YES); + // Bob + const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); + const bobVotingResult = await firma.Gov.vote(bobWallet, lastProposalId, VotingOption.VOTE_OPTION_NO); - if (proposalList.length > 0) { - const proposalId = "1"; - let param = await firma.Gov.getCurrentVoteInfo(proposalId); - // console.log(param); + const voteInfo = await firma.Gov.getCurrentVoteInfo(lastProposalId.toString()); + } catch (error) { + console.log(error); } }) }); \ No newline at end of file diff --git a/test/18.util.test.ts b/test/18.util.test.ts index dd4ea2a..0f52b41 100644 --- a/test/18.util.test.ts +++ b/test/18.util.test.ts @@ -1,9 +1,15 @@ import { expect } from 'chai'; import { FirmaUtil } from '../sdk/FirmaUtil'; +import { FirmaSDK } from '../sdk/FirmaSDK'; +import { TestChainConfig } from './config_test'; describe('[18. util Test]', () => { - // getHashFromString + let firma: FirmaSDK; + + beforeEach(function() { + firma = new FirmaSDK(TestChainConfig); + }) it('getSha1HashFromString test', async () => { @@ -41,10 +47,14 @@ describe('[18. util Test]', () => { it('getValOperAddressFromAccAddress test', async () => { - const accAddress = "firma1a85hxs97rxsrf0yzdn72vhfu39sa0dwxv90ghy"; - const valoperAddress = "firmavaloper1a85hxs97rxsrf0yzdn72vhfu39sa0dwxjkynh2"; + const validatorList = await firma.Staking.getValidatorList() + const validator = validatorList.dataList[0]; + const valoperAddress = validator.operator_address; + + const accAddress = FirmaUtil.getAccAddressFromValOperAddress(valoperAddress); + + const result = FirmaUtil.getValOperAddressFromAccAddress(accAddress); - let result = FirmaUtil.getValOperAddressFromAccAddress(accAddress); expect(result).to.be.equal(valoperAddress); }) diff --git a/test/19.chain.test.ts b/test/19.chain.test.ts index 4bdbbcc..d6f639b 100644 --- a/test/19.chain.test.ts +++ b/test/19.chain.test.ts @@ -1,6 +1,6 @@ import { FirmaConfig } from "../sdk/FirmaConfig"; import { FirmaSDK } from "../sdk/FirmaSDK" -import { TestChainConfig } from './config_test'; +import { aliceMnemonic, bobMnemonic, TestChainConfig } from './config_test'; describe('[19. chain Test]', () => { @@ -15,9 +15,14 @@ describe('[19. chain Test]', () => { //console.log(result); }) - it.skip('getTransactionByHash test', async () => { - const txHash = "0x5DA9D094D15660D21947C9EEF1329CCB70117E7BCD3A451F27E5C7AFF5DB6DF0"; - const result = await firma.BlockChain.getTransactionByHash(txHash); + it('getTransactionByHash test', async () => { + const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); + const bobAddress = await bobWallet.getAddress(); + const sendAmount = 1; + + const txResult = await firma.Bank.send(aliceWallet, bobAddress, sendAmount); + const result = await firma.BlockChain.getTransactionByHash(`0x${txResult.transactionHash}`); //console.log(result); //console.log(result.tx_result.events[0]); diff --git a/test/23.authz_tx.test.ts b/test/23.authz_tx.test.ts index 72624dd..fa29e0e 100644 --- a/test/23.authz_tx.test.ts +++ b/test/23.authz_tx.test.ts @@ -58,7 +58,7 @@ describe('[23. Authz Tx Test]', () => { expect(result.code).to.be.equal(0); }); - it.skip('Authz Revoke Send', async () => { + it('Authz Revoke Send', async () => { let aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const bobAddress = await (await firma.Wallet.fromMnemonic(bobMnemonic)).getAddress(); @@ -119,7 +119,7 @@ describe('[23. Authz Tx Test]', () => { expect(result.code).to.be.equal(0); }); - it.skip('Authz Revoke Delegate', async () => { + it('Authz Revoke Delegate', async () => { let aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const bobAddress = await (await firma.Wallet.fromMnemonic(bobMnemonic)).getAddress(); @@ -176,8 +176,7 @@ describe('[23. Authz Tx Test]', () => { expect(result.code).to.be.equal(0); }); - - it.skip('Authz Revoke UnDelegate', async () => { + it('Authz Revoke UnDelegate', async () => { let aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const bobAddress = await (await firma.Wallet.fromMnemonic(bobMnemonic)).getAddress(); @@ -189,57 +188,61 @@ describe('[23. Authz Tx Test]', () => { }); it('Authz Grant ReDelegate', async () => { - let aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const bobAddress = await (await firma.Wallet.fromMnemonic(bobMnemonic)).getAddress(); const delegationInfo = (await firma.Staking.getTotalDelegationInfo(await aliceWallet.getAddress())).dataList; - const validatorAddress = delegationInfo[0].delegation.validator_address; + const validatorSrcAddress = delegationInfo[0].delegation.validator_address; + const validatorDstAddress = delegationInfo[1].delegation.validator_address; var expirationDate = new Date(); expirationDate.setFullYear(expirationDate.getFullYear() + 1); const maxFCT = 10; - var result = await firma.Authz.grantStakeAuthorization(aliceWallet, bobAddress, [validatorAddress], AuthorizationType.AUTHORIZATION_TYPE_REDELEGATE, expirationDate, maxFCT); + var result = await firma.Authz.grantStakeAuthorization(aliceWallet, bobAddress, [validatorSrcAddress, validatorDstAddress], AuthorizationType.AUTHORIZATION_TYPE_REDELEGATE, expirationDate, maxFCT); //console.log(result); expect(result.code).to.be.equal(0); }); - it.skip('Authz ExecuteAllowance-ReDelegate', async () => { - let aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); - let bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); + it('Authz ExecuteAllowance-ReDelegate', async () => { + const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); const delegationInfo = (await firma.Staking.getTotalDelegationInfo(await aliceWallet.getAddress())).dataList; const validatorSrcAddress = delegationInfo[0].delegation.validator_address; + const validatorDstAddress = delegationInfo[1].delegation.validator_address; - // INFO: add a year expiration to test. - // for test - //const validatorDstAddress = delegationInfo[1].delegation.validator_address; - const validatorDstAddress = validatorSrcAddress; - - const amountFCT = 9; + const amountFCT = 1; const address = await aliceWallet.getAddress(); const sendAmount = { denom: firma.Config.denom, amount: FirmaUtil.getUFCTStringFromFCT(amountFCT) }; - let msgRedelegate = StakingTxClient.msgRedelegate({ + const msgRedelegate = StakingTxClient.msgRedelegate({ delegatorAddress: address, validatorSrcAddress: validatorSrcAddress, validatorDstAddress: validatorDstAddress, amount: sendAmount }); - const anyData = FirmaUtil.getAnyData(StakingTxClient.getRegistry(),msgRedelegate) - - var result = await firma.Authz.executeAllowance(bobWallet, [anyData]); + try { + const anyData = FirmaUtil.getAnyData(StakingTxClient.getRegistry(),msgRedelegate); - //console.log(result); + const gas = await firma.Authz.getGasEstimationExecuteAllowance(bobWallet, [anyData]); + const fee = Math.ceil(gas * 0.1); - expect(result.code).to.be.equal(0); + const result = await firma.Authz.executeAllowance(bobWallet, [anyData], { gas, fee }); + + // console.log(result); + + expect(result.code).to.be.equal(0); + } catch (error) { + console.log(error); + } }); - it.skip('Authz Revoke ReDelegate', async () => { + it('Authz Revoke ReDelegate', async () => { let aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const bobAddress = await (await firma.Wallet.fromMnemonic(bobMnemonic)).getAddress(); @@ -293,7 +296,7 @@ describe('[23. Authz Tx Test]', () => { expect(result.code).to.be.equal(0); }); - it.skip('Authz Revoke-GenericAuthorization MsgWithdrawDelegatorReward', async () => { + it('Authz Revoke-GenericAuthorization MsgWithdrawDelegatorReward', async () => { let aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const bobAddress = await (await firma.Wallet.fromMnemonic(bobMnemonic)).getAddress(); diff --git a/test/24.authz_query.test.ts b/test/24.authz_query.test.ts index 7a42bd1..b1dfdd2 100644 --- a/test/24.authz_query.test.ts +++ b/test/24.authz_query.test.ts @@ -14,11 +14,21 @@ describe('[24. Authz query Test]', () => { it('Authz getSendGrantData', async () => { - const granter = await (await firma.Wallet.fromMnemonic(aliceMnemonic)).getAddress(); - const grantee = await (await firma.Wallet.fromMnemonic(bobMnemonic)).getAddress(); - - let result = await firma.Authz.getSendGrantData(granter, grantee); - + const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); + const granter_aliceAddress = await aliceWallet.getAddress(); + const grantee_bobAddress = await bobWallet.getAddress(); + + const expirationDate = new Date(); + expirationDate.setFullYear(expirationDate.getFullYear() + 1); + const maxFCT = 10; + + // grant send authorization + await firma.Authz.grantSendAuthorization(aliceWallet, grantee_bobAddress, expirationDate, maxFCT); + // get grant data (send) + const result = await firma.Authz.getSendGrantData(granter_aliceAddress, grantee_bobAddress); + // revoke send authorization + await firma.Authz.revokeSendAuthorization(aliceWallet, grantee_bobAddress); //console.log(result); /*console.log(result.dataList[0].authorization['@type']); @@ -31,12 +41,22 @@ describe('[24. Authz query Test]', () => { it('Authz getGenericGrantData', async () => { - const granter = await (await firma.Wallet.fromMnemonic(aliceMnemonic)).getAddress(); - const grantee = await (await firma.Wallet.fromMnemonic(bobMnemonic)).getAddress(); + const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); + const granter_aliceAddress = await aliceWallet.getAddress(); + const grantee_bobAddress = await bobWallet.getAddress(); + + const expirationDate = new Date(); + expirationDate.setFullYear(expirationDate.getFullYear() + 1); const msg = "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward"; - let result = (await firma.Authz.getGenericGrantData(granter, grantee, msg)).dataList; + // grant generic authorization + await firma.Authz.grantGenericAuthorization(aliceWallet, grantee_bobAddress, msg, expirationDate); + // get grant data (generic) + const result = (await firma.Authz.getGenericGrantData(granter_aliceAddress, grantee_bobAddress, msg)).dataList; + // revoke generic authorization + await firma.Authz.revokeGenericAuthorization(aliceWallet, grantee_bobAddress, msg); /*console.log(result[0].authorization['@type']); console.log(result[0].authorization.msg); @@ -47,11 +67,25 @@ describe('[24. Authz query Test]', () => { it('Authz getStakingGrantData - delegate', async () => { - const granter = await (await firma.Wallet.fromMnemonic(aliceMnemonic)).getAddress(); - const grantee = await (await firma.Wallet.fromMnemonic(bobMnemonic)).getAddress(); + const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); + const granter_aliceAddress = await aliceWallet.getAddress(); + const grantee_bobAddress = await bobWallet.getAddress(); - let result = (await firma.Authz.getStakingGrantData(granter, grantee, AuthorizationType.AUTHORIZATION_TYPE_DELEGATE)).dataList; + const expirationDate = new Date(); + expirationDate.setFullYear(expirationDate.getFullYear() + 1); + const maxFCT = 10; + const delegationInfo = (await firma.Staking.getTotalDelegationInfo(await aliceWallet.getAddress())).dataList; + const validatorAddress = delegationInfo[0].delegation.validator_address; + + // grant deletgate authorization + await firma.Authz.grantStakeAuthorization(aliceWallet, grantee_bobAddress, [validatorAddress], AuthorizationType.AUTHORIZATION_TYPE_DELEGATE, expirationDate, maxFCT); + // get grant data (delegate) + const result = (await firma.Authz.getStakingGrantData(granter_aliceAddress, grantee_bobAddress, AuthorizationType.AUTHORIZATION_TYPE_DELEGATE)).dataList; + // revoke deletgate authorization + await firma.Authz.revokeStakeAuthorization(aliceWallet, grantee_bobAddress, AuthorizationType.AUTHORIZATION_TYPE_DELEGATE); + /*console.log(result[0].authorization['@type']); console.log(result[0].authorization.allow_list.address[0]); console.log(result[0].authorization.authorization_type); @@ -65,10 +99,26 @@ describe('[24. Authz query Test]', () => { it('Authz getStakingGrantData - redelegate', async () => { - const granter = await (await firma.Wallet.fromMnemonic(aliceMnemonic)).getAddress(); - const grantee = await (await firma.Wallet.fromMnemonic(bobMnemonic)).getAddress(); + const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); + const granter_aliceAddress = await aliceWallet.getAddress(); + const grantee_bobAddress = await bobWallet.getAddress(); + + const expirationDate = new Date(); + expirationDate.setFullYear(expirationDate.getFullYear() + 1); + const maxFCT = 10; + + const delegationInfo = (await firma.Staking.getTotalDelegationInfo(await aliceWallet.getAddress())).dataList; + const validatorSrcAddress = delegationInfo[0].delegation.validator_address; + const validatorDstAddress = delegationInfo[1].delegation.validator_address; + + // grant redelegate authorization + await firma.Authz.grantStakeAuthorization(aliceWallet, grantee_bobAddress, [validatorSrcAddress, validatorDstAddress], AuthorizationType.AUTHORIZATION_TYPE_REDELEGATE, expirationDate, maxFCT); + // get grant data (redelegate) + const result = (await firma.Authz.getStakingGrantData(granter_aliceAddress, grantee_bobAddress, AuthorizationType.AUTHORIZATION_TYPE_REDELEGATE)).dataList; + // revoke redelegate authorization + await firma.Authz.revokeStakeAuthorization(aliceWallet, grantee_bobAddress, AuthorizationType.AUTHORIZATION_TYPE_REDELEGATE); - let result = (await firma.Authz.getStakingGrantData(granter, grantee, AuthorizationType.AUTHORIZATION_TYPE_REDELEGATE)).dataList; /*console.log(result[0].authorization['@type']); console.log(result[0].authorization.allow_list.address[0]); @@ -83,10 +133,24 @@ describe('[24. Authz query Test]', () => { it('Authz getStakingGrantData - undelegate', async () => { - const granter = await (await firma.Wallet.fromMnemonic(aliceMnemonic)).getAddress(); - const grantee = await (await firma.Wallet.fromMnemonic(bobMnemonic)).getAddress(); + const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); + const granter_aliceAddress = await aliceWallet.getAddress(); + const grantee_bobAddress = await bobWallet.getAddress(); + + const expirationDate = new Date(); + expirationDate.setFullYear(expirationDate.getFullYear() + 1); + const maxFCT = 10; + + const delegationInfo = (await firma.Staking.getTotalDelegationInfo(await aliceWallet.getAddress())).dataList; + const validatorAddress = delegationInfo[0].delegation.validator_address; - let result = (await firma.Authz.getStakingGrantData(granter, grantee, AuthorizationType.AUTHORIZATION_TYPE_UNDELEGATE)).dataList; + // grant undelegate authorization + await firma.Authz.grantStakeAuthorization(aliceWallet, grantee_bobAddress, [validatorAddress], AuthorizationType.AUTHORIZATION_TYPE_UNDELEGATE, expirationDate, maxFCT); + // get grant data (undelegate) + const result = (await firma.Authz.getStakingGrantData(granter_aliceAddress, grantee_bobAddress, AuthorizationType.AUTHORIZATION_TYPE_UNDELEGATE)).dataList; + // revoke undelegate authorization + await firma.Authz.revokeStakeAuthorization(aliceWallet, grantee_bobAddress, AuthorizationType.AUTHORIZATION_TYPE_UNDELEGATE); /*console.log(result[0].authorization['@type']); console.log(result[0].authorization.allow_list.address[0]); diff --git a/test/25.cosmwasm_tx.test.ts b/test/25.cosmwasm_tx.test.ts index 21f9251..1c8c40c 100644 --- a/test/25.cosmwasm_tx.test.ts +++ b/test/25.cosmwasm_tx.test.ts @@ -108,7 +108,7 @@ describe('[25. CosmWasm Tx Test]', () => { expect(result.code).to.be.equal(0); }); - it.skip('CosmWasm MigrateContract', async () => { + it('CosmWasm MigrateContract', async () => { let aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const gas = 3000000; @@ -116,9 +116,12 @@ describe('[25. CosmWasm Tx Test]', () => { const testData = JSON.stringify({ "purchase_price": { "amount": "1000", "denom": "ufct" }, "transfer_price": { "amount": "9990", "denom": "ufct" } }); - var result = await firma.CosmWasm.migrateContract(aliceWallet, contractAddress, codeId, testData, { gas: gas, fee: fee }); - - //console.log(result); - expect(result.code).to.be.equal(0); + try { + const result = await firma.CosmWasm.migrateContract(aliceWallet, contractAddress, codeId, testData, { gas: gas, fee: fee }); + //console.log(result); + expect(result.code).to.be.equal(0); + } catch (error) { + console.log(error); + } }); }); \ No newline at end of file diff --git a/test/26.cosmwasm_query.test.ts b/test/26.cosmwasm_query.test.ts index f21f707..b0f0b09 100644 --- a/test/26.cosmwasm_query.test.ts +++ b/test/26.cosmwasm_query.test.ts @@ -1,5 +1,12 @@ +import { expect } from "chai"; +import fs from "fs"; +import { Coin } from "@cosmjs/proto-signing"; + import { FirmaSDK } from "../sdk/FirmaSDK" -import { TestChainConfig } from './config_test'; +import { FirmaUtil } from "../sdk/FirmaUtil"; +import { AccessConfig, AccessType } from "../sdk/FirmaCosmWasmService"; + +import { aliceMnemonic, TestChainConfig } from './config_test'; describe('[26. cosmwasm query Test]', () => { @@ -9,8 +16,44 @@ describe('[26. cosmwasm query Test]', () => { firma = new FirmaSDK(TestChainConfig); }) + let codeId = ""; let contractAddress = ""; + it('Cosmwasm Storecode & Instantiate', async () => { + let aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + + if (codeId === "") { + const wasmFile = fs.readFileSync("./test/sample/cw_nameservice.wasm"); + const array = new Uint8Array(wasmFile.buffer); + + const storeCodeGas = 3000000; + const storeCodeFee = FirmaUtil.getUFCTFromFCT(0.3); + + const everyBodyAccessConfig: AccessConfig = { permission: AccessType.ACCESS_TYPE_EVERYBODY, address: "" }; + + const storeCodeResult = await firma.CosmWasm.storeCode(aliceWallet, array, everyBodyAccessConfig, { gas: storeCodeGas, fee: storeCodeFee }); + const storeCodeData = JSON.parse(storeCodeResult.rawLog!); + + codeId = storeCodeData[0]["events"][1]["attributes"][1]["value"]; + expect(storeCodeResult.code).to.be.equal(0); + } + + const admin = await aliceWallet.getAddress(); + const label = "test1"; + + const instantiateGas = 3000000; + const instantiateFee = FirmaUtil.getUFCTFromFCT(0.3); + const funds: Coin[] = []; + + const testData = JSON.stringify({ "purchase_price": { "amount": "100", "denom": "ufct" }, "transfer_price": { "amount": "999", "denom": "ufct" } }); + const instantiateResult = await firma.CosmWasm.instantiateContract(aliceWallet, admin, codeId, label, testData, funds, { gas: instantiateGas, fee: instantiateFee }); + const instantiateData = JSON.parse(instantiateResult.rawLog!); + + contractAddress = instantiateData[0]["events"][0]["attributes"][0]["value"]; + + expect(instantiateResult.code).to.be.equal(0); + }); + it('CosmWasm getCodeList', async () => { // Codes gets the metadata for all stored wasm codes @@ -23,8 +66,6 @@ describe('[26. cosmwasm query Test]', () => { it('CosmWasm getCodeData', async () => { - const codeId = "1"; - // Code gets the binary code and metadata for a singe wasm code let result = await firma.CosmWasm.getCodeData(codeId); @@ -38,8 +79,6 @@ describe('[26. cosmwasm query Test]', () => { it('CosmWasm getCodeData', async () => { - const codeId = "1"; - let result = await firma.CosmWasm.getCodeData(codeId); //console.log(result.code_info.code_id); @@ -51,11 +90,8 @@ describe('[26. cosmwasm query Test]', () => { }); it('CosmWasm getContractListFromCodeId', async () => { - - const codeId = "132"; - - // add pagination + // add pagination let result = await firma.CosmWasm.getContractListFromCodeId(codeId); //console.log(result); diff --git a/test/28.ibc_tx.test.ts b/test/28.ibc_tx.test.ts index 604fc6d..d6160a8 100644 --- a/test/28.ibc_tx.test.ts +++ b/test/28.ibc_tx.test.ts @@ -13,14 +13,14 @@ describe('[28. IBC Tx Test]', () => { firma = new FirmaSDK(TestChainConfig); }) - it.skip('IBC transfer', async () => { + it('IBC transfer', async () => { let aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const sourcePort = "transfer"; const sourceChannel = "channel-3"; const denom = "ufct"; const amount = "1000000"; - const receiver = "firma1320eclh4dwzx89qjap2q5n2hna07zs2vm8tzlu"; + const receiver = "firma122a8grlv86q8tfrtpcq9ehrhmnvmtnlh4da0q2"; // new by dh // https://explorer-testnet.firmachain.dev/transactions/B4245412560A108F7C987DF08C05A748278706E045B14961CE61E86786659592 @@ -33,21 +33,17 @@ describe('[28. IBC Tx Test]', () => { let clientState = await firma.Ibc.getClientState(sourceChannel, sourcePort); //console.log(clientState); - let revison_height = clientState.identified_client_state.client_state.latest_height.revision_height; - let revison_number = clientState.identified_client_state.client_state.latest_height.revision_number; - // ways : 1 // Set target revision height : get revision height and add 1000 block (cli tx policy) // issues: because of ibc version difference, can't get revision info directly. - const height: Height = Height.fromPartial({ - revisionHeight: Long.fromString(revison_height, true).add(Long.fromNumber(1000)), - revisionNumber: Long.fromString(revison_number, true), - }); + const height = { + revisionHeight: Long.fromString(clientState.identified_client_state.client_state.latest_height.revision_height, true).add(Long.fromNumber(1000)), + revisionNumber: Long.fromString(clientState.identified_client_state.client_state.latest_height.revision_number, true), + } // ways : 2 // for nano second(000000) and add 10min(600000) for timeout - var timeStamp = (Date.now() + 600000).toString() + "000000"; - + const timeStamp = (Date.now() + 600000).toString() + "000000"; const timeoutTimeStamp = Long.fromString(timeStamp, true); //console.log(timeStamp); diff --git a/test/30.cw20_tx.test.ts b/test/30.cw20_tx.test.ts index 8dc95b5..0ab6b68 100644 --- a/test/30.cw20_tx.test.ts +++ b/test/30.cw20_tx.test.ts @@ -1,14 +1,15 @@ -import { FirmaConfig } from "../sdk/FirmaConfig"; import { FirmaSDK } from "../sdk/FirmaSDK" import { expect } from 'chai'; -import { aliceMnemonic, bobMnemonic } from './config_test'; +import fs from "fs"; + +import { AccessConfig, AccessType } from "../sdk/FirmaCosmWasmService"; import { FirmaWalletService } from "../sdk/FirmaWalletService"; import { Expires } from "../sdk/FirmaCosmWasmCw20"; import { FirmaUtil } from "../sdk/FirmaUtil"; -import fs from "fs"; -import { AccessConfig, AccessType } from "../sdk/FirmaCosmWasmService"; +import { aliceMnemonic, bobMnemonic, TestChainConfig } from './config_test'; + describe('[30. cw20 tx Test]', () => { @@ -19,8 +20,11 @@ describe('[30. cw20 tx Test]', () => { let aliceAddress: string; let bobAddress: string; + let contractAddress = ""; + let codeId = ""; + beforeEach(async function () { - firma = new FirmaSDK(FirmaConfig.TestNetConfig); + firma = new FirmaSDK(TestChainConfig); aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); @@ -29,10 +33,7 @@ describe('[30. cw20 tx Test]', () => { bobAddress = await bobWallet.getAddress(); }) - let contractAddress = ""; - let codeId = ""; - - it.skip('CosmWasm Cw20 StoreCode', async () => { + it('CosmWasm Cw20 StoreCode', async () => { const wasmFile = fs.readFileSync("./test/sample/cw20_base.wasm"); const array = new Uint8Array(wasmFile.buffer); @@ -50,8 +51,7 @@ describe('[30. cw20 tx Test]', () => { expect(result.code).to.be.equal(0); }); - - it.skip('CosmWasm Cw20 InstantiateContract', async () => { + it('CosmWasm Cw20 InstantiateContract', async () => { const admin = await aliceWallet.getAddress(); const label = "test1"; @@ -66,13 +66,17 @@ describe('[30. cw20 tx Test]', () => { initial_balances: [ { address: aliceAddress, - amount: "1000000" + amount: "500000000000" + }, + { + address: bobAddress, + amount: "500000000000" } ], // mint is optional mint: { minter: aliceAddress, - cap: "10000000" + cap: "10000000000000" }, // marketing is optional marketing: { @@ -85,42 +89,42 @@ describe('[30. cw20 tx Test]', () => { } }); - var result = await firma.CosmWasm.instantiateContract(aliceWallet, admin, codeId, label, testData, noFunds, { gas: gas, fee: fee }); - var data = JSON.parse(result.rawLog!); + const result = await firma.CosmWasm.instantiateContract(aliceWallet, admin, codeId, label, testData, noFunds, { gas: gas, fee: fee }); + const data = JSON.parse(result.rawLog!); contractAddress = data[0]["events"][0]["attributes"][0]["value"]; expect(result.code).to.be.equal(0); }); - - it.skip('Cw20 transfer', async () => { - - const aliceBalance = await firma.Cw20.getBalance(contractAddress, aliceAddress); - const bobBalance = await firma.Cw20.getBalance(contractAddress, bobAddress); - - console.log("alice: " + aliceAddress + ", balance: " + aliceBalance); - console.log("bob: " + bobAddress + ", balance: " + bobBalance); - - const amount = "10000"; - const gas = await firma.Cw20.getGasEstimationTransfer(aliceWallet, contractAddress, bobAddress, amount); - const fee = Math.ceil(gas * 0.1); - - var result = await firma.Cw20.transfer(aliceWallet, contractAddress, bobAddress, amount, { gas, fee }) - - expect(result.code).to.be.equal(0); + it('Cw20 transfer', async () => { + + try{ + const aliceBalance = await firma.Cw20.getBalance(contractAddress, aliceAddress); + const bobBalance = await firma.Cw20.getBalance(contractAddress, bobAddress); + + const amount = "10000"; + const gas = await firma.Cw20.getGasEstimationTransfer(aliceWallet, contractAddress, bobAddress, amount); + const fee = Math.ceil(gas * 0.1); + + var result = await firma.Cw20.transfer(aliceWallet, contractAddress, bobAddress, amount, { gas, fee }) + + expect(result.code).to.be.equal(0); + } catch (error) { + console.log(error); + } }); - it.skip('Cw20 transfer_form', async () => { + it('Cw20 transfer_from', async () => { let aliceBalance = await firma.Cw20.getBalance(contractAddress, aliceAddress); let bobBalance = await firma.Cw20.getBalance(contractAddress, bobAddress); - console.log("alice: " + aliceAddress + ", balance: " + aliceBalance); - console.log("bob: " + bobAddress + ", balance: " + bobBalance); - const allowance = await firma.Cw20.getAllowance(contractAddress, aliceAddress, bobAddress); - console.log(allowance); + + if (allowance.allowance === '0') { + await firma.Cw20.increaseAllowance(aliceWallet, contractAddress, bobAddress, "10000000", { never: {} }); + } const amount = "1000"; const gas = await firma.Cw20.getGasEstimationTransferFrom(bobWallet, contractAddress, aliceAddress, bobAddress, amount); @@ -131,23 +135,10 @@ describe('[30. cw20 tx Test]', () => { aliceBalance = await firma.Cw20.getBalance(contractAddress, aliceAddress); bobBalance = await firma.Cw20.getBalance(contractAddress, bobAddress); - console.log("alice: " + aliceAddress + ", balance: " + aliceBalance); - console.log("bob: " + bobAddress + ", balance: " + bobBalance); - - const allowance1 = await firma.Cw20.getAllowance(contractAddress, aliceAddress, bobAddress); - console.log(allowance1); - expect(result.code).to.be.equal(0); }); - - it.skip('Cw20 mint', async () => { - - const aliceBalance = await firma.Cw20.getBalance(contractAddress, aliceAddress); - const bobBalance = await firma.Cw20.getBalance(contractAddress, bobAddress); - - console.log("alice: " + aliceAddress + ", balance: " + aliceBalance); - console.log("bob: " + bobAddress + ", balance: " + bobBalance); + it('Cw20 mint', async () => { const minter = await firma.Cw20.getMinter(contractAddress); @@ -156,79 +147,42 @@ describe('[30. cw20 tx Test]', () => { return; } - console.log(minter.minter); - console.log(minter.cap); - - const info = await firma.Cw20.getTokenInfo(contractAddress) - console.log(info); - const amount = "1000"; const gas = await firma.Cw20.getGasEstimationMint(aliceWallet, contractAddress, aliceAddress, amount); const fee = Math.ceil(gas * 0.1); - var result = await firma.Cw20.mint(aliceWallet, contractAddress, aliceAddress, amount, { gas, fee }) + var result = await firma.Cw20.mint(aliceWallet, contractAddress, aliceAddress, amount, { gas, fee }); - console.log(result); expect(result.code).to.be.equal(0); }); - it.skip('Cw20 burn', async () => { - - const info = await firma.Cw20.getTokenInfo(contractAddress) - console.log(info); - - const aliceBalance = await firma.Cw20.getBalance(contractAddress, aliceAddress); - const bobBalance = await firma.Cw20.getBalance(contractAddress, bobAddress); - - console.log("alice: " + aliceAddress + ", balance: " + aliceBalance); - console.log("bob: " + bobAddress + ", balance: " + bobBalance); + it('Cw20 burn', async () => { const amount = "1000"; const gas = await firma.Cw20.getGasEstimationBurn(aliceWallet, contractAddress, amount); const fee = Math.ceil(gas * 0.1); - var result = await firma.Cw20.burn(aliceWallet, contractAddress, amount, { gas, fee }) - console.log(result); + var result = await firma.Cw20.burn(aliceWallet, contractAddress, amount, { gas, fee }); expect(result.code).to.be.equal(0); }); - it.skip('Cw20 burn_from', async () => { - - const info = await firma.Cw20.getTokenInfo(contractAddress) - console.log(info); - - const aliceBalance = await firma.Cw20.getBalance(contractAddress, aliceAddress); - const bobBalance = await firma.Cw20.getBalance(contractAddress, bobAddress); - - console.log("alice: " + aliceAddress + ", balance: " + aliceBalance); - console.log("bob: " + bobAddress + ", balance: " + bobBalance); + it('Cw20 burn_from', async () => { const amount = "1000"; const gas = await firma.Cw20.getGasEstimationBurnFrom(bobWallet, contractAddress, aliceAddress, amount); const fee = Math.ceil(gas * 0.1); - var result = await firma.Cw20.burnFrom(bobWallet, contractAddress, aliceAddress, amount, { gas, fee }) - console.log(result); + var result = await firma.Cw20.burnFrom(bobWallet, contractAddress, aliceAddress, amount, { gas, fee }); expect(result.code).to.be.equal(0); }); - - it.skip('Cw20 increase_allowance', async () => { - - const aliceBalance = await firma.Cw20.getBalance(contractAddress, aliceAddress); - const bobBalance = await firma.Cw20.getBalance(contractAddress, bobAddress); - - console.log("alice: " + aliceAddress + ", balance: " + aliceBalance); - console.log("bob: " + bobAddress + ", balance: " + bobBalance); - - const olDAllowance = await firma.Cw20.getAllowance(contractAddress, aliceAddress, bobAddress); - console.log(olDAllowance); + it('Cw20 increase_allowance', async () => { const amount = "1000"; @@ -241,26 +195,12 @@ describe('[30. cw20 tx Test]', () => { const gas = await firma.Cw20.getGasEstimationIncreaseAllowance(aliceWallet, contractAddress, bobAddress, amount, expires); const fee = Math.ceil(gas * 0.1); - var result = await firma.Cw20.increaseAllowance(aliceWallet, contractAddress, bobAddress, amount, expires, { gas, fee }) - console.log(result); - - const newAllowance = await firma.Cw20.getAllowance(contractAddress, aliceAddress, bobAddress); - console.log(newAllowance); + var result = await firma.Cw20.increaseAllowance(aliceWallet, contractAddress, bobAddress, amount, expires, { gas, fee }); //expect(Number.parseInt(olDAllowance) + Number.parseInt(amount)).to.be.equal(Number.parseInt(newAllowance)); - }); - it.skip('Cw20 decrease_allowance', async () => { - - const aliceBalance = await firma.Cw20.getBalance(contractAddress, aliceAddress); - const bobBalance = await firma.Cw20.getBalance(contractAddress, bobAddress); - - console.log("alice: " + aliceAddress + ", balance: " + aliceBalance); - console.log("bob: " + bobAddress + ", balance: " + bobBalance); - - const olDAllowance = await firma.Cw20.getAllowance(contractAddress, aliceAddress, bobAddress); - console.log(olDAllowance); + it('Cw20 decrease_allowance', async () => { const amount = "1000"; @@ -273,38 +213,20 @@ describe('[30. cw20 tx Test]', () => { const gas = await firma.Cw20.getGasEstimationDecreaseAllowance(aliceWallet, contractAddress, bobAddress, amount, expires); const fee = Math.ceil(gas * 0.1); - var result = await firma.Cw20.decreaseAllowance(aliceWallet, contractAddress, bobAddress, amount, expires, { gas, fee }) - console.log(result); - - const newAllowance = await firma.Cw20.getAllowance(contractAddress, aliceAddress, bobAddress); - console.log(newAllowance); + var result = await firma.Cw20.decreaseAllowance(aliceWallet, contractAddress, bobAddress, amount, expires, { gas, fee }); //expect(Number.parseInt(olDAllowance) + Number.parseInt(amount)).to.be.equal(Number.parseInt(newAllowance)); - }); - it.skip('Cw20 update_minter', async () => { - - let minter = await firma.Cw20.getMinter(contractAddress) - console.log(minter); + it('Cw20 update_minter', async () => { const gas = await firma.Cw20.getGasEstimationUpdateMinter(aliceWallet, contractAddress, bobAddress); const fee = Math.ceil(gas * 0.1); - var result = await firma.Cw20.updateMinter(aliceWallet, contractAddress, bobAddress, { gas, fee }) - console.log(result); - - minter = await firma.Cw20.getMinter(contractAddress) - console.log(minter); + var result = await firma.Cw20.updateMinter(aliceWallet, contractAddress, bobAddress, { gas, fee }); }); - it.skip('Cw20 update_marketing', async () => { - - const info = await firma.Cw20.getTokenInfo(contractAddress) - console.log(info); - - let marketingInfo = await firma.Cw20.getMarketingInfo(contractAddress); - console.log(marketingInfo); + it('Cw20 update_marketing', async () => { const description = "description"; const marketingAddress = aliceAddress; @@ -313,69 +235,41 @@ describe('[30. cw20 tx Test]', () => { const gas = await firma.Cw20.getGasEstimationUpdateMarketing(aliceWallet, contractAddress, description, marketingAddress, project); const fee = Math.ceil(gas * 0.1); - var result = await firma.Cw20.updateMarketing(aliceWallet, contractAddress, description, marketingAddress, project, { gas, fee }) - console.log(result); - - marketingInfo = await firma.Cw20.getMarketingInfo(contractAddress) - console.log(marketingInfo); + var result = await firma.Cw20.updateMarketing(aliceWallet, contractAddress, description, marketingAddress, project, { gas, fee }); }); - it.skip('Cw20 update_logo', async () => { - - const minter = await firma.Cw20.getMinter(contractAddress); - console.log(minter); - console.log(aliceAddress); - - const info = await firma.Cw20.getTokenInfo(contractAddress) - console.log(info); - - let marketingInfo = await firma.Cw20.getMarketingInfo(contractAddress); - console.log(marketingInfo); + it('Cw20 update_logo', async () => { const url = "https://firmachain.org"; const gas = await firma.Cw20.getGasEstimationUploadLogo(aliceWallet, contractAddress, url); const fee = Math.ceil(gas * 0.1); - var result = await firma.Cw20.uploadLogo(aliceWallet, contractAddress, url, { gas, fee }) - console.log(result); - - marketingInfo = await firma.Cw20.getMarketingInfo(contractAddress) - console.log(marketingInfo); + var result = await firma.Cw20.uploadLogo(aliceWallet, contractAddress, url, { gas, fee }); }); - - it.skip('Cw20 send', async () => { + it('Cw20 send', async () => { // Basic message that sends a token to a contract and triggers an action on the receiving contract. // amount, contract, msg // msg should be a base64 encoded json string. (have to check how to trigger it). - - const aliceBalance = await firma.Cw20.getBalance(contractAddress, aliceAddress); - console.log("alice: " + aliceAddress + ", balance: " + aliceBalance); - - const amount = "10000"; + const amount = "500"; const targetContractAddress = contractAddress; - const msg = { action: "send", data: "example" }; + const msg = { action: "stake", amount: amount }; const gas = await firma.Cw20.getGasEstimationSend(aliceWallet, contractAddress, targetContractAddress, amount, msg); const fee = Math.ceil(gas * 0.1); - var result = await firma.Cw20.send(aliceWallet, contractAddress, targetContractAddress, amount, msg, { gas, fee }) - console.log(result); + var result = await firma.Cw20.send(aliceWallet, contractAddress, targetContractAddress, amount, msg, { gas, fee }); //expect(result.code).to.be.equal(0); }); - it.skip('Cw20 send_from', async () => { + it('Cw20 send_from', async () => { // Basic message that sends a token to a contract and triggers an action on the receiving contract. // amount, contract, msg // msg should be a base64 encoded json string. (have to check how to trigger it). - - const aliceBalance = await firma.Cw20.getBalance(contractAddress, aliceAddress); - console.log("alice: " + aliceAddress + ", balance: " + aliceBalance); - const amount = "100"; const targetContractAddress = contractAddress; @@ -384,8 +278,7 @@ describe('[30. cw20 tx Test]', () => { const gas = await firma.Cw20.getGasEstimationSendFrom(bobWallet, contractAddress, targetContractAddress, owner, amount, msg); const fee = Math.ceil(gas * 0.1); - var result = await firma.Cw20.sendFrom(bobWallet, contractAddress, targetContractAddress, owner, amount, msg, { gas, fee }) - console.log(result); + var result = await firma.Cw20.sendFrom(bobWallet, contractAddress, targetContractAddress, owner, amount, msg, { gas, fee }); //expect(result.code).to.be.equal(0); }); diff --git a/test/31.cw20_query.test.ts b/test/31.cw20_query.test.ts index e1d7f57..e99a068 100644 --- a/test/31.cw20_query.test.ts +++ b/test/31.cw20_query.test.ts @@ -1,21 +1,97 @@ -import { FirmaConfig } from "../sdk/FirmaConfig"; +import fs from "fs"; + import { FirmaSDK } from "../sdk/FirmaSDK" -import { TestChainConfig } from './config_test'; +import { FirmaUtil } from "../sdk/FirmaUtil"; +import { FirmaWalletService } from "../sdk/FirmaWalletService"; +import { aliceMnemonic, bobMnemonic, TestChainConfig } from './config_test'; +import { AccessConfig, AccessType } from "../sdk/FirmaCosmWasmService"; describe('[31. cw20 query Test]', () => { let firma: FirmaSDK; + let aliceWallet: FirmaWalletService; + let bobWallet: FirmaWalletService; + let aliceAddress: string; + let bobAddress: string; + + let contractAddress: string = ""; + let codeId = ""; + + beforeEach(async function() { + firma = new FirmaSDK(TestChainConfig); - beforeEach(function() { - firma = new FirmaSDK(FirmaConfig.TestNetConfig); + aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); + + aliceAddress = await aliceWallet.getAddress(); + bobAddress = await bobWallet.getAddress(); }) - const contractAddress = "firma1fxkx7wd4q7z5zgm8qh0vaxptx4gp0ppgjm0ke56jr55azpzecpcsuexqg8"; + it('Cw20 Storecode & Instantiate', async () => { + if (codeId === "") { + const wasmFile = fs.readFileSync("./test/sample/cw20_base.wasm"); + const array = new Uint8Array(wasmFile.buffer); + + const storeCodeGas = 3000000; + const storeCodeFee = FirmaUtil.getUFCTFromFCT(0.3); + + const everyBodyAccessConfig: AccessConfig = { permission: AccessType.ACCESS_TYPE_EVERYBODY, address: "" }; + //const onlyAddressAccessConfig: AccessConfig = { permission: AccessType.ACCESS_TYPE_ONLY_ADDRESS, address: aliceAddress }; + + const storeCodeResult = await firma.CosmWasm.storeCode(aliceWallet, array, everyBodyAccessConfig, { gas: storeCodeGas, fee: storeCodeFee }); + const storeCodedata = JSON.parse(storeCodeResult.rawLog!); + + codeId = storeCodedata[0]["events"][1]["attributes"][1]["value"]; + } + + if (contractAddress === "") { + const admin = await aliceWallet.getAddress(); + const label = "test1"; + + const instantiateGas = 3000000; + const instantiateFee = FirmaUtil.getUFCTFromFCT(0.3); + const noFunds: any = []; + + const testData = JSON.stringify({ + decimals: 6, + name: "MyToken", + symbol: "MTK", + initial_balances: [ + { + address: aliceAddress, + amount: "500000000000" + }, + { + address: bobAddress, + amount: "500000000000" + } + ], + // mint is optional + mint: { + minter: aliceAddress, + cap: "10000000000000" + }, + // marketing is optional + marketing: { + description: "MyToken's description is like this.", + logo: { + "url": "https://example.com/mytoken-logo.png" + }, + marketing: aliceAddress, + project: "https://mytokenproject.com" + } + }); + + const instantiateResult = await firma.CosmWasm.instantiateContract(aliceWallet, admin, codeId, label, testData, noFunds, { gas: instantiateGas, fee: instantiateFee }); + const instantiateData = JSON.parse(instantiateResult.rawLog!); + + contractAddress = instantiateData[0]["events"][0]["attributes"][0]["value"]; + } + }); it('Cw20 getBalance', async () => { - const address = "firma13hcgnwfpe99htsr92v2keqsgx909rhkwfnxgwr"; - const balance = await firma.Cw20.getBalance(contractAddress, address); + const balance = await firma.Cw20.getBalance(contractAddress, aliceAddress); console.log(balance); }); @@ -42,7 +118,6 @@ describe('[31. cw20 query Test]', () => { it('Cw20 getMinter', async () => { - const address = "firma1d84pmnumnsh80v74lta0vnpd476ncp4pjnuklr"; const minter = await firma.Cw20.getMinter(contractAddress); console.log(minter); @@ -50,45 +125,43 @@ describe('[31. cw20 query Test]', () => { it('Cw20 getAllowance', async () => { - const owner = "firma13hcgnwfpe99htsr92v2keqsgx909rhkwfnxgwr"; - const spender = "firma1d84pmnumnsh80v74lta0vnpd476ncp4pjnuklr"; - - const info = await firma.Cw20.getAllowance(contractAddress, owner, spender); - console.log(info); + const info = await firma.Cw20.getAllowance(contractAddress, aliceAddress, bobAddress); }); - it.skip('Cw20 getAllAllowances', async () => { + it('Cw20 getAllAllowances', async () => { - const owner = "firma13hcgnwfpe99htsr92v2keqsgx909rhkwfnxgwr"; - const info = await firma.Cw20.getAllAllowances(contractAddress, owner); + const info = await firma.Cw20.getAllAllowances(contractAddress, aliceAddress); //console.log(info); }); - it.skip('Cw20 getAllSpenderAllowances', async () => { + it('Cw20 getAllSpenderAllowances', async () => { - const spender = "firma13hcgnwfpe99htsr92v2keqsgx909rhkwfnxgwr"; - const info = await firma.Cw20.getAllSpenderAllowances(contractAddress, spender); + const info = await firma.Cw20.getAllSpenderAllowances(contractAddress, bobAddress); //console.log(info); }); - it.skip('Cw20 getAllAccounts', async () => { + it('Cw20 getAllAccounts', async () => { const info = await firma.Cw20.getAllAccounts(contractAddress); //console.log(info); }); - it.skip('Cw20 getMarketingInfo', async () => { + it('Cw20 getMarketingInfo', async () => { const info = await firma.Cw20.getMarketingInfo(contractAddress); - //console.log(info); + console.log(info); }); it.skip('Cw20 getDownloadLogo', async () => { // INFO: Errors if no logo data is stored for this contract. - const info = await firma.Cw20.getDownloadLogo(contractAddress); + try { + const info = await firma.Cw20.getDownloadLogo(contractAddress); + } catch (error) { + console.log(error); + } //console.log(info); }); diff --git a/test/32.cw721_tx.test.ts b/test/32.cw721_tx.test.ts index c613ca2..26ec115 100644 --- a/test/32.cw721_tx.test.ts +++ b/test/32.cw721_tx.test.ts @@ -2,14 +2,13 @@ import { FirmaConfig } from "../sdk/FirmaConfig"; import { FirmaSDK } from "../sdk/FirmaSDK" import { expect } from 'chai'; -import { aliceMnemonic, bobMnemonic } from './config_test'; +import { aliceMnemonic, bobMnemonic, TestChainConfig } from './config_test'; import { FirmaWalletService } from "../sdk/FirmaWalletService"; import { Expires } from "../sdk/FirmaCosmWasmCw20"; import { FirmaUtil } from "../sdk/FirmaUtil"; import fs from "fs"; import { AccessConfig, AccessType } from "../sdk/FirmaCosmWasmService"; -import { Cw721Expires } from "../sdk/FirmaCosmWasmCw721"; describe('[32. cw721 tx Test]', () => { @@ -20,8 +19,11 @@ describe('[32. cw721 tx Test]', () => { let aliceAddress: string; let bobAddress: string; + let contractAddress = ""; + let codeId = ""; + beforeEach(async function () { - firma = new FirmaSDK(FirmaConfig.TestNetConfig); + firma = new FirmaSDK(TestChainConfig); aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); @@ -30,10 +32,7 @@ describe('[32. cw721 tx Test]', () => { bobAddress = await bobWallet.getAddress(); }) - let contractAddress = ""; - let codeId = ""; - - it.skip('CosmWasm Cw721 StoreCode', async () => { + it('CosmWasm Cw721 StoreCode', async () => { const wasmFile = fs.readFileSync("./test/sample/cw721_base.wasm"); const array = new Uint8Array(wasmFile.buffer); @@ -51,8 +50,7 @@ describe('[32. cw721 tx Test]', () => { expect(result.code).to.be.equal(0); }); - - it.skip('CosmWasm Cw721 InstantiateContract', async () => { + it('CosmWasm Cw721 InstantiateContract', async () => { const admin = await aliceWallet.getAddress(); const label = "test1"; @@ -74,11 +72,10 @@ describe('[32. cw721 tx Test]', () => { expect(result.code).to.be.equal(0); }); - - it.skip('Cw721 mint', async () => { + it('Cw721 mint', async () => { const owner = aliceAddress; - const token_id = "5"; + const token_id = "1"; const token_uri = "https://meta.nft.io/uri/" + token_id; const gas = await firma.Cw721.getGasEstimationMint(aliceWallet, contractAddress, owner, token_id, token_uri); @@ -86,30 +83,36 @@ describe('[32. cw721 tx Test]', () => { var result = await firma.Cw721.mint(aliceWallet, contractAddress, owner, token_id, token_uri, { gas: gas, fee: fee }); - const data = await firma.Cw721.getNftData(contractAddress, token_id); - console.log(data); - expect(result.code).to.be.equal(0); }); - it.skip('Cw721 burn', async () => { + it('Cw721 burn', async () => { const owner = aliceAddress; - const token_id = "2"; - - //const data1 = await firma.Cw721.getAllNftIdList(contractAddress); - //console.log(data1); - - const gas = await firma.Cw721.getGasEstimationBurn(aliceWallet, contractAddress, token_id); - const fee = Math.ceil(gas * 0.1); + const token_id = "10"; + const token_uri = "https://meta.nft.io/uri/" + token_id; - var result = await firma.Cw721.burn(aliceWallet, contractAddress, token_id, { gas: gas, fee: fee }); - expect(result.code).to.be.equal(0); + try { + // Check if the TOKEN ID has been minted + await firma.Cw721.getNftData(contractAddress, token_id); + console.log(`The TOKEN ID(${token_id}) has been minted.`); + } catch (error) { + // If the Token ID has not been minted. + const mintGas = await firma.Cw721.getGasEstimationMint(aliceWallet, contractAddress, owner, token_id, token_uri); + const mintFee = Math.ceil(mintGas * 0.1); + + await firma.Cw721.mint(aliceWallet, contractAddress, owner, token_id, token_uri, { gas: mintGas, fee: mintFee }); + } finally { + const gas = await firma.Cw721.getGasEstimationBurn(aliceWallet, contractAddress, token_id); + const fee = Math.ceil(gas * 0.1); + + var result = await firma.Cw721.burn(aliceWallet, contractAddress, token_id, { gas: gas, fee: fee }); + expect(result.code).to.be.equal(0); + } }); - it.skip('Cw721 transfer', async () => { + it('Cw721 transfer - alice -> bob', async () => { - const owner = aliceAddress; const token_id = "1"; const gas = await firma.Cw721.getGasEstimationTransfer(aliceWallet, contractAddress, bobAddress, token_id); @@ -117,14 +120,10 @@ describe('[32. cw721 tx Test]', () => { var result = await firma.Cw721.transfer(aliceWallet, contractAddress, bobAddress, token_id, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - - const data = await firma.Cw721.getNftData(contractAddress, token_id); - console.log(data); }); - it.skip('Cw721 transfer', async () => { + it('Cw721 transfer - bob -> alice', async () => { - const owner = aliceAddress; const token_id = "1"; const gas = await firma.Cw721.getGasEstimationTransfer(bobWallet, contractAddress, aliceAddress, token_id); @@ -132,14 +131,10 @@ describe('[32. cw721 tx Test]', () => { var result = await firma.Cw721.transfer(bobWallet, contractAddress, aliceAddress, token_id, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - - const data = await firma.Cw721.getNftData(contractAddress, token_id); - console.log(data); }); - it.skip('Cw721 approve', async () => { + it('Cw721 approve', async () => { - const owner = aliceAddress; const token_id = "1"; //const expires: Expires = { at_height: 7216240 }; @@ -153,12 +148,9 @@ describe('[32. cw721 tx Test]', () => { var result = await firma.Cw721.approve(aliceWallet, contractAddress, bobAddress, token_id, expires, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - - const data = await firma.Cw721.getNftData(contractAddress, token_id); - console.log(data); }); - it.skip('Cw721 revoke', async () => { + it('Cw721 revoke', async () => { const token_id = "1"; @@ -167,12 +159,9 @@ describe('[32. cw721 tx Test]', () => { var result = await firma.Cw721.revoke(aliceWallet, contractAddress, bobAddress, token_id, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - - const data = await firma.Cw721.getNftData(contractAddress, token_id); - console.log(data.access); }); - it.skip('Cw721 approve_all', async () => { + it('Cw721 approve_all', async () => { //const expires: Expires = { at_height: 7216240 }; //const expires: Cw721Expires = { at_time: "1852937600000000000" }; // unix timestamp nano seconds @@ -185,26 +174,18 @@ describe('[32. cw721 tx Test]', () => { var result = await firma.Cw721.approveAll(aliceWallet, contractAddress, bobAddress, expires, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - - console.log(aliceAddress) - - const data = await firma.Cw721.getAllOperators(contractAddress, aliceAddress); - console.log(data); }); - it.skip('Cw721 revoke_all', async () => { + it('Cw721 revoke_all', async () => { const gas = await firma.Cw721.getGasEstimationRevokeAll(aliceWallet, contractAddress, bobAddress); const fee = Math.ceil(gas * 0.1); var result = await firma.Cw721.revokeAll(aliceWallet, contractAddress, bobAddress, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - - const data = await firma.Cw721.getAllOperators(contractAddress, aliceAddress); - console.log(data); }); - it.skip('Cw721 transfer ownership', async () => { + it('Cw721 transfer ownership', async () => { const new_owner = bobAddress; @@ -217,36 +198,51 @@ describe('[32. cw721 tx Test]', () => { const gas = await firma.Cw721.getGasEstimationUpdateOwnerShipTransfer(aliceWallet, contractAddress, new_owner, expires); const fee = Math.ceil(gas * 0.1); - var result = await firma.Cw721.updateOwnerShipTransfer(aliceWallet, contractAddress, new_owner, expires,{ gas: gas, fee: fee }); + var result = await firma.Cw721.updateOwnerShipTransfer(aliceWallet, contractAddress, new_owner, expires, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - - const data = await firma.Cw721.getOwnerShip(contractAddress); - console.log(data); }); - it.skip ('Cw721 accept ownership', async () => { + it('Cw721 accept ownership', async () => { const gas = await firma.Cw721.getGasEstimationUpdateOwnerShipAccept(bobWallet, contractAddress); const fee = Math.ceil(gas * 0.1); var result = await firma.Cw721.updateOwnerShipAccept(bobWallet, contractAddress, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - - const data = await firma.Cw721.getOwnerShip(contractAddress); - console.log(data); }); - // give up all ownership. - it.skip('Cw721 renounce ownership', async () => { - - const gas = await firma.Cw721.getGasEstimationUpdateOwnerShipRenounce(aliceWallet, contractAddress); - const fee = Math.ceil(gas * 0.1); + it('Cw721 alice mint, approve, bob transfer', async () => { - var result = await firma.Cw721.updateOwnerShipRenounce(aliceWallet, contractAddress, { gas: gas, fee: fee }); - expect(result.code).to.be.equal(0); + const owner = aliceAddress; + const token_id = "4"; + const token_uri = "https://meta.nft.io/uri/" + token_id; - const data = await firma.Cw721.getOwnerShip(contractAddress); - console.log(data); + try { + let gas = await firma.Cw721.getGasEstimationMint(aliceWallet, contractAddress, owner, token_id, token_uri); + let fee = Math.ceil(gas * 0.1); + + var result = await firma.Cw721.mint(aliceWallet, contractAddress, owner, token_id, token_uri, { gas: gas, fee: fee }); + expect(result.code).to.be.equal(0); + + const expires: Expires = { never: {} }; + + gas = await firma.Cw721.getGasEstimationApproveAll(aliceWallet, contractAddress, bobAddress, expires); + fee = Math.ceil(gas * 0.1); + + result = await firma.Cw721.approveAll(aliceWallet, contractAddress, bobAddress, expires, { gas: gas, fee: fee }); + expect(result.code).to.be.equal(0); + + gas = await firma.Cw721.getGasEstimationTransfer(bobWallet, contractAddress, bobAddress, token_id); + fee = Math.ceil(gas * 0.1); + + var result = await firma.Cw721.transfer(bobWallet, contractAddress, bobAddress, token_id, { gas: gas, fee: fee }); + expect(result.code).to.be.equal(0); + + const data = await firma.Cw721.getNftData(contractAddress, token_id); + console.log(data); + } catch (error) { + console.log(error); + } }); // TODO: check this test case. @@ -260,38 +256,15 @@ describe('[32. cw721 tx Test]', () => { var result = await firma.Cw721.sendNft(aliceWallet, contractAddress, targetContractAddress, bobAddress, token_id, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - - const data = await firma.Cw721.getNftData(contractAddress, token_id); - console.log(data); }); - it.skip('Cw721 alice mint, approve, bob transfer', async () => { - - const owner = aliceAddress; - const token_id = "4"; - const token_uri = "https://meta.nft.io/uri/" + token_id; - - let gas = await firma.Cw721.getGasEstimationMint(aliceWallet, contractAddress, owner, token_id, token_uri); - let fee = Math.ceil(gas * 0.1); - - var result = await firma.Cw721.mint(aliceWallet, contractAddress, owner, token_id, token_uri, { gas: gas, fee: fee }); - expect(result.code).to.be.equal(0); - - const expires: Expires = { never: {} }; - - gas = await firma.Cw721.getGasEstimationApproveAll(aliceWallet, contractAddress, bobAddress, expires); - fee = Math.ceil(gas * 0.1); - - result = await firma.Cw721.approveAll(aliceWallet, contractAddress, bobAddress, expires, { gas: gas, fee: fee }); - expect(result.code).to.be.equal(0); - - gas = await firma.Cw721.getGasEstimationTransfer(bobWallet, contractAddress, bobAddress, token_id); - fee = Math.ceil(gas * 0.1); + // give up all ownership. + it('Cw721 renounce ownership', async () => { + + const gas = await firma.Cw721.getGasEstimationUpdateOwnerShipRenounce(bobWallet, contractAddress); + const fee = Math.ceil(gas * 0.1); - var result = await firma.Cw721.transfer(bobWallet, contractAddress, bobAddress, token_id, { gas: gas, fee: fee }); + var result = await firma.Cw721.updateOwnerShipRenounce(bobWallet, contractAddress, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - - const data = await firma.Cw721.getNftData(contractAddress, token_id); - console.log(data); }); }); \ No newline at end of file diff --git a/test/33.cw721_query.test.ts b/test/33.cw721_query.test.ts index 7b81685..60b3de0 100644 --- a/test/33.cw721_query.test.ts +++ b/test/33.cw721_query.test.ts @@ -1,22 +1,82 @@ -import { FirmaConfig } from "../sdk/FirmaConfig"; +import { Expires } from "../sdk/FirmaCosmWasmCw20"; +import { AccessConfig, AccessType } from "../sdk/FirmaCosmWasmService"; import { FirmaSDK } from "../sdk/FirmaSDK" -import { TestChainConfig } from './config_test'; +import { FirmaUtil } from "../sdk/FirmaUtil"; +import { FirmaWalletService } from "../sdk/FirmaWalletService"; +import { aliceMnemonic, bobMnemonic, TestChainConfig } from './config_test'; +import fs from "fs"; describe('[33. cw721 query Test]', () => { let firma: FirmaSDK; - beforeEach(function() { - firma = new FirmaSDK(FirmaConfig.TestNetConfig); + let aliceWallet: FirmaWalletService; + let aliceAddress: string; + let bobWallet: FirmaWalletService; + let bobAddress: string; + + let contractAddress = ""; + let codeId = ""; + + const tokenId = "1"; + const tokenUri = "https://meta.nft.io/uri"; + + beforeEach(async function() { + firma = new FirmaSDK(TestChainConfig); + + aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); + aliceAddress = await aliceWallet.getAddress(); + bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); + bobAddress = await bobWallet.getAddress(); }) - let contractAddress = "firma17uh2wj875vt64x7pzzy08slsl5pqupfln0vw2k79knfshygy6ausxth5d2"; + it('cw721 init contract address', async () => { + + if (codeId === "") { + const wasmFile = fs.readFileSync("./test/sample/cw721_base.wasm"); + const array = new Uint8Array(wasmFile.buffer); + + const instantiateGas = 3000000; + const instantiateFee = FirmaUtil.getUFCTFromFCT(0.3); + + const everyBodyAccessConfig: AccessConfig = { permission: AccessType.ACCESS_TYPE_EVERYBODY, address: "" }; + //const onlyAddressAccessConfig: AccessConfig = { permission: AccessType.ACCESS_TYPE_ONLY_ADDRESS, address: aliceAddress }; + + const result = await firma.CosmWasm.storeCode(aliceWallet, array, everyBodyAccessConfig, { gas: instantiateGas, fee: instantiateFee }); + const data = JSON.parse(result.rawLog!); + + codeId = data[0]["events"][1]["attributes"][1]["value"]; + } + + if (contractAddress === "") { + const admin = await aliceWallet.getAddress(); + const label = "test1"; + + const gas = 3000000; + const fee = FirmaUtil.getUFCTFromFCT(0.3); + const noFunds: any = []; + + const testData = JSON.stringify({ + minter: aliceAddress, + name: "My Awesome NFT Collection", + symbol: "MAWESOME" + }); + + const result = await firma.CosmWasm.instantiateContract(aliceWallet, admin, codeId, label, testData, noFunds, { gas: gas, fee: fee }); + const data = JSON.parse(result.rawLog!); + + contractAddress = data[0]["events"][0]["attributes"][0]["value"]; + } + }); it('cw721 getOwnerFromNftID', async () => { - const tokenId = "1"; + const gas = await firma.Cw721.getGasEstimationMint(aliceWallet, contractAddress, aliceAddress, tokenId, `${tokenUri}/${tokenId}`); + const fee = Math.ceil(gas * 0.1); + + await firma.Cw721.mint(aliceWallet, contractAddress, aliceAddress, tokenId, `${tokenUri}/${tokenId}`, { gas, fee }); + const owner = await firma.Cw721.getOwnerFromNftID(contractAddress, tokenId); - console.log(owner); }); it('cw721 approval', async () => { @@ -25,27 +85,19 @@ describe('[33. cw721 query Test]', () => { // If spender is different, api call occurs error. // So, I have to decide wrap error case on internal functions. + const expires: Expires = { never: {} }; + await firma.Cw721.approve(aliceWallet, contractAddress, bobAddress, tokenId, expires); + // A point in time in nanosecond precision - - // firma1lkly7qj4w2la2xxlatrtw6wynz8vxkctjlqkch - // firma13hcgnwfpe99htsr92v2keqsgx909rhkwfnxgwr - - const tokenId = "1"; - const spender = "firma1lkly7qj4w2la2xxlatrtw6wynz8vxkctjlqkch"; const isIncludeExpired = true; - const approval = await firma.Cw721.getApproval(contractAddress, tokenId, spender, isIncludeExpired); + const approval = await firma.Cw721.getApproval(contractAddress, tokenId, bobAddress, isIncludeExpired); //console.log(approval.spender); //console.log(approval.expires); - - const expires = approval.expires; - console.log(expires); - }); it('cw721 approvals', async () => { - const tokenId = "1"; const isIncludeExpired = true; const approvals = await firma.Cw721.getApprovals(contractAddress, tokenId, isIncludeExpired); @@ -55,18 +107,16 @@ describe('[33. cw721 query Test]', () => { const approval = approvals[i]; const expires = approval.expires; - console.log(approval.spender); - console.log(expires); - + // console.log(approval.spender); + // console.log(expires); } }); it('cw721 getAllOperators', async () => { // operator : approve all user info - const owner = "firma13hcgnwfpe99htsr92v2keqsgx909rhkwfnxgwr"; const isIncludeExpired = false; - const operators = await firma.Cw721.getAllOperators(contractAddress, owner, isIncludeExpired); + const operators = await firma.Cw721.getAllOperators(contractAddress, aliceAddress, isIncludeExpired); //console.log(operators); }); @@ -87,7 +137,6 @@ describe('[33. cw721 query Test]', () => { it('cw721 getNftTokenUri', async () => { - const tokenId = "1"; const nftInfo = await firma.Cw721.getNftTokenUri(contractAddress, tokenId); //console.log(nftInfo); @@ -95,7 +144,6 @@ describe('[33. cw721 query Test]', () => { it('cw721 getNftData', async () => { - const tokenId = "1"; const nftInfo = await firma.Cw721.getNftData(contractAddress, tokenId); //console.log(nftInfo.access.owner); @@ -106,10 +154,8 @@ describe('[33. cw721 query Test]', () => { it('cw721 getNFTIdListOfOwner', async () => { - const owner = "firma13hcgnwfpe99htsr92v2keqsgx909rhkwfnxgwr"; - - const nftIdList = await firma.Cw721.getNFTIdListOfOwner(contractAddress, owner); - console.log(nftIdList); + const nftIdList = await firma.Cw721.getNFTIdListOfOwner(contractAddress, aliceAddress); + // console.log(nftIdList); }); it('cw721 getAllNftIdList', async () => { diff --git a/test/34.cw_bridge_tx.test.ts b/test/34.cw_bridge_tx.test.ts index 5fd473a..50b6ba3 100644 --- a/test/34.cw_bridge_tx.test.ts +++ b/test/34.cw_bridge_tx.test.ts @@ -2,9 +2,13 @@ import { FirmaConfig } from "../sdk/FirmaConfig"; import { FirmaSDK } from "../sdk/FirmaSDK" import { expect } from 'chai'; -import { aliceMnemonic, bobMnemonic } from './config_test'; +import { aliceMnemonic, bobMnemonic, TestChainConfig } from './config_test'; import { FirmaWalletService } from "../sdk/FirmaWalletService"; +import fs from "fs"; +import { FirmaUtil } from "../sdk/FirmaUtil"; +import { AccessConfig, AccessType } from "../sdk/FirmaCosmWasmService"; + describe('[34. Bridge tx Test]', () => { let firma: FirmaSDK; @@ -14,8 +18,14 @@ describe('[34. Bridge tx Test]', () => { let aliceAddress: string; let bobAddress: string; + let cw721ContractAddress = ""; + let bridgeContractAddress = ""; + + const tokenId: string = "21"; + const tokenIds: string[] = ["30", "40"]; + beforeEach(async function () { - firma = new FirmaSDK(FirmaConfig.TestNetConfig); + firma = new FirmaSDK(TestChainConfig); aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); @@ -24,27 +34,78 @@ describe('[34. Bridge tx Test]', () => { bobAddress = await bobWallet.getAddress(); }) - let cw721ContractAddress = "firma1mp3dl27wwhdkhkyed5d4ypaq7h5dewazqkqhny98sxcy2cpu23ls369adt"; - let bridgeContractAddress = "firma1zj39neajvynzv4swf3a33394z84l6nfduy5sntw58re3z7ef9p4qk8lwk4" - let codeId = ""; - - it.skip('Cw bridge mint temp', async () => { - - // [ 15,16,17,18,19,20 ] - - const owner = aliceAddress; - const new_token_id = "21"; - const new_token_uri = "https://meta.nft.io/uri/" + new_token_id; - - let gas = await firma.Cw721.getGasEstimationMint(aliceWallet, cw721ContractAddress, owner, new_token_id, new_token_uri); - let fee = Math.ceil(gas * 0.1); - - var result = await firma.Cw721.mint(aliceWallet, cw721ContractAddress, owner, new_token_id, new_token_uri, { gas: gas, fee: fee }); - expect(result.code).to.be.equal(0); + it('Cw721 instantiate && mint Nft ID', async () => { + + if (cw721ContractAddress === "") { + const wasmFile = fs.readFileSync("./test/sample/cw721_base.wasm"); + const array = new Uint8Array(wasmFile.buffer); + + const storeCodeGas = 3000000; + const storeCodeFee = FirmaUtil.getUFCTFromFCT(0.3); + + const everyBodyAccessConfig: AccessConfig = { permission: AccessType.ACCESS_TYPE_EVERYBODY, address: "" }; + const storeCodeResult = await firma.CosmWasm.storeCode(aliceWallet, array, everyBodyAccessConfig, { gas: storeCodeGas, fee: storeCodeFee }); + const storeCodeData = JSON.parse(storeCodeResult.rawLog!); + + const codeId = storeCodeData[0]["events"][1]["attributes"][1]["value"]; + + const admin = await aliceWallet.getAddress(); + const label = "test1"; + + const instantiateGas = 3000000; + const instantiateFee = FirmaUtil.getUFCTFromFCT(0.3); + const noFunds: any = []; + + const testData = JSON.stringify({ + minter: aliceAddress, + name: "My Awesome NFT Collection", + symbol: "MAWESOME" + }); + + const instantiateResult = await firma.CosmWasm.instantiateContract(aliceWallet, admin, codeId, label, testData, noFunds, { gas: instantiateGas, fee: instantiateFee }); + const instantiateData = JSON.parse(instantiateResult.rawLog!); + + cw721ContractAddress = instantiateData[0]["events"][0]["attributes"][0]["value"]; + + expect(instantiateResult.code).to.be.equal(0); + } + }); - }), + it('Cw bridge instantiate', async () => { + + if (bridgeContractAddress === "") { + const wasmFile = fs.readFileSync("./test/sample/bridge_contract.wasm"); + const array = new Uint8Array(wasmFile.buffer); + + const gas = 3000000; + const fee = FirmaUtil.getUFCTFromFCT(0.3); + + const everyBodyAccessConfig: AccessConfig = { permission: AccessType.ACCESS_TYPE_EVERYBODY, address: "" }; + const storeCodeResult = await firma.CosmWasm.storeCode(aliceWallet, array, everyBodyAccessConfig, { gas: gas, fee: fee }); + const storeCodeData = JSON.parse(storeCodeResult.rawLog!); + + const codeId = storeCodeData[0]["events"][1]["attributes"][1]["value"]; + + const admin = await aliceWallet.getAddress(); + const label = "test1"; + + const noFunds: any = []; + + const testData = JSON.stringify({ + owner: admin, + cw721_address: cw721ContractAddress + }); + + const instantiateResult = await firma.CosmWasm.instantiateContract(aliceWallet, admin, codeId, label, testData, noFunds, { gas: gas, fee: fee }); + const instantiateData = JSON.parse(instantiateResult.rawLog!); + + bridgeContractAddress = instantiateData[0]["events"][0]["attributes"][0]["value"]; + + expect(instantiateResult.code).to.be.equal(0); + } + }); - it.skip('Cw bridge chage_owner', async () => { + it('Cw bridge chage_owner', async () => { const new_owner = bobAddress; @@ -56,162 +117,214 @@ describe('[34. Bridge tx Test]', () => { const data = await firma.CwBridge.getOwner(bridgeContractAddress); console.log(data); - }), + }); - it.skip('Cw bridge add_authorized_user', async () => { + it('Cw bridge add_authorized_user', async () => { const user = aliceAddress; const gas = await firma.CwBridge.getGasEstimationAddAuthorizedUser(bobWallet, bridgeContractAddress, user); const fee = Math.ceil(gas * 0.1); - var result = await firma.CwBridge.addAuthorizedUser(bobWallet, bridgeContractAddress, user, { gas: gas, fee: fee }); + const result = await firma.CwBridge.addAuthorizedUser(bobWallet, bridgeContractAddress, user, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); const data = await firma.CwBridge.getAuthorizedUsers(bridgeContractAddress); console.log(data); - }), - - it.skip('Cw bridge remove_authorized_user', async () => { - - const user = aliceAddress; - - const gas = await firma.CwBridge.getGasEstimationRemoveAuthorizedUser(aliceWallet, bridgeContractAddress, user); - const fee = Math.ceil(gas * 0.1); + }); - var result = await firma.CwBridge.removeAuthorizedUser(aliceWallet, bridgeContractAddress, user, { gas: gas, fee: fee }); - expect(result.code).to.be.equal(0); + it('Cw bridge lock', async () => { - const data = await firma.CwBridge.getAuthorizedUsers(bridgeContractAddress); - console.log(data); - }), + // minting nft id + const owner = aliceAddress; + const new_token_uri = "https://meta.nft.io/uri/" + tokenId; - it.skip('Cw bridge lock', async () => { + const mintGas = await firma.Cw721.getGasEstimationMint(aliceWallet, cw721ContractAddress, owner, tokenId, new_token_uri); + const mintFee = Math.ceil(mintGas * 0.1); - const token_id = "14"; + await firma.Cw721.mint(aliceWallet, cw721ContractAddress, owner, tokenId, new_token_uri, { gas: mintGas, fee: mintFee }); - const gas = await firma.CwBridge.getGasEstimationLock(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id); + const gas = await firma.CwBridge.getGasEstimationLock(aliceWallet, bridgeContractAddress, cw721ContractAddress, tokenId); const fee = Math.ceil(gas * 0.1); - var result = await firma.CwBridge.lock(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id, { gas: gas, fee: fee }); + const result = await firma.CwBridge.lock(aliceWallet, bridgeContractAddress, cw721ContractAddress, tokenId, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - - const data = await firma.Cw721.getNftData(cw721ContractAddress, token_id); - console.log(data); }); - it.skip('cw bridge lock bulk', async () => { + it('cw bridge lock bulk', async () => { - const token_id1 = "15"; - const token_id2 = "16"; - - const tx1 = await firma.CwBridge.getUnsignedTxLock(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id1); - const tx2 = await firma.CwBridge.getUnsignedTxLock(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id2); - - const gas = await firma.CwBridge.getGasEstimationSignAndBroadcast(aliceWallet, [tx1, tx2]); - const fee = Math.ceil(gas * 0.1); - - var result = await firma.CwBridge.signAndBroadcast(aliceWallet, [tx1, tx2], { gas: gas, fee: fee }); - expect(result.code).to.be.equal(0); - - const data = await firma.Cw721.getNFTIdListOfOwner(cw721ContractAddress, aliceAddress); - console.log(data); + const owner = aliceAddress; + const tokenUri = "https://meta.nft.io/uri"; + + try { + const allNftList = await firma.Cw721.getAllNftIdList(cw721ContractAddress); + + // bulk minting + const bulkMintList = []; + for (let i = 0; i < tokenIds.length; i++) { + if (allNftList.includes(tokenIds[i])) continue; + + const unsignedMsg = await firma.Cw721.getUnsignedTxMint(aliceWallet, cw721ContractAddress, owner, tokenIds[i], `${tokenUri}/${tokenIds[i]}`); + bulkMintList.push(unsignedMsg); + } + + if (bulkMintList.length > 0) { + const bulkMintGas = await firma.Cw721.getGasEstimationSignAndBroadcast(aliceWallet, bulkMintList); + const bulkMintFee = Math.ceil(bulkMintGas * 0.1); + + await firma.Cw721.signAndBroadcast(aliceWallet, bulkMintList, {gas: bulkMintGas, fee: bulkMintFee }); + } + + // lock bulk + const bulkLockList = []; + + for (let i = 0; i < tokenIds.length; i++) { + const cw721NftData = await firma.Cw721.getNftData(cw721ContractAddress, tokenIds[i]); + if (cw721NftData.access.owner === bridgeContractAddress) continue; + + const unsignedMsg = await firma.CwBridge.getUnsignedTxLock(aliceWallet, bridgeContractAddress, cw721ContractAddress, tokenIds[i]); + bulkLockList.push(unsignedMsg); + } + + if (bulkLockList.length > 0) { + const bulkLockGas = await firma.CwBridge.getGasEstimationSignAndBroadcast(aliceWallet, bulkLockList); + const bulkLockFee = Math.ceil(bulkLockGas * 0.1); + + const result = await firma.CwBridge.signAndBroadcast(aliceWallet, bulkLockList, { gas: bulkLockGas, fee: bulkLockFee }); + expect(result.code).to.be.equal(0); + } else { + console.log("The lock bulk test has not been conducted."); + } + } catch (error) { + expect(1).to.be.equal(0); + + console.log(error); + } }); - it.skip('Cw bridge unlock', async () => { - - const token_id = "14"; + it('Cw bridge unlock', async () => { - const gas = await firma.CwBridge.getGasEstimationUnlock(aliceWallet, bridgeContractAddress, token_id); + const gas = await firma.CwBridge.getGasEstimationUnlock(aliceWallet, bridgeContractAddress, tokenId); const fee = Math.ceil(gas * 0.1); - var result = await firma.CwBridge.unlock(aliceWallet, bridgeContractAddress, token_id, { gas: gas, fee: fee }); + var result = await firma.CwBridge.unlock(aliceWallet, bridgeContractAddress, tokenId, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - const data = await firma.Cw721.getNftData(cw721ContractAddress, token_id); + const data = await firma.Cw721.getNftData(cw721ContractAddress, tokenId); console.log(data); }); - it.skip('Cw bridge unlock bulk', async () => { - - const token_id1 = "2"; - const token_id2 = "3"; - - const tx1 = await firma.CwBridge.getUnsignedTxUnlock(bobWallet, bridgeContractAddress, token_id1); - const tx2 = await firma.CwBridge.getUnsignedTxUnlock(bobWallet, bridgeContractAddress, token_id2); - - const gas = await firma.CwBridge.getGasEstimationSignAndBroadcast(bobWallet, [tx1, tx2]); - const fee = Math.ceil(gas * 0.1); - - var result = await firma.CwBridge.signAndBroadcast(bobWallet, [tx1, tx2], { gas: gas, fee: fee }); - expect(result.code).to.be.equal(0); - - const data = await firma.Cw721.getNFTIdListOfOwner(cw721ContractAddress, bobAddress); - console.log(data); + it('Cw bridge unlock bulk', async () => { + + try { + const bulkUnlockList = []; + + for (let i = 0; i < tokenIds.length; i++) { + const cw721NftData = await firma.Cw721.getNftData(cw721ContractAddress, tokenIds[i]); + if (cw721NftData.access.owner !== bridgeContractAddress) continue; + + const unsignedMsg = await firma.CwBridge.getUnsignedTxUnlock(aliceWallet, bridgeContractAddress, tokenIds[i]); + bulkUnlockList.push(unsignedMsg); + } + + if (bulkUnlockList.length > 0) { + const bulkUnlockGas = await firma.CwBridge.getGasEstimationSignAndBroadcast(aliceWallet, bulkUnlockList); + const bulkUnlockFee = Math.ceil(bulkUnlockGas * 0.1); + + const result = await firma.CwBridge.signAndBroadcast(aliceWallet, bulkUnlockList, { gas: bulkUnlockGas, fee: bulkUnlockFee }); + expect(result.code).to.be.equal(0); + } else { + console.log("The unlock bulk test has not been conducted."); + } + } catch (error) { + expect(1).to.be.equal(0); + + console.log(error); + } }); - it.skip('Cw bridge deposit', async () => { + it('Cw bridge deposit', async () => { - const token_id = "21"; + const nftData = await firma.Cw721.getNftData(cw721ContractAddress, tokenId); + console.log(nftData); - const gas = await firma.CwBridge.getGasEstimationDeposit(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id, bobAddress); + const gas = await firma.CwBridge.getGasEstimationDeposit(aliceWallet, bridgeContractAddress, cw721ContractAddress, tokenId, bobAddress); const fee = Math.ceil(gas * 0.1); - var result = await firma.CwBridge.deposit(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id, bobAddress, { gas: gas, fee: fee }); + const result = await firma.CwBridge.deposit(aliceWallet, bridgeContractAddress, cw721ContractAddress, tokenId, bobAddress, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - - const data = await firma.Cw721.getNftData(cw721ContractAddress, token_id); - console.log(data); - }); - it.skip('Cw bridge deposit bulk', async () => { - - const token_id1 = "17"; - const token_id2 = "18"; + it('Cw bridge deposit bulk', async () => { + + try { + const bulkDepositList = []; + + for (let i = 0; i < tokenIds.length; i++) { + const nftData = await firma.Cw721.getNftData(cw721ContractAddress, tokenIds[i]); + if (nftData.access.owner !== aliceAddress) continue; + + const unsignedMsg = await firma.CwBridge.getUnsignedTxDeposit(aliceWallet, bridgeContractAddress, cw721ContractAddress, tokenIds[i], bobAddress); + bulkDepositList.push(unsignedMsg); + } + + if (bulkDepositList.length > 0) { + const bulkUnlockGas = await firma.CwBridge.getGasEstimationSignAndBroadcast(aliceWallet, bulkDepositList); + const bulkUnlockFee = Math.ceil(bulkUnlockGas * 0.1); + + const result = await firma.CwBridge.signAndBroadcast(aliceWallet, bulkDepositList, { gas: bulkUnlockGas, fee: bulkUnlockFee }); + expect(result.code).to.be.equal(0); + } else { + console.log("The deposit bulk test has not been conducted."); + } + } catch (error) { + expect(1).to.be.equal(0); + + console.log(error); + } + }); - const tx1 = await firma.CwBridge.getUnsignedTxDeposit(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id1, bobAddress); - const tx2 = await firma.CwBridge.getUnsignedTxDeposit(aliceWallet, bridgeContractAddress, cw721ContractAddress, token_id2, bobAddress); + it('Cw bridge withdraw', async () => { - const gas = await firma.CwBridge.getGasEstimationSignAndBroadcast(aliceWallet, [tx1, tx2]); + const gas = await firma.CwBridge.getGasEstimationWithdraw(bobWallet, bridgeContractAddress, tokenId); const fee = Math.ceil(gas * 0.1); - var result = await firma.CwBridge.signAndBroadcast(aliceWallet, [tx1, tx2], { gas: gas, fee: fee }); + var result = await firma.CwBridge.withdraw(bobWallet, bridgeContractAddress, tokenId, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - const data = await firma.Cw721.getNFTIdListOfOwner(cw721ContractAddress, bobAddress); + const data = await firma.Cw721.getNftData(cw721ContractAddress, tokenId); console.log(data); }); - it.skip('Cw bridge withdraw', async () => { + it('Cw bridge withdraw bulk', async () => { - const token_id = "14"; + const bulkWithdrawLisk = []; - const gas = await firma.CwBridge.getGasEstimationWithdraw(bobWallet, bridgeContractAddress, token_id); - const fee = Math.ceil(gas * 0.1); + for (let i = 0; i < tokenIds.length; i++) { + const unsignedMsg = await firma.CwBridge.getUnsignedTxWithdraw(bobWallet, bridgeContractAddress, tokenIds[i]); + bulkWithdrawLisk.push(unsignedMsg); + } - var result = await firma.CwBridge.withdraw(bobWallet, bridgeContractAddress, token_id, { gas: gas, fee: fee }); - expect(result.code).to.be.equal(0); - - const data = await firma.Cw721.getNftData(cw721ContractAddress, token_id); - console.log(data); + if (bulkWithdrawLisk.length > 0) { + const gas = await firma.CwBridge.getGasEstimationSignAndBroadcast(bobWallet, bulkWithdrawLisk); + const fee = Math.ceil(gas * 0.1); + + const result = await firma.CwBridge.signAndBroadcast(bobWallet, bulkWithdrawLisk, { gas: gas, fee: fee }); + expect(result.code).to.be.equal(0); + } }); - it.skip('Cw bridge withdraw bulk', async () => { + it('Cw bridge remove_authorized_user', async () => { - const token_id1 = "8"; - const token_id2 = "9"; - - const tx1 = await firma.CwBridge.getUnsignedTxWithdraw(bobWallet, bridgeContractAddress, token_id1); - const tx2 = await firma.CwBridge.getUnsignedTxWithdraw(bobWallet, bridgeContractAddress, token_id2); + const user = aliceAddress; - const gas = await firma.CwBridge.getGasEstimationSignAndBroadcast(bobWallet, [tx1, tx2]); + const gas = await firma.CwBridge.getGasEstimationRemoveAuthorizedUser(bobWallet, bridgeContractAddress, user); const fee = Math.ceil(gas * 0.1); - var result = await firma.CwBridge.signAndBroadcast(bobWallet, [tx1, tx2], { gas: gas, fee: fee }); + var result = await firma.CwBridge.removeAuthorizedUser(bobWallet, bridgeContractAddress, user, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - const data = await firma.Cw721.getNFTIdListOfOwner(cw721ContractAddress, bobAddress); + const data = await firma.CwBridge.getAuthorizedUsers(bridgeContractAddress); console.log(data); }); }); \ No newline at end of file diff --git a/test/35.cw_bridge_tx_low.test.ts b/test/35.cw_bridge_tx_low.test.ts index 1e21946..36d59fe 100644 --- a/test/35.cw_bridge_tx_low.test.ts +++ b/test/35.cw_bridge_tx_low.test.ts @@ -1,9 +1,12 @@ -import { FirmaConfig } from "../sdk/FirmaConfig"; import { FirmaSDK } from "../sdk/FirmaSDK" import { expect } from 'chai'; -import { aliceMnemonic, bobMnemonic } from './config_test'; +import fs from "fs"; + +import { aliceMnemonic, bobMnemonic, TestChainConfig } from './config_test'; import { FirmaWalletService } from "../sdk/FirmaWalletService"; +import { FirmaUtil } from "../sdk/FirmaUtil"; +import { AccessConfig, AccessType } from "../sdk/FirmaCosmWasmService"; describe('[35. Bridge tx low Test]', () => { @@ -14,8 +17,16 @@ describe('[35. Bridge tx low Test]', () => { let aliceAddress: string; let bobAddress: string; + let cw721ContractAddress = ""; + let bridgeContractAddress = ""; + + const tokenId: string = "21"; + const tokenIds: string[] = ["30", "40"]; + + const tokenUri = "https://meta.nft.io/uri"; + beforeEach(async function () { - firma = new FirmaSDK(FirmaConfig.TestNetConfig); + firma = new FirmaSDK(TestChainConfig); aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); @@ -24,65 +35,156 @@ describe('[35. Bridge tx low Test]', () => { bobAddress = await bobWallet.getAddress(); }) - let cw721ContractAddress = "firma1mp3dl27wwhdkhkyed5d4ypaq7h5dewazqkqhny98sxcy2cpu23ls369adt"; - let bridgeContractAddress = "firma1zj39neajvynzv4swf3a33394z84l6nfduy5sntw58re3z7ef9p4qk8lwk4" - let codeId = ""; + // init for low level test + //---------------------------------------------------------------------- + + it('Cw721 instantiate', async () => { + + if (cw721ContractAddress !== "") { + console.log('already cw721ContractAddress'); + return ; + } + + const wasmFile = fs.readFileSync("./test/sample/cw721_base.wasm"); + const array = new Uint8Array(wasmFile.buffer); + + const storeCodeGas = 3000000; + const storeCodeFee = FirmaUtil.getUFCTFromFCT(0.3); + + const everyBodyAccessConfig: AccessConfig = { permission: AccessType.ACCESS_TYPE_EVERYBODY, address: "" }; + const storeCodeResult = await firma.CosmWasm.storeCode(aliceWallet, array, everyBodyAccessConfig, { gas: storeCodeGas, fee: storeCodeFee }); + const storeCodeData = JSON.parse(storeCodeResult.rawLog!); + + const codeId = storeCodeData[0]["events"][1]["attributes"][1]["value"]; + + const admin = await aliceWallet.getAddress(); + const label = "test1"; + + const instantiateGas = 3000000; + const instantiateFee = FirmaUtil.getUFCTFromFCT(0.3); + const noFunds: any = []; + + const testData = JSON.stringify({ + minter: aliceAddress, + name: "My Awesome NFT Collection", + symbol: "MAWESOME" + }); + + const instantiateResult = await firma.CosmWasm.instantiateContract(aliceWallet, admin, codeId, label, testData, noFunds, { gas: instantiateGas, fee: instantiateFee }); + const instantiateData = JSON.parse(instantiateResult.rawLog!); + + cw721ContractAddress = instantiateData[0]["events"][0]["attributes"][0]["value"]; + + expect(instantiateResult.code).to.be.equal(0); + }); + + it('Cw bridge instantiate', async () => { + + if (bridgeContractAddress !== "") { + console.log('already bridgeContractAddress'); + return ; + } + + const wasmFile = fs.readFileSync("./test/sample/bridge_contract.wasm"); + const array = new Uint8Array(wasmFile.buffer); + + const gas = 3000000; + const fee = FirmaUtil.getUFCTFromFCT(0.3); + + const everyBodyAccessConfig: AccessConfig = { permission: AccessType.ACCESS_TYPE_EVERYBODY, address: "" }; + const storeCodeResult = await firma.CosmWasm.storeCode(aliceWallet, array, everyBodyAccessConfig, { gas: gas, fee: fee }); + const storeCodeData = JSON.parse(storeCodeResult.rawLog!); + + const codeId = storeCodeData[0]["events"][1]["attributes"][1]["value"]; + + const admin = await aliceWallet.getAddress(); + const label = "test1"; + + const noFunds: any = []; + + const testData = JSON.stringify({ + owner: admin, + cw721_address: cw721ContractAddress + }); + + const instantiateResult = await firma.CosmWasm.instantiateContract(aliceWallet, admin, codeId, label, testData, noFunds, { gas: gas, fee: fee }); + const instantiateData = JSON.parse(instantiateResult.rawLog!); + + bridgeContractAddress = instantiateData[0]["events"][0]["attributes"][0]["value"]; + + expect(instantiateResult.code).to.be.equal(0); + }); // low level test //---------------------------------------------------------------------- - it.skip('[low] Cw721 send_nft & lock', async () => { + it('[low] Cw721 mint token id', async () => { + + const allNftList = await firma.Cw721.getAllNftIdList(cw721ContractAddress); + + if (!allNftList.includes(tokenId)) { + await firma.Cw721.mint(aliceWallet, cw721ContractAddress, aliceAddress, tokenId, `${tokenUri}/${tokenId}`); + } else { + console.log(`[low] already mint token id ${tokenId}`); + } + }); + + it('[low] Cw721 send_nft & lock', async () => { - const token_id = "6"; const targetContractAddress = bridgeContractAddress; const msg = firma.CwBridge.getCwBridgeMsgData().getMsgDataLock(); - const gas = await firma.Cw721.getGasEstimationSendNft(aliceWallet, cw721ContractAddress, targetContractAddress, token_id, msg); + const gas = await firma.Cw721.getGasEstimationSendNft(aliceWallet, cw721ContractAddress, targetContractAddress, tokenId, msg); const fee = Math.ceil(gas * 0.1); - var result = await firma.Cw721.sendNft(aliceWallet, cw721ContractAddress, targetContractAddress, token_id, msg, { gas: gas, fee: fee }); + var result = await firma.Cw721.sendNft(aliceWallet, cw721ContractAddress, targetContractAddress, tokenId, msg, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - const data = await firma.Cw721.getNftData(cw721ContractAddress, token_id); + const data = await firma.Cw721.getNftData(cw721ContractAddress, tokenId); console.log(data); }); - it.skip('[low] cw bridge unlock', async () => { + it('[low] cw bridge unlock', async () => { - const token_id = "1"; const noFunds: any = []; const msgData = JSON.stringify({ "unlock": { - token_id, + token_id: tokenId, } }); - const gas = await firma.CosmWasm.getGasEstimationExecuteContract(aliceWallet, bridgeContractAddress, msgData, noFunds); + const gas = await firma.CosmWasm.getGasEstimationExecuteContract(aliceWallet, bridgeContractAddress, msgData, noFunds); const fee = Math.ceil(gas * 0.1); var result = await firma.CosmWasm.executeContract(aliceWallet, bridgeContractAddress, msgData, noFunds, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - const data = await firma.Cw721.getNftData(cw721ContractAddress, token_id); + const data = await firma.Cw721.getNftData(cw721ContractAddress, tokenId); console.log(data); }); - it.skip('[low] Cw721 send_nft & deposit', async () => { + it('[low] Cw721 send_nft & deposit', async () => { - const owner = aliceAddress; - const new_token_id = "6"; - const new_token_uri = "https://meta.nft.io/uri/" + new_token_id; - - let gas = await firma.Cw721.getGasEstimationMint(aliceWallet, cw721ContractAddress, owner, new_token_id, new_token_uri); - let fee = Math.ceil(gas * 0.1); + // add authorization + const gas = await firma.CwBridge.getGasEstimationAddAuthorizedUser(aliceWallet, bridgeContractAddress, aliceAddress); + const fee = Math.ceil(gas * 0.1); - var result = await firma.Cw721.mint(aliceWallet, cw721ContractAddress, owner, new_token_id, new_token_uri, { gas: gas, fee: fee }); - expect(result.code).to.be.equal(0); + const authorizationResult = await firma.CwBridge.addAuthorizedUser(aliceWallet, bridgeContractAddress, aliceAddress, { gas: gas, fee: fee }); + expect(authorizationResult.code).to.be.equal(0); - const data1 = await firma.Cw721.getNftData(cw721ContractAddress, new_token_id); - console.log(data1); + const owner = aliceAddress; + const new_token_uri = "https://meta.nft.io/uri/" + tokenId; + + const allNftList = await firma.Cw721.getAllNftIdList(cw721ContractAddress); + if (!allNftList.includes(tokenId)) { + const gas = await firma.Cw721.getGasEstimationMint(aliceWallet, cw721ContractAddress, owner, tokenId, new_token_uri); + const fee = Math.ceil(gas * 0.1); + + const result = await firma.Cw721.mint(aliceWallet, cw721ContractAddress, owner, tokenId, new_token_uri, { gas: gas, fee: fee }); + expect(result.code).to.be.equal(0); + } const targetContractAddress = bridgeContractAddress; @@ -91,74 +193,103 @@ describe('[35. Bridge tx low Test]', () => { target_addr: bobAddress } - let gas1 = await firma.Cw721.getGasEstimationSendNft(aliceWallet, cw721ContractAddress, targetContractAddress, new_token_id, msg); + let gas1 = await firma.Cw721.getGasEstimationSendNft(aliceWallet, cw721ContractAddress, targetContractAddress, tokenId, msg); let fee1 = Math.ceil(gas1 * 0.1); - var result = await firma.Cw721.sendNft(aliceWallet, cw721ContractAddress, targetContractAddress, new_token_id, msg, { gas: gas1, fee: fee1 }); + var result = await firma.Cw721.sendNft(aliceWallet, cw721ContractAddress, targetContractAddress, tokenId, msg, { gas: gas1, fee: fee1 }); expect(result.code).to.be.equal(0); - const data = await firma.Cw721.getNftData(cw721ContractAddress, new_token_id); + const data = await firma.Cw721.getNftData(cw721ContractAddress, tokenId); console.log(data); }); - it.skip('[low] cw bridge withdraw', async () => { + it('[low] cw bridge withdraw', async () => { - const token_id = "3"; const noFunds: any = []; const msgData = JSON.stringify({ "withdraw": { - token_id, + token_id: tokenId, } }); - const gas = await firma.CosmWasm.getGasEstimationExecuteContract(bobWallet, bridgeContractAddress, msgData, noFunds); + const gas = await firma.CosmWasm.getGasEstimationExecuteContract(bobWallet, bridgeContractAddress, msgData, noFunds); const fee = Math.ceil(gas * 0.1); - var result = await firma.CosmWasm.executeContract(bobWallet, bridgeContractAddress, msgData, noFunds, { gas: gas, fee: fee }); + const result = await firma.CosmWasm.executeContract(bobWallet, bridgeContractAddress, msgData, noFunds, { gas: gas, fee: fee }); expect(result.code).to.be.equal(0); - const data = await firma.Cw721.getNftData(cw721ContractAddress, token_id); + const data = await firma.Cw721.getNftData(cw721ContractAddress, tokenId); console.log(data); }); - it.skip('[low] cw721 send_nft & bridge lock bulk', async () => { + it('[low] cw721 send_nft & bridge lock bulk(msg) & bridge unlock bulk', async () => { - const token_id1 = "4"; - const token_id2 = "6"; - - const contractMsg = firma.CwBridge.getCwBridgeMsgData().getMsgDataLock(); + const allNftList = await firma.Cw721.getAllNftIdList(cw721ContractAddress); + for (let i = 0; i < tokenIds.length; i++) { + if (!allNftList.includes(tokenIds[i])) { + console.log(`new minting nft id by ${tokenIds[i]}`); + await firma.Cw721.mint(aliceWallet, cw721ContractAddress, aliceAddress, tokenIds[i], `${tokenUri}/${tokenIds[i]}`); + } + } - const tx1 = await firma.Cw721.getUnsignedTxSendNft(bobWallet, cw721ContractAddress, bridgeContractAddress, token_id1, contractMsg); - const tx2 = await firma.Cw721.getUnsignedTxSendNft(bobWallet, cw721ContractAddress, bridgeContractAddress, token_id2, contractMsg); + const contractLockMsg = firma.CwBridge.getCwBridgeMsgData().getMsgDataLock(); - const gas = await firma.Cw721.getGasEstimationSignAndBroadcast(bobWallet, [tx1, tx2]); - const fee = Math.ceil(gas * 0.1); + const bulkLockList = []; + for (let i = 0; i < tokenIds.length; i++) { + const nftData = await firma.Cw721.getNftData(cw721ContractAddress, tokenIds[i]); + if (nftData.access.owner !== aliceAddress) continue; - var result = await firma.Cw721.signAndBroadcast(bobWallet, [tx1, tx2], { gas: gas, fee: fee }); - expect(result.code).to.be.equal(0); + const unsignedMsg = await firma.Cw721.getUnsignedTxSendNft(aliceWallet, cw721ContractAddress, bridgeContractAddress, tokenIds[i], contractLockMsg); + bulkLockList.push(unsignedMsg); + } - const data = await firma.Cw721.getNFTIdListOfOwner(cw721ContractAddress, bobAddress); - console.log(data); - }); + if (bulkLockList.length > 0) { + const gas = await firma.Cw721.getGasEstimationSignAndBroadcast(aliceWallet, bulkLockList); + const fee = Math.ceil(gas * 0.1); - it.skip('[low] cw721 send_nft & bridge deposit bulk', async () => { + const result = await firma.Cw721.signAndBroadcast(aliceWallet, bulkLockList, { gas: gas, fee: fee }); + expect(result.code).to.be.equal(0); + } else { + console.log("The cw721 send_nft & bridge lock bulk test has not been conducted."); + } - const token_id1 = "4"; - const token_id2 = "6"; + const bulkUnlockList = []; + for (let i = 0; i < tokenIds.length; i++) { + const nftData = await firma.Cw721.getNftData(cw721ContractAddress, tokenIds[i]); + if (nftData.access.owner !== bridgeContractAddress) continue; - const contractMsg = firma.CwBridge.getCwBridgeMsgData().getMsgDataDeposit(bobAddress); + const unsignedMsg = await firma.CwBridge.getUnsignedTxUnlock(aliceWallet, bridgeContractAddress, tokenIds[i]); + bulkUnlockList.push(unsignedMsg); + } - const tx1 = await firma.Cw721.getUnsignedTxSendNft(bobWallet, cw721ContractAddress, bridgeContractAddress, token_id1, contractMsg); - const tx2 = await firma.Cw721.getUnsignedTxSendNft(bobWallet, cw721ContractAddress, bridgeContractAddress, token_id2, contractMsg); + if (bulkUnlockList.length > 0) { + const gas = await firma.Cw721.getGasEstimationSignAndBroadcast(aliceWallet, bulkUnlockList); + const fee = Math.ceil(gas * 0.1); - const gas = await firma.Cw721.getGasEstimationSignAndBroadcast(bobWallet, [tx1, tx2]); - const fee = Math.ceil(gas * 0.1); + const result = await firma.Cw721.signAndBroadcast(aliceWallet, bulkUnlockList, { gas: gas, fee: fee }); + expect(result.code).to.be.equal(0); + } + }); - var result = await firma.Cw721.signAndBroadcast(bobWallet, [tx1, tx2], { gas: gas, fee: fee }); - expect(result.code).to.be.equal(0); + it('[low] cw721 send_nft & bridge deposit bulk', async () => { - const data = await firma.Cw721.getNFTIdListOfOwner(cw721ContractAddress, bobAddress); - console.log(data); + const contractMsg = firma.CwBridge.getCwBridgeMsgData().getMsgDataDeposit(aliceAddress); + + const txList = []; + for (let i = 0; i < tokenIds.length; i++) { + const unsignedMsg = await firma.Cw721.getUnsignedTxSendNft(aliceWallet, cw721ContractAddress, bridgeContractAddress, tokenIds[i], contractMsg); + txList.push(unsignedMsg); + } + + if (txList.length > 0) { + const gas = await firma.Cw721.getGasEstimationSignAndBroadcast(aliceWallet, txList); + const fee = Math.ceil(gas * 0.1); + + const result = await firma.Cw721.signAndBroadcast(aliceWallet, txList, { gas: gas, fee: fee }); + expect(result.code).to.be.equal(0); + } else { + console.log("The cw721 send_nft & bridge deposit bulk test has not been conducted."); + } }); }); \ No newline at end of file diff --git a/test/36.cw_bridge_query.test.ts b/test/36.cw_bridge_query.test.ts index 0bd3dae..98dd0b8 100644 --- a/test/36.cw_bridge_query.test.ts +++ b/test/36.cw_bridge_query.test.ts @@ -1,9 +1,12 @@ -import { FirmaConfig } from "../sdk/FirmaConfig"; import { FirmaSDK } from "../sdk/FirmaSDK" import { expect } from 'chai'; -import { aliceMnemonic, bobMnemonic } from './config_test'; +import fs from "fs"; + +import { aliceMnemonic, bobMnemonic, TestChainConfig } from './config_test'; import { FirmaWalletService } from "../sdk/FirmaWalletService"; +import { FirmaUtil } from "../sdk/FirmaUtil"; +import { AccessConfig, AccessType } from "../sdk/FirmaCosmWasmService"; describe('[36. Bridge query Test]', () => { @@ -14,8 +17,13 @@ describe('[36. Bridge query Test]', () => { let aliceAddress: string; let bobAddress: string; + let cw721ContractAddress = ""; + let bridgeContractAddress = ""; + + const tokenId = "1"; + beforeEach(async function () { - firma = new FirmaSDK(FirmaConfig.TestNetConfig); + firma = new FirmaSDK(TestChainConfig); aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); @@ -24,53 +32,133 @@ describe('[36. Bridge query Test]', () => { bobAddress = await bobWallet.getAddress(); }) - let bridgeContractAddress = "firma1pug0zu6f93nmvjl559s0uymr92jhmn5t76p7knh9zg4sqlpygqyqg6edtf" + // init for query test + //---------------------------------------------------------------------- + + it('Cw721 instantiate', async () => { + + if (cw721ContractAddress !== "") { + console.log('already cw721ContractAddress'); + return ; + } + + try { + const wasmFile = fs.readFileSync("./test/sample/cw721_base.wasm"); + const array = new Uint8Array(wasmFile.buffer); + + const storeCodeGas = 3000000; + const storeCodeFee = FirmaUtil.getUFCTFromFCT(0.3); + + const everyBodyAccessConfig: AccessConfig = { permission: AccessType.ACCESS_TYPE_EVERYBODY, address: "" }; + const storeCodeResult = await firma.CosmWasm.storeCode(aliceWallet, array, everyBodyAccessConfig, { gas: storeCodeGas, fee: storeCodeFee }); + const storeCodeData = JSON.parse(storeCodeResult.rawLog!); + + const codeId = storeCodeData[0]["events"][1]["attributes"][1]["value"]; + + const admin = await aliceWallet.getAddress(); + const label = "test1"; + + const instantiateGas = 3000000; + const instantiateFee = FirmaUtil.getUFCTFromFCT(0.3); + const noFunds: any = []; + + const testData = JSON.stringify({ + minter: aliceAddress, + name: "My Awesome NFT Collection", + symbol: "MAWESOME" + }); + + const instantiateResult = await firma.CosmWasm.instantiateContract(aliceWallet, admin, codeId, label, testData, noFunds, { gas: instantiateGas, fee: instantiateFee }); + const instantiateData = JSON.parse(instantiateResult.rawLog!); + + cw721ContractAddress = instantiateData[0]["events"][0]["attributes"][0]["value"]; + + expect(instantiateResult.code).to.be.equal(0); + } catch (error) { + console.log(error); + } + }); + + it('Cw bridge instantiate', async () => { + + if (bridgeContractAddress !== "") { + console.log('already bridgeContractAddress'); + return ; + } + + const wasmFile = fs.readFileSync("./test/sample/bridge_contract.wasm"); + const array = new Uint8Array(wasmFile.buffer); + + const gas = 3000000; + const fee = FirmaUtil.getUFCTFromFCT(0.3); + + const everyBodyAccessConfig: AccessConfig = { permission: AccessType.ACCESS_TYPE_EVERYBODY, address: "" }; + const storeCodeResult = await firma.CosmWasm.storeCode(aliceWallet, array, everyBodyAccessConfig, { gas: gas, fee: fee }); + const storeCodeData = JSON.parse(storeCodeResult.rawLog!); - it.skip('cw bridge get_config', async () => { + const codeId = storeCodeData[0]["events"][1]["attributes"][1]["value"]; + + const admin = await aliceWallet.getAddress(); + const label = "test1"; + + const noFunds: any = []; + + const testData = JSON.stringify({ + owner: admin, + cw721_address: cw721ContractAddress + }); + + const instantiateResult = await firma.CosmWasm.instantiateContract(aliceWallet, admin, codeId, label, testData, noFunds, { gas: gas, fee: fee }); + const instantiateData = JSON.parse(instantiateResult.rawLog!); + + bridgeContractAddress = instantiateData[0]["events"][0]["attributes"][0]["value"]; + + expect(instantiateResult.code).to.be.equal(0); + }); + + it('cw bridge get_config', async () => { const result = await firma.CwBridge.getConfig(bridgeContractAddress); - console.log(result); + // console.log(result); }); - it.skip('cw bridge get_owner', async () => { + it('cw bridge get_owner', async () => { const result = await firma.CwBridge.getOwner(bridgeContractAddress); - console.log(result); + // console.log(result); }); - it.skip('cw bridge get_authorized_user', async () => { + it('cw bridge get_authorized_user', async () => { const result = await firma.CwBridge.getAuthorizedUsers(bridgeContractAddress); - console.log(result); + // console.log(result); }); - it.skip('cw bridge nft_info', async () => { - const token_id = "15"; - + it('cw bridge nft_info', async () => { // not exist token_id -> return null. - const result = await firma.CwBridge.getNftInfo(bridgeContractAddress, token_id); - console.log(result); + const result = await firma.CwBridge.getNftInfo(bridgeContractAddress, tokenId); + // console.log(result); }); - it.skip('cw bridge owner_nfts', async () => { + it('cw bridge owner_nfts', async () => { const result = await firma.CwBridge.getOwnerNfts(bridgeContractAddress, aliceAddress); - console.log(result); + // console.log(result); }); - it.skip('cw bridge owner_nfts_info', async () => { + it('cw bridge owner_nfts_info', async () => { const result = await firma.CwBridge.getOwnerNftsInfo(bridgeContractAddress, aliceAddress); - console.log(result); + // console.log(result); }); - it.skip('cw bridge owner_withdrawable_nfts', async () => { + it('cw bridge owner_withdrawable_nfts', async () => { const result = await firma.CwBridge.getOwnerWithdrawableNfts(bridgeContractAddress, bobAddress); - console.log(result); + // console.log(result); }); - it.skip('cw bridge owner_unlockable_nfts', async () => { + it('cw bridge owner_unlockable_nfts', async () => { const result = await firma.CwBridge.getOwnerUnlockableNfts(bridgeContractAddress, aliceAddress); - console.log(result); + // console.log(result); }); - it.skip('cw bridge global_tx_counts', async () => { + it('cw bridge global_tx_counts', async () => { const result = await firma.CwBridge.getGlobalTxCounts(bridgeContractAddress); - console.log(result); + // console.log(result); }); }); \ No newline at end of file