Skip to content

Commit

Permalink
Reimburse tokens received that we don't need and storage cost refunded
Browse files Browse the repository at this point in the history
  • Loading branch information
AurelienFT committed May 7, 2024
1 parent f739518 commit c34e2cf
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
45 changes: 45 additions & 0 deletions smart-contract/assembly/__tests__/dns.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('Test DNS allocation', () => {
});
afterEach(() => {
mockTransferredCoins(0);
mockBalance(scAddress, 0);
switchUser(owner);
});
test('Testing success alloc', () => {
Expand All @@ -67,17 +68,20 @@ describe('Test DNS allocation', () => {
let args = new Args();
args.add(domain);
args.add(target);
mockBalance(scAddress, transferredAmount);
expect(dnsAlloc(args.serialize())).toStrictEqual(u256ToBytes(u256.Zero));
});
test('Testing multiple alloc', () => {
let argsCost = new Args();
argsCost.add(domain);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
let args = new Args();
args.add(domain);
args.add(target);
expect(dnsAlloc(args.serialize())).toStrictEqual(u256ToBytes(u256.Zero));
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
let args2 = new Args();
args2.add('test29087890');
args2.add(target);
Expand All @@ -94,21 +98,26 @@ describe('Test DNS allocation', () => {
args.add(domain);
args.add(target);
mockTransferredCoins(100_000_000);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
});
throws('Invalid domain', () => {
let args = new Args();
args.add('(invalid)');
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
});
throws('Domain already exists', () => {
let args = new Args();
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
});
});
Expand All @@ -121,6 +130,7 @@ describe('Test DNS free', () => {
mockAdminContext(false);
});
afterEach(() => {
mockBalance(scAddress, 0);
mockTransferredCoins(0);
switchUser(owner);
});
Expand All @@ -129,9 +139,11 @@ describe('Test DNS free', () => {
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
const tokenId = bytesToU256(dnsAlloc(args.serialize()));
let argsFree = new Args();
argsFree.add(tokenId);
mockTransferredCoins(0);
mockBalance(scAddress, transferredAmount / 2);
dnsFree(argsFree.serialize());
});
Expand All @@ -140,15 +152,18 @@ describe('Test DNS free', () => {
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
const tokenId = bytesToU256(dnsAlloc(args.serialize()));
let argsFree = new Args();
argsFree.add(tokenId);
mockBalance(scAddress, transferredAmount / 2);
mockTransferredCoins(0);
dnsFree(argsFree.serialize());
let args2 = new Args();
args2.add(domain);
args2.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args2.serialize());
});
throws('Domain not found', () => {
Expand All @@ -161,10 +176,12 @@ describe('Test DNS free', () => {
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
const tokenId = bytesToU256(dnsAlloc(args.serialize()));
let argsFree = new Args();
argsFree.add(tokenId);
mockBalance(scAddress, transferredAmount / 4);
mockTransferredCoins(0);
dnsFree(argsFree.serialize());
});
});
Expand All @@ -178,13 +195,15 @@ describe('Test DNS resolve', () => {
});
afterEach(() => {
mockTransferredCoins(0);
mockBalance(scAddress, 0);
switchUser(owner);
});
test('Test success resolve', () => {
let args = new Args();
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
let argsResolve = new Args();
argsResolve.add(domain);
Expand All @@ -208,13 +227,15 @@ describe('Test DNS change target', () => {
});
afterEach(() => {
mockTransferredCoins(0);
mockBalance(scAddress, 0);
switchUser(owner);
});
test('Test success change target', () => {
let args = new Args();
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
let newTarget = 'AU12W92UyGW4Bd94BPniTq4Ra5yhiv6RvjazV2G9Q9GyekYkgqbme';
let argsChange = new Args();
Expand All @@ -237,6 +258,7 @@ describe('Test DNS change target', () => {
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
let argsChange = new Args();
argsChange.add(domain);
Expand All @@ -255,13 +277,15 @@ describe('Test transfer internal coins', () => {
});
afterEach(() => {
mockTransferredCoins(0);
mockBalance(scAddress, 0);
switchUser(owner);
});
test('Test transfer internal coins', () => {
let args = new Args();
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
let argsTransfer = new Args();
argsTransfer.add(target);
Expand Down Expand Up @@ -289,13 +313,15 @@ describe('Test get domain from tokenId', () => {
});
afterEach(() => {
mockTransferredCoins(0);
mockBalance(scAddress, 0);
switchUser(owner);
});
test('Test get domain from tokenId', () => {
let args = new Args();
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
const tokenId = bytesToU256(dnsAlloc(args.serialize()));
let argsGet = new Args();
argsGet.add(tokenId);
Expand All @@ -308,6 +334,7 @@ describe('Test get domain from tokenId', () => {
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
let argsGet = new Args();
argsGet.add<u256>(u256.One);
Expand All @@ -324,13 +351,15 @@ describe('Test get tokenId from domain', () => {
});
afterEach(() => {
mockTransferredCoins(0);
mockBalance(scAddress, 0);
switchUser(owner);
});
test('Test get tokenId from domain', () => {
let args = new Args();
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
const tokenId = bytesToU256(dnsAlloc(args.serialize()));
let argsGet = new Args();
argsGet.add(domain);
Expand All @@ -343,6 +372,7 @@ describe('Test get tokenId from domain', () => {
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
let argsGet = new Args();
argsGet.add('(invalid)');
Expand All @@ -359,6 +389,7 @@ describe('Test NFT name', () => {
});
afterEach(() => {
mockTransferredCoins(0);
mockBalance(scAddress, 0);
switchUser(owner);
});
test('Test NFT name', () => {
Expand All @@ -377,6 +408,7 @@ describe('Test NFT symbol', () => {
});
afterEach(() => {
mockTransferredCoins(0);
mockBalance(scAddress, 0);
switchUser(owner);
});
test('Test NFT symbol', () => {
Expand All @@ -393,13 +425,15 @@ describe('Test NFT balanceOf', () => {
});
afterEach(() => {
mockTransferredCoins(0);
mockBalance(scAddress, 0);
switchUser(owner);
});
test('Test NFT balanceOf one elem', () => {
let args = new Args();
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
bytesToU256(dnsAlloc(args.serialize()));
let argsBalance = new Args();
argsBalance.add(owner);
Expand All @@ -412,11 +446,13 @@ describe('Test NFT balanceOf', () => {
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
let args2 = new Args();
args2.add('test256789');
args2.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args2.serialize());
let argsBalance = new Args();
argsBalance.add(owner);
Expand All @@ -442,13 +478,15 @@ describe('Test NFT ownerOf', () => {
});
afterEach(() => {
mockTransferredCoins(0);
mockBalance(scAddress, 0);
switchUser(owner);
});
test('Test NFT ownerOf', () => {
let args = new Args();
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
const tokenId = bytesToU256(dnsAlloc(args.serialize()));
let argsOwner = new Args();
argsOwner.add(tokenId);
Expand All @@ -471,10 +509,12 @@ describe('Test NFT transferFrom', () => {
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
});
afterEach(() => {
mockTransferredCoins(0);
mockBalance(scAddress, 0);
switchUser(owner);
});
test('Test NFT transferFrom success', () => {
Expand Down Expand Up @@ -519,10 +559,12 @@ describe('Test NFT approve', () => {
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
});
afterEach(() => {
mockTransferredCoins(0);
mockBalance(scAddress, 0);
switchUser(owner);
});
test('Test NFT approve success', () => {
Expand Down Expand Up @@ -572,6 +614,7 @@ describe('Test set Owner', () => {
afterEach(() => {
switchUser(owner);
mockTransferredCoins(0);
mockBalance(scAddress, 0);
});
test('Test set Owner', () => {
let args = new Args();
Expand Down Expand Up @@ -601,10 +644,12 @@ describe('Test setApprovalForAll', () => {
args.add(domain);
args.add(target);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
});
afterEach(() => {
mockTransferredCoins(0);
mockBalance(scAddress, 0);
switchUser(owner);
});
test('Test setApprovalForAll', () => {
Expand Down
10 changes: 9 additions & 1 deletion smart-contract/assembly/contracts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export function dnsAlloc(binaryArgs: StaticArray<u8>): StaticArray<u8> {
domainCost.toString() +
'.',
);
transferCoins(Context.caller(), transferredCoins - domainCost);
return u256ToBytes(counter);
}

Expand All @@ -159,6 +160,7 @@ export function dnsAlloc(binaryArgs: StaticArray<u8>): StaticArray<u8> {
* @returns void
*/
export function dnsFree(binaryArgs: StaticArray<u8>): void {
let initialBalance = balance();
let args = new Args(binaryArgs);
let tokenId = args.nextU256().unwrap();

Expand All @@ -172,7 +174,13 @@ export function dnsFree(binaryArgs: StaticArray<u8>): void {
_update('', tokenId, '');
Storage.del(buildTargetKey(domain));
Storage.del(buildTokenIdKey(domain));
transferCoins(Context.caller(), calculateRefund(domain.length));
let finalBalance = balance();
let storageCostsRefunded = initialBalance - finalBalance;
let refundTotal =
calculateRefund(domain.length) +
storageCostsRefunded +
Context.transferredCoins();
transferCoins(Context.caller(), refundTotal);
return;
}

Expand Down

0 comments on commit c34e2cf

Please sign in to comment.