Skip to content

Commit

Permalink
fix: convert stacks-m2m-v2 to use aBTC for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
whoabuddy committed Feb 21, 2024
1 parent 22efc28 commit e4091c6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
24 changes: 9 additions & 15 deletions src/stacks-m2m-v2.clar
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
;;
(impl-trait .stacks-m2m-trait-v1.stacks-m2m-trait-v1)

;; tokens
;;
;; MAINNET
;; xBTC SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.token-wbtc
;; aBTC SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.token-abtc
;; TESTNET
(define-constant ABTC_CONTRACT .stacks-m2m-abtc)

;; constants
;;

Expand All @@ -37,7 +29,6 @@
(define-constant ERR_USER_NOT_FOUND (err u1008))
(define-constant ERR_INVOICE_ALREADY_PAID (err u1009))
(define-constant ERR_SAVING_INVOICE_DATA (err u1010))
(define-constant ERR_SETTING_MEMO_ON_TRANSFER (err u1011))

;; data vars
;;
Expand Down Expand Up @@ -82,8 +73,8 @@
uint ;; resource index
{
createdAt: uint,
;; enabled: bool, ;; use instead of deleting resources?
;; url: (string-utf8 255), ;; would need setter, health check
;; enabled: bool, ;; TODO: use instead of deleting resources?
;; url: (optional (string-utf8 255)), ;; TODO: would need setter, health check
name: (string-utf8 50),
description: (string-utf8 255),
price: uint,
Expand Down Expand Up @@ -275,8 +266,8 @@
(userIndex (unwrap! (get-or-create-user contract-caller) ERR_USER_NOT_FOUND))
(userData (unwrap! (get-user-data userIndex) ERR_USER_NOT_FOUND))
)
;; update InvoiceIndexes map, check invoice hash is unique
;; (asserts! (map-insert InvoiceIndexes invoiceHash newCount) ERR_INVOICE_ALREADY_PAID)
;; check that resourceIndex is > 0
(asserts! (> resourceIndex u0) ERR_INVALID_PARAMS)
;; update InvoiceData map
(asserts! (map-insert InvoiceData
newCount
Expand Down Expand Up @@ -324,8 +315,11 @@
;; make transfer
(if (is-some memo)
;; TODO: check for guards on price setting
(try! (stx-transfer-memo? (get price resourceData) contract-caller (var-get paymentAddress) (unwrap! memo ERR_SETTING_MEMO_ON_TRANSFER)))
(try! (stx-transfer? (get price resourceData) contract-caller (var-get paymentAddress)))
;; MAINNET
;; xBTC SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.token-wbtc
;; aBTC SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.token-abtc
(try! (contract-call? .stacks-m2m-abtc transfer (get price resourceData) contract-caller (var-get paymentAddress) memo))
(try! (contract-call? .stacks-m2m-abtc transfer (get price resourceData) contract-caller (var-get paymentAddress) none))
)
;; return new count
(ok newCount)
Expand Down
53 changes: 50 additions & 3 deletions tests/stacks-m2m-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ enum ErrCode {
ERR_USER_NOT_FOUND,
ERR_INVOICE_ALREADY_PAID,
ERR_SAVING_INVOICE_DATA,
ERR_SETTING_MEMO_ON_TRANSFER,
}

const createResource = (name: string, desc: string, price: number) => {
return [Cl.stringUtf8(name), Cl.stringUtf8(desc), Cl.uint(price)];
};

const defaultPrice = 1_000_000; // 1 STX
const defaultPrice = 10_000; // 0.0001 aBTC

const testResource = [
Cl.stringUtf8("Bitcoin Face"),
Expand Down Expand Up @@ -469,6 +468,13 @@ describe("Paying an invoice", () => {
const address1 = accounts.get("wallet_1")!;
const expectedCount = 1;
// ACT
// mint aBTC to pay for resources
simnet.callPublicFn(
"stacks-m2m-abtc",
"faucet-flood",
[Cl.principal(address1)],
address1
);
// add a resource
simnet.callPublicFn(
"stacks-m2m-v2",
Expand Down Expand Up @@ -498,6 +504,13 @@ describe("Paying an invoice", () => {
const expectedCount = 1;
const memo = Buffer.from("This is a memo test!");
// ACT
// mint aBTC to pay for resources
simnet.callPublicFn(
"stacks-m2m-abtc",
"faucet-flood",
[Cl.principal(address1)],
address1
);
// add a resource
simnet.callPublicFn(
"stacks-m2m-v2",
Expand Down Expand Up @@ -529,6 +542,27 @@ describe("Paying an invoice", () => {
const expectedCount = 1;
const memo = Cl.none();
// ACT
// mint aBTC to pay for resources
simnet.callPublicFn(
"stacks-m2m-abtc",
"faucet-flood",
[Cl.principal(address1)],
address1
);
// mint aBTC to pay for resources
simnet.callPublicFn(
"stacks-m2m-abtc",
"faucet-flood",
[Cl.principal(address2)],
address2
);
// mint aBTC to pay for resources
simnet.callPublicFn(
"stacks-m2m-abtc",
"faucet-flood",
[Cl.principal(address3)],
address3
);
// add a resource
simnet.callPublicFn(
"stacks-m2m-v2",
Expand Down Expand Up @@ -612,6 +646,19 @@ describe("Paying an invoice", () => {
const deployer = accounts.get("deployer")!;
const memo = Cl.none();
// ACT
// mint aBTC to pay for resources
simnet.callPublicFn(
"stacks-m2m-abtc",
"faucet-flood",
[Cl.principal(address1)],
address1
);
simnet.callPublicFn(
"stacks-m2m-abtc",
"faucet-flood",
[Cl.principal(address2)],
address2
);
// add a resource
simnet.callPublicFn(
"stacks-m2m-v2",
Expand Down Expand Up @@ -666,7 +713,7 @@ describe("Paying an invoice", () => {
// ASSERT
expect(resourceResponse.result).toBeSome(
Cl.tuple({
createdAt: Cl.uint(2),
createdAt: Cl.uint(4),
description: Cl.stringUtf8("Generate a unique Bitcoin face."),
name: Cl.stringUtf8("Bitcoin Face"),
price: Cl.uint(defaultPrice),
Expand Down

0 comments on commit e4091c6

Please sign in to comment.