Set Sore
To set the Score you need to follow the next steps:
- Get transaction value
- Get nonce;
- Get Score data from Nomis API;
- Execute setScore function in the contract with the data API response.
- Confirm transaction.
Transaction Value
Value of the transaction may be 0 if the account is whitelisted. Or it may seted individually (ex. for contest winners) for the account or it may be the default value.
Here is an example for the Multichain Score on the Polygon chain:
import { type Address, createPublicClient, getContract, http } from 'viem';
import { polygon } from 'viem/chains';
import { abi } from './abi';
const calculationModel = 11;
const id = 9999999999999n;
const publicClient = createPublicClient({
chain: polygon,
transport: http(),
});
const contract = getContract({
abi,
address: '0x4724F4B3936B4c2d52f8F452719870c5d4c86b4D',
client: publicClient,
});
const getValue = async (address: Address) => {
try {
// if account is whitelisted both mint and update fees are 0
const isWhitelisted = await contract.read.whitelist([
address,
calculationModel,
]);
if (isWhitelisted) return 0n;
const [score] = await contract.read.getScore([
address,
chainId,
calculationModel,
]);
// update
if (score !== 0) {
const individual = await contract.read.getIndividualUpdateFee([
address,
calculationModel,
]);
if (individual !== 0n) return individual;
return await contract.read.getUpdateFee();
}
// mint
const individual = await contract.read.getIndividualMintFee([
address,
calculationModel,
]);
if (individual !== 0n) return individual;
return await contract.read.getMintFee();
} catch (error) {
console.error(error);
}
};
console.log(await getValue('0x...'));
Nonce
Nonce is a number that is used to prevent replay attacks
const getNonce = async (address: Address) => {
return await contract.read.getNonce([address]);
};
console.log(await getNonce('0x...'));
Score Data
To get the Score data you need to call the Nomis Wallet Stats API with the wallet’s address and other configurations.
Score | Chain | Stats API slug | Model | Id |
---|
const getScoreData = async (address: string) => {
try {
const searchParams = new SearchParams({
nonce: await getNonce(address),
deadline: Date.now() + 1000 * 60 * 60 * 24, // time given to the user to sign the transaction
calculationModel,
chainId,
});
const res = await fetch(
`https://api.nomis.com/api/v1/${apiSlug}/wallet/${address}/score?${searchParams.toString()}`
);
const data = await res.json();
return { score: data.score, ...mintData };
} catch (error) {
console.error(error);
}
};
You will get an mintData object in the response:
type MintData = {
signature: string;
mintedScore: number;
calculationModel: number;
deadline: number;
metadataUrl: string;
chainId: number;
referralCode: string;
referrerCode: string;
onftMetadataUrl: string;
mintedChain: {...};
};
Transaction
To set the Score you need to call the setScore function in the contract with the data API response.
const scoreData = {...} // data from the API response
const setScore = async (address: Address) => {
try {
const scoreData = getScoreData(address);
const trx = contract.write.setScore([
scoreData.signature,
scoreData.score,
scoreData.calculationModel,
scoreData.deadline,
scoreData.metadataUrl,
scoreData.chainId,
scoreData.referralCode,
scoreData.referrerCode
])
} catch (error) {
console.error(error)
}
}