diff --git a/package.json b/package.json index 7caece2..090372b 100644 --- a/package.json +++ b/package.json @@ -37,4 +37,4 @@ "axios": "^0.26.1", "lunesrs": "^1.9.1" } -} \ No newline at end of file +} diff --git a/src/blockchain/address/address.types.ts b/src/blockchain/address/address.types.ts new file mode 100644 index 0000000..d49e55c --- /dev/null +++ b/src/blockchain/address/address.types.ts @@ -0,0 +1,68 @@ +/* +{ + "address": "387LjpQ5fdBdcY4nRcfDU7gPYdesbc1Md4D", + "balances": [ + { + "assetId": "FJL6J61NFWmZksXh3KnZdbN4ZWwgkZkUswWQ1G9DLvUk", + "balance": 13140000, + "reissuable": true, + "quantity": 1000000000000000, + "issueTransaction": { + "type": 3, + "id": "FJL6J61NFWmZksXh3KnZdbN4ZWwgkZkUswWQ1G9DLvUk", + "sender": "37kGaGFGgzw5E6cr5thLVsxiXHoJSMuso92", + "senderPublicKey": "EkUV6ihoPtvXKX8q6KhkSasihjxWZcXivBuf4HpN4sRp", + "fee": 100000000, + "timestamp": 1529557133065, + "signature": "5uEUY5JgQ9756RAPCHNStsFNyLGU9pp1kv3q3N2MdwUnYAyzvU7wdtG5MTsKwNmYDvkTUwHP6fjaJqH6rzuCm7NP", + "assetId": "FJL6J61NFWmZksXh3KnZdbN4ZWwgkZkUswWQ1G9DLvUk", + "name": "NEO Token", + "description": "Reward Token for lunesrealnode.com Leasers", + "quantity": 1000000000000000, + "decimals": 8, + "reissuable": true + } + } + ] + } +*/ +interface IIssueTransaction { + type: number + id: string + sender: string + senderPublicKey: string + fee: number + timestamp: number + signature: string + assetId: string + name: string + description: string + quantity: number + decimals: number + reissuable: boolean +} + +interface IBalances { + assetId: string + balance: number + reissuable: boolean + quantity: number + issueTransaction: Array +} + +interface IHeader { + address: string +} + +export interface IAddress { + header: IHeader + body: Array +} + +/* + * IAddressError : error messages to Address functions + */ +export interface IAddressError { + status: string + message: string +} diff --git a/src/blockchain/address/service.address.ts b/src/blockchain/address/service.address.ts new file mode 100644 index 0000000..af0ae56 --- /dev/null +++ b/src/blockchain/address/service.address.ts @@ -0,0 +1,164 @@ +import { IAddress, IAddressError } from "./address.types" + +import axios from "axios" + +const BASEURL = "https://lunesnode.lunes.io/" + +/* +/assets/{asset_id}/distribution +/assets/balance/{address} [ok] +assets/balance/{address}/{asset_id} + + +/leasing/active/{address} + +/addresses +/addresses/alias/by-alias/{alias} +/addresses/balance/{address} +addresses/alias/by-address/{address} +/* + + + +/* + * This function check whether address {address} is valid or not + * --- validation: {address} : string + * https://lunesnode.lunes.io/addresses/validate/${address} + */ + +export async function validateAddr( + address: String +): Promise { + const url = `${BASEURL}addresses/validate/${address}` + + if (typeof address !== "string") { + const error: IAddressError = { + status: `error`, + message: `the type of address cannot different of string` + } + return error + } else { + const response = await axios.get(url) + //return response.data + if ( + response.status === 404 || + response.status === 401 || + response.status === 403 || + response.status === 501 + ) { + const error: IAddressError = { + status: `error`, + message: `system error, come back later` + } + return error + } else if (response.data.valid === false) { + const error: IAddressError = { + status: `error`, + message: `address invalid` + } + return error + } else if (response.status === 200) { + return response.data + } + } + + // const response = await axios.get(url) + // return response.data +} + +/* + * This function check Account's balances for all assets + * --- validation: {address} : string + * GET /assets/balance/{address} + */ +export async function assetBalance( + address: string +): Promise { + const url = `${BASEURL}assets/balance/${address}` + + if (typeof address !== "string") { + const error: IAddressError = { + status: `error`, + message: `the type of address cannot different of string` + } + return error + } else { + const response = await axios.get(url) + // return response.data + + //const response = await axios.get(url) + //return response.data + if ( + response.status === 404 || + response.status === 401 || + response.status === 403 || + response.status === 501 + ) { + const error: IAddressError = { + status: `error`, + message: `system error, come back later` + } + return error + } else if (response.status === 400) { + const error: IAddressError = { + status: `error`, + message: `address invalid` + } + return error + } else response.status === 200 + return response.data + } +} + +/* + * This function show Asset balance distribution by account + * --- validation: {assetId} string + * GET /assets/{assetId}/distribution + */ +export async function assetDistribution(assetId: String): Promise { + + const url = `${BASEURL}assets/${assetId}/distribution` + + if (typeof assetId !== "string") { + const error: IAddressError = { + status: `error`, + message: `the type of asset Id cannot different of string` + } + return error + } else { + return new Promise(async (resolve, reject) => { + const response = await axios.get(url) + + if ( + response.status === 404 || + response.status === 401 || + response.status === 403 || + response.status === 501) { + const error: IAddressError = { + status: `error`, + message: `system error, come back later` + } + return error + } else if (response.status === 200) { + resolve(response.data) + } else { + reject(response.data) + } return response.data + }) + + + } +} + + +//const response = await axios.get(url) +//return response.data + +/* + * This function + * --- validation: + * +export async function xxxx(address:String): Promise { + +} + */ diff --git a/src/tx/transfer/utils.ts b/src/tx/transfer/utils.ts index 79dac9e..112974e 100644 --- a/src/tx/transfer/utils.ts +++ b/src/tx/transfer/utils.ts @@ -64,7 +64,7 @@ export async function broadcastTransfer( } resolve(x) }) - .catch(erro => { + .catch((erro) => { resolve({ isSuccess: false, response: { @@ -100,4 +100,3 @@ type TransferFail = { message: string } } - diff --git a/test/blockchain/address/address.service.test.ts b/test/blockchain/address/address.service.test.ts new file mode 100644 index 0000000..9192e3e --- /dev/null +++ b/test/blockchain/address/address.service.test.ts @@ -0,0 +1,96 @@ +//import { IAddress, IAddressError } from "../../../src/blockchain/address/address.types" +import { + validateAddr, + assetBalance, assetDistribution +} from "../../../src/blockchain/address/service.address" + +describe("validateAddr function- suite test block service", () => { + it("validateAddr - validate address, passing true addrress", async () => { + const result = await validateAddr("37pU1WLv8vettr9UHGfPpTMWe1twne8FCBn") + expect(result).toStrictEqual({ + address: "37pU1WLv8vettr9UHGfPpTMWe1twne8FCBn", + valid: true + }) + }) + + it("validateAddr - incorrect address, testing response.data.valid=== false", async () => { + const result = await validateAddr("-1") + expect(result).toStrictEqual({ + message: "address invalid", + status: "error" + }) + }) +}) + +describe("ASSETS functions - suite test ASSETS service", () => { + it("assetBalance - check asset, passing true addrress", async () => { + const result = await assetBalance("387LjpQ5fdBdcY4nRcfDU7gPYdesbc1Md4D") + expect(result).toStrictEqual({ + address: "387LjpQ5fdBdcY4nRcfDU7gPYdesbc1Md4D", + balances: [ + { + assetId: "FJL6J61NFWmZksXh3KnZdbN4ZWwgkZkUswWQ1G9DLvUk", + balance: 13140000, + issueTransaction: { + assetId: "FJL6J61NFWmZksXh3KnZdbN4ZWwgkZkUswWQ1G9DLvUk", + decimals: 8, + description: + "Reward Token for lunesrealnode.com Leasers", + fee: 100000000, + id: "FJL6J61NFWmZksXh3KnZdbN4ZWwgkZkUswWQ1G9DLvUk", + name: "NEO Token", + quantity: 1000000000000000, + reissuable: true, + sender: "37kGaGFGgzw5E6cr5thLVsxiXHoJSMuso92", + senderPublicKey: + "EkUV6ihoPtvXKX8q6KhkSasihjxWZcXivBuf4HpN4sRp", + signature: + "5uEUY5JgQ9756RAPCHNStsFNyLGU9pp1kv3q3N2MdwUnYAyzvU7wdtG5MTsKwNmYDvkTUwHP6fjaJqH6rzuCm7NP", + timestamp: 1529557133065, + type: 3 + }, + quantity: 1000000000000000, + reissuable: true + } + ] + }) + }) + + it("assetBalance -error 400 - check asset, passing invalid addrress", async () => { + ///const result = await assetOne("387LjpQ5fdBdcY4nRcfDU7gPYdesbc1Md4Dm") + //expect(result).toStrictEqual("invalid address") + // expect(result()).rejects.toThrow() + + const result = async () => { + await assetBalance("387LjpQ5fdBdcY4nRcfDU7gPYdesbc1Md4Dm") + } + + expect(result()).rejects.toThrow() + }) +}) + + + +describe(" assetDistribution function- suite test ASSET DIST service", () => { + it("assetDistribution - check ASSET DIST, passing VALID assetID", async () => { + const result = await assetDistribution("4xxGB1BgeiegawpZnvFssacbKzFuPKoueGD7k7xjmJn8") + expect(result).toStrictEqual({ + "37rjJJQvE1g9qENxGTKubD3XkK5s5RAJS2f": 0, + "385ZRVhNfK7oZhPFeBRgpvChdZrzPhwjDZR": 1 + }) + + }) + + it("assetDistribution - check ASSET DIST, passing inexistent assetID", async () => { + const result = await assetDistribution("abracadabra") + expect(result).toStrictEqual({}) + + }) + +}) +//37qrqmmQ8jwJJB2aXMnXt98kiwezyzb5ww7 richlist com vĂ¡rios assets + +/* +describe("validateAddr function- suite test block service", () => { + +})*/