Skip to content

Commit

Permalink
Merge pull request #10 from sonicdex/icrc1/burn
Browse files Browse the repository at this point in the history
Icrc1/burn
  • Loading branch information
sideffect0 authored May 10, 2024
2 parents 0424bf5 + 3b73b1c commit 97898ed
Showing 1 changed file with 6 additions and 221 deletions.
227 changes: 6 additions & 221 deletions src/token/main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,12 @@ shared(msg) actor class ICRC1Canister(args : {tokenOwner : Principal}) = this {
(result: TxnResult) {
let from = _from;
let to = _to;
let operation: Operation = #transfer({ action = #send; });
// destination is minter or not
let operation: Operation = if(_to == owner_){
#transfer({ action = #burn; });
} else {
#transfer({ action = #send; });
};
// check fee
if(not(_checkFee(from, _value))){
return #err({ code=#InsufficientBalance; message="Insufficient Balance"; });
Expand Down Expand Up @@ -587,22 +592,6 @@ shared(msg) actor class ICRC1Canister(args : {tokenOwner : Principal}) = this {
Principal.equal, Principal.hash
);


// helper functions
private func createEligibleTokenList() {
// reset before creating new list
eligible_tokens := [];
eligibleTokens := Map.fromIter<MotokoNft.AccountIdentifier, Nat>([].vals(), 1, Text.equal, Text.hash);
for (x in raw_snapshot.vals()) {
let _tokens = switch(eligibleTokens.get(x.1)){
case(?c) {c};
case(_) {0};
};
eligibleTokens.put(x.1, _tokens + 1);
};
return;
};

// Hail the Vikings
public composite query func get_transactions(page : Nat32) : async Root.GetTransactionsResponseBorrowed {
let c : Root.Self = actor(capRootBucketId);
Expand Down Expand Up @@ -643,208 +632,4 @@ shared(msg) actor class ICRC1Canister(args : {tokenOwner : Principal}) = this {
snapshot_time = raw_snapshot_time
};
};

public query func getEligibleTokensSnap() : async [(MotokoNft.AccountIdentifier, Nat)] {
return Iter.toArray(eligibleTokens.entries());
};

private func findEligibleTokens(user: Principal) : Nat {
var totalTokens : Nat = 0;
let accountIdx : [Nat] = Iter.toArray(Iter.range(0, 50));
for (i in accountIdx.vals()) {
let subaccount_array : [Nat8] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Nat8.fromNat(i)];
let address = EAID.fromPrincipal(user, ?subaccount_array);
totalTokens += Option.get(eligibleTokens.get(address), 0);
};
return totalTokens;
};

private func _getEligibleTokenOfUser(user: Principal) : TokenClaimStatus {
switch(airdropedTokens.get(user)) {
case(?tokens) {
return #Airdroped { tx = ""; tokens };
};
case(_){};
};
switch(claimedTokens.get(user)){
case(?tokens){

return #Claimed { tx = ""; tokens };
};
case(_){};
};
return #Unclaimed(findEligibleTokens(user));
};

public shared query(msg) func getEligibleTokenOfUser(user : Principal) : async TokenClaimStatus {
_getEligibleTokenOfUser(user)
};

// updates
public shared(msg) func createSnap() : async () {
assert(Principal.isController(msg.caller));
// Tuesday, January 30, 2024 3:00:00 PM UTC with 1 minute gap
if(raw_snapshot_time > 0) {
if(Time.now() < (1706626800000000000) or Time.now() > (1706626860000000000)) {
throw Error.reject("not allowed in this time");
};
};
raw_snapshot := await motoko_nft.getRegistry();
raw_snapshot_time := Time.now();
createEligibleTokenList();
};

public shared(msg) func claimTokens() : async TokenClaimStatus {
assert(Principal.isController(msg.caller));
if(airdropedTokens.size() < 2301 or claimedTokens.size() < 53){
throw Error.reject("please connect to the developer");
};
switch(_getEligibleTokenOfUser(msg.caller)) {
case(#Unclaimed(tokens)){
if(tokens > 0){
let user = AID.principalToAccountBlob(msg.caller, null);
let tokenAmount = tokens * 100000000;
let res = _transferFrom(msg.caller, owner_, user, tokenAmount - fee_, null, null);
switch(res) {
case(#ok(tx)){
claimedTokens.put(msg.caller, tokens);
claimedTxs.put(msg.caller, tx);
ignore addRecord(
msg.caller, "claimTokens",
[
("to", #Principal(msg.caller)),
("value", #U64(u64(tokenAmount))),
("fee", #U64(u64(fee_)))
]
);

return #Claimed { tx; tokens };
};
case(_){
throw Error.reject("unexpected error");
};
};
};
return #Unclaimed(0);
};

case(status){
return status;
};

};
};

public shared(msg) func airdropTokens(user : Principal) : async TokenClaimStatus {

assert(Principal.isController(msg.caller));

switch(_getEligibleTokenOfUser(user)){
case(#Unclaimed(tokens)) {
if(tokens > 0){
let _user = AID.principalToAccountBlob(user, null);
let tokenAmount = tokens * 100000000;
let res = _transferFrom(msg.caller, owner_, _user, tokenAmount - fee_, null, null);
switch(res) {
case(#ok(tx)){
airdropedTokens.put(user, tokens);
airdropTxs.put(user, tx);
ignore addRecord(
msg.caller, "airdropTokens",
[
("to", #Principal(user)),
("value", #U64(u64(tokenAmount))),
("fee", #U64(u64(fee_)))
]
);
return #Airdroped { tx; tokens };
};
case(_){
throw Error.reject("unexpected error");
};
};
};
return #Unclaimed(0);
};
case(status){
return status;
};
};
};

public shared(msg) func g_balanceOf(hex : Text) : async (Nat) {
let account = Option.unwrap(AID.accountHexToAccountBlob(hex));
let balance = _getBalance(account);
return balance;
};

// public shared(msg) func checkData(start : Nat, end : Nat) : async (Nat, Nat, Nat, Nat) {
// assert(Principal.isController(msg.caller));
// let affected_pages :[Nat] = Iter.toArray(Iter.range(start, end));
// let c : Root.Self = actor(capRootBucketId);
// for (page in affected_pages.vals()){
// let transactions = await c.get_transactions({page = ?Nat32.fromNat(page); witness = false});
// for(event in transactions.data.vals()){
// if(event.operation == "claimTokens"){
// var p : Principal = Principal.fromText("aaaaa-aa");
// var v : Nat64 = 0;
// for(details in event.details.vals()) {
// if(details.0 == "to") {
// switch(details.1){
// case(#Principal(user)){
// p := user;
// };
// case(_){};
// };
// };
// if(details.0 == "value"){
// switch(details.1){
// case(#U64(value)){
// v := value;
// };
// case(_){};
// };
// };
// };
// if (p != Principal.fromText("aaaaa-aa") and v != 0){
// claimedTokens.put(p, Nat64.toNat(v / 100000000));
// };
// };
// if(event.operation == "airdropTokens"){
// var p : Principal = Principal.fromText("aaaaa-aa");
// var v : Nat64 = 0;
// for(details in event.details.vals()) {
// if(details.0 == "to") {
// switch(details.1){
// case(#Principal(user)){
// p := user;
// };
// case(_){};
// };
// };
// if(details.0 == "value"){
// switch(details.1){
// case(#U64(value)){
// v := value;
// };
// case(_){};
// };
// };
// };
// if (p != Principal.fromText("aaaaa-aa") and v != 0){
// airdropedTokens.put(p, Nat64.toNat(v / 100000000));
// };
// };
// };
// };
// var aTokens = 0;
// var cTokens = 0;
// for(x in Iter.toArray(airdropedTokens.entries()).vals()){
// aTokens += x.1;
// };
// for(x in Iter.toArray(claimedTokens.entries()).vals()){
// cTokens += x.1;
// };
// return (airdropedTokens.size(), claimedTokens.size(), aTokens, cTokens);
// };
};

0 comments on commit 97898ed

Please sign in to comment.