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 claims", async () => {
const httpCall = jest.fn().mockImplementationOnce(() =>
Promise.resolve({
data: {
address: "address",
claims: [],
net: testUrl,
total_claim: 0,
total_unspent_claim: 0
}
})
);
axios.get = httpCall;
expect(await neonDB.getClaims(testUrl, "address")).toEqual(
new wallet.Claims({
net: testUrl,
address: "address"
} as wallet.ClaimsLike)
);
});
});
test("create with the correct args", async () => {
const config = {
api: {} as any,
account: { address: "address" },
claims: new wallet.Claims(),
override: {}
} as ClaimGasConfig;
const addClaimsFunc = jest.fn();
const claimTx = {
addClaims: addClaimsFunc
};
tx.ClaimTransaction.mockImplementationOnce(() => claimTx);
const result = await create.createClaimTx(config);
expect(result.tx).toBe(claimTx);
expect(addClaimsFunc).toBeCalledWith(config.claims);
});
});
test("skips if claims present", async () => {
const expectedClaims = new wallet.Claims({
claims: [new wallet.ClaimItem()]
});
const config = {
claims: expectedClaims
} as ClaimGasConfig;
const result = await fill.fillClaims(config);
expect(result.claims).toBe(expectedClaims);
});
test("throws if claims not present and api returned no claims", async () => {
const expectedClaims = new wallet.Claims();
const config = {
net: "UnitTestNet",
account: {
address: jest.fn()
},
api: {
getClaims: jest.fn().mockImplementationOnce(() => expectedClaims)
} as any
} as any;
expect(fill.fillClaims(config)).rejects.toThrow("No Claims found");
});
});
test("fills if claims not present", async () => {
const expectedClaims = new wallet.Claims({
claims: [new wallet.ClaimItem()]
});
const config = {
net: "UnitTestNet",
account: {
address: "address"
} as any,
api: {
getClaims: jest.fn().mockImplementationOnce(() => expectedClaims)
} as any
} as ClaimGasConfig;
const result = await fill.fillClaims(config);
expect(result.claims).toBe(expectedClaims);
expect(config.api.getClaims).toBeCalledWith(config.account.address);
});
unclaimed: 1,
txid: "1",
sys_fee: 0.01,
start_height: 5,
n: 2,
generated: 0.1,
end_height: 11
}
],
address: "address"
}
})
);
axios.get = httpCall;
expect(await neoscan.getClaims(testUrl, "address")).toEqual(
new wallet.Claims({
net: testUrl,
address: "address",
claims: [
{ claim: 1, txid: "1", index: 2, value: 10, start: 5, end: 11 }
]
} as wallet.ClaimsLike)
);
expect(httpCall).toBeCalledWith(testUrl + "/v1/get_claimable/address");
});
address: string
): Promise {
const response = await axios.get(url + "/v2/address/claims/" + address);
const data = response.data as NeonDbClaims;
const claims = data.claims.map(c => {
return {
claim: new u.Fixed8(c.claim || 0).div(100000000),
index: c.index,
txid: c.txid,
start: c.start || 0,
end: c.end || 0,
value: c.value
};
});
log.info(`Retrieved Claims for ${address} from neonDB ${url}`);
return new wallet.Claims({
net: url,
address,
claims
} as wallet.ClaimsLike);
}
export async function getClaims(
url: string,
address: string
): Promise {
const response = await axios.post(
url,
Object.assign({}, BASE_REQ, { method: "getclaimable", params: [address] })
);
const data = response.data as NeoCliGetClaimableResponse;
if (data.error) {
throwRpcError(data.error);
}
return new wallet.Claims({
net: url,
address: data.result.address,
claims: data.result.claimable.map(convertNeoCliClaimable)
});
}
export async function getClaims(
url: string,
address: string
): Promise {
const response = await axios.get(url + "/v1/get_claimable/" + address);
const data = response.data as NeoscanV1GetClaimableResponse;
if (data.address === "not found" && data.claimable === null) {
return new wallet.Claims({ address: data.address });
}
const claims = parseClaims(data.claimable as NeoscanClaim[]);
log.info(`Retrieved Claims for ${address} from neoscan ${url}`);
return new wallet.Claims({
net: url,
address: data.address,
claims
});
}