Nep5Token extends SmartContract { public TransactionBuilder transferFromDefaultAccount(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"); } Account acc = wallet.getDefaultAccount(); BigInteger fractions = getAmountAsBigInteger(amount); BigInteger accBalance = getBalanceOf(acc.getScriptHash()); if (accBalance.compareTo(fractions) < 0) { throw new InsufficientFundsException("The wallet's default account does not hold enough" + " tokens. Transfer amount is " + fractions.toString() + " but account" + " only holds " + accBalance.toString() + " (in token fractions)."); } return invokeFunction(NEP5_TRANSFER, ContractParameter.hash160(acc.getScriptHash()), ContractParameter.hash160(to), ContractParameter.integer(fractions)) .wallet(wallet) .signers(Signer.calledByEntry(acc.getScriptHash())); } 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 transferFromDefaultAccountShouldAddAccountAsSigner() throws Throwable { setUpWireMockForCall("invokescript", "invokescript_transfer.json"); setUpWireMockForInvokeFunction("decimals", "invokefunction_decimals_gas.json"); setUpWireMockForGetBlockCount(1000); setUpWireMockForBalanceOf(account1.getScriptHash(), "invokefunction_balanceOf_300000000.json"); Transaction tx = gasToken.transferFromDefaultAccount(Wallet.withAccounts(account1), RECIPIENT_SCRIPT_HASH, BigDecimal.ONE) .buildTransaction(); assertThat(tx.getSigners().get(0).getScriptHash(), is(account1.getScriptHash())); assertThat(tx.getSigners().get(0).getScopes().get(0), is(WitnessScope.CALLED_BY_ENTRY)); assertThat(tx.getSender(), is(account1.getScriptHash())); }
@Test public void transferFromDefaultAccountShouldCreateTheCorrectScript() throws Throwable { setUpWireMockForCall("invokescript", "invokescript_transfer.json"); setUpWireMockForInvokeFunction("decimals", "invokefunction_decimals_gas.json"); setUpWireMockForGetBlockCount(1000); setUpWireMockForInvokeFunction("balanceOf", "invokefunction_balanceOf_300000000.json"); byte[] expectedScript = new ScriptBuilder().contractCall(GAS_TOKEN_SCRIPT_HASH, NEP5_TRANSFER, Arrays.asList( ContractParameter.hash160(account1.getScriptHash()), ContractParameter.hash160(RECIPIENT_SCRIPT_HASH), ContractParameter.integer(100000000))).toArray(); Transaction tx = gasToken.transferFromDefaultAccount(Wallet.withAccounts(account1, account2), RECIPIENT_SCRIPT_HASH, BigDecimal.ONE) .buildTransaction(); assertThat(tx.getScript(), is(expectedScript)); }
@Test(expected = InsufficientFundsException.class) public void testFailTransferFromDefaultAccount_InsufficientBalance() throws Exception { setUpWireMockForInvokeFunction("decimals", "invokefunction_decimals_gas.json"); setUpWireMockForGetBlockCount(1000); setUpWireMockForBalanceOf(account1.getScriptHash(), "invokefunction_balanceOf_300000000.json"); setUpWireMockForCall("invokescript", "invokescript_transfer.json"); gasToken.transferFromDefaultAccount(Wallet.withAccounts(account1), RECIPIENT_SCRIPT_HASH, new BigDecimal("4")); }
@Test(expected = IllegalArgumentException.class) public void testTransferFromDefaultAccount_negativeAmount() throws IOException { neoToken.transferFromDefaultAccount(Wallet.withAccounts(account1), RECIPIENT_SCRIPT_HASH, new BigDecimal("-1")); }