How to use the @cityofzion/neon-core.wallet.Balance function in @cityofzion/neon-core

To help you get started, we’ve selected a few @cityofzion/neon-core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github CityOfZion / neon-js / packages / neon-api / __tests__ / provider / neoscan / core.ts View on Github external
test("returns empty balance for new address", async () => {
    const httpCall = jest.fn().mockImplementationOnce(() =>
      Promise.resolve({
        data: {
          txids: null,
          claimed: null,
          balance: null,
          address: "not found"
        }
      })
    );
    axios.get = httpCall;
    expect(await neoscan.getBalance(testUrl, "address")).toEqual(
      new wallet.Balance({
        net: testUrl,
        address: "address"
      } as wallet.BalanceLike)
    );
  });
});
github CityOfZion / neon-js / packages / neon-api / __tests__ / func / create.ts View on Github external
test("create with the correct args", async () => {
    const config = {
      api: {} as any,
      balance: new wallet.Balance(),
      intents: [],
      override: {},
      fees: 0
    } as SendAssetConfig;
    const calculateFunc = jest.fn();
    const contractTx = {
      calculate: calculateFunc
    };
    tx.ContractTransaction.mockImplementationOnce(() => contractTx);
    const result = await create.createContractTx(config);
    expect(result.tx).toBe(contractTx);
    expect(calculateFunc).toBeCalledWith(
      config.balance,
      undefined,
      config.fees
    );
github CityOfZion / neon-js / packages / neon-api / __tests__ / func / fill.ts View on Github external
test("fills if balance not present", async () => {
    const expectedBalance = new wallet.Balance();
    const config = {
      api: {
        getBalance: jest.fn().mockImplementationOnce(() => expectedBalance)
      } as any,
      account: {
        address: "address"
      }
    } as SendAssetConfig;

    const result = await fill.fillBalance(config);

    expect(result.balance).toBe(expectedBalance);
    expect(config.api.getBalance).toBeCalledWith(config.account.address);
  });
});
github CityOfZion / neon-js / packages / neon-api / __tests__ / func / fill.ts View on Github external
test("skips if balance present", async () => {
    const expectedBalance = new wallet.Balance();
    const config = {
      api: {
        getBalance: jest.fn()
      } as any,
      balance: expectedBalance
    } as SendAssetConfig;

    const result = await fill.fillBalance(config);

    expect(result.balance).toBe(expectedBalance);
    expect(config.api.getBalance).not.toBeCalled();
  });
github CityOfZion / neon-js / packages / neon-api / __tests__ / provider / neonDB / core.ts View on Github external
GAS: {
            balance: 1.234,
            unspent: [{ index: 1, txid: "2", value: 1.234 }]
          },
          NEO: {
            balance: 5,
            unspent: [{ index: 1, txid: "1", value: 5 }]
          },
          address: "address",
          net: testUrl
        }
      })
    );
    axios.get = httpCall;
    expect(await neonDB.getBalance(testUrl, "address")).toEqual(
      new wallet.Balance({
        net: testUrl,
        address: "address",
        assetSymbols: ["NEO", "GAS"],
        assets: {
          NEO: {
            spent: [],
            unspent: [{ value: 5, txid: "1", index: 1 }],
            balance: 5
          },
          GAS: {
            unspent: [{ value: 1.234, txid: "2", index: 1 }],
            balance: 1.234
          } as any
        }
      } as any)
    );
github CityOfZion / neon-js / packages / neon-api / src / provider / neoscan / core.ts View on Github external
export async function getBalance(
  url: string,
  address: string
): Promise {
  const response = await axios.get(url + "/v1/get_balance/" + address);
  const data = response.data as NeoscanV1GetBalanceResponse;
  if (data.address === "not found" && data.balance === null) {
    return new wallet.Balance({ net: url, address });
  }
  const bal = new wallet.Balance({
    net: url,
    address: data.address
  });
  const neoscanBalances = data.balance as NeoscanBalance[];
  for (const b of neoscanBalances) {
    if (b.amount > 0 && b.unspent.length > 0) {
      bal.addAsset(b.asset, {
        unspent: parseUnspent(b.unspent)
      } as Partial);
    } else {
      bal.addToken(b.asset, b.amount);
    }
  }
  log.info(`Retrieved Balance for ${address} from neoscan ${url}`);
github CityOfZion / neon-js / packages / neon-api / src / provider / neoCli / core.ts View on Github external
export async function getBalance(
  url: string,
  address: string
): Promise {
  const response = await axios.post(
    url,
    Object.assign({}, BASE_REQ, { method: "getunspents", params: [address] })
  );
  const data = response.data as NeoCliGetUnspentsResponse;
  if (data.error) {
    throwRpcError(data.error);
  }
  const bal = new wallet.Balance({
    net: url,
    address: data.result.address
  });
  for (const assetBalance of data.result.balance) {
    if (assetBalance.amount === 0) {
      continue;
    }
    if (assetBalance.unspent.length > 0) {
      bal.addAsset(assetBalance.asset_symbol, {
        unspent: assetBalance.unspent.map(convertNeoCliTx)
      });
    } else {
      bal.addToken(assetBalance.asset_symbol, assetBalance.amount);
    }
  }
  log.info(`Retrieved Balance for ${address} from neonDB ${url}`);
github CityOfZion / neon-js / packages / neon-api / src / provider / neonDB / core.ts View on Github external
export async function getBalance(
  url: string,
  address: string
): Promise {
  const response = await axios.get(url + "/v2/address/balance/" + address);
  const data = response.data as NeonDbBalance;
  const bal = new wallet.Balance({ net: url, address } as wallet.BalanceLike);
  if (data.NEO.balance > 0) {
    bal.addAsset("NEO", data.NEO);
  }
  if (data.GAS.balance > 0) {
    bal.addAsset("GAS", data.GAS);
  }
  log.info(`Retrieved Balance for ${address} from neonDB ${url}`);
  return bal;
}
github CityOfZion / neon-js / packages / neon-api / src / provider / neoscan / core.ts View on Github external
export async function getBalance(
  url: string,
  address: string
): Promise {
  const response = await axios.get(url + "/v1/get_balance/" + address);
  const data = response.data as NeoscanV1GetBalanceResponse;
  if (data.address === "not found" && data.balance === null) {
    return new wallet.Balance({ net: url, address });
  }
  const bal = new wallet.Balance({
    net: url,
    address: data.address
  });
  const neoscanBalances = data.balance as NeoscanBalance[];
  for (const b of neoscanBalances) {
    if (b.amount > 0 && b.unspent.length > 0) {
      bal.addAsset(b.asset, {
        unspent: parseUnspent(b.unspent)
      } as Partial);
    } else {
      bal.addToken(b.asset, b.amount);
    }
  }
  log.info(`Retrieved Balance for ${address} from neoscan ${url}`);
  return bal;
}
github CityOfZion / neon-js / packages / neon-api / src / funcs / create.ts View on Github external
const processedScript =
    typeof config.script === "object"
      ? sc.createScript(config.script)
      : config.script;
  config.tx = new tx.InvocationTransaction(
    Object.assign(
      {
        outputs: config.intents || [],
        script: processedScript,
        gas: config.gas || 0
      },
      config.override
    )
  );
  config.tx.calculate(
    config.balance || new wallet.Balance(),
    undefined,
    config.fees
  );
  return config;
}