Skip to content
Next
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Minds Backend - Engine
Project
Project
Details
Activity
Releases
Dependency List
Cycle Analytics
Insights
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Issues
149
Issues
149
List
Boards
Labels
Service Desk
Milestones
Merge Requests
47
Merge Requests
47
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Minds
Minds Backend - Engine
Commits
41ae7d2a
Commit
41ae7d2a
authored
8 minutes ago
by
Emiliano Balbuena
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(wip): Onchain payments
parent
68a973d9
goal/boost-campaigns-e24
1 merge request
!235
WIP: Boost Campaigns (&24)
Pipeline
#69968069
running with stages
Changes
7
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
193 additions
and
55 deletions
+193
-55
Events.php
Core/Blockchain/Events.php
+3
-1
TokenEvent.php
Core/Blockchain/Events/TokenEvent.php
+147
-0
Manager.php
Core/Blockchain/Transactions/Manager.php
+11
-1
BoostProvider.php
Core/Boost/BoostProvider.php
+2
-4
PaymentsDelegate.php
Core/Boost/Campaigns/Delegates/PaymentsDelegate.php
+3
-3
Manager.php
Core/Boost/Campaigns/Manager.php
+17
-5
Onchain.php
Core/Boost/Campaigns/Payments/Onchain.php
+10
-41
No files found.
Core/Blockchain/Events.php
View file @
41ae7d2a
...
...
@@ -10,6 +10,7 @@ namespace Minds\Core\Blockchain;
use
Minds\Core\Blockchain\Events\BlockchainEventInterface
;
use
Minds\Core\Blockchain\Events\BoostEvent
;
use
Minds\Core\Blockchain\Events\TokenEvent
;
use
Minds\Core\Blockchain\Events\TokenSaleEvent
;
use
Minds\Core\Blockchain\Events\WireEvent
;
use
Minds\Core\Blockchain\Events\WithdrawEvent
;
...
...
@@ -20,9 +21,10 @@ class Events
{
protected
static
$handlers
=
[
TokenSaleEvent
::
class
,
TokenEvent
::
class
,
WireEvent
::
class
,
BoostEvent
::
class
,
WithdrawEvent
::
class
WithdrawEvent
::
class
,
];
public
function
register
()
...
...
This diff is collapsed.
Click to expand it.
Core/Blockchain/Events/TokenEvent.php
0 → 100644
View file @
41ae7d2a
<?php
/**
* TokenEvent
* @author edgebal
*/
namespace
Minds\Core\Blockchain\Events
;
use
Exception
;
use
Minds\Core\Blockchain\Transactions\Transaction
;
use
Minds\Core\Blockchain\Util
;
use
Minds\Core\Boost\Campaigns\Manager
as
BoostCampaignsManager
;
use
Minds\Core\Boost\Campaigns\Payments\Payment
;
use
Minds\Core\Config
;
use
Minds\Core\Di\Di
;
use
Minds\Core\Util\BigNumber
;
class
TokenEvent
implements
BlockchainEventInterface
{
/** @var array */
public
static
$eventsMap
=
[
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
=>
'tokenTransfer'
,
'blockchain:fail'
=>
'tokenFail'
,
];
/** @var Config */
protected
$config
;
/** @var BoostCampaignsManager */
protected
$boostCampaignsManager
;
/**
* TokenEvent constructor.
* @param Config $config
* @param BoostCampaignsManager $boostCampaignsManager
*/
public
function
__construct
(
$config
=
null
,
$boostCampaignsManager
=
null
)
{
$this
->
config
=
$config
?:
Di
::
_
()
->
get
(
'Config'
);
$this
->
boostCampaignsManager
=
$boostCampaignsManager
?:
Di
::
_
()
->
get
(
'Boost\Campaigns\Manager'
);
}
/**
* @return array
*/
public
function
getTopics
()
{
return
array_keys
(
static
::
$eventsMap
);
}
/**
* @param $topic
* @param array $log
* @param Transaction $transaction
* @return void
* @throws Exception
*/
public
function
event
(
$topic
,
array
$log
,
$transaction
)
{
$method
=
static
::
$eventsMap
[
$topic
];
if
(
$log
[
'address'
]
!=
$this
->
config
->
get
(
'blockchain'
)[
'token_address'
])
{
throw
new
Exception
(
'Event does not match address'
);
}
if
(
method_exists
(
$this
,
$method
))
{
$this
->
{
$method
}(
$log
,
$transaction
);
}
else
{
throw
new
Exception
(
'Method not found'
);
}
}
/**
* @param array $log
* @param Transaction $transaction
* @throws Exception
*/
public
function
tokenTransfer
(
$log
,
$transaction
)
{
list
(
$amount
)
=
Util
::
parseData
(
$log
[
'data'
],
[
Util
::
NUMBER
]);
list
(
$destination
)
=
Util
::
parseData
(
$log
[
'topics'
][
2
],
[
Util
::
ADDRESS
]);
$data
=
$transaction
->
getData
();
if
(
!
$destination
)
{
throw
new
Exception
(
'Invalid transfer destination'
);
}
switch
(
$transaction
->
getContract
())
{
case
'boost_campaign'
:
$wallet
=
$this
->
config
->
get
(
'blockchain'
)[
'contracts'
][
'boost_campaigns'
][
'wallet_address'
]
??
null
;
if
(
$destination
!=
$wallet
)
{
throw
new
Exception
(
'Invalid Boost Campaign wallet address'
);
}
$payment
=
new
Payment
();
$payment
->
setOwnerGuid
(
$data
[
'payment'
][
'owner_guid'
])
->
setCampaignGuid
(
$data
[
'payment'
][
'campaign_guid'
])
->
setTx
(
$data
[
'payment'
][
'tx'
])
->
setAmount
(
BigNumber
::
fromPlain
(
BigNumber
::
fromHex
(
$amount
),
18
)
->
toDouble
());
$this
->
boostCampaignsManager
->
onPaymentSuccess
(
$payment
);
break
;
}
}
/**
* @param array $log
* @param Transaction $transaction
* @throws Exception
*/
public
function
tokenFail
(
$log
,
$transaction
)
{
list
(
$amount
)
=
Util
::
parseData
(
$log
[
'data'
],
[
Util
::
NUMBER
]);
list
(
$destination
)
=
Util
::
parseData
(
$log
[
'topics'
][
2
],
[
Util
::
ADDRESS
]);
$data
=
$transaction
->
getData
();
if
(
!
$destination
)
{
throw
new
Exception
(
'Invalid transfer destination'
);
}
switch
(
$transaction
->
getContract
())
{
case
'boost_campaign'
:
$wallet
=
$this
->
config
->
get
(
'blockchain'
)[
'contracts'
][
'boost_campaigns'
][
'wallet_address'
]
??
null
;
if
(
$destination
!=
$wallet
)
{
throw
new
Exception
(
'Invalid Boost Campaign wallet address'
);
}
$payment
=
new
Payment
();
$payment
->
setOwnerGuid
(
$data
[
'payment'
][
'owner_guid'
])
->
setCampaignGuid
(
$data
[
'payment'
][
'campaign_guid'
])
->
setTx
(
$data
[
'payment'
][
'tx'
])
->
setAmount
(
BigNumber
::
fromPlain
(
BigNumber
::
fromHex
(
$amount
),
18
)
->
toDouble
());
$this
->
boostCampaignsManager
->
onPaymentFailed
(
$payment
);
break
;
}
}
}
This diff is collapsed.
Click to expand it.
Core/Blockchain/Transactions/Manager.php
View file @
41ae7d2a
...
...
@@ -154,7 +154,7 @@ class Manager
(
new
$topicHandlerClass
())
->
event
(
$topic
,
$log
,
$transaction
);
error_log
(
"Tx[
{
$this
->
tx
}
][
{
$topicHandlerClass
}
]
{
$topic
}
... OK!"
);
}
catch
(
\Exception
$e
)
{
error_log
(
"Tx[
{
$this
->
tx
}
][
{
$topicHandlerClass
}
]
{
$topic
}
threw
"
.
get_class
(
$e
)
.
":
{
$e
->
getMessage
()
}
"
);
error_log
(
"Tx[
{
$this
->
tx
}
][
{
$topicHandlerClass
}
]
{
$topic
}
threw
{
$e
}
"
);
}
}
}
...
...
@@ -182,4 +182,14 @@ class Manager
]);
}
/**
* @param $tx
* @return int
* @throws \Exception
*/
public
function
exists
(
$tx
)
{
return
$this
->
repo
->
exists
(
$tx
);
}
}
This diff is collapsed.
Click to expand it.
Core/Boost/BoostProvider.php
View file @
41ae7d2a
...
...
@@ -2,8 +2,6 @@
namespace
Minds\Core\Boost
;
use
Minds\Core\Boost\Campaigns\Manager
as
CampaignsManager
;
use
Minds\Core\Boost\Campaigns\Repository
as
CampaignsRepository
;
use
Minds\Core\Boost\Network
;
use
Minds\Core\Data
;
use
Minds\Core\Data\Client
;
...
...
@@ -62,10 +60,10 @@ class BoostProvider extends Provider
return
new
Payment
();
},
[
'useFactory'
=>
true
]);
$this
->
di
->
bind
(
'Boost\Campaigns\Manager'
,
function
(
$di
)
{
return
new
CampaignsManager
();
return
new
Campaigns
\
Manager
();
},
[
'useFactory'
=>
true
]);
$this
->
di
->
bind
(
'Boost\Campaigns\Repository'
,
function
(
$di
)
{
return
new
CampaignsRepository
();
return
new
Campaigns
\
Repository
();
},
[
'useFactory'
=>
true
]);
}
...
...
This diff is collapsed.
Click to expand it.
Core/Boost/Campaigns/Delegates/PaymentsDelegate.php
View file @
41ae7d2a
...
...
@@ -44,7 +44,7 @@ class PaymentsDelegate
*/
public
function
onCreate
(
Campaign
$campaign
,
$paymentPayload
=
null
)
{
// NOTE: Do not spec test
// NOTE: Do not spec test
. Individually test the other methods.
$this
->
validateBudget
(
$campaign
);
$this
->
registerCampaignPayment
(
$campaign
,
$paymentPayload
);
...
...
@@ -63,7 +63,7 @@ class PaymentsDelegate
*/
public
function
onUpdate
(
Campaign
$campaign
,
Campaign
$campaignRef
,
$paymentPayload
=
null
)
{
// NOTE: Do not spec test
// NOTE: Do not spec test
. Individually test the other methods.
$campaignRef
->
setBudgetType
(
$campaign
->
getBudgetType
());
...
...
@@ -82,7 +82,7 @@ class PaymentsDelegate
*/
public
function
onStateChange
(
Campaign
$campaign
)
{
// NOTE: Do not spec test
// NOTE: Do not spec test
. Individually test the other methods.
// TODO: Check that campaign is in a final complete or incomplete status (revoked/rejected)
// TODO: Refund!
...
...
This diff is collapsed.
Click to expand it.
Core/Boost/Campaigns/Manager.php
View file @
41ae7d2a
...
...
@@ -9,6 +9,7 @@ namespace Minds\Core\Boost\Campaigns;
use
Exception
;
use
Minds\Common\Repository\Response
;
use
Minds\Common\Urn
;
use
Minds\Core\Boost\Campaigns\Payments\Payment
;
use
Minds\Core\Boost\Campaigns\Payments\Repository
as
PaymentsRepository
;
use
Minds\Entities\User
;
...
...
@@ -182,11 +183,6 @@ class Manager
$campaign
=
$this
->
normalizeEntityUrnsDelegate
->
onCreate
(
$campaign
);
$campaign
=
$this
->
normalizeHashtagsDelegate
->
onCreate
(
$campaign
);
// Add
$campaign
->
setCreatedTimestamp
(
time
()
*
1000
);
// Run payments delegate (should be ALWAYS called after normalizing dates)
$campaign
=
$this
->
paymentsDelegate
->
onCreate
(
$campaign
,
$paymentPayload
);
...
...
@@ -436,4 +432,20 @@ class Manager
return
$campaign
;
}
/**
* @param Payment $paymentRef
*/
public
function
onPaymentSuccess
(
Payment
$paymentRef
)
{
}
/**
* @param Payment $paymentRef
*/
public
function
onPaymentFailed
(
Payment
$paymentRef
)
{
error_log
(
print_r
([
'FAIL!'
,
$paymentRef
],
true
));
}
}
This diff is collapsed.
Click to expand it.
Core/Boost/Campaigns/Payments/Onchain.php
View file @
41ae7d2a
...
...
@@ -8,7 +8,7 @@ namespace Minds\Core\Boost\Campaigns\Payments;
use
Exception
;
use
Minds\Core\Blockchain\Services\Ethereum
;
use
Minds\Core\Blockchain\Transactions\
Repository
as
TransactionsRepository
;
use
Minds\Core\Blockchain\Transactions\
Manager
as
TransactionsManager
;
use
Minds\Core\Blockchain\Transactions\Transaction
;
use
Minds\Core\Boost\Campaigns\Campaign
;
use
Minds\Core\Config
;
...
...
@@ -26,20 +26,20 @@ class Onchain
/** @var Repository */
protected
$repository
;
/** @var Transactions
Repository
*/
protected
$tx
Repository
;
/** @var Transactions
Manager
*/
protected
$tx
Manager
;
public
function
__construct
(
$config
=
null
,
$eth
=
null
,
$repository
=
null
,
$tx
Repository
=
null
$tx
Manager
=
null
)
{
$this
->
config
=
$config
?:
Di
::
_
()
->
get
(
'Config'
);
$this
->
eth
=
$eth
?:
Di
::
_
()
->
get
(
'Blockchain\Services\Ethereum'
);
$this
->
repository
=
$repository
?:
new
Repository
();
$this
->
tx
Repository
=
$txRepository
?:
Di
::
_
()
->
get
(
'Blockchain\Transactions\Repository
'
);
$this
->
tx
Manager
=
$txManager
?:
Di
::
_
()
->
get
(
'Blockchain\Transactions\Manager
'
);
}
/**
...
...
@@ -49,7 +49,7 @@ class Onchain
*/
public
function
register
(
Payment
$payment
)
{
if
(
$this
->
tx
Repository
->
exists
(
$payment
->
getTx
()))
{
if
(
$this
->
tx
Manager
->
exists
(
$payment
->
getTx
()))
{
throw
new
Exception
(
'Payment transaction already exists'
);
}
...
...
@@ -61,44 +61,13 @@ class Onchain
->
setWalletAddress
(
$payment
->
getSource
())
->
setTimestamp
(
$payment
->
getTimeCreated
())
->
setUserGuid
(
$payment
->
getOwnerGuid
())
->
setData
([]);
->
setData
([
'payment'
=>
$payment
->
export
(),
]);
$this
->
repository
->
add
(
$payment
);
$this
->
tx
Repository
->
add
(
$transaction
);
$this
->
tx
Manager
->
add
(
$transaction
);
return
true
;
}
// public function validate($from, $tx)
// {
// $tokenAddress = $this->config->get('blockchain')['token_address'] ?? null;
// $boostCampaignWalletAddress = $this->config->get('blockchain')['contracts']['boost_campaigns']['wallet_address'] ?? null;
//
// if (!$tokenAddress || !$boostCampaignWalletAddress) {
// throw new Exception('Missing token or boost campaign wallet addresses');
// }
//
// $receipt = $this->eth->request('eth_getTransactionReceipt', [ $tx ]);
//
// // TODO: Skip failed transactions
//
// // TODO: Review this
// $destinationAddress = sprintf("0x%s", substr($receipt['logs'][0]['topics'][2] ?? '', -40));
//
// // TODO: Review this
// $hexWei = ltrim(str_replace('0x', '', $receipt['logs'][0]['data'] ?? ''), '0');
// $amount = BigNumber::fromPlain(BigNumber::fromHex($hexWei), 18)->toDouble();
//
// if (!$receipt) {
// throw new Exception("Invalid payment: {$tx}");
// } elseif (strtolower($receipt['from']) !== strtolower($from)) {
// throw new Exception("Invalid payment source for {$tx}");
// } elseif (strtolower($receipt['to']) !== strtolower($tokenAddress)) {
// throw new Exception("Invalid token address for {$tx}");
// } elseif (strtolower($destinationAddress) !== strtolower($boostCampaignWalletAddress)) {
// throw new Exception("Invalid boost campaign address for {$tx}");
// }
//
// return $amount;
// }
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment