Skip to content

Commit

Permalink
Add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chiro-hiro committed Sep 3, 2024
1 parent 17f5a1b commit f02052c
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 15 deletions.
19 changes: 11 additions & 8 deletions test/008-orand-provider-v3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,35 +395,38 @@ describe('OrandProviderV3', function () {
it('OrandProviderV3 should be upgradeable', async () => {
const { ethers } = hre;
const accounts = await ethers.getSigners();
const orocleV1Factory = await ethers.getContractFactory('OrocleV1');
const orocleV2Factory = await ethers.getContractFactory('OrocleV2');
const orandECVRFV3Factory = await ethers.getContractFactory('OrandECVRFV3');
const orand1Factory = await ethers.getContractFactory('OrandProviderTest');
const orand2Factory = await ethers.getContractFactory('OrandProviderV3');
const orandV3Factory1 = await ethers.getContractFactory('OrandProviderTest');
const orandV3Factory2 = await ethers.getContractFactory('OrandProviderV3');

const orocleV1 = await orocleV1Factory.deploy([accounts[0]]);
console.log('Orocle V1', await orocleV1.getAddress());
const orocleV2 = await upgrades.deployProxy(orocleV2Factory, [[accounts[0].address]]);
console.log('Orocle V2', await orocleV2.getAddress());
let correspondingAddress = getAddress(`0x${keccak256(`0x${pk.substring(2, 130)}`).substring(26, 66)}`);
const orandECVRFV3 = await orandECVRFV3Factory.deploy();
console.log('Orand ECVRF V3', await orandECVRFV3.getAddress());

const instance = await upgrades.deployProxy(orand1Factory, [
const instance = await upgrades.deployProxy(orandV3Factory1, [
// uint256[2] memory publicKey
OrandEncoding.pubKeyToAffine(HexString.hexPrefixAdd(pk)),
// address operator
correspondingAddress,
// address ecvrfAddress
await orandECVRFV3.getAddress(),
// address oracleAddress
await orocleV1.getAddress(),
await orocleV2.getAddress(),
// uint256 maxBatchingLimit
100,
]);

console.log('Instance:', await instance.getOracle(), await instance.getAddress());

const upgraded = await upgrades.upgradeProxy(await instance.getAddress(), orand2Factory);
let upgraded = await upgrades.upgradeProxy(await instance.getAddress(), orandV3Factory1);

console.log('Upgrade', await upgraded.getOracle(), await upgraded.getAddress());

upgraded = await upgrades.upgradeProxy(await instance.getAddress(), orandV3Factory2);

orandProviderV3 = upgraded as any;
});

Expand Down
71 changes: 64 additions & 7 deletions test/009-orocle-v2.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,75 @@
import hre, { upgrades } from 'hardhat';
import { OrocleV2 } from '../typechain-types';
import { FixedFloat, OrocleEncoding } from '@orochi-network/utilities';
import { HardhatEthersSigner } from '@nomicfoundation/hardhat-ethers/signers';

describe('OrocleV1', function () {
it('OrocleV1 should be upgradeable', async () => {
let orocleV2: OrocleV2;
let operator: HardhatEthersSigner;

describe('OrocleV2', function () {
it('OrocleV2 should be upgradeable', async () => {
const { ethers } = hre;
const accounts = await ethers.getSigners();
const orocleV1Factory = await ethers.getContractFactory('OrocleTest');
const orocleV2Factory = await ethers.getContractFactory('OrocleV1');
[operator] = await ethers.getSigners();
const orocleV2Factory1 = await ethers.getContractFactory('OrocleTest');
const orocleV2Factory2 = await ethers.getContractFactory('OrocleV2');

const instance = await upgrades.deployProxy(orocleV1Factory, [accounts[0]]);
const instance = await upgrades.deployProxy(orocleV2Factory1, [[operator.address]]);

console.log('Instance:', await instance.owner(), await instance.getAddress());

const upgraded = await upgrades.upgradeProxy(await instance.getAddress(), orocleV2Factory);
const upgraded = await upgrades.upgradeProxy(await instance.getAddress(), orocleV2Factory2);

console.log('Upgrade', await upgraded.owner(), await upgraded.getAddress());
orocleV2 = upgraded as any;
});

it('OrocleV2 should be upgradeable', async () => {
await orocleV2.connect(operator).publishPrice(
OrocleEncoding.encodeTokenPrice([
{
symbol: 'BTC',
price: 42000n * 10n ** 18n,
},
]),
);
const [round, lastUpdate, price] = await orocleV2.getLatestRound(1, OrocleEncoding.toIdentifier('BTC'));

console.log('Round data:', { round, lastUpdate, price });

console.log(
'BTC/USDT',
FixedFloat.fromFixedFloat({
basedValue: BigInt(price),
decimals: 18,
}).pretty('en-us', 2),
);
});

it('OrocleV2 should be upgradeable', async () => {
await orocleV2.connect(operator).publishPrice(
OrocleEncoding.encodeTokenPrice([
{
symbol: 'BTC',
price: 42000n * 10n ** 18n,
},
{
symbol: 'ETH',
price: 3821n * 10n ** 18n,
},
]),
);
const [round, lastUpdate, price] = await orocleV2.getLatestRound(1, OrocleEncoding.toIdentifier('ETH'));

console.log('Round data:', { round, lastUpdate, price });

console.log(
'ETH/USDT',
FixedFloat.fromFixedFloat({
basedValue: BigInt(price),
decimals: 18,
}).pretty('en-us', 2),
);

console.log(await orocleV2.getLatestRound(1, OrocleEncoding.toIdentifier('BTC')));
});
});
32 changes: 32 additions & 0 deletions test/010-xoro.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import hre from 'hardhat';
import Deployer from '../helpers/deployer';
import { XORO } from '../typechain-types';
import { expect } from 'chai';
import { ByteBuffer } from '@orochi-network/utilities';
import { getBytes } from 'ethers';

const TOKEN_NAME = 'ORC [Beta Token]';
const TOKEN_SYMBOL = 'XORO';
Expand Down Expand Up @@ -156,6 +158,36 @@ describe('XORO token', function () {
expect(await contract.balanceOf(player03)).eq(115n);
});

it('user can redeem the token with valid digital signature', async () => {
const wallet = hre.ethers.Wallet.createRandom(hre.ethers.provider);
// Send transaction fee
await operator1.sendTransaction({
to: wallet.address,
value: 10n ** 18n,
});
// uint96 chainId;
// address beneficiary;
// uint64 nonce;
// uint192 value;
const { chainId } = await hre.ethers.provider.getNetwork();
const message = ByteBuffer.getInstance()
.writeUint96(chainId)
.writeAddress(wallet.address as `0x${string}`)
.writeUint64(0)
.writeUint192(1000000)
.invoke();

const signature = await operator1.signMessage(getBytes(message));
const combineSignatureAndMessage = ByteBuffer.getInstance()
.writeBytes(signature as `0x${string}`)
.writeBytes(message)
.invoke();

await contract.connect(wallet).redeem(combineSignatureAndMessage);

expect(await contract.balanceOf(wallet.address)).eq(1000000n);
});

it('Operator can burn token', async () => {
await contract.connect(operator1).batchBurn([packData(12n, player01.address)]);
expect(await contract.balanceOf(player01)).to.eq(10n);
Expand Down

0 comments on commit f02052c

Please sign in to comment.