Skip to content

Commit

Permalink
Merge pull request #100 from TosiDrop/airdrop
Browse files Browse the repository at this point in the history
translate both testnet and mainnet address
  • Loading branch information
aryelciu001 authored May 5, 2022
2 parents c66d50c + f51175c commit 289fca7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 35 deletions.
48 changes: 13 additions & 35 deletions client/src/pages/Rewards/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ import "./index.scss";
import { useCallback } from "react";
import { useDispatch } from "react-redux";
import { showModal } from "src/reducers/modalSlice";
import {
Address,
BaseAddress,
RewardAddress,
} from "@emurgo/cardano-serialization-lib-asmjs";
import { getStakeKey } from "./utils/common.function";

let Buffer = require("buffer").Buffer;

Expand Down Expand Up @@ -84,28 +80,8 @@ function Rewards({ connectedWallet, wrongNetwork }: Params) {
* we want the stake address
* if it is cardano address, get the staking address
*/

let address = searchAddress;

if (searchAddress.includes("addr")) {
const addressObject = Address.from_bech32(searchAddress);
const stakeAddress =
BaseAddress.from_address(addressObject);
let rewardAddressBytes = new Uint8Array(29);
rewardAddressBytes.set([0xe0], 0);

if (stakeAddress == null) return;
rewardAddressBytes.set(
stakeAddress.stake_cred().to_bytes().slice(4, 32),
1
);
let rewardAddress = RewardAddress.from_address(
Address.from_bytes(rewardAddressBytes)
);

if (rewardAddress == null) return;
address = rewardAddress?.to_address().to_bech32();
}
let address = getStakeKey(searchAddress);
if (address == null) throw new Error();

const rewards = await getRewards(address);

Expand All @@ -125,14 +101,16 @@ function Rewards({ connectedWallet, wrongNetwork }: Params) {
setRewardsLoader(false);
}
} catch (ex: any) {
if (ex?.response?.status === 404) {
dispatch(
showModal({
text: "Account not found.",
type: ModalTypes.info,
})
);
setRewardsLoader(false);
switch (true) {
case ex?.response?.status === 404:
default:
dispatch(
showModal({
text: "Account not found.",
type: ModalTypes.info,
})
);
setRewardsLoader(false);
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions client/src/pages/Rewards/utils/common.function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
Address,
BaseAddress,
RewardAddress,
} from "@emurgo/cardano-serialization-lib-asmjs";

export const getStakeKey = (searchAddress: string): string | null => {
const prefix = searchAddress.slice(0, 5);
const addressObject = Address.from_bech32(searchAddress);
const baseAddress = BaseAddress.from_address(addressObject);
let rewardAddressBytes = new Uint8Array(29);

switch (true) {
/**
* check if testnet address
*/
case prefix === "addr_":
rewardAddressBytes.set([0xe0], 0);
break;
/**
* check if mainnet address
*/
case prefix === "addr1":
rewardAddressBytes.set([0xe1], 0);
break;
/**
* check if not receive address
*/
default:
return searchAddress;
}

if (baseAddress == null) return null;
rewardAddressBytes.set(baseAddress.stake_cred().to_bytes().slice(4, 32), 1);

let rewardAddress = RewardAddress.from_address(
Address.from_bytes(rewardAddressBytes)
);

if (rewardAddress == null) return null;

return rewardAddress.to_address().to_bech32();
};

0 comments on commit 289fca7

Please sign in to comment.