Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
value: ""
};
const tld = domain.split(".").reverse()[0];
const regExp = new RegExp(`.${tld}$`);
const subdomain = domain.replace(regExp, "");
const hashSubdomain = u.sha256(u.str2hexstring(subdomain));
const hashDomain = u.sha256(u.str2hexstring(tld));
const hashName = u.sha256(hashSubdomain.concat(hashDomain));
const parsedName = sc.ContractParam.byteArray(hashName, "name");
const args = [protocol, parsedName, empty];
const sb = new sc.ScriptBuilder();
const script = sb.emitAppCall(contract, operation, args).str;
const res = await rpc.Query.invokeScript(script).execute(url);
return rpc.StringParser(res.result.stack[0]);
}
export async function getTokenBalances(
url: string,
scriptHashArray: string[],
address: string
): Promise<{ [symbol: string]: u.Fixed8 }> {
const addrScriptHash = u.reverseHex(wallet.getScriptHashFromAddress(address));
const sb = new sc.ScriptBuilder();
scriptHashArray.forEach(scriptHash => {
sb.emitAppCall(scriptHash, "symbol")
.emitAppCall(scriptHash, "decimals")
.emitAppCall(scriptHash, "balanceOf", [addrScriptHash]);
});
const res = await rpc.Query.invokeScript(sb.str).execute(url);
const tokenList = {} as { [symbol: string]: u.Fixed8 };
if (
!res ||
!res.result ||
!res.result.stack ||
res.result.stack.length !== 3 * scriptHashArray.length
) {
throw new Error("Stack returned was invalid");
}
return (sb = new sc.ScriptBuilder()) => {
const addressHash = addressToScriptHash(addr);
return sb.emitAppCall(scriptHash, "balanceOf", [addressHash]);
};
}
export function getNetworkFeeForMultiSig(
signingThreshold: number,
pubkeysNum: number
): number {
const sb = new sc.ScriptBuilder();
return (
sc.OpCodePrices[sc.OpCode.PUSHBYTES64] * signingThreshold +
sc.OpCodePrices[
sb.emitPush(signingThreshold).str.slice(0, 2) as sc.OpCode
] +
sc.OpCodePrices[sc.OpCode.PUSHBYTES33] * pubkeysNum +
sc.OpCodePrices[sb.emitPush(pubkeysNum).str.slice(0, 2) as sc.OpCode] +
sc.getInteropServicePrice(sc.InteropServiceCode.NEO_CRYPTO_CHECKMULTISIG, {
size: pubkeysNum
})
);
}
export async function getToken(
url: string,
scriptHash: string,
address?: string
): Promise {
const parser = address ? parseTokenInfoAndBalance : parseTokenInfo;
const sb = new sc.ScriptBuilder();
abi.name(scriptHash)(sb);
abi.symbol(scriptHash)(sb);
abi.decimals(scriptHash)(sb);
abi.totalSupply(scriptHash)(sb);
if (address) {
abi.balanceOf(scriptHash, address)(sb);
}
const script = sb.str;
try {
const res = await rpc.Query.invokeScript(script)
.parseWith(parser)
.execute(url);
const result: TokenInfo = {
name: res[0],
symbol: res[1],
decimals: res[2],
export async function getTokens(
url: string,
scriptHashArray: string[],
address?: string
): Promise {
try {
const sb = new sc.ScriptBuilder();
scriptHashArray.forEach(scriptHash => {
if (address) {
const addrScriptHash = u.reverseHex(
wallet.getScriptHashFromAddress(address)
);
sb.emitAppCall(scriptHash, "name")
.emitAppCall(scriptHash, "symbol")
.emitAppCall(scriptHash, "decimals")
.emitAppCall(scriptHash, "totalSupply")
.emitAppCall(scriptHash, "balanceOf", [addrScriptHash]);
} else {
sb.emitAppCall(scriptHash, "name")
.emitAppCall(scriptHash, "symbol")
.emitAppCall(scriptHash, "decimals")
.emitAppCall(scriptHash, "totalSupply");
}
export async function getTokenBalance(
url: string,
scriptHash: string,
address: string
): Promise {
const sb = new sc.ScriptBuilder();
abi.decimals(scriptHash)(sb);
abi.balanceOf(scriptHash, address)(sb);
const script = sb.str;
try {
const res = await rpc.Query.invokeScript(script).execute(url);
const decimals = rpc.IntegerParser(res.result.stack[0]);
return rpc
.Fixed8Parser(res.result.stack[1])
.mul(Math.pow(10, 8 - decimals));
} catch (err) {
log.error(`getTokenBalance failed with : ${err.message}`);
throw err;
}
}