Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
);
});
});
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
);
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);
});
});
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();
});
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)
);
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}`);
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}`);
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;
}
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;
}
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;
}