Nep5Token extends SmartContract { public TransactionBuilder transfer(Wallet wallet, ScriptHash to, BigDecimal amount) throws IOException { if (amount.signum() < 0) { throw new IllegalArgumentException( "The parameter amount must be greater than or equal to 0"); } List<Account> accountsOrdered = new ArrayList<>(wallet.getAccounts()); accountsOrdered.remove(wallet.getDefaultAccount()); accountsOrdered.add(0, wallet.getDefaultAccount()); return buildMultiTransferInvocation(wallet, to, amount, accountsOrdered); } Nep5Token(ScriptHash scriptHash, Neow3j neow); String getName(); String getSymbol(); BigInteger getTotalSupply(); int getDecimals(); BigInteger getBalanceOf(ScriptHash scriptHash); BigInteger getBalanceOf(Wallet wallet); TransactionBuilder transfer(Wallet wallet, ScriptHash to, BigDecimal amount); TransactionBuilder transferFromSpecificAccounts(Wallet wallet, ScriptHash to,
            BigDecimal amount, ScriptHash... from); TransactionBuilder transferFromDefaultAccount(Wallet wallet, ScriptHash to,
            BigDecimal amount);  }
@Test public void testTransferWithTheFirstTwoAccountsNeededToCoverAmount() throws IOException { setUpWireMockForCall("invokescript", "invokescript_transfer.json"); setUpWireMockForGetBlockCount(1000); setUpWireMockForInvokeFunction("decimals", "invokefunction_decimals.json"); setUpWireMockForBalanceOf(account1.getScriptHash(), "invokefunction_balanceOf_5.json"); setUpWireMockForBalanceOf(account2.getScriptHash(), "invokefunction_balanceOf_4.json"); byte[] expectedScript = new ScriptBuilder() .contractCall(NEO_TOKEN_SCRIPT_HASH, NEP5_TRANSFER, Arrays.asList( ContractParameter.hash160(account1.getScriptHash()), ContractParameter.hash160(RECIPIENT_SCRIPT_HASH), ContractParameter.integer(5))) .contractCall(NEO_TOKEN_SCRIPT_HASH, NEP5_TRANSFER, Arrays.asList( ContractParameter.hash160(account2.getScriptHash()), ContractParameter.hash160(RECIPIENT_SCRIPT_HASH), ContractParameter.integer(2))).toArray(); TransactionBuilder b = neoToken.transfer(Wallet.withAccounts(account1, account2, account3), RECIPIENT_SCRIPT_HASH, new BigDecimal("7")); assertThat(b.getScript(), is(expectedScript)); }
@Test public void testTransfer_allAccountsNeededToCoverAmount() throws IOException { setUpWireMockForCall("invokescript", "invokescript_transfer.json"); setUpWireMockForGetBlockCount(1000); setUpWireMockForInvokeFunction("decimals", "invokefunction_decimals.json"); setUpWireMockForBalanceOf(account1.getScriptHash(), "invokefunction_balanceOf_5.json"); setUpWireMockForBalanceOf(account2.getScriptHash(), "invokefunction_balanceOf_4.json"); setUpWireMockForBalanceOf(account3.getScriptHash(), "invokefunction_balanceOf_3.json"); byte[] expectedScript = new ScriptBuilder() .contractCall(NEO_TOKEN_SCRIPT_HASH, NEP5_TRANSFER, Arrays.asList( ContractParameter.hash160(account1.getScriptHash()), ContractParameter.hash160(RECIPIENT_SCRIPT_HASH), ContractParameter.integer(5))) .contractCall(NEO_TOKEN_SCRIPT_HASH, NEP5_TRANSFER, Arrays.asList( ContractParameter.hash160(account2.getScriptHash()), ContractParameter.hash160(RECIPIENT_SCRIPT_HASH), ContractParameter.integer(4))) .contractCall(NEO_TOKEN_SCRIPT_HASH, NEP5_TRANSFER, Arrays.asList( ContractParameter.hash160(account3.getScriptHash()), ContractParameter.hash160(RECIPIENT_SCRIPT_HASH), ContractParameter.integer(3))) .toArray(); TransactionBuilder b = neoToken.transfer(Wallet.withAccounts(account1, account2, account3), RECIPIENT_SCRIPT_HASH, new BigDecimal("12")); assertThat(b.getScript(), is(expectedScript)); }
@Test public void testTransfer_defaultAccountCoversAmount() throws IOException { setUpWireMockForCall("invokescript", "invokescript_transfer.json"); setUpWireMockForGetBlockCount(1000); setUpWireMockForInvokeFunction("decimals", "invokefunction_decimals.json"); setUpWireMockForBalanceOf(account1.getScriptHash(), "invokefunction_balanceOf_5.json"); byte[] expectedScript = new ScriptBuilder().contractCall(NEO_TOKEN_SCRIPT_HASH, NEP5_TRANSFER, Arrays.asList( ContractParameter.hash160(account1.getScriptHash()), ContractParameter.hash160(RECIPIENT_SCRIPT_HASH), ContractParameter.integer(4))).toArray(); TransactionBuilder b = neoToken.transfer(Wallet.withAccounts(account1, account2), RECIPIENT_SCRIPT_HASH, new BigDecimal("4")); assertThat(b.getScript(), is(expectedScript)); }
@Test public void testTransfer_defaultAccountHasNoBalance() throws IOException { setUpWireMockForCall("invokescript", "invokescript_transfer.json"); setUpWireMockForGetBlockCount(1000); setUpWireMockForInvokeFunction("decimals", "invokefunction_decimals.json"); setUpWireMockForBalanceOf(account1.getScriptHash(), "invokefunction_balanceOf_0.json"); setUpWireMockForBalanceOf(account2.getScriptHash(), "invokefunction_balanceOf_0.json"); setUpWireMockForBalanceOf(account3.getScriptHash(), "invokefunction_balanceOf_3.json"); byte[] expectedScript = new ScriptBuilder().contractCall(NEO_TOKEN_SCRIPT_HASH, NEP5_TRANSFER, Arrays.asList( ContractParameter.hash160(account3.getScriptHash()), ContractParameter.hash160(RECIPIENT_SCRIPT_HASH), ContractParameter.integer(1))).toArray(); TransactionBuilder b = neoToken.transfer(Wallet.withAccounts(account1, account2, account3), RECIPIENT_SCRIPT_HASH, new BigDecimal("1")); assertThat(b.getScript(), is(expectedScript)); }
@Test public void testTransfer_MultiSig_NotEnoughSignersPresent() throws IOException { setUpWireMockForCall("invokescript", "invokescript_transfer.json"); setUpWireMockForGetBlockCount(1000); setUpWireMockForInvokeFunction("decimals", "invokefunction_decimals.json"); setUpWireMockForBalanceOf(account1.getScriptHash(), "invokefunction_balanceOf_4.json"); setUpWireMockForBalanceOf(multiSigAccount.getScriptHash(), "invokefunction_balanceOf_3.json"); byte[] expectedScript = new ScriptBuilder().contractCall(NEO_TOKEN_SCRIPT_HASH, NEP5_TRANSFER, Arrays.asList( ContractParameter.hash160(account1.getScriptHash()), ContractParameter.hash160(RECIPIENT_SCRIPT_HASH), ContractParameter.integer(2))).toArray(); TransactionBuilder b = neoToken.transfer(Wallet.withAccounts(multiSigAccount, account1), RECIPIENT_SCRIPT_HASH, new BigDecimal("2")); assertThat(b.getScript(), is(expectedScript)); }
@Test(expected = InsufficientFundsException.class) public void testTransfer_MultiSigNotEnoughSignersPresent_NoOtherAccountPresent() throws IOException { setUpWireMockForInvokeFunction("decimals", "invokefunction_decimals.json"); setUpWireMockForInvokeFunction("symbol", "invokefunction_symbol.json"); setUpWireMockForBalanceOf(multiSigAccount.getScriptHash(), "invokefunction_balanceOf_3.json"); Wallet wallet = Wallet.withAccounts(multiSigAccount); neoToken.transfer(wallet, RECIPIENT_SCRIPT_HASH, new BigDecimal("2")); }
@Test(expected = IllegalArgumentException.class) public void testTransfer_InvalidAmount() throws IOException { neoToken.transfer(Wallet.createWallet(), RECIPIENT_SCRIPT_HASH, new BigDecimal(-1)); }
@Test(expected = InsufficientFundsException.class) public void testTransfer_insufficientBalance() throws IOException { setUpWireMockForInvokeFunction("decimals", "invokefunction_decimals.json"); setUpWireMockForInvokeFunction("symbol", "invokefunction_symbol.json"); setUpWireMockForBalanceOf(account1.getScriptHash(), "invokefunction_balanceOf_5.json"); setUpWireMockForBalanceOf(account2.getScriptHash(), "invokefunction_balanceOf_4.json"); setUpWireMockForBalanceOf(account3.getScriptHash(), "invokefunction_balanceOf_3.json"); neoToken.transfer(Wallet.withAccounts(account1, account2, account3), RECIPIENT_SCRIPT_HASH, new BigDecimal("20")); }